Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldn't connect to server 127.0.0.1:27017 connection attempt failed MongoDB

Tags:

mongodb

I am working on Ubuntu OS 16.04. I am starting mongodb using commands:: sudo service mongod start and then mongo

It generated this error for me::

MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017
2018-09-27T16:50:41.345+0530 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

Now I am not able to start mongodb. How to resolve this error and what are the causes of these error. I tried all of solution for this problem but didn't find any solution.

like image 452
Archana Sharma Avatar asked Sep 27 '18 11:09

Archana Sharma


People also ask

Why is my MongoDB not connecting?

Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.

Can't connect to server connection refused MongoDB?

Normally this caused because you didn't start mongod process before you try starting mongo shell. This case worked out for me, i had to do ~>sudo mongod --dbpath /dbpathlocation and then opened the new terminal to run mongo...

Why is MongoDB not connecting to Atlas?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.


4 Answers

Unfortunately, none of the above solutions worked for me. Looking at the mongodb logs, it seems the .sock socket file could not be unlinked, which is restricting the service from restarting. Doing the unlink manually and restarting is actually worked for me.

Checking last 100 lines of mongodb logs:

sudo tail -100 /var/log/mongodb/mongod.log


{"t":{"$date":"2021-04-07T22:11:44.059-04:00"},"s":"E",  "c":"NETWORK",  "id":23024,   "ctx":"initandlisten","msg":"Failed to unlink socket file","attr":{"path":"/tmp/mongodb-27017.sock","error":"Operation not permitted"}}
{"t":{"$date":"2021-04-07T22:11:44.059-04:00"},"s":"F",  "c":"-",        "id":23091,   "ctx":"initandlisten","msg":"Fatal assertion","attr":{"msgid":40486,"file":"src/mongo/transport/transport_layer_asio.cpp","line":919}}
{"t":{"$date":"2021-04-07T22:11:44.059-04:00"},"s":"F",  "c":"-",        "id":23092,   "ctx":"initandlisten","msg":"\n\n***aborting after fassert() failure\n\n"}

Unlinking manually

sudo unlink /tmp/mongodb-27017.sock

Followed by start command

 sudo systemctl start mongod

A successful status returned :)

$ sudo systemctl status mongod
 mongod.service - MongoDB Database Server
 Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
 Active: active (running) since Wed 2021-04-07 22:47:23 EDT; 6s ago
   Docs: https://docs.mongodb.org/manual
 Main PID: 96418 (mongod)
 Memory: 177.0M
 CGroup: /system.slice/mongod.service
         └─96418 /usr/bin/mongod --config /etc/mongod.conf
like image 193
PravyNandas Avatar answered Oct 18 '22 02:10

PravyNandas


This worked for me (MAC):

Step 1 : Check the status if MongoDB is running or not

brew services list | grep mongo

Step 2 : if you get something like :

Name           Status  User    Plist
mongodb        stopped

Step 3: : start the service:

brew services start mongodb 

else :

if you get like this after step 2:

Name           Status  User    Plist
mongodb        started u1 /Users/u1/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

then it is already running then you can restart your service by

brew services restart mongodb
like image 27
Deeksha Sharma Avatar answered Oct 18 '22 01:10

Deeksha Sharma


mongod --repair worked for me ,

sudo mongod --repair
sudo mongod

Then open a different tab/terminal:

mongo

After this your local setup will work properly

like image 25
Archana Sharma Avatar answered Oct 18 '22 01:10

Archana Sharma


This error occurs when your mongodb server is not running on 27017 port. Try the below command to start the mongodb server.

sudo systemctl start mongod

And after running this command run the mongo command.

like image 7
Chaitanya Avatar answered Oct 18 '22 01:10

Chaitanya