python-3.x - ServerSelectionTimeoutError : basicdatabase-w4eg3. mongodb.net:27017: [Errno 11001] getaddrinfo 失败

标签 python-3.x mongodb mongodb-query anaconda spring-data-mongodb

我不明白为什么在将代码连接到 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)

这是我单独制作的凭据文件,并且在上面的代码中调用了此凭据模块:

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

最佳答案

您好,像这样更改此代码部分,

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 按钮进行刷新。

关于python-3.x - ServerSelectionTimeoutError : basicdatabase-w4eg3. mongodb.net:27017: [Errno 11001] getaddrinfo 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50845649/

相关文章:

python - 将 "stamp"字符串转换为所需字符串的最佳方法

python - 如何在Python中销毁一个对象?

sql - 如何用 "like"查询MongoDB

MongoDB 将mapReduce 转换为聚合

Mongodb $addFields 如果为 null 则不输出数据

python - Pygame 绘图无法正常工作

python - Pygame 蛇吃自己

mongodb - BinData 是 Mongo ObjectId 吗?

java.lang.NoSuchMethodError : org. hibernate.ogm.cfg.OgmConfiguration.addAnnotatedClass

mongodb - 选择哪个数据库(Cassandra、MongoDB、?)来存储和查询事件/日志/指标数据?