MongoDB running but can't connect using shell

2019-03-08 13:42发布

CentOS 5.x Linux with MongoDB 2.0.1 (tried main and legacy-static)

MongoDB is running:

root     31664  1.5  1.4  81848 11148 ?        Sl   18:40   0:00 ./mongod -f mongo.conf -vvvvv --fork

Using a simple shell connect to get to the server fails:

[root@xxxx bin]# ./mongo
MongoDB shell version: 2.0.1
connecting to: test
Mon Oct 31 18:41:32 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed

The web interface on port 28017 loads fine, as does using the MongoDB shell from a remote Linux host. Can also telnet to localhost:27017, which means no ports are blocked. There is no SELinux running on this machine as well. I have also tried explicitly specifying localhost:2017/db to no avail.

$ ./mongo remote-ip:27017
MongoDB shell version: 2.0.1
connecting to: remote-ip:27017/test
> show dbs
local   0.03125GB
>

Logs are completely mum on the subject:

.....
Mon Oct 31 18:40:34 [initandlisten] fd limit hard:1024 soft:1024 max conn: 819
Mon Oct 31 18:40:34 [initandlisten] waiting for connections on port 27017
Mon Oct 31 18:40:34 BackgroundJob starting: snapshot
Mon Oct 31 18:40:34 BackgroundJob starting: ClientCursorMonitor
Mon Oct 31 18:40:34 BackgroundJob starting: PeriodicTask::Runner
Mon Oct 31 18:40:34 [websvr] fd limit hard:1024 soft:1024 max conn: 819
Mon Oct 31 18:40:34 [websvr] admin web console waiting for connections on port 28017

Stracing the mongo shell client shows only one problematic call:

[pid 31708] connect(4, {sa_family=AF_INET, sin_port=htons(27017), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EBADF (Bad file descriptor)

Filesystem is clean, no ulimit restrictions (running as root for testing). I can see from the strace that the mongo client is trying to connect via TCP (AF_INET), but since it is local and MongoDB creates a file socket, is there a way to tell the client to connect through that instead? Or better yet, why would the client be throwing a EBADF?

EDIT: My basic Mongo conf:

dbpath=/root/mongodb-linux-i686-2.0.1/data
logpath=/root/mongodb-linux-i686-2.0.1/logs/mongo.log
slowms=15
rest=1

标签: shell mongodb
16条回答
做自己的国王
2楼-- · 2019-03-08 14:19

You may want to check your config to see if the bind_ip is set

bind_ip: 127.0.0.1

If it is then this permits only local logins. Comment this out and restart mongo, this may help.

查看更多
爷、活的狠高调
3楼-- · 2019-03-08 14:22

On Ubuntu:

Wed Jan 27 10:21:32 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84 exception: connect failed

Solution

look for if mongodb is running by following command:

ps -ef | grep mongo

If mongo is not running you get:

 vimal     1806  1698  0 10:11 pts/0    00:00:00 grep --color=auto mongo

You are seeing that the mongo daemon is not there.

Then start it through configuration file(with root priev):

root@vimal:/data# mongod --config /etc/mongodb.conf &
[1] 2131
root@vimal:/data# all output going to: /var/log/mongodb/mongodb.log

you can see the other details:

root@vimal:~# more /etc/mongodb.conf

Open a new terminal to see the result of mongod --config /etc/mongodb.conf & then type mongo. It should be running or grep

root@vimal:/data# ps -ef | grep mongo

root      3153     1  2 11:39 ?        00:00:23 mongod --config /etc/mongodb.conf
root      3772  3489  0 11:55 pts/1    00:00:00 grep --color=auto mongo 

NOW

root@vimal:/data# mongo

MongoDB shell version: 2.0.4
connecting to: test

you get the mongoDB shell

This is not the end of story. I will post the repair method so that it starts automatically every time, most development machine shutdowns every day and the VM must have mongo started automatically at next boot.

查看更多
狗以群分
4楼-- · 2019-03-08 14:23

Not so much an answer but more of an FYI:I've just hit this and found this question as a result of searching. Here is the details of my experience:

Shell error

markdsievers@ip-xx-xx-xx-xx:~$ mongo
MongoDB shell version: 2.0.1
connecting to: test
Wed Dec 21 03:36:13 Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
Wed Dec 21 03:36:13 SocketException: remote: 127.0.0.1:27017 error: 9001 socket exception [1] server [127.0.0.1:27017] 
Wed Dec 21 03:36:13 DBClientCursor::init call() failed
Wed Dec 21 03:36:13 Error: Error during mongo startup. :: caused by :: DBClientBase::findN: transport error: 127.0.0.1 query: { whatsmyuri: 1 } shell/mongo.js:84
exception: connect failed

Mongo logs reveal

Wed Dec 21 03:35:04 [initandlisten] connection accepted from 127.0.0.1:50273 #6612
Wed Dec 21 03:35:04 [initandlisten] connection refused because too many open connections: 819

This perhaps indicates the other answer (JaKi) was experiencing the same thing, where some connections were purged and access made possible again for the shell (other clients)

查看更多
forever°为你锁心
5楼-- · 2019-03-08 14:24

This is actually not an error... What happens here is that Mongo relies on a daemon in order to run the local database server, so in order to "fire up" the mongo server in your shell, you have to start the mongo service first.

For Fedora Linux (wich is the Distro I use) You have to run these commands:

1 sudo service mongod start
2 mongo

And there you have it! the server is going to run. Now, If you want Mongo service to Start when the system boots then you have to run:

sudo chkconfig --levels 235 mongod on

And that's all! If you do that, now in the shell you just have to type mongo in order to start the server but that's pretty much it, the problem is you have to start the SERVICE first and then the SERVER :)

P.S. The commands I posted might work on other linux distros as well, not just in fedora... In case not maybe you have to tweak some words depending on the distro you're using ;)

查看更多
Explosion°爆炸
6楼-- · 2019-03-08 14:27

I had a similar problem, well actually the same (mongo process is running but can't connect to it). What I did was went to my database path and removed mongod.lock, and then gave it another try (restarted mongo). After that it worked.

Hope it works for you too. mongodb repair on ubuntu

查看更多
太酷不给撩
7楼-- · 2019-03-08 14:29

Delete /var/lib/mongodb/mongod.lock, then issue sudo service mongodb start, then mongo.

查看更多
登录 后发表回答