[shell] MongoDB running but can't connect using shell

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

This question is related to shell mongodb

The answer is


After starting the mongod

$mongod --dbpath <db name>

to get shell

$mongo --shell

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]

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


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)


I had this problem as well. Is your MongoDB journaling? I noticed the following "preallocate" entries in the log file. Once I saw the last line "waiting for connections on port", I could connect. Notice that this "faster" mode took 12 minutes to intialize.

William

Tue Apr 17 16:48:01 [initandlisten] MongoDB starting : pid=2248 port=27017 dbpath=E:\MongoData 64-bit host=ME
Tue Apr 17 16:48:01 [initandlisten] db version v2.0.0-rc0, pdfile version 4.5
Tue Apr 17 16:48:01 [initandlisten] git version: 8d4bf50111352cee5a4f1abf25b63442d6c45dc4
Tue Apr 17 16:48:01 [initandlisten] build info: windows (6, 1, 7601, 2, 'Service Pack 1') BOOST_LIB_VERSION=1_42
Tue Apr 17 16:48:01 [initandlisten] options: { bind_ip: "ip", dbpath: "E:\MongoData", directoryperdb: true, journal: true, logpath: "E:\MongoData\mongo.log", quiet: true, rest: true, service: true }
Tue Apr 17 16:48:01 [initandlisten] journal dir=E:/MongoData/journal
Tue Apr 17 16:48:01 [initandlisten] recover : no journal files present, no recovery needed
Tue Apr 17 16:48:02 [initandlisten] preallocateIsFaster=true 9.68
Tue Apr 17 16:48:04 [initandlisten] preallocateIsFaster=true 8.44
Tue Apr 17 16:48:06 [initandlisten] preallocateIsFaster=true 9.68
Tue Apr 17 16:48:06 [initandlisten] preallocateIsFaster check took 4.921 secs
Tue Apr 17 16:48:06 [initandlisten] preallocating a journal file E:/MongoData/journal/prealloc.0
Tue Apr 17 16:52:37 [initandlisten] preallocating a journal file E:/MongoData/journal/prealloc.1
Tue Apr 17 16:56:54 [initandlisten] preallocating a journal file E:/MongoData/journal/prealloc.2
Tue Apr 17 17:01:42 [initandlisten] waiting for connections on port 27017
Tue Apr 17 17:01:42 [websvr] admin web console waiting for connections on port 28017

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.


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.


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


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.


If your mongoDB server(remote server)'s version is greater then 4.0.3, then you will face this issue. Hence you should replace your current mongo-client shell with below mongo :

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org

Then you mongo client will be able to connect your remove mongodb


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.


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


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.


I don't see this having an accepted answer yet, so I'll just add my 2 cents.

I had the exact same issue just now. After a while I realized I've locked localhost out in my iptables rules. So, check your firewall.


I had same problem. In my case MongoDB server wasn't running.

Try to open this in your web browser:

http://localhost:28017

If you can't, this means that you have to start MongoDB server.

Run mongod in another terminal tab. Then in your main tab run mongo which is is the shell that connects to your MongoDB server.


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

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 ;)