代码
myclient = pymongo.MongoClient("mongodb://root:[email protected]:27017/xxx?authSource=admin") mydb = myclient["xxx"] mycol = mydb["xxx"]
报错全文
pymongo.errors.ServerSelectionTimeoutError: Could not reach any servers in [('localhost', 27017)]. Replica set is configured with internal hostnames or IPs?, Timeout: 30s, Topology Description: <TopologyDescription id: 6673d766f36938e3f8b3bc51, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')>]>
已经配置了ip,还显示localhostxxx,网上说是因为MongoClient默认HOST是localhost
这个其实不重要,参考google上搜到的,然后自己测试,得出解决办法
myclient = pymongo.MongoClient("mongodb://root:[email protected]:27017/?directConnection=true") mydb = myclient["xxx"] mycol = mydb["xxx"]
重点是directConnection=true,不太理解,反正解决了
标签:xxx,mydb,27017,myclient,errors,pymongo,localhost From: https://www.cnblogs.com/daizichuan/p/18258767