无法使用mongoid连接到mongodb

时间:2017-07-26 09:41:34

标签: ruby-on-rails mongodb mongoid

如果我尝试使用rails控制台中的mongo驱动程序进行连接,则可以正常工作:

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

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

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:password@dds-xxxxxxxx.mongodb.singapore.rds.aliyuncs.com:3717,dds-xxxxxxxx.mongodb.singapore.rds.aliyuncs.com:3717/graspire?replicaSet=mgset-xxxxxxx
  options:

使用mongoid version-6.1.0,mongo version - 2.4.1

1 个答案:

答案 0 :(得分:0)

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