我不明白为什么在将代码连接到MongoDB数据库时我的代码中出现此错误。这是我的代码: -
import Credentials
client = Credentials.client
db = client.VisitorBook
company = input('Enter Company Name: ')
result=db.Company_name.insert_one({'Name':company})
print(result.inserted_id)
这是我单独制作的Credentials文件,在上面的代码中调用了这个Credentials模块:
from pymongo import MongoClient
from bson.objectid import ObjectId
client = MongoClient('mongodb+srv://Username:<Password>@basicdatabase-w4eg3.mongodb.net/test?retryWrites=true');
执行我的代码后输入我的用户名和密码后,会显示以下错误:
---------------------------------------------------------------------------
ServerSelectionTimeoutError Traceback (most recent call last)
<ipython-input-10-754b7e794657> in <module>()
5
6 company = input('Enter Company Name: ')
----> 7 result=db.Company_name.insert_one({'Name':company})
8 print(result.inserted_id)
C:\ProgramData\Anaconda3\lib\site-packages\pymongo\collection.py in insert_one(self, document, bypass_document_validation, session)
681 self._insert(document,
682 bypass_doc_val=bypass_document_validation,
--> 683 session=session),
684 self.write_concern.acknowledged)
685
C:\ProgramData\Anaconda3\lib\site-packages\pymongo\collection.py in _insert(self, docs, ordered, check_keys, manipulate, write_concern, op_id, bypass_doc_val, session)
597 return self._insert_one(
598 docs, ordered, check_keys, manipulate, write_concern, op_id,
--> 599 bypass_doc_val, session)
600
601 ids = []
C:\ProgramData\Anaconda3\lib\site-packages\pymongo\collection.py in _insert_one(self, doc, ordered, check_keys, manipulate, write_concern, op_id, bypass_doc_val, session)
577
578 result = self.__database.client._retryable_write(
--> 579 True, _insert_command, session)
580 _check_write_command_response(result)
581 else:
C:\ProgramData\Anaconda3\lib\site-packages\pymongo\mongo_client.py in _retryable_write(self, retryable, func, session)
1099 def _retryable_write(self, retryable, func, session):
1100 """Internal retryable write helper."""
-> 1101 with self._tmp_session(session) as s:
1102 return self._retry_with_session(retryable, func, s, None)
1103
C:\ProgramData\Anaconda3\lib\contextlib.py in __enter__(self)
79 def __enter__(self):
80 try:
---> 81 return next(self.gen)
82 except StopIteration:
83 raise RuntimeError("generator didn't yield") from None
C:\ProgramData\Anaconda3\lib\site-packages\pymongo\mongo_client.py in _tmp_session(self, session, close)
1405 return
1406
-> 1407 s = self._ensure_session(session)
1408 if s and close:
1409 with s:
C:\ProgramData\Anaconda3\lib\site-packages\pymongo\mongo_client.py in _ensure_session(self, session)
1392 # Don't make implied sessions causally consistent. Applications
1393 # should always opt-in.
-> 1394 return self.start_session(causal_consistency=False)
1395 except (ConfigurationError, InvalidOperation):
1396 # Sessions not supported, or multiple users authenticated.
C:\ProgramData\Anaconda3\lib\site-packages\pymongo\mongo_client.py in start_session(self, causal_consistency)
1370
1371 # Raises ConfigurationError if sessions are not supported.
-> 1372 server_session = self._get_server_session()
1373 opts = client_session.SessionOptions(
1374 causal_consistency=causal_consistency)
C:\ProgramData\Anaconda3\lib\site-packages\pymongo\mongo_client.py in _get_server_session(self)
1378 def _get_server_session(self):
1379 """Internal: start or resume a _ServerSession."""
-> 1380 return self._topology.get_server_session()
1381
1382 def _return_server_session(self, server_session, lock):
C:\ProgramData\Anaconda3\lib\site-packages\pymongo\topology.py in get_server_session(self)
425 any_server_selector,
426 self._settings.server_selection_timeout,
--> 427 None)
428 elif not self._description.readable_servers:
429 self._select_servers_loop(
C:\ProgramData\Anaconda3\lib\site-packages\pymongo\topology.py in _select_servers_loop(self, selector, timeout, address)
197 if timeout == 0 or now > end_time:
198 raise ServerSelectionTimeoutError(
--> 199 self._error_message(selector))
200
201 self._ensure_opened()
ServerSelectionTimeoutError: basicdatabase-w4eg3.mongodb.net:27017: [Errno 11001] getaddrinfo failed
答案 0 :(得分:2)
您好像这样更改此代码部分
import Credentials
client = Credentials.client
db = client['VisitorBook']
collection = db['Company_name']
company = input('Enter Company Name: ')
result = collection.insert_one({'Name':company})
print(result.inserted_id)
或尝试手动运行mongod.exe。有时您会看到Mongod.exe控制台卡住,在这种情况下,请点击mongod控制台内的enter按钮进行刷新。