在MacOSX上运行MongoDb时出错

时间:2014-06-14 05:38:28

标签: ruby-on-rails macos mongodb ruby-on-rails-4 homebrew

我正在尝试使用rails运行mongo我得到以下错误

Harshas-MacBook-Pro:~ harshamv$ mongo
MongoDB shell version: 2.6.1
connecting to: test
2014-06-14T12:07:46.356+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2014-06-14T12:07:46.357+0530 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

当我尝试修复MongoDB时

Harshas-MacBook-Pro:nomad harshamv$ mongod --repair
2014-06-14T11:06:52.964+0530 [initandlisten] MongoDB starting : pid=5504 port=27017 dbpath=/data/db 64-bit host=Harshas-MacBook-Pro.local
2014-06-14T11:06:52.964+0530 [initandlisten] 
2014-06-14T11:06:52.964+0530 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2014-06-14T11:06:52.964+0530 [initandlisten] db version v2.6.1
2014-06-14T11:06:52.964+0530 [initandlisten] git version: nogitversion
2014-06-14T11:06:52.964+0530 [initandlisten] build info: Darwin minimavericks.local 13.1.0 Darwin Kernel Version 13.1.0: Wed Apr  2 23:52:02 PDT 2014; root:xnu-2422.92.1~2/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2014-06-14T11:06:52.964+0530 [initandlisten] allocator: tcmalloc
2014-06-14T11:06:52.964+0530 [initandlisten] options: { repair: true }
2014-06-14T11:06:52.964+0530 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
2014-06-14T11:06:52.964+0530 [initandlisten] dbexit: 
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: going to close listening sockets...
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: going to flush diaglog...
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: going to close sockets...
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: waiting for fs preallocator...
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: closing all files...
2014-06-14T11:06:52.964+0530 [initandlisten] closeAllFiles() finished
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: removing fs lock...
2014-06-14T11:06:52.964+0530 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
2014-06-14T11:06:52.964+0530 [initandlisten] dbexit: really exiting now

这是我在尝试运行Rails应用程序时遇到的错误

Moped::Errors::ConnectionFailure in VenuesController#index
Could not connect to a primary node for replica set #<Moped::Cluster:70100620147140 @seeds=[<Moped::Node resolved_address="127.0.0.1:27017">]>

2 个答案:

答案 0 :(得分:4)

错误Unable to create/open lock file可能由以下三件事引起:

  1. MongoDB进程正在运行且无响应
  2. 您以前的MongoDB进程没有干净地关闭。
  3. 您对该文件夹/文件没有写入权限。
  4.   

    案例1:

    您需要检查mongod进程是否处于活动状态。在终端控制台中,输入:

    ps aux | grep mongod
    

    如果你能看到一个过程,你可以用以下方法杀死它:

    kill $(pidof mongod)

    kill -2 $(pidof mongod)

    仅使用-9选项作为最后的手段。

    您还需要删除旧的mongod.lock文件,然后启动mongod

      

    案例2:

    如果没有活动进程,那么MongoDB没有干净地关闭。

    您只需要删除mongod.lock文件,然后启动mongod

      

    案例3:

    如果您删除了mongod.lock文件并且收到了同样的错误,则应检查dbpath文件夹(/data/db/)上的权限。如果您使用sudo启动mongod,就会发生这种情况。

    您的用户或mongod应该是该文件夹的所有者。您可以使用以下命令进行更改:

    chown -R $(id -u) /data/db
    

答案 1 :(得分:2)

mongodb的另一个进程/实例在后台运行,因此首先终止它。即使没有运行此类进程,您也需要转到mongodb数据目录所在的位置,并清除名称为.lock(mongod.lock)末尾扩展名的文件中的内容。只有这样你才能正常运行mongodb。

终止进程的步骤:

  1. 浏览到/Applications/Utilities位置,然后双击&#39;终端&#39;

  2. 运行ps aux | grep mongo

  3. 然后运行kill -9 <PROCESS-ID>获取第一行中的进程号(我相信总共有两行,除非有更多具有相似名称的进程正在运行。)