首页 > 解决方案 > ServerSelectionTimeoutError(pymongo.errors.ServerSelectionTimeoutError:147.234.32.246:27017

问题描述

我尝试从 pycharm(使用 RDP)远程连接到 mongodb 服务器。这是我运行的功能:

cluster = MongoClient("mongodb://admin:Passw0rd!@147.234.32.246:27017/NEG")
db = cluster["NEG"]

for word in Setting.dictionary_global.keys():
    if word in db.list_collection_names():
        collection = db[word]
        for file in Setting.dictionary_global[word].keys():
            if collection.find({"url":Setting.dictionary_global[word][file].url}):
                continue
            num_of_appearance = len(Setting.dictionary_global[word][file].indexes.get(word))
            post = {"url": file, "title": Setting.dictionary_global[word][file].title,
                    "description": Setting.dictionary_global[word][file].description,"word in page": Setting.dictionary_global[word][file].indexes,"appearance": num_of_appearance, "date modified": Setting.dictionary_global[word][file].time}
            collection.insert_one(post)

    else:
        collection = db.create_collection(word)
        for file in Setting.dictionary_global[word].keys():

            #print(Setting.dictionary_global)
            num_of_appearance = len(Setting.dictionary_global[word][file].indexes.get(word))
            post = {"url": file, "title": Setting.dictionary_global[word][file].title,
                    "description": Setting.dictionary_global[word][file].description,"word in page": Setting.dictionary_global[word][file].indexes, "appearance": num_of_appearance, "date modified":Setting.dictionary_global[word][file].time}
            collection.insert_one(post)

我得到这个错误:

'''

回溯(最后一次调用):文件“C:/Users/edend/PycharmProjects/pythonProject11/main.py”,第 118 行,在 crawler.start() 文件“C:/Users/edend/PycharmProjects/pythonProject11/main.py”中。 py”,第 110 行,开始 insertDB() 文件“C:\Users\edend\PycharmProjects\pythonProject11\DB.py”,第 10 行,insertDB 如果 db.list_collection_names() 中的单词:文件“C:\Users\ edend\PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\database.py”,第 863 行,list_collection_names 中的结果 self.list_collections(session=session, **kwargs)] 文件“C:\Users\edend \PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\database.py",第 825 行,在 list_collections 中返回 self.__client._retryable_read( 文件 "C:\Users\edend\PycharmProjects\pythonProject11\venv\lib\site -包\pymongo\mongo_client。py”,第 1460 行,在 _retryable_read 服务器 = self._select_server(文件“C:\Users\edend\PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\mongo_client.py”,第 1278 行,在 _select_server 服务器 = 拓扑中.select_server(server_selector) 文件“C:\Users\edend\PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\topology.py”,第 241 行,在 select_server 返回 random.choice(self.select_servers(selector, File “C:\Users\edend\PycharmProjects\pythonProject11\venv\lib\site-packages\pymongo\topology.py”,第 199 行,在 select_servers server_descriptions = self._select_servers_loop(文件“C:\Users\edend\PycharmProjects\pythonProject11 \venv\lib\site-packages\pymongo\topology.py”,第 215 行,在 _select_servers_loop 中引发 ServerSelectionTimeoutError(pymongo.errors.ServerSelectionTimeoutError:147.234。32.246:27017:超时,超时:30s,拓扑描述:<TopologyDescription id:5ff3d15a2dcaa1e4fb3db4cd,topology_type:Single,服务器:[<ServerDescription('147.234.32.246',27017)server_type:未知,rtt:无,错误=网络超时( '147.234.32.246:27017: 超时')>]>

'''

请帮助我,我被卡住了,我会尝试一切,谢谢!

标签: windowsmongodbserverpymongordp

解决方案


常见原因:

  • MongoDB 服务器未运行
  • MongoDB 服务器在不同的端口上运行
  • 客户端和服务器之间没有连接(你能ping通吗)
  • mongod.conf 默认配置为仅允许本地连接(设置 bind_ip_all?)

推荐阅读