Hi everyone,
I am facing problems to connect to a mongoDB container from Matlab using mongoc. I am able to connect using MongoDB Compass and pymongo with two different username/password combinations. The Python code looks as follows:
# Define MongoDB connection parameters
host = 'localhost' # Hostname where MongoDB is running
port = 27017 # Port MongoDB is listening on
database_name = 'test' # Database you want to connect to
# Define credentials
username = 'testUser'
password = 'testPassword'
# Construct the connection URI
uri = f'mongodb://{username}:{password}@{host}:{port}'
try:
# Create a MongoClient object
client = MongoClient(uri)
# Access the database
db = client[database_name]
# Print a success message
print('Successfully connected to MongoDB with authentication')
# Optionally, check if you can list collections (as a simple test)
collections = db.list_collection_names()
print('Collections in the database:', collections)
except ServerSelectionTimeoutError as e:
# Handle server selection timeout errors
print('Failed to connect to MongoDB: Server selection timeout', e)
except ConnectionFailure as e:
# Handle general connection failures
print('Failed to connect to MongoDB: Connection failure', e)
except Exception as e:
# Handle any other exceptions
print('An error occurred:', e)
If I try to connect with Matlab I use
conn = mongoc("localhost",27017,"test", "UserName", "testUser", "Password", "testPassword");
Unfortunately the obtained error Message just indicates that authentication fails:
Error using database.mongo.connection
[Mongo Driver Error]: Authentication failed..
Error in mongoc (line 52)
conn = database.mongo.connection(varargin{:});
If I the other username/password I obtain the same error. I would highly appreciate any suggestions what to do.
All the best
Moritz