33
votes

I can't connect to MongoDB. In Ubuntu it works, but I'm working in CentOS now. This is the error message:

MongoDB shell version: 2.4.2
connecting to: test
Sat Apr 20 07:22:09.390 JavaScript execution failed: Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112
exception: connect failed

I tried removing the mongod.lock file, but it doesn't work.

8
Sounds like you haven't started mongod: docs.mongodb.org/manual/tutorial/…WiredPrairie

8 Answers

34
votes

Try to remove /var/lib/mongodb/mongod.lock and restart mongdo service

sudo rm /var/lib/mongodb/mongod.lock
sudo service mongodb restart
78
votes

If you are running Ubuntu, then there is an issue with folder ownership.

Run these commands:

  1. Stop the MongoDB service

    sudo service mongodb stop
    
  2. Remove the MongoDB lock file

    sudo rm /var/lib/mongodb/mongod.lock
    
  3. Change ownership from root to the MongoDB path

    sudo chown -R mongodb:mongodb /var/lib/mongodb/
    
  4. Start the MongoDB service

    sudo service mongodb start
    
  5. Test the mongo application

    mongo
    

Then you will be able to execute successfully (I hope).

Reference: an answer on Stack Exchange site Database Administrators to Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91 when changing mongodb data directory!

16
votes

This method only works if you want to repair your data files without preserving the original files. To find where you dbpath resides, use

vim /etc/mongodb.conf

Check for option dbpath=.

(I have dbpath=/var/lib/mongodb.)

Default: /data/db/

Typical locations include /srv/mongodb, /var/lib/mongodb or /opt/mongodb.

Replace the /var/lib/mongodb with your dbpath

sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --dbpath /var/lib/mongodb/ --repair
sudo mongod --dbpath /var/lib/mongodb/ --journal

(Make sure that you leave you terminal running in which you have run the above lines. Don't press Ctrl + C or quit it.) Type the command to start mongo now in another window.

I hope this works for you! For those who want to repair your data files while preserving the original files, see mongo recover.

3
votes

I had the same problem in the past. In my case, I had insufficient free space for journal files. Freeing some space solved the problem.

2
votes

I faced the same problem. Running mongod inside bin (where you have your mongodb installed) worked for me. I also had insufficient space.

2
votes

I don't know why @lkrzysiak answer was downvoted: it worked for me ! Insufficient free space CAN be the source of this error that I got too, see the logs I got:

    Mon Aug 12 17:02:59.159 [initandlisten] recover : no journal files present, no recovery needed
    Mon Aug 12 17:02:59.159 [initandlisten] 
    Mon Aug 12 17:02:59.159 [initandlisten] ERROR: Insufficient free space for journal files
    Mon Aug 12 17:02:59.159 [initandlisten] Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles
    Mon Aug 12 17:02:59.159 [initandlisten] 
    Mon Aug 12 17:02:59.159 [initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
    Mon Aug 12 17:02:59.159 dbexit: 
    Mon Aug 12 17:02:59.159 [initandlisten] shutdown: going to close listening sockets...

After some cleaning to free some space, it worked like a charm !

0
votes

You also get this error if you've changed the default port for mongoDB in /etc/mongo.conf

To connect via the shell in this case use:-

$mongo 127.0.0.1:your_new_port_number

OR

$mongo -u <user> -p <pass> --host <host> --port your_new_port_number

from MongoDB docs

0
votes

It could be combination of $PATH and Permission issue.

Try following steps given below:

Update your $PATH variable to point to your MongoDB bin file. In my case brew install MongoDB to this folder:

/usr/local/Cellar/mongodb/2.4.6/

In order to update your $PATH variable, do following:

$ sudo vi /etc/paths

Then, press ‘i’ to insert text in Vi and append the your MongoDB path to the end of the ‘paths’ file and restart the terminal.

/usr/local/Cellar/mongodb/2.4.6/bin

Use ‘Esc : w q’ to save and exit from Vi editor.

Use echo to display your path variable:

$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/Cellar/mongodb/2.4.6/bin

Now try to check the Mongo version, if you get following, then you are on the right track!

$ mongo --version
MongoDB shell version: 2.4.6

Now we need to create the database directory. I used the default ‘/data/db’ location suggested in MongoDB docs. I also created a log directory to avoid any permission issues while Mongo tries to create any logs. Change ownership and that will do the job.

$ sudo mkdir /data/db
$ sudo mkdir /data/log
$ whoami
username
$ chown -R username /data

Now, we will create a default config file for MongoDB to be provided for the first time we run ‘mongod’ command. Now, I will also like to point out that ‘mongod’ will start a service, which will listen for incoming data connections. This is similar having ‘$service mysqld start’ executed.Let’s go ahead and create the config file. Please keep in mind that I have created very basic config file. However, you can add many other variables to configure MongoDB. This is the first time I am playing with MongoDB, so I just know as much as I read on MongoDB docs!I created ‘mongodb.conf’.

$ sudo vi /etc/mongodb.conf

Add following:

fork = true
port = 27017
quiet = true
dbpath = /data/db
logpath = /data/log/mongod.log
logappend = true
journal = true

Please note that the default port for MongoDB server is 27017. Use your own path for dbpath and logpath you created in Step – 5. Don’t forget to close and save the conf file.

Now we are all set to start our MongoDB service. Open two instances of Terminal.In Terminal 1, type in:

$ sudo mongod -f /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3516
all output going to: /data/log/mongod.log
child process started successfully, parent exiting

If you get above message, then know that you have successfully started your Mongod service.

Now, to connect to it, in Terminal 2 type following:

$mongo test
MongoDB shell version: 2.4.6
connecting to: test
Server has startup warnings:
Tue Sep 3 16:55:43.527 [initandlisten]
Tue Sep 3 16:55:43.527 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>

Ignore the warnings, but you are successfully connected to the ‘test’ database! Cool!

That's all. I applied this solution, when I tried to install copy of MongoDB on my Mac for the first time. See if this help you too.

For detailed post you can go here - http://arcanebytes.com/2013/09/03/mongodb-installation-on-mac-os-x/#comment-1036112094.

I hope it helps!

Cheers, Chinmay