Connection from Matlab to Mongo using mongoc does not work
Show older comments
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");
and
conn = mongoc("localhost",27017,"test", UserName= "testUser", Password="testPassword");
Both options do not work as expected. 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
Accepted Answer
More Answers (0)
Categories
Find more on Manage Products in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!