如果我尝试从 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 版本 - 6.1.0,mongo 版本 - 2.4.1
你如何运行你的 Rails 控制台?.如果不指定环境,则默认情况下,rails 控制台在开发中运行。请检查您的开发组是否在mongoid.yml中定义。