Cannot connect to mongoDB (version 4.2.6, authentification error)

32 views (last 30 days)
I am having a difficulty in using database toolbox, espectcially for mongoDB.
Now, I am uinsg mongodb version: 4.2.6. There was no problem when I connect to my DB using Python. For example,
in Python
import pymongo
mongo_client = pymongo.MongoClient('', 27017, username="my_name", password='my_pwd')
mongo_db = mongo_client.Weather # Weather is my db name
It works perfectly.
but when I use MATLAB, I cannot connect.
server = "";
port = 27017;
dbname = "Weather";
conn = mongo(server, port, dbname, 'UserName', "my_name", ...
'Password', "my_pwd");
and I have the following error message.
If I look into the part error occurs, the error occurs in 'mongo.m' file in 358 line
% Manually iterate through the list and build a CELL ARRAY
iter = dblist.iterator;
The error message is
Caused by: com.mongodb.MongoCommandException: Command failed with error
18 (AuthenticationFailed): 'Authentication failed.' on server The full response is { "ok" : 0.0, "errmsg" :
"Authentication failed.", "code" : 18, "codeName" :
"AuthenticationFailed" }
Based on this MATLAB documentation, they support MongoDB version up to 4.0. Is that a reason why I cannot connect to my db?
However, When I search the offical mongoDB doccumentation, it seems to me there is no big changes in authentification mechanism.
It would be grate if you let me know how to solve this problem.
  1 Comment
PetHen on 16 Jun 2020
Edited: PetHen on 17 Jun 2020
I had a similar issue where the problem was that the credentials are stored in one database (say 'admin') but the user only had priviliges to another database (say 'data').
From java you can authenticate using one database and then call getDatabase to get the handle to another database, but in MatLab's Database Toolbox there is the one and only mongo() function that does it all. However it DOES NOT distinguish between source and database, hence in order to authenticate you need to use 'admin' database but since the user does not have priviliges to that database, only to 'data', the authentication will fail. Using the mongo() function as is, I did not find any way around this.
My simple (but somewhat ugly) solution (to a maybe oversimplified implementation?) was to hardcode the 'admin' database in the MongoCredential-calls inside mongo()-function in the file mongo.m
There are probably better and for sure nicer solutions, but it was a quick work around.

Sign in to comment.

Answers (1)

Neeraj Rajpurohit
Neeraj Rajpurohit on 1 Jul 2020
This is a problem I encountered as well. The problem occurs becuae the database name is not being set in the mongo.m class. A quick remedial would be clicking on the error to open the mongo.m class and commenting out that particular segment. You would have to hardcode the database name in the mongo.m file.
Better alternative would be to connect your Database to a server and send the data to your server through HTTP Requests. You can read abut webwrite and webread, they help in sending HTTP Requests to the server.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!