couldn't connect to server 127.0.0.1 shell/mongo.js

94,734

Solution 1

  • Manually remove the lockfile: sudo rm /var/lib/mongodb/mongod.lock
  • Run the repair script: sudo -u mongodb mongod -f /etc/mongodb.conf --repair

Please note the following:

  • You must run this command as the mongodb user. If you run it as root, then root will own files in /var/lib/mongodb/ that are necessary to run the mongodb daemon and therefore when the daemon trys to run later as the mongodb user, it won't have permissions to start. In that case you'll get this error: Unable to create / open lock file for lockfilepath: /var/lib/mongodb/mongod.lock errno:13 Permission denied, terminating.
  • On Ubuntu, you must specify the configuration file /etc/mongodb.conf using the -f flag. Otherwise it will look for the data files in the wrong place and you will see the following error: dbpath (/data/db/) does not exist, terminating.

Solution 2

sudo rm /var/lib/mongodb/mongod.lock   
sudo -u mongodb mongod -f /etc/mongodb.conf --repair 
sudo service mongodb start

Here is all, sometimes, it takes a little while to start mongo after performing these operations.

Solution 3

Trying running $mongod

If you get en error such as

MongoDB shell version: 2.0.5
connecting to: test
Fri Jun  1 11:20:33 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed

hisham-agil:~ hisham$ mongod
mongod --help for help and startup options
Fri Jun  1 11:24:47 [initandlisten] MongoDB starting : pid=53452 port=27017 dbpath=/data/db/ 64-bit host=hisham-agil.local
Fri Jun  1 11:24:47 [initandlisten] db version v2.0.5, pdfile version 4.5
Fri Jun  1 11:24:47 [initandlisten] git version: nogitversion
Fri Jun  1 11:24:47 [initandlisten] build info: Darwin gamma.local 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:48:32 PST 2012; root:xnu-1699.24.23~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49
Fri Jun  1 11:24:47 [initandlisten] options: {}
Fri Jun  1 11:24:47 [initandlisten] exception in initAndListen: 10296 dbpath (/data/db/) does not exist, terminating
Fri Jun  1 11:24:47 dbexit: 
Fri Jun  1 11:24:47 [initandlisten] shutdown: going to close listening sockets...
Fri Jun  1 11:24:47 [initandlisten] shutdown: going to flush diaglog...
Fri Jun  1 11:24:47 [initandlisten] shutdown: going to close sockets...
Fri Jun  1 11:24:47 [initandlisten] shutdown: waiting for fs preallocator...
Fri Jun  1 11:24:47 [initandlisten] shutdown: lock for final commit...
Fri Jun  1 11:24:47 [initandlisten] shutdown: final commit...
Fri Jun  1 11:24:47 [initandlisten] shutdown: closing all files...
Fri Jun  1 11:24:47 [initandlisten] closeAllFiles() finished
Fri Jun  1 11:24:47 dbexit: really exiting now

Then you've run into a basic startup error that is pretty common.

By default mongod will try to use /data/db for its database files, which in this case, does not exist.

You can't start

mongo 

until you handle

mongod.

Try creating those directories and make sure they are writable by the same user that is running the mongod process.

**See similar question-- Getting an error, "Error: couldn't connect to server 127.0.0.1 shell/mongo.js" & when trying to run mongodb on mac osx lion

Solution 4

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

Solution 5

I got the same problem when I tried to install mongo. I got Error as,

Error

"Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84"

Solution:

First install mongod by using:

sudo apt-get install mongodb-server

Then type

mongod --dbpath /mongo/db

Then

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

Then

sudo -u mongodb mongod -f /etc/mongodb.conf  --repair

Thank You

Share:
94,734
zjm1126
Author by

zjm1126

Every morning when I opened my eyes ,the responsibility of deliver the earth falls on my shoulders.

Updated on July 08, 2022

Comments

  • zjm1126
    zjm1126 almost 2 years

    when i setup mongodb in my ubuntu , i try : ./mongo it show this error :

     couldn't connect to server 127.0.0.1 shell/mongo.js
    

    so what can i do ,

    thanks

  • Jmlevick
    Jmlevick over 12 years
    Same Solution applies to question: stackoverflow.com/questions/7958228/…
  • jaketrent
    jaketrent about 12 years
    Great link. Solved the problem exactly.
  • me_and
    me_and almost 12 years
    The OP is on Ubuntu, so I'm pretty sure they won't be using any .exe files.
  • ben author
    ben author almost 12 years
    So many times I get stumped and scratch my head for a long time, and finally realize: 'oh, I just have to sudo."
  • boulder_ruby
    boulder_ruby almost 12 years
    I'm getting this error message, and I can't find a "mongod.lock" on my file system. Do these instructions apply to Mac OSX?
  • user4951
    user4951 over 11 years
    how to run that as the mongodb user?
  • Fady Mohamed Othman
    Fady Mohamed Othman over 11 years
    Simply add yourself to the mongodb group: useradd -G mongodb fady replace fady with your user name.
  • ljs.dev
    ljs.dev over 11 years
    and for Debian/Ubuntu variants: sudo service mongodb start
  • Shahzeb Khan
    Shahzeb Khan over 11 years
    i run repair many times but didn't worked till i manually removed the .lock file (as you stated), its now working, but i just want to know that why manually removing .lock file work ?
  • Fady Mohamed Othman
    Fady Mohamed Othman over 11 years
    The lock file is supposed to prevent mongodb from running when database is in inconsistent state to prevent data corruption and in normal cases you shouldn't remove it manually.
  • Fady Mohamed Othman
    Fady Mohamed Othman over 11 years
    For more information you can refer to this page: mongodb.org/display/DOCS/Durability+and+Repair
  • Fabricio Buzeto
    Fabricio Buzeto over 11 years
    I've had to use both the information of this answer and from @boulder_ruby 's answer. This because my '/data/db/' was missing. Maybe it could be a good update for this answer to be more complete.
  • cbaigorri
    cbaigorri about 11 years
    you can also set the dbpath like so: mongod --dbpath ../data
  • DmitrySandalov
    DmitrySandalov almost 11 years
    My mongod was not started after the repair script on Ubuntu 12.10. Had to start it manually: sudo service mongodb start
  • FRD
    FRD over 10 years
    How come there are no upvotes to this? This was the only solution, among half a dozen SO entries that I looked, that worked for me. Thank you, sir.
  • Radim Köhler
    Radim Köhler over 10 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
  • Chinmay
    Chinmay over 10 years
    @RadimKöhler, thanks for heads up. I have corrected the answer. Hope this will help someone. Cheers.
  • ccsakuweb
    ccsakuweb about 10 years
    @fadymohamedosman if I use my mongodb user in sudo -u I get errors of permission denied of mongod.log file modification.
  • okoboko
    okoboko almost 10 years
    The command to start with the config file and force a repair is sudo -u mongodb mongod -f /etc/mongod.conf --repair on Ubuntu 14.
  • Evan
    Evan over 8 years
    Will this method delete the existing databases in mongo?
  • Aleksandrus
    Aleksandrus almost 8 years
    Mon Aug 22 11:28:17.919 Can't specify both --journal and --repair options.
  • Aleksandrus
    Aleksandrus almost 8 years
    If I try ` sudo -u mongodb mongod -f /etc/mongodb.conf --repair` here is what i get: Mon Aug 22 11:28:17.919 Can't specify both --journal and --repair options.
  • Fady Mohamed Othman
    Fady Mohamed Othman almost 8 years
    Why are you specifying the --journal option?