node.js mongoError not authorized on admin to execute command

mongoose not authorized on to execute command
mongodb compass not authorized to execute command
pymongo errors operationfailure not authorized on admin to execute command
general mongodb error not authorized on admin to execute command serverstatus 1
unauthorized: not authorized on admin to execute command ( replsetheartbeat)
is not authorized to execute
not authorized on test1 to execute command
ok 0.0 errmsg not authorized to execute command code 13

I'm studying Node.js and i can'y find any solution.. my consol send me many messages an one is more particulary strange : GET/signup - - ms - - has someone any idea about that ? Thanks in advance !

///////////////////////inclusion des librairies
 // 3 librairies pour gérer les messages flash
 var session = require('express-session');
 var cookieParser = require('cookie-parser');
 var flash = require('express-flash');
 //passerelle pour se connecter à node(node->bdd)
 var passport =require('passport');
 // stockage des sessions(id) et cookies côté serveur uniquement
 var mongoStore =require('connect-mongo')(session); // le session de express-session

//inclure al librairie  express
 var express = require('express');
// Inclusion de la librairie morgan (faire le lien avec la base de données)
var morgan = require('morgan');

// Inclusion de mongoose
 var mongoose = require('mongoose');

 //Inclusion moteur templates ejs
 var ejs = require('ejs');
  var engine =require('ejs-mate');
  // Inclusion de body parser pour les données des formulaires
  var bodyParser = require('body-parser');



  /////////////////////fin des librairies ///////////////

// stocker l'objet express dans une variable plus courte
 
 var app = express();



//inclure le fichier secret.js
var secret = require('./config/secret');
 ////////////connexion à la bd avec mongoose///
 
 mongoose.connect(secret.database, // voir pour création de db en ligne !!
 	{useNewUrlParser:true},
 	function(err){
 		if(err){console.log(err)
 		}else{
 			console.log('connexion OK');
 		}
 	});


/////////////////// gestion des Passerelles (middleware)/////////////////////////
app.use(express.static(__dirname + '/public')); // pour le style
app.use(morgan('dev'));
app.engine('ejs',engine);
app.set('view engine','ejs');
// les deux lignes ci-dessous pour récupérer les données des formulaires
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
// affichage messages flash et gestion des cookies
app.use(cookieParser());
app.use(session({
	resave :true,
	saveUninitialized:true,
	secret :secret.secretKey,
	store : new mongoStore({
		url:secret.database,
		autoReconnect:true })
}));

app.use(flash());

//authentification
app.use(passport.initialize());
app.use(passport.session());


////////////////définition du chemin des pages principales////////////////////////

var mainRoutes =require('./routes/main');
app.use(mainRoutes);

var userRoutes =require('./routes/user');
app.use(userRoutes);



//app.post()

//app.put()

//app.delete()

The npm module connect-mongo can't handle mongodb+srv:// connection strings. You'll have to use the older connection string types that start with mongodb://.

If you are using MongoDB Atlas I recommend to go to connect on the cluster view, then connect your application and then select Node.js version 2.2.12, not 3.0.

You probably also have to change the /test or /admin in your connection string to /TheNameOfYourDatabase with your database name there. (See radihuq's answer below)

MongoError: not authorized on [db] to execute command, /app/node_modules/mongodb/lib/utils.js:98 ^ : MongoError: not authorized on fcc-​dgw to execute command { count: “urls”, query: {} } Finally, make sure you're an admin user - Here's the steps from mLab's start guide: While the node application was able to successfully connect to MongoDB, the  MongoError: not authorized on admin to execute command { listIndexes: "users" hfort Hi, I was performing the last lecture of node.js course: "9 - MongoDB in the Cloud", but the mlab.com changed to cloud.mongodb.com

@ConstJS answer worked for me. Expanding on it a little bit -

If you're using Postman and your error looks like this:

"errmsg": "not authorized on admin to execute command { insert: [..] }, $db: \"admin\" }"

Note this specific part:

$db: \"admin\"

You'll want to change that to your DB name so go to your URI and find

mongodb.net:27017/admin

and change admin to your database name

MongoError: not authorized on admin to execute command , Hi, I was performing the last lecture of node.js course: "9 - MongoDB in the Cloud​", but the mlab.com changed to cloud.mongodb.com. Anyway  ENABLE_ADMIN=true causes "MongoError: not authorized on admin to execute command { listDatabases: 1 }" #352 aurelienrb opened this issue Jul 25, 2017 · 7 comments Comments

It appears you are trying to access MongoDB's admin database and insert something which is not allowed. I am assuming you are using a connection string like:

mongodb://mongodb0.example.com:27017/admin

Make sure you update the /admin piece to the database you want to connect to.

ENABLE_ADMIN=true causes "MongoError: not authorized on , Hello, I tried to setup Mongo Express (v0.42 & v0.40) with MongoError: not authorized on admin to execute command { listDatabases: 1 } at Function. "4.4.​0-66-generic" }, platform: "Node.js v6.9.5, LE, mongodb-core: 2.1.8"  I got the following error: { [MongoError: not authorized on admin to execute command { listDatabases: 1 }] name: 'MongoError', message: 'not authorized on admin to execute command { listDatabases: 1 }', ok: 0, errmsg: 'not authorized on

This error is specific to the fact that you are passing a url mongo-connect doesn't know how to handle. Instead of the URL, you can pass the mongoose.Connection object and use the mongooseConnection property in the MongoStore options object.

new mongoStore({
  mongooseConnection: mongoose.connection,
  autoReconnect: true 
})

In order for this to work, you'll need to establish a connection first and then pass the mongoose.connection object to your mongoStore class.

 mongoose.connect(secret.database, {useNewUrlParser:true}, function(err) {
    if(err) { 
      console.log(err)
    } else {
      app.use(session({
        resave :true,
        saveUninitialized:true,
        secret :secret.secretKey,
        store : new mongoStore({
          url:secret.database,
          autoReconnect:true })
      }));
      console.log('connexion OK');
    }
});

There are more elegant ways to handle it but that should get you started.

Mongodb URI connection error like not authorized - MEAN, (node:14441) UnhandledPromiseRejectionWarning: MongoError: not authorized on sWays to execute command { insert: MongoError.create (/home/bitnami/​sways/node_modules/mongoose/node_modules/mongodb-core/lib/error.js:31:11​) .connect('mongodb://root:qwIByv276P9E@localhost/admin');. Do you want to request a feature or report a bug? bug What is the current behavior? Under high load, lots of errors appear in the console like this: MongoError: command find requires authentication

[NODE-853] not authorized on [database_name] to execute command, Uploaded image for project: 'Node.js Driver' mongodb-core@2.0.13 "​message": "not authorized on appdb to execute command { insert: \"devices\", documents: 609, ordered: true, writeConcern: authSource=admin  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Node.js MongoError: not authorized on admin to execute command

MongoDB - admin user not authorized, The problem was the role was not set to the root so set the role to be root when adding the first admin user. use admin. db.createUser(. {. I can successfully complete the . odebb setup command. It also works as expected against local instance of Mongodb but when I run it against Atlas cluster I get: not authorized on admin to execute command { create: "socket.io", capped: true, size: 5242880 }" My config.json

MongoDB not authorized error on Ubuntu 16.04 - node.js - html, MongoDB not authorized error on Ubuntu 16.04 - node.js. Here is the error: MongoError: not authorized on DATABASE_NAME to execute command From your mongo you have to execute the following commands: mongo shell use admin  The MongoError: not authorized on rocketchat to execute command { listIndexes: "users", cursor: {} } should appear in the logs. Expected behavior: Rocket.Chat to successfully start up

Comments
  • Tanks a lot ConstJS ! I didn't see your asweer before now. everything is working well !! Thanks again :)
  • Glad it worked! @cessother Could you please mark it as answered? :)
  • I will do it with pleasure, but I have one more question : How do I do to mark it as answered ? :)
  • Click on the check mark beside the answer to toggle it from greyed out to filled in. This marks it as answered :)
  • been trying to solve this forever, this finally worked. thank you. To help others-- this also surfaces as: MongoError: Cannot do raw queries on admin in atlas
  • This should be also an accepted answer. Thanks @Taslim
  • Thanks, I updated the answer and added a reference to your answer in my answer above :)
  • Hi Nick, thank you for your answer. I use this kind of connexion for mongodb Atlas : mongodb+srv://root:<password>@cluster0-zfgku.mongodb.net/test?retryWrites=true. I can add user, but no session id, that's strange. Maybe there is a problem withe the library "connect-mongo" ?
  • If you can access the DB to save a user, then the connection is okay. I think your issue is related to not passing the Mongoose connection to the session. I think you may want to look into reusing your connection. Look at the section "Re-use a Mongoose connection" here: npmjs.com/package/connect-mongo