• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

290
Views
mongoose unable to connect authorized db

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???

about 3 years ago · Santiago Trujillo
3 answers
Answer question

0

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);
about 3 years ago · Santiago Trujillo Report

0

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/

about 3 years ago · Santiago Trujillo Report

0

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'}])
about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error