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:14

By default, mongodb is configured to listen only to localhost. Excerpt from mongodb default config file :

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip=127.0.0.1

One needs to comment the bind_ip to listen from external entities.

You wont be able to add shards unless you start listening on non-local interfaces.

HTH,

Abhay Dandekar

查看更多
Animai°情兽
3楼-- · 2019-03-08 14:14

I found this very useful.

If you are getting the following message

start: Rejected send message, 1 matched rules; type="method_call", sender=":1.84" (uid=1000 pid=3215 comm="start mongodb ") interface="com.ubuntu.Upstart0_6.Job" member="Start" error name="(unset)" requested_reply="0" destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init")
shriprasad@shriprasad-HP-430-Notebook-PC:/var/lib/mongodb$ mongo

You must be trying to start the mongodb service as user other than root. You must be root user. Thus log in as root and then run following command as follows:

sudo bash

followed by

service mongodb start
查看更多
Ridiculous、
4楼-- · 2019-03-08 14:16

If your bind_ip is set to anything other than 127.0.0.1 then you'll need to add the ip explicitly even from the local machine. So simply use the same method that you're using on the remote box on the local box. At least that's what did it for me.

查看更多
该账号已被封号
5楼-- · 2019-03-08 14:17

Facing the same issue with the error described by Garrett above. 1. MongoDB Server with journaling enabled is running as seen using ps command 2. Mongo client or Mongoose driver are unable to connect to the database.

Solution : 1. Deleting the Mongo.lock file seems to bring life back to normal on the CentOS server. 2. We are fairly new in running MongoDB in production and have been seeing the same issue cropping up a couple of times a week. 3. We've setup a cron schedule to regularly cleanup the lock file and intimate the admin that an incident has occurred.

Searching for a bug fix to this issue or any other more permanent way to resolve it.

查看更多
闹够了就滚
6楼-- · 2019-03-08 14:18

Open the file /etc/mongod.conf and add the ip of the machine from where you are connecting, to bind_ip

bind_ip = 127.0.0.1,your Remote Machine Ip Address Here

Ex:-

bind_ip = 127.0.0.1,192.168.1.5

Restart mongodb service:

sudo service mongod restart

Make sure mongodb port is opened in the firewall.

You can also comment the line, if you are not worried about security.

查看更多
欢心
7楼-- · 2019-03-08 14:18

After starting the mongod

$mongod --dbpath <db name>

to get shell

$mongo --shell
查看更多
登录 后发表回答