Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB running but can't connect using shell

Tags:

shell

mongodb

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 
like image 242
Garrett Avatar asked Oct 31 '11 18:10

Garrett


People also ask

Why is my MongoDB not connecting?

Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.

How does shell script connect to MongoDB?

To open up the MongoDB shell, run the mongo command from your server prompt. By default, the mongo command opens a shell connected to a locally-installed MongoDB instance running on port 27017 . Try running the mongo command with no additional parameters: mongo.

Can't connect to server connection refused MongoDB?

Normally this caused because you didn't start mongod process before you try starting mongo shell. This case worked out for me, i had to do ~>sudo mongod --dbpath /dbpathlocation and then opened the new terminal to run mongo...

How do I access MongoDB from command prompt?

Open up your command prompt and type mongod to start the MongoDB server.


2 Answers

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.

like image 42
Andrew M. Slack Avatar answered Sep 26 '22 04:09

Andrew M. Slack


I think there is some default config what is missing in this version of mongoDb client. Try to run:

mongo 127.0.0.1:27017 

It's strange, but then I've experienced the issue went away :) (so the simple command 'mongo' w/o any params started to work again for me)

[Ubuntu Linux 11.10 x64 / MongoDB 2.0.1] 
like image 151
JaKi Avatar answered Sep 24 '22 04:09

JaKi