mongodb 失败:连接到数据库服务器时出错:没有可访问的服务器

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

mongodb Failed: error connecting to db server: no reachable servers

mongodb

提问by James Chien

I installed mongodb in Ubuntu14.04 server

我在 Ubuntu14.04 服务器上安装了 mongodb

I was not able to connect to mongodb via "mongoimport", "mongodump", "mongostat", etc. It always show "no reachable server"

我无法通过“mongoimport”、“mongodump”、“mongostat”等连接到 mongodb。它总是显示“没有可访问的服务器”

mongoimport --db test --collection restaurants --drop --file dataset.json
2015-08-25T13:08:29.802+0800    [........................] test.restaurants 0.0 B/11.3 MB (0.0%)
2015-08-25T13:08:30.306+0800    Failed: error connecting to db server: no reachable servers
2015-08-25T13:08:30.306+0800    imported 0 documents

Somehow, I was able to connect with mongo shell

不知何故,我能够与 mongo shell 连接

mongo --port 27017
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1:27017/test

At first, I doubt if it cause by my iptables, so I flush all iptables rules and create rules for ALL accept, but it still same

起初,我怀疑它是否由我的 iptables 引起,所以我刷新所有 iptables 规则并为 ALL 接受创建规则,但它仍然相同

 sudo iptables -S
    -P INPUT ACCEPT
    -P FORWARD ACCEPT
    -P OUTPUT ACCEPT
    -A INPUT -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A FORWARD -j ACCEPT
    -A OUTPUT -j ACCEPT
    -A OUTPUT -o lo -j ACCEPT

I search the stackOverflow and google and someone told marked off bind_ip or set bind_ip to 0.0.0.0, I tried all but still failed.

我搜索了 stackOverflow 和谷歌,有人告诉标记了 bind_ip 或将 bind_ip 设置为 0.0.0.0,我尝试了所有但仍然失败。

Below are my mangodb config, would any one can help me to check? Thanks for your help

下面是我的 mangodb 配置,有人可以帮我检查一下吗?谢谢你的帮助

james@localhost:~$ cat /etc/mongod.conf 
# mongod.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/mongod.log

logappend=true

port = 27017

# Listen to local interface only. Comment out to listen on all interfaces. 
#bind_ip = 127.0.0.1
# Disables write-ahead journaling
# nojournal = true

# Enables periodic logging of CPU utilization and I/O wait
#cpu = true

# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true

# Verbose logging output.
verbose = true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true

# Enable db quota management
#quota = true

# Set oplogging level where n is
#   0=off (default)
#   1=W
#   2=R
#   3=both
#   7=W+some reads
#diaglog = 0

# Ignore query hints
#nohints = true

# Enable the HTTP interface (Defaults to port 28017).
#httpinterface = true

# Turns off server-side scripting.  This will result in greatly limited
# functionality
#noscripting = true

# Turns off table scans.  Any query that would do a table scan fails.
#notablescan = true

# Disable data file preallocation.
#noprealloc = true

# Specify .ns file size for new databases.
# nssize = <size>

# Replication Options

# in replicated mongo databases, specify the replica set name here
#replSet=setname
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile

回答by whythecode

A temporary workaround is to add the hostparam to your mongoimportcall, letting mongo know that the host is your own machine (127.0.0.1):

临时解决方法是将host参数添加到您的mongoimport调用中,让 mongo 知道主机是您自己的机器 ( 127.0.0.1):

mongoimport --host=127.0.0.1

The full command in your case is then:

您的情况下的完整命令是:

mongoimport --host=127.0.0.1 \
  --db test --collection restaurants --drop --file dataset.json

(Source: ranjeetcao @ mongorestore Failed: no reachable servers)

(来源:ranjeetcao @mongorestore 失败:没有可访问的服务器



Update:

更新:

Updating to Mongo >= 3.0.7 should solve the issue

更新到 Mongo >= 3.0.7 应该可以解决问题

(Source: Colin Marshall @?mongorestore Failed: no reachable servers)

(来源:Colin Marshall @?mongorestore 失败:没有可访问的服务器



Update 2:

更新 2:

It seems the Bug is still happening for some users.

对于某些用户而言,该错误似乎仍在发生

There's also an open JIRA issue here.

还有一个开放JIRA问题在这里

(Source: James Chien @?this question thread)

(来源: James Chien @?this question thread)



Update 3:

更新 3:

In some cases, this can be caused by Mongo running as a ReplicaSet. To solve this, as far as I've seen, one can either disable the ReplicaSet functionality or reset it. See:

在某些情况下,这可能是由 Mongo 作为 ReplicaSet 运行引起的。为了解决这个问题,据我所知,可以禁用 ReplicaSet 功能或重置它。看:

(Source: Maxim Yefremov @ mongorestore Failed: no reachable servers)

(来源:Maxim Yefremov @ mongorestore 失败:没有可访问的服务器

回答by James Chien

I find the answer by this links https://jira.mongodb.org/browse/TOOLS-620It seem it is a mongodb tools bugs that doesn't resolved yet. Not sure if anyone know if the latest tools have fixed the issue?

我通过这个链接找到了答案https://jira.mongodb.org/browse/TOOLS-620看来这是一个尚未解决的 mongodb 工具错误。不确定是否有人知道最新的工具是否解决了该问题?

BTW, I uninstall the mongodb-tools 3.x and reinstall to 2.6.0 that resolved the problem

顺便说一句,我卸载了 mongodb-tools 3.x 并重新安装到 2.6.0 解决了问题

sudo apt-get purge mongodb-org-tools
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install -y mongodb-org-tools=2.6.0

mongoimport --db test --collection restaurants --drop --file dataset.json
connected to: 127.0.0.1
2015-08-25T15:19:59.494+0800 dropping: test.restaurants
2015-08-25T15:20:00.089+0800 check 9 25359
2015-08-25T15:20:00.089+0800 imported 25359 objects

回答by Shemeer M Ali

You need to provide host option try the following command :

您需要提供主机选项​​尝试以下命令:

mongoimport --host=127.0.0.1 -d dbName -c collectionName --file output.txt

回答by Dere Sagar

To provide the options hostand port

提供选项主机端口

mongoimport --host X.X.X.X --port 27017 --db dbName --collection collectionName --file fileName.json --jsonArray

回答by RtmY

Adding another answer for those who are struggling with the same issue while using MongoDB Atlas.

为那些在使用 MongoDB Atlas 时遇到同样问题的人添加另一个答案。

I tried to follow the offical guides like Seed with mongorestoreand Load File with mongoimportbut received the same no reachable serverserror.

我试图按照官方导游像与mongorestore种子,并与mongoimport加载文件,但收到了同样的no reachable servers错误。

I tried to change the command arguments over and over - nothing changed.

我试图一遍又一遍地更改命令参数 - 没有任何改变。

But then I navigated inside my cluster to the "Command Line Tools" tab and saw the exact command I should runfor mongorestore, mongodump, mongoimport,mongoexportetc':

但后来 我导航我的群集的“命令行工具”选项卡中,看到确切的命令,我应该跑mongorestoremongodumpmongoimportmongoexport等':

enter image description here

在此处输入图片说明

Adding as inline the full command:

添加内联完整命令:

mongorestore --host 
<cluster-name>-shard-0/
<cluster-name>-shard-00-00-obe3i.mongodb.net:27017,
<cluster-name>-shard-00-01-obe3i.mongodb.net:27017,
<cluster-name>-shard-00-02-obe3i.mongodb.net:27017 
--ssl 
--username <User-Name> 
--password <PASSWORD> 
--authenticationDatabase admin 

The mongorestorecomand can be replaced with mongodump,mongoimport,mongoexportetc'.

mongorestoreCOMAND可以替换mongodumpmongoimportmongoexport等”。

(*) Prefer copying the command directly from dashboard because the DNS of the replica-sets might change.

(*) 更喜欢直接从仪表板复制命令,因为副本集的 DNS 可能会更改。

(**) MongoDB shell version v4.2.5.

(**) MongoDB shell 版本 v4.2.5。

回答by Fomove

If you have installed mongodb through homebrew then you can simply start mongodb through

如果您已经通过自制软件安装了 mongodb,那么您只需通过以下方式启动 mongodb

brew services start mongodb

Then access the shell by

然后通过访问shell

mongo

You can shut down your db by

您可以通过以下方式关闭数据库

brew services stop mongodb

For more options

更多选择

brew info mongodb

回答by Jekayode

mongorestore --db <thedb> --host=127.0.0.1 --port=<port> --drop path/db