title: Authentication failed to connect to mongodb using pymongo
body: <p>We have written a piece of code in python script using <code>pymongo</code> that connects to mongodb.  </p>
  
  <pre><code>username = 'abc'
  password = 'xxxxxx'
  server = 'dns name of that server'
  port = 27017
  </code></pre>
  
  <p>In program, the code looks like:  </p>
  
  <pre><code>import pymongo
  from pymongo import MongoClient
  client = MongoClient(url, serverSelectionTimeoutMS=300)
  database = client.database_name
  data_insert = database.collection_name.insert_one({'id': 1, 'name': xyz})
  </code></pre>
  
  <p>When I tried to do these operations, it raises an error:  </p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 1114, in next
      if len(self.__data) or self._refresh():
    File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 1036, in _refresh
      self.__collation))
    File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 873, in __send_message
      **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 905, in _send_message_with_response
      exhaust)
    File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 916, in _reset_on_error
      return func(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/pymongo/server.py", line 99, in send_message_with_response
      with self.get_socket(all_credentials, exhaust) as sock_info:
    File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
      return self.gen.next()
    File "/usr/local/lib/python2.7/dist-packages/pymongo/server.py", line 168, in get_socket
      with self.pool.get_socket(all_credentials, checkout) as sock_info:
    File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
      return self.gen.next()
    File "/usr/local/lib/python2.7/dist-packages/pymongo/pool.py", line 792, in get_socket
      sock_info.check_auth(all_credentials)
    File "/usr/local/lib/python2.7/dist-packages/pymongo/pool.py", line 512, in check_auth
      auth.authenticate(credentials, self)
    File "/usr/local/lib/python2.7/dist-packages/pymongo/auth.py", line 470, in authenticate
      auth_func(credentials, sock_info)
    File "/usr/local/lib/python2.7/dist-packages/pymongo/auth.py", line 450, in _authenticate_default
      return _authenticate_scram_sha1(credentials, sock_info)
    File "/usr/local/lib/python2.7/dist-packages/pymongo/auth.py", line 201, in _authenticate_scram_sha1
      res = sock_info.command(source, cmd)
    File "/usr/local/lib/python2.7/dist-packages/pymongo/pool.py", line 419, in command
      collation=collation)
    File "/usr/local/lib/python2.7/dist-packages/pymongo/network.py", line 116, in command
      parse_write_concern_error=parse_write_concern_error)
    File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line 210, in _check_command_response
      raise OperationFailure(msg % errmsg, code, response)
  pymongo.errors.OperationFailure: Authentication failed.
  </code></pre>
  
  <p>In MongoDB, while performing queries we are getting the responses normally, without raising any errors.</p>