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

前端 未结 13 2049
后悔当初
后悔当初 2020-11-29 19:07

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

相关标签:
13条回答
  • 2020-11-29 19:26
    • 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.
    0 讨论(0)
  • 2020-11-29 19:29

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

    0 讨论(0)
  • 2020-11-29 19:33

    First you have to make sure that all the files and directories in your /var/lib/mongodb/ folder (or whichever folder dbpath points to) belong to the mongodb user and mongodb group.

    cd /var/lib/mongodb/
    sudo chown mongodb filename.*
    sudo chgrp mongodb filename.*
    sudo chown -R mongodb directory
    sudo chgrp -R mongodb directory
    

    (Replace filename and directory with their respective names)

    Then you can remove the lock, repair the database and restart the daemon as other people already mentioned:

    sudo rm /var/lib/mongodb/mongod.lock   
    sudo -u mongodb mongod -f /etc/mongodb.conf --repair 
    sudo service mongodb start
    
    0 讨论(0)
  • 2020-11-29 19:34

    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

    0 讨论(0)
  • 2020-11-29 19:40

    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

    0 讨论(0)
  • 2020-11-29 19:40

    On Ubuntu, try this:

    sudo invoke-rc.d mongodb start
    
    0 讨论(0)
提交回复
热议问题