2017-07-26 113 views
0

如果我尝试使用从铁轨控制台蒙戈驱动器连接到MongoDB的,它工作正常:无法连接使用mongoid

client = Mongo::Client.new('mongodb://username:[email protected]:3717,dds-xxxxxxxx.mongodb.singapore.rds.aliyuncs.com:3717/graspire?replicaSet=mgset-xxxxxxx') 
client.database.collection(:courses).count 

上面的代码工作正常,并给我的结果。但是,如果从铁轨控制台我做的:

Course.count 

然后我得到这个错误:

Mongo::Error::NoServerAvailable: No server is available matching preference: #<Mongo::ServerSelector::Primary:0x52072480 tag_sets=[] max_staleness=nil> using server_selection_timeout=30 and local_threshold=0.015 

这是我mongoid.yml

production: 
    # Configure available database clients. (required) 
    clients: 
    # Defines the default client. (required) 
    default: 
     uri: mongodb://username:[email protected]:3717,dds-xxxxxxxx.mongodb.singapore.rds.aliyuncs.com:3717/graspire?replicaSet=mgset-xxxxxxx 
    options: 

使用mongoid版本 - 6.1.0, mongo版本 - 2.4.1

+0

哪个mongoid版本? – meshin

+0

mongoid version- 6.1.0,mongo version - 2.4.1 – vigenere

回答

0

你如何运行你的Rails控制台?在没有指定环境的情况下,rails控制台默认在开发中运行。请检查您的开发组是否在mongoid.yml中定义。

+0

我只在生产环境中使用 - “rails c production” – vigenere

+1

你能在控制台下面运行命令吗? Mongoid.load!(“config/mongoid.yml”,:production) – Sanjiv

+0

Mongodb服务器是否启动。请检查您的mongodb服务器。 – Sanjiv