Errr 'mongo.js:L112 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112'

36,449

Solution 1

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

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

Solution 2

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!

Solution 3

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.

Solution 4

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.

Solution 5

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 !

Share:
36,449
user1992275
Author by

user1992275

Updated on June 14, 2020

Comments

  • user1992275
    user1992275 about 4 years

    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.