无法连接到 MongoDB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9154030/
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:28:06  来源:igfitidea点击:

Unable to connect to MongoDB

mongodbubuntuopenvz

提问by Stuart.Sklinar

I've just installed MongoDB (standard Ubuntu build, not the latest stable) and for some reason I can't connect:

我刚刚安装了 MongoDB(标准的 Ubuntu 版本,不是最新的稳定版),但由于某种原因我无法连接:

Mon Feb  6 03:11:22 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:79
exception: connect failed

Now my machine isn't 127.x.x.1 it's for some reason x.x.x.2 (But i've changed my config to bind to that address, and also tried to bind to my public IP which provided no avail.

现在我的机器不是 127.xx1,它是出于某种原因 xxx2(但我已经更改了我的配置以绑定到该地址,并且还尝试绑定到我的公共 IP,但无济于事。

Config:

配置:

#mongodb.conf

#mongodb.conf

# Where to store the data.

# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/var/lib/mongodb

#where to log
logpath=/var/log/mongodb/mongodb.log

logappend=true

bind_ip = 199.21.114.XX
port = 27017

I've checked the logs and only startup info is displaying in there.

我检查了日志,只有启动信息显示在那里。

I've also checked that the deamon is running too, and it is - I even tried running it manually with a --fork.

我还检查了守护进程是否也在运行,并且确实如此 - 我什至尝试使用--fork.

Has anyone come across this before? Or any suggestions?

有没有人遇到过这个?或者有什么建议?

Thx

谢谢

UPDATE:

更新:

After a restart - here are the logs:

重新启动后 - 以下是日志:

** WARNING: You are running in OpenVZ. This is known to be broken!!!

Tue Feb  7 19:43:32 [initandlisten] db version v1.8.2, pdfile version 4.5
Tue Feb  7 19:43:32 [initandlisten] git version: nogitversion
Tue Feb  7 19:43:32 [initandlisten] build sys info: Linux allspice 2.6.24-28-se$
Tue Feb  7 19:43:32 [initandlisten] *** warning: spider monkey build without ut$
Tue Feb  7 19:43:32 [initandlisten] waiting for connections on port 27017
Tue Feb  7 19:43:32 [websvr] web admin interface listening on port 28017

采纳答案by Eve Freeman

You must specify the IP (199.21.114.XX), by running mongo 199.21.114.XX.

您必须通过运行指定 IP (199.21.114.XX) mongo 199.21.114.XX

Otherwise, if you want it to listen on localhost as well, you should remove the bind_ipsetting in your config file.

否则,如果您还希望它在 localhost 上侦听,则应删除bind_ip配置文件中的设置。

UpdateCheck your firewall config. Since you're connecting to the external IP, it can be configured to block, even from the local box.

更新检查您的防火墙配置。由于您要连接到外部 IP,因此可以将其配置为阻止,即使是从本地设备。

回答by Jeremy White

I solved this problem by commenting out the following line in my /etc/mongod.conffile.

我通过注释掉/etc/mongod.conf文件中的以下行解决了这个问题。

# Listen to local interface only. Comment out to listen on all interfaces. 
#bind_ip = 127.0.0.1

You will need to follow this config change with restarting the mongod process.

您将需要通过重新启动 mongod 进程来遵循此配置更改。

sudo service mongod restart

回答by andyshi

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

回答by Gabber

I had the same issue just because the silly mistake.

我有同样的问题只是因为愚蠢的错误。

first remove mongodb file which is fine

首先删除很好的mongodb文件

rm -rf /tmp/mongodb-27017.sock

where I did mistake inside the /etc/mongod.conf

我在 /etc/mongod.conf 中做错的地方

# network interfaces
net:
  port: 27017
 #bindIp: 127.0.0.1
bindIp: privateIp

instead of

代替

net:
  port: 27017
  bindIp: 10.1.2.4

for listen to all available ips

用于收听所有可用的 ip

bindIp: [127.0.0.1,10.128.0.2]

restart the mongodb

重启mongodb

sudo service mongod start

hopefully this answer help for someone

希望这个答案对某人有帮助

回答by jdi

By default, running the mongoconsole command will look for mongodb on your localhost, hence the 127.0.0.1IP which is your local loopback. Also, the default config for mongodshould be available on localhost. Its better to do it this way for now to start, unless you have turned on auth. Make sure you have auth enabled or your database is running openly on your public ip.

默认情况下,运行mongo控制台命令将在您的本地主机上查找 mongodb,因此127.0.0.1IP 是您的本地环回。此外,默认配置mongod应该在本地主机上可用。最好现在开始这样做,除非您已打开身份验证。确保您已启用身份验证或您的数据库在您的公共 IP 上公开运行。

When you want to connect to a host other than localhost or your default config, you do:

当您想连接到 localhost 或默认配置以外的主机时,您可以:

mongo <host>

回答by rahul sharma

following steps may help you :

以下步骤可能对您有所帮助:

  1. open terminal and run "sudo rm /var/lib/mongodb/mongod.lock"
  2. now run " sudo mongod --repair"
  3. now "sudo start mongodb"
  4. now just check the status of mongodb status "sudo status mongodb"
  1. 打开终端并运行“ sudo rm /var/lib/mongodb/mongod.lock
  2. 现在运行“ sudo mongod --repair
  3. 现在“ sudo 启动mongodb
  4. 现在只需检查 mongodb status “ sudo status mongodb”的状态

5 lastly just invoke command "mongo"

5 最后调用命令“ mongo

回答by reselbob

in /etc/mongod.conf

在 /etc/mongod.conf

I changed,

我变了,

bindIp: 127.0.0.1

bindIp: 127.0.0.1

to

bindIp: 0.0.0.0

bindIp: 0.0.0.0

which is all ports. Yeah, I know it's risky, but I am running on an internal network.

这是所有端口。是的,我知道这有风险,但我在内部网络上运行。

BTW commenting out #bindIp: 127.0.0.1did NOT work for me, running under Ubuntu 18.

顺便说一句#bindIp: 127.0.0.1,在 Ubuntu 18 下运行的注释对我不起作用。

回答by rakeshz

Another way is to add mongod option param bind_ip

另一种方法是添加 mongod 选项参数 bind_ip

mongod -–help

--bind_ip arg comma separated list of ip addresses to listen on - localhost by default

--bind_ip arg 逗号分隔的要侦听的 IP 地址列表 - 默认为 localhost

--bind_ip_all bind to all ip addresses

--bind_ip_all 绑定所有ip地址

mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db --bind_ip 0.0.0.0

回答by Algem Mojedo-MCPD-EA

If you are using windows, run the services then check mongoDB server is running, if not running, Start it.

如果您使用的是 Windows,请运行服务,然后检查 mongoDB 服务器是否正在运行,如果未运行,请启动它。

mongoDB server by default run on localhost:27017. Click localhost:27017 the create an admin user first.

mongoDB 服务器默认在 localhost:27017 上运行。点击 localhost:27017 首先创建一个管理员用户。