2

我的代码:

import certifi
import ssl
from pymongo import MongoClient

connection = MongoClient("mongodb+srv://<USER>:<PASSWORD>@sensor-cluster-jhiup.mongodb.net/test", ssl_cert_reqs=ssl.CERT_REQUIRED, ssl_ca_certs=certifi.where())
print connection

db = connection.test
print db

items = db.items
print items

db.items.insert_one(
      {"item": "canvas"})

尝试通过 python 2.7 向 mongodb atlas 进行简单插入时出现以下错误:

MongoClient(host=['sensor-cluster-shard-00-02-jhiup.mongodb.net:27017', 'sensor-cluster-shard-00-00-jhiup.mongodb.net:27017', 'sensor-cluster- shard-00-01-jhiup.mongodb.net:27017'],document_class=dict,tz_aware=False,connect=True,ssl_ca_certs='/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ site-packages/certifi/cacert.pem',replicaset=u'Sensor-Cluster-shard-0',ssl_cert_reqs=2,ssl=True,authsource=u'admin')数据库(MongoClient(host=['sensor-cluster -shard-00-02-jhiup.mongodb.net:27017','sensor-cluster-shard-00-00-jhiup.mongodb.net:27017','sensor-cluster-shard-00-01-jhiup.mongodb .net:27017'],document_class=dict,tz_aware=False,connect=True,ssl_ca_certs='/Library/Frameworks/Python.framework/Versions/2.7/lib/python2。7/site-packages/certifi/cacert.pem', replicaset=u'Sensor-Cluster-shard-0', ssl_cert_reqs=2, ssl=True, authsource=u'admin'), u'test') 集合(数据库(MongoClient(host=['sensor-cluster-shard-00-02-jhiup.mongodb.net:27017', 'sensor-cluster-shard-00-00-jhiup.mongodb.net:27017', 'sensor-cluster -shard-00-01-jhiup.mongodb.net:27017'],document_class=dict,tz_aware=False,connect=True,ssl_ca_certs='/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /site-packages/certifi/cacert.pem', replicaset=u'Sensor-Cluster-shard-0', ssl_cert_reqs=2, ssl=True, authsource=u'admin'), u'test'), u'items ')测试')集合(数据库(MongoClient(主机= ['sensor-cluster-shard-00-02-jhiup.mongodb.net:27017','sensor-cluster-shard-00-00-jhiup.mongodb.net:27017 ', 'sensor-cluster-shard-00-01-jhiup.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, ssl_ca_certs='/Library/Frameworks/Python.framework/Versions/2.7 /lib/python2.7/site-packages/certifi/cacert.pem', replicaset=u'Sensor-Cluster-shard-0', ssl_cert_reqs=2, ssl=True, authsource=u'admin'), u'test '), u'项目')测试')集合(数据库(MongoClient(主机= ['sensor-cluster-shard-00-02-jhiup.mongodb.net:27017','sensor-cluster-shard-00-00-jhiup.mongodb.net:27017 ', 'sensor-cluster-shard-00-01-jhiup.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, ssl_ca_certs='/Library/Frameworks/Python.framework/Versions/2.7 /lib/python2.7/site-packages/certifi/cacert.pem', replicaset=u'Sensor-Cluster-shard-0', ssl_cert_reqs=2, ssl=True, authsource=u'admin'), u'test '), u'项目')ssl_ca_certs='/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/certifi/cacert.pem', replicaset=u'Sensor-Cluster-shard-0', ssl_cert_reqs=2, ssl=True, authsource=u'admin'), u'test'), u'items')ssl_ca_certs='/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/certifi/cacert.pem', replicaset=u'Sensor-Cluster-shard-0', ssl_cert_reqs=2, ssl=True, authsource=u'admin'), u'test'), u'items')

回溯(最后一次调用):文件“/Users/danielmatthews/Python/sunfounderpy3.py”,第 18 行,在 {“item”:“canvas”})文件“/Library/Frameworks/Python.framework/Versions/2.7 /lib/python2.7/site-packages/pymongo/collection.py”,第 682 行,在 insert_one session=session 中),文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site -packages/pymongo/collection.py”,第 598 行,在 _insert bypass_doc_val,会话中)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py” ,第 578 行,在 _insert_one True,_insert_command,会话)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/mongo_client.py”,第 1099 行,在 _retryable_write 返回 self._retry_with_session(retryable, func, s, None) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/mongo_client.py”,第 1066 行,在 _retry_with_session 中使用 self._get_socket(server) 作为 sock_info:文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py”,第 17 行,在在带有 server.get_socket 的 _get_socket 中输入 return self.gen.next() 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/mongo_client.py”,第 944 行( self.all_credentials) as sock_info: File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter return self.gen.next() File "/ Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/server.py",第 138 行,在 get_socket 中,self.pool.get_socket(all_credentials, checkout) as sock_info: File" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py”,第 17 行,输入 返回 self.gen.next() 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py”,第 893 行,在 get_socket sock_info.check_auth(all_credentials ) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py”,第 588 行,在 check_auth auth.authenticate(credentials, self) 文件“/Library /Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/auth.py”,第 486 行,auth_func(credentials, sock_info) 文件“/Library/Frameworks/Python.framework/ Versions/2.7/lib/python2.7/site-packages/pymongo/auth.py”,第 466 行,在 _authenticate_default 返回 _authenticate_scram_sha1(credentials, sock_info) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/auth.py”,第 237 行,在 _authenticate_scram_sha1 res = sock_info.command(source, cmd) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib /python2.7/site-packages/pymongo/pool.py”,第 496 行,在命令 collat​​ion=collat​​ion)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ pymongo/network.py”,第 125 行,在命令 parse_write_concern_error=parse_write_concern_error) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/helpers.py”,第 146 行, in _check_command_response raise OperationFailure(msg % errmsg, code, response) OperationFailure: bad auth 认证失败。在 _authenticate_scram_sha1 res = sock_info.command(source, cmd) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py”,第 496 行,在命令整理中=collat​​ion)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/network.py”,第 125 行,在命令 parse_write_concern_error=parse_write_concern_error)文件“/Library/Frameworks /Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/helpers.py”,第 146 行,在 _check_command_response 中引发 OperationFailure(msg % errmsg, code, response) OperationFailure: bad auth 身份验证失败。在 _authenticate_scram_sha1 res = sock_info.command(source, cmd) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py”,第 496 行,在命令整理中=collat​​ion)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/network.py”,第 125 行,在命令 parse_write_concern_error=parse_write_concern_error)文件“/Library/Frameworks /Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/helpers.py”,第 146 行,在 _check_command_response 中引发 OperationFailure(msg % errmsg, code, response) OperationFailure: bad auth 身份验证失败。在命令 collat​​ion=collat​​ion)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/network.py”,第 125 行,在命令 parse_write_concern_error=parse_write_concern_error)文件“/ Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/helpers.py”,第 146 行,在 _check_command_response 中引发 OperationFailure(msg % errmsg, code, response) OperationFailure: bad auth Authentication失败的。在命令 collat​​ion=collat​​ion)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/network.py”,第 125 行,在命令 parse_write_concern_error=parse_write_concern_error)文件“/ Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/helpers.py”,第 146 行,在 _check_command_response 中引发 OperationFailure(msg % errmsg, code, response) OperationFailure: bad auth Authentication失败的。第 146 行,在 _check_command_response 中引发 OperationFailure(msg % errmsg, code, response) OperationFailure: bad auth 身份验证失败。第 146 行,在 _check_command_response 中引发 OperationFailure(msg % errmsg, code, response) OperationFailure: bad auth 身份验证失败。

4

0 回答 0