Connection from Matlab to Mongo using mongoc does not work
9 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Abhishek Kumar Singh
2024 年 9 月 5 日
I understand you're facing an issue connecting to your MongoDB container from MATLAB using the mongoc function, even though you're able to connect using MongoDB Compass and Python's pymongo. This seems to be a known limitation with the mongoc function in MATLAB regarding authentication.
Here's a suggested workaround that has helped others in similar situations:
Instead of using the mongoc function with separate parameters, you can use a connection string (URI) format that includes additional parameters. This approach can help ensure that all necessary authentication details are included. Please try the following syntax:
conn = mongoc('mongodb://testUser:testPassword@localhost:27017/?authSource=admin', 'test');
Replace testUser, testPassword, and localhost with your actual username, password, and host details. Additionally, ensure the URI includes any other necessary parameters from your MongoDB Compass connection string, such as authSource, readPreference, and ssl settings.
This workaround should help resolve the authentication error you're experiencing.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Database Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!