未能连接到127.0.0.1:27017,原因: errno: 111 Connection 拒绝

在 Ubuntu 中尝试这个 mongo 命令 我得到了这个错误。

    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

我怎样才能纠正这个问题,是任何人都有同样的问题。

195493 次浏览

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

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

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

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

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
...
>

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

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

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.

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

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.

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

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.

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

First, Start MongoDB:

sudo service mongod start

Then, Run:

mongo

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

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

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!

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

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>

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

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.

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

For mongo v3.6.3+ (or 2019 versions)

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

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

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

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.

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.