Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js

mongdb启动的时候会报如上标题的错误,查询了很多资料终于找到解决办法,现把解决方法记录下来,以免以后忘记。

[root@localhost bin]# ./mongo
但是出现如下错误:
[root@localhost bin]# ./mongo
MongoDB shell version: 2.6.1
connecting to: test
2014-05-30T19:58:07.179+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-05-30T19:58:07.181+0800 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

1、若数据库出现如上不能连接的原因,可能是data目录下的mongod.lock文件问题,可以用如下命令修复:

[root@localhost mongodb]# ./bin/mongod --repair

或者直接删除mongod.lock

rm -f /usr/local/mongodb/data/db/mongod.lock

然后再启动mongodb。


2、如果第一步解决不了,那么就是路径设置有问题,通过重新对路径的设置解决此问题

[root@localhost mongodb]# ./bin/mongod --dbpath=/usr/local/mongodb/data/db/

然后界面会出现:

2015-03-19T15:32:27.923+0800 [initandlisten] MongoDB starting : pid=2079 port=27017 dbpath=/root/mongodb/data/db/ 64-bit host=localhost.localdomain
2015-03-19T15:32:27.923+0800 [initandlisten] db version v2.6.7
2015-03-19T15:32:27.923+0800 [initandlisten] git version: a7d57ad27c382de82e9cb93bf983a80fd9ac9899
2015-03-19T15:32:27.923+0800 [initandlisten] build info: Linux build7.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-03-19T15:32:27.923+0800 [initandlisten] allocator: tcmalloc
2015-03-19T15:32:27.923+0800 [initandlisten] options: { storage: { dbPath: "/root/mongodb/data/db/" } }
2015-03-19T15:32:27.935+0800 [initandlisten] journal dir=/root/mongodb/data/db/journal
2015-03-19T15:32:27.935+0800 [initandlisten] recover begin
2015-03-19T15:32:27.936+0800 [initandlisten] info no lsn file in journal/ directory
2015-03-19T15:32:27.936+0800 [initandlisten] recover lsn: 0
2015-03-19T15:32:27.936+0800 [initandlisten] recover /root/mongodb/data/db/journal/j._0
2015-03-19T15:32:27.938+0800 [initandlisten] recover cleaning up
2015-03-19T15:32:27.938+0800 [initandlisten] removeJournalFiles
2015-03-19T15:32:27.938+0800 [initandlisten] recover done
2015-03-19T15:32:27.990+0800 [initandlisten] waiting for connections on port 27017
2015-03-19T15:33:06.907+0800 [initandlisten] connection accepted from 127.0.0.1:56793 #1 (1 connection now open)
2015-03-19T15:33:27.991+0800 [clientcursormon] mem (MB) res:37 virt:456
2015-03-19T15:33:27.991+0800 [clientcursormon]  mapped (incl journal view):160
2015-03-19T15:33:27.991+0800 [clientcursormon]  connections:1
2015-03-19T15:38:28.011+0800 [clientcursormon] mem (MB) res:37 virt:456
2015-03-19T15:38:28.011+0800 [clientcursormon]  mapped (incl journal view):160
2015-03-19T15:38:28.011+0800 [clientcursormon]  connections:1
2015-03-19T15:43:28.032+0800 [clientcursormon] mem (MB) res:37 virt:456
2015-03-19T15:43:28.032+0800 [clientcursormon]  mapped (incl journal view):160
2015-03-19T15:43:28.032+0800 [clientcursormon]  connections:1
2015-03-19T15:44:42.655+0800 [conn1] end connection 127.0.0.1:56793

表示设置成功,此时不要关闭这个界面,需要重新打开一个窗口来启动mongo

[root@localhost]# cd /usr/local/mongodb
[root@localhost mongodb]# ./bin/mongo
MongoDB shell version: 2.6.7
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

当出现这个时候表示启动成功


你可能感兴趣的:(MongDB)