2

my code:

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"})

I get the following error when trying a simple insert via python 2.7 to 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') Database(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') Collection(Database(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')

Traceback (most recent call last): File "/Users/danielmatthews/Python/sunfounderpy3.py", line 18, in {"item": "canvas"}) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py", line 682, in insert_one session=session), File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py", line 598, in _insert bypass_doc_val, session) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py", line 578, in _insert_one True, _insert_command, session) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/mongo_client.py", line 1099, in _retryable_write return self._retry_with_session(retryable, func, s, None) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/mongo_client.py", line 1066, in _retry_with_session with self._get_socket(server) 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/mongo_client.py", line 944, in _get_socket with server.get_socket(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", line 138, in get_socket with self.pool.get_socket(all_credentials, checkout) 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/pool.py", line 893, in get_socket sock_info.check_auth(all_credentials) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py", line 588, in check_auth auth.authenticate(credentials, self) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/auth.py", line 486, in authenticate auth_func(credentials, sock_info) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/auth.py", line 466, in _authenticate_default return _authenticate_scram_sha1(credentials, sock_info) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/auth.py", line 237, in _authenticate_scram_sha1 res = sock_info.command(source, cmd) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py", line 496, in command collation=collation) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/network.py", line 125, in command parse_write_concern_error=parse_write_concern_error) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/helpers.py", line 146, in _check_command_response raise OperationFailure(msg % errmsg, code, response) OperationFailure: bad auth Authentication failed.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 3
    Based on the error message, please double check your credentials (user/password). If the password contains any special chars (i.e. ;#@) please escape them. See also related question: [How To Escape Password in Pymongo](https://stackoverflow.com/questions/39237813/how-to-escape-in-a-password-in-pymongo-connection). If you still have further issues connecting please open a ticket via [MongoDB Atlas Support](https://cloud.mongodb.com/support) – Wan B. Apr 05 '18 at 23:49

0 Answers0