0

我正在尝试连接到 mongoDB,但似乎无法访问主机。

这个问题出现在 mongoshell 和 pymongo 中。在我使用的外壳上

mongo --ssl --sslAllowInvalidCertificates host:port/db -u user -p pass --sslCAFile ca.pem

我收到下面的错误消息。

MongoDB shell version: 3.2.9
connecting to: host:port/db
2016-09-30T13:41:57.268-0300 W NETWORK  [thread1] Failed to connect to host_ip:port after 5000 milliseconds, giving up.
2016-09-30T13:41:57.323-0300 E QUERY    [thread1] Error: couldn't connect to server host:port, connection attempt failed :
connect@src/mongo/shell/mongo.js:231:14
@(connect):1:6

exception: connect failed

在 pymongo 上,我使用下面的代码连接

Config = configparser.ConfigParser()
Config.read('configurations.cfg')
mongo_conf = Config['mongoDB_test']

connect = "mongodb://%s:%s@%s:%s/%s?ssl=true" \
    %(mongo_conf['user'],mongo_conf['pass'],mongo_conf['host'],mongo_conf['port'],mongo_conf['database'])
client = MongoClient(connect,ssl_ca_certs=mongo_conf['cert'])
db = client[mongo_conf['database']]

当我跑步时,我得到了这个

Traceback (most recent call last):
  File "test.py", line 24, in <module>
    db.test.insert_one(data)
  File "/home/jmpf13/repos/laura/dev_env/lib/python3.5/site-packages/pymongo/collection.py", line 627, in insert_one
    with self._socket_for_writes() as sock_info:
  File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/home/jmpf13/repos/laura/dev_env/lib/python3.5/site-packages/pymongo/mongo_client.py", line 762, in _get_socket
    server = self._get_topology().select_server(selector)
  File "/home/jmpf13/repos/laura/dev_env/lib/python3.5/site-packages/pymongo/topology.py", line 210, in select_server
    address))
  File "/home/jmpf13/repos/laura/dev_env/lib/python3.5/site-packages/pymongo/topology.py", line 186, in select_servers
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: host:port: timed out 
4

1 回答 1

2

我刚刚连接到一个使用 mongo shell 部署到 bluemix 的 mongoDB 实例,该实例的语法与您所用的类似,它运行良好。

您的部署可能存在问题。在 bluemix ui 中,当您打开 mongo 服务时,“状态”指示器是否显示为绿色?

我要做的另一件事是尝试验证主机在该端口上是否可访问。例如,使用类似 tcping 的东西: tcping <host> <port>

或者,如果在紧要关头,telnet 到端口: telnet <host> <port> 对于后者,你会得到类似的东西:

Trying <ip>... Connected to <host>. Escape character is '^]'.

如果其中任何一个失败,我会确保您端没有任何东西阻塞流量,然后在您的部署未正确配置或出现其他问题时联系支持。

于 2016-09-30T19:10:18.200 回答