Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to solve [MongoError: connect ECONNREFUSED]

    • 0
    • 1
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 13.5k
    Comment on it

    Hello Readers,

    I came across a solution while working on mongodb and would like to share with you all in case you face any such problem while working on mongodb.

    If you are coming across the error like the following:

    Connection error:  { [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }

    while trying to connect to mongodb on Ubuntu, you can use the following process to resolve it and gain the mongodb connection again.

    Reason behind the above error

    The above type of error could occur If you forget to close the database connection after you finish your work last time or If your system/server got crashed during any work process.

    Actually the mongodb keeps a lock file while it's running or started and delete the file when stopped. But when the system got crashed or you close the terminal without closing the mongodb server, it keeps the old lock file and terminate / shutdown mongodb whenever you start the server next time and tried to connect to database. You can check its log file in /var/log/mongodb/mongod.log for the following line:

    2015-05-11T09:07:28.216+0530 [initandlisten] exception in initAndListen: 12596 old lock file, terminating
    2015-05-11T09:07:28.216+0530 [initandlisten] dbexit: 
    2015-05-11T09:07:28.216+0530 [initandlisten] shutdown: going to close listening sockets...
    2015-05-11T09:07:28.216+0530 [initandlisten] shutdown: going to flush diaglog...
    2015-05-11T09:07:28.216+0530 [initandlisten] shutdown: going to close sockets...
    2015-05-11T09:07:28.216+0530 [initandlisten] shutdown: waiting for fs preallocator...
    2015-05-11T09:07:28.216+0530 [initandlisten] shutdown: closing all files...
    2015-05-11T09:07:28.216+0530 [initandlisten] closeAllFiles() finished
    2015-05-11T09:07:28.216+0530 [initandlisten] dbexit: really exiting now

    The following text above clearly shows the existance of old lock file which terminating the connection.


    1. To resolve the issue the safest process is to to follow MongoDB's Durability and Repair guide and to do that you can use the following command in the terminal.

      sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/
      and then..
      sudo service mongod start
    2. -OR- If this is the starting of your project and you have not been using the database, then you can remove the lock file manually.

    Hope this could be helpful and save some of your valuable time. Keep Reading..

 1 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: