15

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.

Archana Sharma
  • 1,953
  • 6
  • 33
  • 65
  • It seems that your server did not start here. Did you try to see your mongod status using `sudo service mongod status` ? – aveuiller Sep 27 '18 at 11:43
  • yes I checked it's status and found there is some error in process:: `Process: 14618 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=100)` – Archana Sharma Sep 27 '18 at 12:00

9 Answers9

27

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

Archana Sharma
  • 1,953
  • 6
  • 33
  • 65
9

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
Deeksha Sharma
  • 3,199
  • 1
  • 19
  • 16
8

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
PravyNandas
  • 607
  • 11
  • 12
7

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.

Chaitanya
  • 237
  • 2
  • 11
  • I didn't created any systemd service for mongo. MongoDB is installed on my local system and I am trying to start this, but it is giving me this error again and again – Archana Sharma Sep 27 '18 at 11:39
  • Have you tried `mongod --repair` and then start mongodb server? – Chaitanya Sep 27 '18 at 11:43
  • I got it, but still there is a question why it's not running with the previous commands – Archana Sharma Sep 27 '18 at 11:59
  • Most probably this was because of an unclean shutdown of mongod instance which leaves data in an inconsistent state. Explore [this](https://docs.mongodb.com/manual/tutorial/recover-data-following-unexpected-shutdown/) for more. – Chaitanya Sep 27 '18 at 12:10
  • With mongod --shutdown. You can its detail [here](https://docs.mongodb.com/manual/tutorial/manage-mongodb-processes/). – Chaitanya Sep 27 '18 at 12:25
6

Windows

  • Open task manager.
  • click on the services tab.
  • Search for MongoDB service.
  • After selecting click on the 'view services' tab.
  • Search for MongoDB server.
  • After Selecting click on the Run button on the top left side menu.

And you are good to go.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
IamVk009
  • 61
  • 1
  • 1
5

On MacOS , I executed the 4th step of this https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x-tarball/

tutorial with sudo

sudo mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork

and It worked!

2

For windows,

1)Right click on the task bar and select Task Manager.

2)Select server at the given menu

3)Search mongoDB and select that and right click (click open services).

4)Search mongoDB Server, select that and click "start" at the left side window

Now it's ready to go

Dakshan
  • 21
  • 2
0

MAC: For my case I forgot to turn on mongo server locally, so I fixed the issue by firstly open a new tab on terminal then turn on mongodb server like so: -

mongod 

After that just try to open mongo shell again like so: -

mongo 

Finally it worked!

Mussa Charles
  • 4,014
  • 2
  • 29
  • 24
0

i too got the same problem.here is the way how i resoleved.go to taskmanager.select>services and find mongodbserver in the list make sure the mongodb is running by selecting it.

image source

Happy Man
  • 39
  • 7