Cannot connect to mongoDB (version 4.2.6, authentification error)
7 次查看(过去 30 天)
显示 更早的评论
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/298698/image.jpeg)
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
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
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
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!