好久没学习MongoDB了,今天一打开出现如下的错误:

C:\Users\willsun>mongo

MongoDB shell version: 2.2.6

connecting to: test

Mon Jan 06 13:20:28 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91

exception: connect failed

网上查了下,有的说是重新设置下dpath的路径就修复下就可以了:

1) Remove .lock file

sudo rm /var/lib/mongodb/mongod.lock 

2) repair the mongodb

mongod –repair

3) start the mongodb

sudo service mongodb start 

4) start the mongo client

mongo

说是由于直接强制关机会留下一个mongod.lock的文件,需要rm -rf掉。

但是事实是:

我的机器上的mongodb的服务没有启动起来,我就在命令行里输入mongo就得到了上述的错误

所以就好解决了:

命令行输入:service mongodb start | net  start mongodb

然后启动mongodb就好了:

C:\Users\willsun>mongo

MongoDB shell version: 2.2.6

connecting to: test

Welcome to the MongoDB shell.

For interactive help, type "help".

For more comprehensive documentation, see

       http://docs.mongodb.org/

Questions? Try the support group

       http://groups.google.com/group/mongodb-user

>

PS :

数据目录,缺省为/data/db/。每个mongod进程都需要自己的数据目录,如果你要运行3个mongod的实例,那么就需要3个独自的目录。mongod启动的时候会在数据目录创建一个mongod.lock文件,阻止其他进程使用此目录。