Cannot connect to mongoDB (version 4.2.6, authentification error)

조회 수: 8 (최근 30일)
Sangmin Lee
Sangmin Lee 2020년 5월 23일
답변: Neeraj Rajpurohit 2020년 7월 1일
Hi,
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('xxx.126.241.xxx', 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 = "xxx.126.241.xxx";
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
xxx.126.241.xxx:27017. 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.
Thanks.
  댓글 수: 1
PetHen
PetHen 2020년 6월 16일
편집: PetHen 2020년 6월 17일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Neeraj Rajpurohit
Neeraj Rajpurohit 2020년 7월 1일
Hi,
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.
Thanks,
Neeraj

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by