mongodb失败:连接到数据库服务器时出错:没有可访问的服务器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb失败:连接到数据库服务器时出错:没有可访问的服务器相关的知识,希望对你有一定的参考价值。
我在Ubuntu 14.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规则并为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
我搜索stackOverflow和google,有人告诉标记为bind_ip或将bind_ip设置为0.0.0.0,我尝试了所有但仍然失败。
下面是我的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
一个临时的解决方法是将host
param添加到你的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 Failed: no reachable servers)
更新:
更新到Mongo> = 3.0.7应该可以解决问题
(来源:Colin Marshall @ mongorestore Failed: no reachable servers)
更新2:
看来Bug is still happening for some users。
还有一个开放的JIRA问题here。
(来源:James Chien @ this question thread)
更新3:
在某些情况下,这可能是由Mongo作为ReplicaSet运行引起的。为了解决这个问题,据我所见,可以禁用ReplicaSet功能或重置它。看到:
- How to reset MongoDB replica set settings
- How to convert a MongoDB replica set to a stand alone server
(来源:Maxim Yefremov @ mongorestore Failed: no reachable servers)
我找到了这个链接的答案https://jira.mongodb.org/browse/TOOLS-620它似乎是一个尚未解决的mongodb工具bug。不确定是否有人知道最新工具是否已解决问题?
顺便说一下,我卸载了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
完整命令:
mongoimport --host=127.0.0.1 -d testdb -c testc --file test.txt
如果您已经通过自制软件安装了mongodb,那么您只需启动mongodb即可
brew services start mongodb
然后通过访问shell
mongo
你可以关闭你的数据库
brew services stop mongodb
有更多选择
brew info mongodb
提供选项主机和端口
mongoimport --host X.X.X.X --port 27017 --db dbName --collection collectionName --file fileName.json --jsonArray
mongorestore --db <thedb> --host=127.0.0.1 --port=<port> --drop path/db
以上是关于mongodb失败:连接到数据库服务器时出错:没有可访问的服务器的主要内容,如果未能解决你的问题,请参考以下文章
连接到服务器 localhost:27017 时,在监视器线程中连接远程 MongoDB 异常时出错
将MongoDB数据库连接到MATLAB时出错(无法加载消息目录'mongodb:mongodb')