我是mongodb和pymongo的新手。昨天对于一个小项目,我开始测试nosql的感觉。但是,由于服务器选择超时错误,我无法执行任何操作
这是整个代码
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
ContactsDB = client["ContactsDB"]
ContactsCollection = ContactsDB["ContactsCollection"]
to_insert = { "name": f"{name}", "phone": f"{phone}"}
ContactsCollection.insert_one(to_insert)
这是错误
File "main.py", line 20, in add_contact
x = ContactsCollection.insert_one(mydict)
File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/collection.py", line 700, in insert_one
session=session),
File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/collection.py", line 614, in _insert
bypass_doc_val, session)
File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/collection.py", line 602, in _insert_one
acknowledged, _insert_command, session)
File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1279, in _retryable_write
with self._tmp_session(session) as s:
File "/usr/lib/python3.6/contextlib.py", line 81, in __enter__
return next(self.gen)
File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1611, in _tmp_session
s = self._ensure_session(session)
File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1598, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1551, in __start_session
server_session = self._get_server_session()
File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1584, in _get_server_session
return self._topology.get_server_session()
File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/topology.py", line 429, in get_server_session
None)
File "/home/erfan2003/.local/lib/python3.6/site-packages/pymongo/topology.py", line 200, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: localhost:27027: [Errno 111] Connection refused