Reputation: 121
I installed the mongodb 3.4.1 on the ubuntu 16.04 and hosted them on digital ocean.
My local mongo client was mongochef 4.5.2
At first, I didn’t set up the authorization for my app, it’s hacked, and was asked for bitcoin as ransom.
So I tried to set up the authorization. However, encountered a wired problem, once set the authorization: enable
in the /etc/mongo.conf . It’s unable to connect the db remotely and locally with the mongoose
, and got the error auth failed. But it could be connected with the terminal and the mongoChef.
here is my connection with mongoose
global.db=mongoose.connect('mongodb://admin:admin123@ip:27017/nodedb');
and i also tried
var options = {
user : "admin",
pass : “admin123",
auth : {authMechanism: 'MONGODB-CR'}
}
global.db = mongoose.connect('mongodb://@ip:27017/test',options);
and also changed the ip with localhost and 127.0.0.1 locally, failed the same.
Just in case the localhost and 127.0.0.1 didn’t recognised. Also set the configuration in the /etc/hosts with:
127.0.0.1 localhost 127.0.0.1 computename
update~:
there are two users in my admin DB. One is root user. And another one is admin with password admin123 which authenticated DB nodedb
really exhausted by this problem, has any one encounter this problem???
Upvotes: 1
Views: 1256
Reputation: 1
Access your nodedb from terminal(mongosh) then show users. Then check your user roles. Add role for readWrite to user for the nodedb database if it does not exist.
use nodedb
show users
db.grantRolesToUser('admin',[{role:'readWrite', db:'nodedb'}])
Upvotes: 0
Reputation: 4787
You have not specified the authSource (the database against which it is authenticated). So, if the authSource
is admin
it should be specified in the connection options.
options = {
"auth": {
"authSource": "admin"
},
"user": "apiuser",
"pass": "admin123"
};
// If host=localhost, port=27017 and database name=nodedb
mongoose.connect('mongodb://localhost:27017/nodedb', options);
Upvotes: 2
Reputation: 137
Authentication is done at a database level. In your connection string db is nodedb, but your password is stored in admin database. You can connect to nodedb either by adding a user to this db or by defining db for auth with --authenticationDatabase admin. You can read more here. For more info, please check manual http://docs.mongodb.org/manual/reference/privilege-documents/
Upvotes: 0