mongodb mongodb无法连接本地主机但可以连接本地主机的IP地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8904991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:25:25  来源:igfitidea点击:

Mongodb can't connect to localhost but can connect to localhost's IP address

mongodblocalhost

提问by MFB

If I try to run mongodb shell with the mongocommand alone, I get:

如果我尝试mongo单独使用命令运行 mongodb shell ,我会得到:

Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84

exception: connect failed

异常:连接失败

However, if I stipulate localhost's LAN IP address like this:

但是,如果我这样规定 localhost 的 LAN IP 地址:

mongo 10.10.5.90

...it connects fine.

...连接正常。

Any clues??

有什么线索吗??

回答by Eve Freeman

Do you have a bind_ipset in your mongodb.conf (or startup script)? edit for clarityA bind_ipsetting limits the IP it will listen on to that IP only.

bind_ip您的 mongodb.conf(或启动脚本)中有设置吗?编辑为清楚起见,一个bind_ip设置限制IP会监听到只有IP。

See the IP Address Binding section: http://www.mongodb.org/display/DOCS/Security+and+Authentication

请参阅 IP 地址绑定部分:http: //www.mongodb.org/display/DOCS/Security+and+Authentication

If not, do you have any firewall rules blocking the localhost access? That would be kind of strange, but I can't think of another reason why it wouldn't work while the LAN IP would work.

如果没有,您是否有阻止本地主机访问的防火墙规则?这有点奇怪,但我想不出另一个原因,为什么它不能工作而 LAN IP 可以工作。

回答by benchwarmer

This error could also appear if mongodb was not properly shutdown. If you type sudo start mongodband if it gives a new process id on every execution then your mongodb was not properly shutdown. To resolve this type

如果 mongodb 未正确关闭,也可能出现此错误。如果您键入sudo start mongodb并且在每次执行时都给出一个新的进程 ID,那么您的 mongodb 没有正确关闭。要解决此类型

sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair 
sudo start mongodb

回答by saimadhu.polamuri

Just follow all these steps to solve this problem

只需按照所有这些步骤来解决这个问题

Step 1: Remove lock file.
sudo rm /var/lib/mongodb/mongod.lock

Step 2: Repair mongodb.
mongod --repair 

Step 3: start mongodb.
sudo start mongodb 
or
sudo service mongodb start

Step 4: Check status of mongodb.
sudo status mongodb 
or   
sudo service mongodb status

Step 5: Start mongo console.
mongo

回答by Pallav

I added localhost along with the ip I had in bind_ip while starting mongo and it solved my problem, for example: bin/mongod --dbpath data --logpath mongo.log --fork --bind_ip localhost,10.100.1.2

我在启动 mongo 时添加了 localhost 和 bind_ip 中的 ip,它解决了我的问题,例如: bin/mongod --dbpath data --logpath mongo.log --fork --bind_ip localhost,10.100.1.2

回答by Prashobh

Edit your mongod.conf as follow

编辑您的 mongod.conf 如下

bindIp: 0.0.0.0,localhost

bindIp:0.0.0.0,本地主机

It works for me.

这个对我有用。