mongodb失败:连接到数据库服务器的错误:没有可达的服务器

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

我无法通过“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 

不知何故,我可以连接mongo shell

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

起初,我怀疑是否由我的iptables造成的,所以我刷新所有的iptables规则,并创build所有接受的规则,但它仍然是

  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 

我searchstackoverflow和谷歌和有人告诉标记了bind_ip或设置bind_ip为0.0.0.0,我试了一下,但仍然失败。

下面是我的mangodbconfiguration,有没有人可以帮我检查? 谢谢你的帮助

 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, # eg, 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 

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

 mongoimport --host=127.0.0.1 

在你的情况下,完整的命令是:

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

(来源: ranjeetcao @ mongorestore失败:无法访问服务器 )


更新:

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

(来源: Colin Marshall @ mongorestore失败:无法访问服务器 )


更新2:

看来这个Bug 还在为一些用户发生 。

这里还有一个开放的JIRA问题。

(来源: 詹姆斯·钱恩@这个问题的线索 )


更新3:

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

  • 如何重置MongoDB副本集设置
  • 如何将MongoDB副本集转换为独立的服务器

(来源: Maxim Yefremov @ mongorestore失败:无法访问服务器 )

我find了这个链接的答案https://jira.mongodb.org/browse/TOOLS-620它似乎是一个尚未解决的mongodb工具错误。; 不知道是否有人知道最新的工具是否解决了这个问题?

顺便说一句,我卸载mongodb工具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 

完整命令:

 mongoimport --host=127.0.0.1 -d testdb -c testc --file test.txt 
 mongorestore --db <thedb> --host=127.0.0.1 --port=<port> --drop path/db