How to authenticate mongoose mongodb connection in node.js

I created user mongodb with command

use admin
db.createUser(
    {
      user: "superuser",
      pwd: "12345678",
      roles: [ "root" ]
    }
)

      

then in my application i am trying to connect mongoose like this

var options = {
user: "superuser",
pass: "12345678"
};


var mongooseConnectionString = 'mongodb://localhost/twitter-mongo';

mongoose.connect(mongooseConnectionString,options);
mongoose.model('User', UserSchema);

var User = mongoose.model('User');

      

I am getting this error when inserting data via mongoose

MongoError: not authorized for insert on twitter-mongo.users

      

please tell me what is wrong in my code.

+8


source to share


5 answers


You must declare the authSource parameter in your connection string to specify the name of the database containing your credentials:

var options = {
  user: "superuser",
  pass: "12345678",
  useMongoClient: true
};

var mongooseConnectionString = 'mongodb://localhost/twitter-mongo?authSource=admin';

      



Note that I also set the useMongoClient parameter to true. This forces users to authenticate using MongoClient.connect with auth credentials and open () stale error messages.

+13


source


This works fine:



var options = { server: { socketOptions: { keepAlive: 1 } } };
var connectionString = 'mongodb://admin:admin1234@localhost:27017/myDB';

 mongoose.connect(connectionString, options);

//Add those events to get more info about mongoose connection:

// Connected handler
mongoose.connection.on('connected', function (err) {
  console.log("Connected to DB using chain: " + connectionString);
});

// Error handler
mongoose.connection.on('error', function (err) {
  console.log(err);
});

// Reconnect when closed
mongoose.connection.on('disconnected', function () {
   self.connectToDatabase();
});

      

+7


source


I started Mongo service using Docker and then connected my Mongoose to it using this code

const _database = 'mongodb://user:pass@localhost:port/MyDB?authSource=admin';
mongoose.connect(_database, {
    useNewUrlParser: true
})
.then(() => console.log('Connected to MongoDB ...'))
.catch(err => console.error('Could not connect to MongoDB:β€Œ', err));

      

it is equal

mongo --username user --password pass --authenticationDatabase admin --port 27017

      

when connecting, and then use the database MyDB

to do operations like search, aggregation, insert, etc. if you don't have a user (MongoDB default user) for your database, you can change Database

as shown below:

const _database = 'mongodb://localhost:port/MyDB';

      

and 27017

- the default MongoDB port, if your MongoDB port hasn't changed as well, then you can do the same for the port. as below:

const _database = 'mongodb://localhost/MyDB';

      

and this is the same as below:

mongo

      

the above code is that there is no user and no port, so no authentication database is required for a non-user as well.

+1


source


try it

const mongooseConnectionString = 'mongodb://superuser:12345678@ localhost/twitter-mongo?authSource=admin'

      

0


source


You need to create a user in the database you are working with, not a database administrator.

Use this command,

use twitter-mongo;
db.createUser({
  user: "superuser",
  pwd: "12345678",
  roles: [ "root" ]
});

      

Instead of this,

use twitter-mongo
db.createUser({
  user: "superuser",
  pwd: "12345678",
  roles: [ "root" ]
});

      

0


source







All Articles