拒绝连接 MongoDB errno 111

我有一个运行 Ubuntu 12.04 LTS 和 MongoDB 实例的 Linode 服务器(服务正在运行,CAN 在本地连接) ,我无法从外部源连接到它。

我已经将这两条规则添加到我的 IP 表中,其中 < IP address > 是我想连接 FROM 的服务器(如本 MongoDB 引用所述) :

Iptables-A INPUT-s < ip-address >-p tcp-target-port 27017-m state-state NEW,ESTABLISHED-j ACCEPT

Iptables-A OUTPUT-d < ip-address >-p tcp-source-port 27017-m state-state ESTABLISHED-j ACCEPT

我看到我的 IP 表中的规则允许27017与 < IP 地址 > 之间的连接,但是当我尝试使用这样的命令从 < IP 地址 > 连接到我的 mongo 数据库时:

mongo databasedomain/databasename -u username -p password

我得到了这个错误:

2014-07-22T23:54:03.093.0000警告: 无法连接到 database eserverip: 27017,原因: errno: 111 Connection 拒绝 2014-07-22T23:54:03.094 + 0000错误: 无法连接到服务器 < ip 地址 > : 27017(database aseserverip) ,连接尝试在 src/mongo/shell/mongo.js: 148失败 异常: 连接失败

任何帮助都非常感谢! ! ! 谢谢! ! !

233422 次浏览

Try the following:

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

Thanks for the help everyone!

Turns out that it was an iptable conflict. Two rules listing the port open (which resulted in a closed port).

However, one of the comments by aka and another by manu2013 were problems that I would have run into, if not for the conflict.

So! Always remember to edit the /etc/mongod.conf file and set your bind_ip = 0.0.0.0 in order to make connections externally.

Also, make sure that you don't have conflicting rules in your iptable for the port mongo wants (see link on mongodb's site to set up your iptables properly).

One other option is to just repair your database like so (note: db0 directory should be pre-created first):

mongod --dbpath /var/lib/mongodb/ --repairpath /var/lib/mongodb/db0

This is also an acceptable option in production environments...

These commands fixed the issue for me,

sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --repair
sudo service mongod start
sudo service mongod status

If you are behind proxy, use:-
export http_proxy="http://username:password@company.com:port/"
export https_proxy="http://username:password@company.com:port/"

Reference: https://stackoverflow.com/a/24410282/4359237

For Ubuntu Server 15.04 and 16.04 you need execute only this command

sudo apt-get install --reinstall mongodb

I Didn't have a /data/db directory. I created one and gave a chmod 777 permission and it worked for me

I also had the same issue.Make a directory in dbpath.In my case there wasn't a directory in /data/db .So i created one.Now its working.Make sure to give permission to that directory.

In my case previous version was 3.2. I have upgraded to 3.6 but data files was not compatible to new version so I removed all data files as it was not usable for me and its works.

You can check logs using /var/log/mongodb

I follow this tutorial's instructions for installation

How to Install MongoDB on Ubuntu 16.04

I had the same mistake. Finally, I found out that I needed to set the port number

The default port number for the mongo command is 27017

But the default port number in mongo.conf is 29999

Even though the port is open, MongoDB is currently only listening on the local address 127.0.0.1. To allow remote connections, add your server’s publicly-routable IP address to the mongod.conf file.

Open the MongoDB configuration file in your editor:

sudo nano /etc/mongodb.conf

Add your server’s IP address to the bindIP value:

...
logappend=true


bind_ip = 127.0.0.1,your_server_ip
#port = 27017


...

Note that now everybody who has the username and password can login to your DB and you want to avoid this by restrict the connection only for specific IP's. This can be done using Firewall (read about UFW service at Google). But in short this should be something like this:

sudo ufw allow from YOUR_IP to any port 27017

This done the trick for me

sudo service mongod restart

For me, changing the ownership of /var/lib/mongodb and /tmp/mongodb-27017.sock to mongodb was the way to go.

Just do:

sudo chown -R mongodb:mongodb /var/lib/mongodb

and then:

sudo chown mongodb:mongodb /tmp/mongodb-27017.sock

and then start or restart the mongodb server:

sudo systemctl start mongod

or

sudo systemctl restart mongod

and check the status

sudo systemctl status mongod