The following error happens when I'm trying to authenticate with an "@" in the password:
pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', 'code': 18, 'codeName': 'AuthenticationFailed'}
Here's the following code:
username = parse.quote_plus(db_info['username'])
password = parse.quote(db_info['password'])
ssh_server = SSHTunnelForwarder((ssh_info['ip'], ssh_info['port']),
ssh_username=ssh_info['username'],
ssh_password=ssh_info['password'],
remote_bind_address=('127.0.0.1', 27017))
ssh_server.start()
test = pymongo.MongoClient('127.0.0.1', username=username, password=password)
db = test['DictatorBot']['ReactionRoles']
print(db.find_one())
The password is something along the lines of "p@ssw0rd" which is then turned to p%40ssw0rd" when using parse.quote/_plus.
I have to SSH Tunnel into the database before I can connect, not sure why it changed to where I need to when I didn't need to previously.
The Full Stack Trace/
Traceback (most recent call last):
File "A:\Discord Bots\Dictator Bot\test.py", line 50, in <module>
print(db.find_one())
^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\collection.py", line 1495, in find_one
for result in cursor.limit(-1):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\cursor.py", line 1243, in
next
if len(self.__data) or self._refresh():
^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\cursor.py", line 1160, in
_refresh
self.__send_message(q)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\cursor.py", line 1039, in
__send_message
response = client._run_operation(
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\_csot.py", line 108, in csot_wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\mongo_client.py", line 1425, in _run_operation
return self._retryable_read(
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\mongo_client.py", line 1534, in _retryable_read
return self._retry_internal(
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\_csot.py", line 108, in csot_wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\mongo_client.py", line 1501, in _retry_internal
).run()
^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\mongo_client.py", line 2347, in run
return self._read() if self._is_read else self._write()
^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\mongo_client.py", line 2479, in _read
with self._client._conn_from_server(self._read_pref, self._server, self._session) as (
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 137, in __enter__
return next(self.gen)
^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\mongo_client.py", line 1350, in _conn_from_server
with self._checkout(server, session) as conn:
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 137, in __enter__
return next(self.gen)
^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\mongo_client.py", line 1260, in _checkout
with server.checkout(handler=err_handler) as conn:
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 137, in __enter__
return next(self.gen)
^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\pool.py", line 1758, in checkout
conn = self._get_conn(checkout_started_time, handler=handler)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\pool.py", line 1916, in _get_conn
conn = self.connect(handler=handler)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\pool.py", line 1720, in connect
conn.authenticate()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\pool.py", line 1093, in authenticate
auth.authenticate(creds, self, reauthenticate=reauthenticate)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\auth.py", line 656, in authenticate
auth_func(credentials, conn)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\auth.py", line 560, in _authenticate_default
return _authenticate_scram(credentials, conn, "SCRAM-SHA-1")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\auth.py", line 299, in _authenticate_scram
res = conn.command(source, cmd)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\helpers.py", line 327, in
inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\pool.py", line 985, in command
return command(
^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\network.py", line 212, in
command
helpers._check_command_response(
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pymongo\helpers.py", line 233, in
_check_command_response
raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', 'code': 18, 'codeName': 'AuthenticationFailed'}
I have confirmed the username and password are correct as I am able to copy and paste the connection string into MongoDBCompass: mongodb://us3rname:p@ssw0rd(or p%40ssw0rd)@127.0.0.1:27017/?directConnection=true&ssl=false&authSource=admin
I've tried adding "?directConnection=true&ssl=false&authSource=admin" to the end of the connection string with each setting removed/swapped out.
@aneroid provided the answer
What he suggested was to change this line:
test = pymongo.MongoClient('127.0.0.1', username=username, password=password)
to
test = pymongo.MongoClient(host='127.0.0.1', port=ssh_server.local_bind_port, username=db_info['username'], password=db_info['password'])
This fixed the authentication issue failing when the username or password contained an "@" symbol.