Python reports an error when connecting to the database. Command listdatabases requires authentication, full error: {OK ‘: 0.0,’ errmsg ‘:’ command listdatabases requires authentication ‘,’ code ‘: 13,’ codename ‘:’ unauthorized ‘}
The reason for the error is that authentication is required, indicating that user name and password authentication are required to connect to mongodb database.
Connect mongodb without password. The code is as follows:
from pymongo import MongoClient
class MongoDBConn:
def __init__(self, host, port, db_name, user, password):
"""
Establishing database connections
"""
self.conn = MongoClient(host, port)
self.mydb = self.conn[db_name]
With password authentication, connect to Mongo database, and the code is as follows:
from pymongo import MongoClient
class MongoDBConn:
def __init__(self, host, port, db_name, user, password):
"""
Establishing database connections
"""
self.conn = MongoClient(host, port)
self.db = self.conn.admin
self.db.authenticate(user, password)
self.mydb = self.conn[db_name]
Pit record
In fact, an error was reported in the middle:
Authentication failed., full error: {‘ok’: 0.0, ‘errmsg’: ‘Authentication failed.’, ‘code’: 18, ‘codeName’: ‘AuthenticationFailed’}
Originally used directly in the code:
self.db = self.conn[db_name]
self.db.authenticate(user, password)
If you directly use the target database for link authentication, you will report the above error. Instead, first connect to the system default database admin, use admin for authentication, and you will succeed, and then do the corresponding operation for the target database.
Read More:
- Python classes that connect to the database
- Pychar can’t connect to Python console, but it can run. Py file, and Anaconda’s command line can run Python command
- [Solved] Plugin requires missing class loader for ‘Python‘
- [Solved] error: when using the property decorator in Python, an error occurs: typeerror: descriptor ‘setter’ requires a ‘property’ object but
- Design python+mysql database with multi-level menu directory
- Python Error aiohttp.client_exceptions.ClientConnectorCertificateError, Cannot connect to host:443
- How does Python output colored fonts in the CMD command line window
- [Solved] Jupyter Notebook Error: SparkException: Python worker failed to connect back
- [Solved] Command “python setup.py egg_info” failed with error code 1
- How to Fix Errors encountered in executing Python scripts with command line parameters
- Command “/usr/bin/python -u -c “import setuptools, tokenize;__file__=‘/tmp/pip-cus9V0-build/setup.py
- [Solved] command “python setup.py egg_info“ failed with error code 1
- Mac install python_Levenshtein Error: error: command ‘clang‘ failed with exit status 1
- Python2.7 Pyinstaller Install Error: ERROR: Command errored out with exit status 1
- Pip3 install Mysqlclient Error: Command “python setup.py egg_info“ failed with error
- [Solved] Python install kenlm error: ERROR: Command errored out with exit status 1: …
- [Solved] Ubuntu18.04 Install pyqt5 error: Command “python setup.py egg_info“ failed with error code 1 in /tmp/pip-build-92
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- [Solved] RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place