Pymongo无法连接到数据库:pymongo.errors.ServerSelectionTimeoutError:连接已关闭

时间:2018-10-03 01:17:29

标签: python pymongo

我想通过pymongo读取集合,但是出现以下错误:

  

回溯(最近通话最近):文件   “ C:/Users/GSingh/Py​​charmProjects/MC/quizQuestionAnswers.py”,第21行,   在       pprint.pprint(posts.find_one())文件“ C:\ Users \ GSingh \ AppData \ Local \ Continuum \ anaconda2 \ lib \ site-packages \ pymongo \ collection.py”,   第1262行,在find_one中       对于cursor.limit(-1)中的结果:文件“ C:\ Users \ GSingh \ AppData \ Local \ Continuum \ anaconda2 \ lib \ site-packages \ pymongo \ cursor.py”,   1189行,在下一个       如果是len(self .__ data)或self._refresh():文件“ C:\ Users \ GSingh \ AppData \ Local \ Continuum \ anaconda2 \ lib \ site-packages \ pymongo \ cursor.py”,   _refresh中的第1087行       self .__ session = self .__ collection.database.client._ensure_session()文件   “ C:\ Users \ GSingh \ AppData \ Local \ Continuum \ anaconda2 \ lib \ site-packages \ pymongo \ mongo_client.py”,   第1558行,在_ensure_session中       返回self。__start_session(正确,causal_consistency = False)文件“ C:\ Users \ GSingh \ AppData \ Local \ Continuum \ anaconda2 \ lib \ site-packages \ pymongo \ mongo_client.py”,   第1511行,在__start_session中       server_session = self._get_server_session()文件“ C:\ Users \ GSingh \ AppData \ Local \ Continuum \ anaconda2 \ lib \ site-packages \ pymongo \ mongo_client.py”,   第1544行,在_get_server_session中       返回self._topology.get_server_session()文件“ C:\ Users \ GSingh \ AppData \ Local \ Continuum \ anaconda2 \ lib \ site-packages \ pymongo \ topology.py”,   get_server_session中的第427行       无)文件“ C:\ Users \ GSingh \ AppData \ Local \ Continuum \ anaconda2 \ lib \ site-packages \ pymongo \ topology.py”,   _select_servers_loop中的第199行       self._error_message(selector))pymongo.errors.ServerSelectionTimeoutError:连接已关闭

以下是我正在运行的代码:

from pymongo import MongoClient
import pprint

username = 'username'
password = 'Password'

uri = "mongodb://"+username+":"+password+"@mc-*****.mongodb.net:27017/?authMechanism=SCRAM-SHA-256"
client = MongoClient(uri)
database = client["sws-quiz"]
collection = database["attempts"]

try:
    posts = database.posts
    pprint.pprint(posts.find_one())

finally:
    client.close()

1 个答案:

答案 0 :(得分:0)

那是因为我没有做ssl = True