86

while trying this mongo command in ubuntu I am getting this error.

    ritzysystem@ritzysystem-Satellite-L55-A:~$ mongo
    MongoDB shell version: 2.6.1
    connecting to: test
    2014-10-06T12:59:35.802+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
    2014-10-06T12:59:35.802+0530 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
    exception: connect failed

how can I rectify this is anyone had the same problem.

Ashish Ranjan
  • 1,169
  • 1
  • 9
  • 8
  • 6
    `mongod` server is not running, turn it up by typing `mongod` on terminal. – Ravi Oct 06 '14 at 07:32
  • 2
    Your mongod is not running on 127.0.0.1 Can you try like this "mongo --host localhost" – Lalit Agarwal Oct 06 '14 at 07:33
  • 1
    thank you everyone I tried running mongo it was not running finally I found there was no space in my hard disc I emptied some of the space and finally re installed it the same installation procedure as followed to install as given in mongodb docs finally restarted the server and it is running. http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ – Ashish Ranjan Oct 06 '14 at 09:14
  • 4
    you can answer your own question (rather than post a comment). The solution would be more visible and it will help others – mihai Oct 06 '14 at 10:39
  • is IP address appear here (VPS available IPs): `ifconfig -a | grep "inet"` is IP listened by mongo? `sudo lsof -i | grep mongo` If not, there is system corruption because even you define your mongod.conf file, it won't be listening your defined values. Also make sure to allow mongo and port for firewall to pass `sudo ufw allow yourport` and `sudo ufw app list` to see if it's allowed app among others – Bitfinicon Jul 02 '22 at 06:31
  • Maybe it's not required mongodb app to be listed as an app because it uses ports – Bitfinicon Jul 02 '22 at 06:39

27 Answers27

101

Run the following command :

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

Connection refused to MongoDB errno 111

MacOS:

rm /usr/local/var/mongodb/mongod.lock  
sudo service mongod restart
Community
  • 1
  • 1
Jagdish Barabari
  • 2,673
  • 3
  • 20
  • 21
33

I had the same problem on Mac OS Try to run sudo mongod and in a new terminal tab run mongo

levon
  • 689
  • 6
  • 13
22

thank you everyone I tried running mongo it was not running finally I found there was no space in my hard disc I emptied some of the space and finally re installed it the same installation procedure as followed to install as given in mongodb docs finally restarted the server and it is running.

https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu

Alison R.
  • 4,204
  • 28
  • 33
Ashish Ranjan
  • 1,169
  • 1
  • 9
  • 8
  • I don't know why you are reinstalled. I think its enough to free some space by deleting unwanted files and then you might want to restart mongod service, because the mongod service might be killed due to no space. is there any specific reason to reinstall?. – Siva S Jan 23 '18 at 06:30
19

In my case the error was due to missing /data/db folder which the mongodb uses to store your data. Please type this command $sudo mongod in your terminal. If the error message is something like :

missing data/db folder error

Simply create the folder and you are good to go.

Sourav Prem
  • 1,063
  • 13
  • 26
11

First, Start MongoDB:

sudo service mongod start

Then, Run:

mongo
Akash Adhikari
  • 393
  • 4
  • 13
9

Probably you don't have space in your hard drive. Check it by typing in the promt df -h

Please note that mongo might fail even with 3gb available in the corresponding partition. For details, you might want to check the log: cat /var/log/mongodb/mongod.log

josinalvo
  • 1,366
  • 13
  • 26
Pablo Tapia
  • 353
  • 2
  • 10
  • "mongodb.log" had a typo and is written as "mongod.log". I tried to edit but stackoverflow told me I had to edit at least 6 chars. Bad idea imo SO. – Robert Bracco May 07 '20 at 13:22
8

Make sure you have run the MongoDB's Service before you want to connect the console.

run and start the server:

$ sudo mongod

then:

$ mongo
...
>
GuoX
  • 421
  • 5
  • 8
8

I had the same problem on my Mac, and I installed mongodb via homebrew. I solve this problem by the homebrew services command.

First start mongodb service:

$ brew services start mongodb

Start mongodb terminal

$ mongo

Stop mongodb service:

$ brew services stop mongodb

Yingbo Cui
  • 259
  • 3
  • 5
5

Do this:

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

If you are on Ubuntu 16.04 which you can figure out by runnign this:

lsb_release -a

You need to Create a new file at /lib/systemd/system/mongod.service with the following contents:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
Iman Mohamadi
  • 6,552
  • 3
  • 34
  • 33
4

In my case the problem was caused due to an apparent lost of permission over mongodb.lock file. I could solve the problem changing the permission with the following command :

sudo chown mongodb:mongodb /var/lib/mongodb/mongodb.lock

There follows my investigation: step-by-step verification

pablo.vix
  • 2,103
  • 2
  • 15
  • 12
4

I think except for disk space issuse, you should check the log in /var/log/mongodb to know the details for why mongodb start failed.

cat /var/log/mongodb/mongod.log

2016-06-26T15:26:26.642+0800 I CONTROL  [main] ***** SERVER RESTARTED *****
2016-06-26T15:26:26.649+0800 I CONTROL  [initandlisten] MongoDB starting : pid=8130 port=27017 dbpath=/var/lib/mongodb 64-bit host=hadoop-master
2016-06-26T15:26:26.649+0800 I CONTROL  [initandlisten] db version v3.2.7
2016-06-26T15:26:26.649+0800 I CONTROL  [initandlisten] git version: 4249c1d2b5999ebbf1fdf3bc0e0e3b3ff5c0aaf2
2016-06-26T15:26:26.649+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
2016-06-26T15:26:26.649+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2016-06-26T15:26:26.649+0800 I CONTROL  [initandlisten] modules: none
2016-06-26T15:26:26.649+0800 I CONTROL  [initandlisten] build environment:
2016-06-26T15:26:26.649+0800 I CONTROL  [initandlisten]     distmod: ubuntu1404
2016-06-26T15:26:26.649+0800 I CONTROL  [initandlisten]     distarch: x86_64
2016-06-26T15:26:26.649+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2016-06-26T15:26:26.649+0800 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1,192.168.3.10", port: 27017 }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2016-06-26T15:26:26.678+0800 E NETWORK  [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted
2016-06-26T15:26:26.678+0800 I -        [initandlisten] Fatal Assertion 28578
2016-06-26T15:26:26.678+0800 I -        [initandlisten] 

***aborting after fassert() failure

So, here I need to rm all the files in the /tmp. That works fine for me.

GoingMyWay
  • 16,802
  • 32
  • 96
  • 149
4

For mongo v3.6.3+ (or 2019 versions)

rm /var/lib/mongodb/mongod.lock
service mongodb restart
penta
  • 2,536
  • 4
  • 25
  • 50
3

I understand the question is with regards to Ubuntu.

But if you're getting the same error on a Mac and don't want $ sudo mongod running on a separate tab, you can do the following to fix the connection error, if you have Homebrew -

brew services start mongodb

Based on the answer from this thread - Cannot connect to mongodb errno:61 Connection refused

Community
  • 1
  • 1
Aswin Ramakrishnan
  • 3,195
  • 2
  • 41
  • 65
3

In my case bind_ip was not 127.0.0.1 in /etc/mongodb.conf file so change bind_ip to 127.0.0.1(it may be comma separated values so make sure 127.0.0.1 is one of them) Then restart your system to take effect. Restart only to the those who are facing

$sudo service mongod restart
Failed to restart mongod.service: Unit mongod.service not found.
Mr code.
  • 307
  • 2
  • 17
2

Follow this simple steps; (Works on MAC OS too)

  1. Open terminal and run sudo mongod

  2. Open a new terminal tab(Don't close step 1 tab) and run sudo mongo

That's all

Victor Ejiogu
  • 1,184
  • 14
  • 23
1

is very simple, only delete a file /var/lib/mongodb/mongodb.lock. after only execute: mongo. finished

1

It is possible that the data directory for mongo specified in /etc/mongod.conf is not a valid path.

Try rechecking the path if it really exists sudo vi /etc/mongod.conf and check dbPath.

Mike
  • 14,010
  • 29
  • 101
  • 161
1

Just some thoughts on my case.

If you have changed the dbPath and logPath dirs to your custom values (say /data/mongodb/data, /data/mongodb/log), you must chown them to mongodb user, otherwise, the non-existent /data/db/ dir will be used.

sudo chown -R mongodb:mongodb /data/mongodb/

sudo service mongod restart
Aram Paronikyan
  • 1,598
  • 17
  • 22
1

I have the same error with you, this is my case:

~# mongod
2018-07-15T05:27:08.265+0000 I JOURNAL  [initandlisten] journal dir=/data/db/journal
2018-07-15T05:27:08.265+0000 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2018-07-15T05:27:08.301+0000 I JOURNAL  [durability] Durability thread started
2018-07-15T05:27:08.301+0000 I JOURNAL  [journal writer] Journal writer thread started
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] MongoDB starting : pid=26796 port=27017 dbpath=/data/db 64-bit host=ubuntu-s-2vcpu-4gb-sfo2-01
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] 
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] 
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] 
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] db version v3.0.6
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] git version: 1ef45a23a4c5e3480ac919b28afcba3c615488f2
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] build info: Linux build6.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2018-07-15T05:27:08.302+0000 I CONTROL  [initandlisten] options: {}
2018-07-15T05:27:08.308+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

I type mongod to start the server, and I type control + c to exit to shell

then I type mongo and I got

~# mongo
MongoDB shell version: 3.0.6
connecting to: test
2018-07-15T05:05:02.738+0000 W NETWORK  Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2018-07-15T05:05:02.739+0000 E QUERY    Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
    at connect (src/mongo/shell/mongo.js:179:14)
    at (connect):1:6 at src/mongo/shell/mongo.js:179

As you can see, my error info is same with you.

This is case mongod doesn't start with backend process, when I type control + c I quit mongod.

We can add --fork args to make the process daemon process.

#  mongod --logpath /usr/local/mongodb/log.txt --fork

you have to set --logpath if you want to use --fork

then you will success to connect to mongo

Shuai Li
  • 2,426
  • 4
  • 24
  • 43
1

I faced the same problem, so please check is there mongodb folder, firstly try to remove that folder

rm -rf /var/lib/mongodb/*

Now try to start mongo and if same problem then its mongodb lock file which prevent to start mongo, so please delete the mongo lock file

rm /var/lib/mongodb/mongod.lock

For forcefully rm -rf /var/lib/mongodb/mongod.lock

service mongodb restart if already in sudo mode otherwise you need to use like sudo service mongod start

or you can start the server using fork method

mongod --fork --config /etc/mongod.conf

and for watching the same is it forked please check using the below command

ps aux | grep mongo
VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
0

Mongo 3.*.* - OSX - 2017

From README

RUNNING

Change directory to mongodb-osx-x86_64-3.*.*/bin

To run a single server database:

$ mkdir /data/db
$ ./mongod

Go to new Terminal

$ # The mongo javascript shell connects to localhost and test database 
$ # by default -Run the following command in new terminal
$ ./mongo 
> help
Mina Gabriel
  • 23,150
  • 26
  • 96
  • 124
0

I solved that simply by entering sudo mongo after mongod command.

pavle
  • 909
  • 14
  • 38
0

I just removed mongo following the instructions here: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ And then I installed again following the instructions in the given link. Everything worked smoothly!

David I. Rock
  • 95
  • 2
  • 4
0

There are changes in mongod.conf file in the latest MongoDB v 3.6.5 +

Here is how I fixed this issue on mac os High Sierra v 10.12.3

Note: I assume that you have installed/upgrade MongoDB using homebrew

mongo --version

MongoDB shell version v3.6.5 
git version: a20ecd3e3a174162052ff99913bc2ca9a839d618 
OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018 
allocator: system modules: none build environment:
distarch: x86_64
target_arch: x86_64
  1. find mongod.conf file

     sudo find / -name mongod.conf` 
    

    /usr/local/etc/mongod.conf > first result .

  2. open mongod.conf file

    sudo vi /usr/local/etc/mongod.conf
    
  3. edit in the file for remote access under net: section

     port: 27017  
     bindIpAll: true 
     #bindIp: 127.0.0.1 // comment this out
    
  4. restart mongodb

    if you have installed using brew than

    brew services stop mongodb
    
    brew services start mongodb
    

otherwise, kill the process.

   sudo kill -9 <procssID>
Community
  • 1
  • 1
xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • 2
    To people reading this answer: CAREFUL! Unless I'm mistaken, commenting out `bindIp` will expose your databases to the open internet unless you've got authorization enabled (it isn't enabled by default). –  Aug 30 '18 at 18:42
0

Issue was with the connection to the database, it could be due to several reasons. For me it was low disk space on my server machine, and when mongo tries to connect it refuse because of low disk, check your server machine local storage with du -sh if you have low storage space, check your logs size and take step accordingly, issue may exist because of dbpath contradict in /etc/mongod.conf and server actually running dbpath with mongod If you are facing such type of issue, please check my answer in the given link below. https://stackoverflow.com/a/53057695/8247133

Touseef Murtaza
  • 1,548
  • 14
  • 20
0

For those with Windows. I had the same problem on Windows. Restarting Mongodb service solves this.

Following are the steps to restart on Windows:

i). Go To "Services" on Windows.

ii). Search for "MongoDB" in the list of services.

iii). Right click "start"/"restart" depending on your status.

iv). After the status turns to "running" check if issue is resolved by running "Mongo" or connecting your Mongo client.

varad_s
  • 764
  • 1
  • 12
  • 24
0

For macOS many of the answers are already outdated according to official docs. Somehow the brew has changed and how we should install MongoDB, firstly uninstall it from your macOS (even though it might be not necessary) and then install it using these steps:

  1. brew tap mongodb/brew
  2. brew install mongodb-community@4.4
  3. brew services start mongodb-community@4.4

Watch out for the 4.4 part, it'll change. If in the future it would break then refer to the official docs and install the version, which is suggested in the linked tutorial.

Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94