0

I have generated a MongoDb database named "mydb" and a collection called "mydb". And I also generated a db user and granted that user these roles :

db.updateUser("myadmin", {pwd:"mypasswd", roles:[{role :"readWrite", db: "admin"},{role : "readWriteAnyDatabase", db: "admin"},{role : "userAdminAnyDatabase", db: "admin"},{role : "dbAdminAnyDatabase", db: "admin"}]})

I am starting mongod like this :

mongod --auth --dbpath C:\data\db

I am starting mongo like this :

mongo --port 27017 -u "myadmin" -p "mypasswd" --authenticationDatabase "admin"

Then I'm connecting to admin db like this :

var MongoClient = require('mongodb').MongoClient;

// Connect to the db
MongoClient.connect("mongodb://x.x.x.x:27017/admin", function(err, db) {
  if(!err) {
    console.log("successfully connected to the database");

  }else{
    console.log("Error on connecting... aborting and exiting");
    return console.dir(err);
    throw err;
   }

    db.authenticate('myadmin', 'mypasswd', function(err, res) {

        if(!err) {

console.log("authenticate ok");

        }   

        else{
    console.log("Error on connecting... aborting and exiting");
    return console.dir(err);
    throw err;
   }

    });
});

Now I need to work with "mydb" database. Can you tell me how I should modify my code so that I can work with "mydb"? Thanks in advance.

jason
  • 6,962
  • 36
  • 117
  • 198
  • 1
    `MongoClient.connect("mongodb://:@:/mydb")` just like the [documentation actually says](https://docs.mongodb.com/manual/reference/connection-string/). Don't know where you are reading `admin` from, but it sounds like you are reading someones blog, and not the official documentation. `MongoClient` handles the authentication for you. – Neil Lunn Jul 16 '17 at 10:47
  • @NeilLunn So should I set `--authenticationDatabase` as `mydb` when starting mongo? – jason Jul 16 '17 at 10:49
  • Nope. Not required. I see the problem might be the higher voted answers here https://stackoverflow.com/questions/4688693/how-do-i-connect-to-mongodb-with-node-js-and-authenticate. But all the **correct** responses are way down the list. Unfortunately 6-7 year old responses have a lot more upvotes. – Neil Lunn Jul 16 '17 at 10:51
  • @chf Wrong. Please delete that. There is no need to specify the authSource unless you actually changed it from the default. – Neil Lunn Jul 16 '17 at 10:52
  • @NeilLunn I have done what you said but I get : `Authentication failed.` – jason Jul 16 '17 at 11:13
  • Btw, When I authenticate using `admin` using `MongoDB Compass`, I can reach `mydb`. – jason Jul 16 '17 at 11:16
  • Well that's because you did not give yourself permission to any other database than "admin". You need to give permissions to "mydb". Also, don't use so many roles. You application only needs `readWrite` at the most, for most general cases. Also, this is all covered in the documentation. Please [read the documentation](https://docs.mongodb.com/manual/tutorial/manage-users-and-roles/) and not blog posts. And "reach" and "use" are two completely different things. – Neil Lunn Jul 16 '17 at 11:16

0 Answers0