我的代码:
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(主机= [&#39; sensor-cluster-shard-00-02-jhiup.mongodb.net:27017&#39 ;, &#39; sensor-cluster-shard-00-00-jhiup.mongodb.net:27017&#39 ;, &#39; sensor-cluster-shard-00-01-jhiup.mongodb.net:27017&#39], document_class = dict,tz_aware = False,connect = True, ssl_ca_certs =&#39; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/certifi/cacert.pem' ;, replicaset = u&#39; Sensor-Cluster-shard-0&#39;,ssl_cert_reqs = 2,ssl = True, authsource = U&#39;管理&#39;) 数据库(MongoClient(主机= [&#39; sensor-cluster-shard-00-02-jhiup.mongodb.net:27017&#39 ;, &#39; sensor-cluster-shard-00-00-jhiup.mongodb.net:27017&#39 ;, &#39; sensor-cluster-shard-00-01-jhiup.mongodb.net:27017&#39], document_class = dict,tz_aware = False,connect = True, ssl_ca_certs =&#39; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/certifi/cacert.pem' ;, replicaset = u&#39; Sensor-Cluster-shard-0&#39;,ssl_cert_reqs = 2,ssl = True, authsource = u&#39; admin&#39;),u&#39; test&#39;) 集合(数据库(MongoClient(主机= [&#39; sensor-cluster-shard-00-02-jhiup.mongodb.net:27017&#39 ;, &#39; sensor-cluster-shard-00-00-jhiup.mongodb.net:27017&#39 ;, &#39; sensor-cluster-shard-00-01-jhiup.mongodb.net:27017&#39], document_class = dict,tz_aware = False,connect = True, ssl_ca_certs =&#39; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/certifi/cacert.pem' ;, replicaset = u&#39; Sensor-Cluster-shard-0&#39;,ssl_cert_reqs = 2,ssl = True, authsource = u&#39; admin&#39;),u&#39; test&#39;),u&#39; items&#39;)
Traceback(最近一次调用最后一次):文件 &#34; /Users/danielmatthews/Python/sunfounderpy3.py" ;,第18行,在 {&#34; item&#34;:&#34; canvas&#34;})File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/ collection.py&#34 ;, 第682行,在insert_one中 session = session),File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py", 第598行,在_insert中 bypass_doc_val,session)File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py", 第578行,在_insert_one中 True,_insert_command,session)File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/mongo_client.py", 第1099行,在_retryable_write中 return self._retry_with_session(retryable,func,s,None)File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/mongo_client.py", 第1066行,在_retry_with_session中 self._get_socket(server)as sock_info:File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", 第17行,在输入 return self.gen.next()File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/mongo_client.py", 第944行,在_get_socket中 使用server.get_socket(self。 all_credentials)作为sock_info:文件 &#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py" ;, 第17行,在__enter return self.gen.next()File&#34; /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)作为sock_info:文件 &#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py" ;, 第17行,在输入 return self.gen.next()File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py", 第893行,在get_socket中 sock_info.check_auth(all_credentials)File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py", 第588行,在check_auth中 auth.authenticate(credentials,self)File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/auth.py", 第486行,在认证中 auth_func(credentials,sock_info)File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/auth.py", 第466行,在_authenticate_default中 return _authenticate_scram_sha1(credentials,sock_info)File&#34; /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)File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py", 第496行,在命令中 collation = collation)File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/network.py", 第125行,在命令中 parse_write_concern_error = parse_write_concern_error)File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/helpers.py", 第146行,在_check_command_response中 提出OperationFailure(msg%errmsg,代码,响应)OperationFailure:bad auth身份验证失败。