問題描述
1. 使用 mongo 資料庫的replication模式建立了複製集叢集,發現在使用程式連線資料庫時報了一個 timeout 的錯誤,uri 為多個主機 ip。
2. 將 uri 寫成其中的任意一個 ip,發現可以正常連線到主機進行查詢。
測試程式如下
import pymongo,time
username="test"
password="test"
authdb="admin"
connection=pymongo.mongo_client.MongoClient(host="192.168.10.9:27017,192.168.10.11:27017",connect=False,username=username,password=password)
dbnames=connection.server_info()
connection.close()
print(dbnames)
複製程式碼
以上測試程式報錯如下
/usr/local/bin/python3.7 /Users/qixiao/IdeaProjects/mongod_rep_set/repset.py
Traceback (most recent call last):
File "/Users/qixiao/IdeaProjects/mongod_rep_set/repset.py", line 10, in <module>
dbnames=connection.server_info()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1620, in server_info
session=session)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymongo/database.py", line 611, in command
read_preference) as (sock_info, slave_ok):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
return next(self.gen)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1101, in _socket_for_reads
server = topology.select_server(read_preference)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymongo/topology.py", line 224, in select_server
address))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymongo/topology.py", line 183, in select_servers
selector, server_timeout, address)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymongo/topology.py", line 199, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: 192.168.10.9:27017: timed out,192.168.10.11:27017: timed out
Process finished with exit code 1
複製程式碼
下面單獨連線一臺資料庫程式如下
import pymongo
username="test"
password="test"
authdb="admin"
connection=pymongo.mongo_client.MongoClient(host="192.168.10.9:27017,connect=False,username=username,password=password)
dbnames=connection.server_info()
connection.close()
print(dbnames)
複製程式碼
/usr/local/bin/python3.7 /Users/qixiao/IdeaProjects/mongod_rep_set/repset.py
{'version': '3.6.5', 'gitVersion': 'a20ecd3e3a174162052ff99913bc2ca9a839d618', 'modules': [], 'allocator': 'system', 'javascriptEngine': 'mozjs', 'sysInfo': 'deprecated', 'versionArray': [3, 6, 5, 0], 'openssl': {'running': 'OpenSSL 1.1.0h 27 Mar 2018', 'compiled': 'OpenSSL 1.1.0h 27 Mar 2018'}, 'buildEnvironment': {'distmod': '', 'distarch': 'x86_64', 'cc': 'gcc: Apple LLVM version 8.1.0 (clang-802.0.42)', 'ccflags': '-mmacosx-version-min=10.10 -fno-omit-frame-pointer -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -Werror -O2 -Wno-unused-local-typedefs -Wno-unused-function -Wno-unused-private-field -Wno-deprecated-declarations -Wno-tautological-constant-out-of-range-compare -Wno-unused-const-variable -Wno-missing-braces -Wno-inconsistent-missing-override -Wno-potentially-evaluated-expression -Wno-exceptions -fstack-protector-strong -fno-builtin-memcmp', 'cxx': 'g++: Apple LLVM version 8.1.0 (clang-802.0.42)', 'cxxflags': '-Woverloaded-virtual -Werror=unused-result -Wpessimizing-move -Wredundant-move -Wno-undefined-var-template -stdlib=libc++ -std=c++14', 'linkflags': '-mmacosx-version-min=10.10 -Wl,-bind_at_load -Wl,-fatal_warnings -fstack-protector-strong -stdlib=libc++', 'target_arch': 'x86_64', 'target_os': 'macOS'}, 'bits': 64, 'debug': False, 'maxBsonObjectSize': 16777216, 'storageEngines': ['devnull', 'ephemeralForTest', 'mmapv1', 'wiredTiger'], 'ok': 1.0}
Process finished with exit code 0
複製程式碼
總結: 看到某一網站上說增加一個傳參 conn=False,來進行等待建立程式,待程式建立完畢後執行連線庫操作,我將程式的 True 改成 False 發現依然報超時錯誤,來掘金髮覺我的解決辦法