MongoDB shell version v3.6.13
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6feed04d-eb09-4329-bdb6-9c11d5188b10") }
MongoDB server version: 3.6.13
> show dbs
2019-10-24T20:44:23.221-0400 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, lsid: { id: UUID(\"6feed04d-eb09-4329-bdb6-9c11d5188b10\") }, $db: \"admin\" }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:67:1
shellHelper.show@src/mongo/shell/utils.js:860:19
shellHelper@src/mongo/shell/utils.js:750:15
@(shellhelp2):1:1
>
This is for learning purposes, so I don’t really need users or manage them.
How can I create db’s, access, and modify them without needing to authenticate each time?
When you installed MongoDB, here is no user defined. You can login without providing username or password. However, if you want to create database and manage users, you have to create an administrator account.
So, if you want to create your first database and basically do anything beyond merely viewing anything that might already exist (and nothing should exist in a new, default install), you’ll need to create your admin user and log in as that. Whether you create any other users is up to you, but the admin account by itself should have sufficient permissions to everything in your mongodb.
var MongoClient = require('mongodb').MongoClient,
f = require('util').format,
assert = require('assert');
var user = encodeURIComponent('serval');
var password = encodeURIComponent('tanoshi');
var authMechanism = 'DEFAULT';
// Connection URL
var url = f('mongodb://%s:%s@localhost:27017/japaripark?authMechanism=%s',
user, password, authMechanism);
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
db.close();
});
I created a user called “root” and password “password”, with role as “root”, just to make things easier.
But whenever I want to create a db in my .js file and run it, I get “no authenticated user” error.
Using
mongo --authenticationDatabase admin -u root -p
and entering password “password” works.
I don’t know what else to do?
Do I really need to create a user, can’t this be done without any hassle?