我一直在尝试在Ubuntu上使用Mongoid和Rails。使用rails app 3.0到3.2,Mongodb对我工作正常,
今天突然,我收到错误“/home/pravinmishra/.rvm/gems/ruby-1.9.2-p318/gems/mongo-1.5.2/lib/mongo/connection.rb:413:in`connect ':无法连接到localhost上的主节点:27017(Mongo :: ConnectionFailure)“
我经常遇到这个问题,以下命令对我有用。
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo start mongodb
sudo status mongodb
但今天我没有运气。
前几天我改变了我的etc / hosts文件并添加了27.0.0.1 localhost.me
127.0.0.1 localhost.me:3000 localhost ubuntu
127.0.0.1 localhost
127.0.1.1 ubuntu
127.0.0.1 localhost.me locahost ubuntu
我想,这就造成了问题。过来我把mongoid.config文件改为localhost localhost.me,但仍然收到错误“/home/pravinmishra/.rvm/gems/ruby-1.9.2-p318/gems/mongo-1.5.2/lib/mongo/connection.rb:413:in`connect':无法连接到localhost.me:27017(Mongo :: ConnectionFailure)的主节点“
mongoid.yml文件
defaults: &defaults
host: localhost
allow_dynamic_fields: false
development:
<<: *defaults
database: xxx_development
test:
<<: *defaults
database: xxx_test
# set these environment variables on your prod server
production:
# host: <%= ENV['MONGOID_HOST'] %>
# port: <%= ENV['MONGOID_PORT'] %>
# username: <%= ENV['MONGOID_USERNAME'] %>
# password: <%= ENV['MONGOID_PASSWORD'] %>
# database: <%= ENV['MONGOID_DATABASE'] %>
# set these environment variables on your prod server
production:
uri: <%= ENV['MONGOHQ_URL'] %>
任何建议都会受到赞赏,在此先感谢.. !!
答案 0 :(得分:1)
您的/etc/hosts
文件已损坏
27.0.0.1 localhost.me:3000 localhost ubuntu
localhost
位于127.0.0.1,而不是27.0.0.1 hosts
视为本地DNS覆盖。因为Mongoid在运行的MongoDB中查看27.0.0.1,所以找不到它。并且无法连接。