I tried to connect to mongodb by following string url
DATA = "mongodb://admin:admin@localhost:27017/ais_mlm?authSource=admin"
here is db file
const mongoose = require("mongoose");
//const mongoDB = "mongodb://admin:admin@localhost:27017/ais_mlm?authSource=admin";
const chalk = require('chalk');
const path = require('path');
const connected = chalk.bold.cyan;
const error = chalk.bold.yellow;
const disconnected = chalk.bold.red;
const termination = chalk.bold.magenta;
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
console.log('here...', path.resolve(__dirname, '../.env'),process.env.DATA);
mongoose.connect(process.env.DATA, async(err)=>{
if(err) throw err;
console.log("conncted to db")
}
);
mongoose.connection.on('connected', function(){
console.log(connected("Mongoose default connection is open to ", process.env.DATA));
});
mongoose.connection.on('error', function(err){
console.log(error("Mongoose default connection has occured "+err+" error"));
});
mongoose.connection.on('disconnected', function(){
console.log(disconnected("Mongoose default connection is disconnected"));
});
process.on('SIGINT', function(){
mongoose.connection.close(function(){
console.log(termination("Mongoose default connection is disconnected due to application termination"));
process.exit(0)
});
});
here is env
DATA = "mongodb://admin:admin@localhost:27017/ais_mlm?authSource=admin"
here is my createUser in db by command
> show users
{
"_id" : "ais_mlm.Admin",
"userId" : UUID("04c87ab3-4129-4e8f-ab8a-d76c385dc5d4"),
"user" : "Admin",
"db" : "ais_mlm",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
{
"_id" : "ais_mlm.admin",
"userId" : UUID("e7f7d693-151b-4db7-b3c1-e5e3e90d0e40"),
"user" : "admin",
"db" : "ais_mlm",
"roles" : [
{
"role" : "readWrite",
"db" : "ais_mlm"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
{
"_id" : "ais_mlm.ko",
"userId" : UUID("bec76659-235a-44b8-8816-08a1de983c80"),
"user" : "ko",
"db" : "ais_mlm",
"roles" : [
{
"role" : "readWrite",
"db" : "ais_mlm"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
> use ais_mlm;
switched to db ais_mlm
> db.createUser(
... {
... user: "admin",
... pwd: "admin",
... roles: [ "userAdmin" ]
... }
... )
uncaught exception: Error: couldn't add user: User "admin@ais_mlm" already exists :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1362:11
@(shell):1:1
> show users
{
"_id" : "ais_mlm.Admin",
"userId" : UUID("04c87ab3-4129-4e8f-ab8a-d76c385dc5d4"),
"user" : "Admin",
"db" : "ais_mlm",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
{
"_id" : "ais_mlm.admin",
"userId" : UUID("e7f7d693-151b-4db7-b3c1-e5e3e90d0e40"),
"user" : "admin",
"db" : "ais_mlm",
"roles" : [
{
"role" : "readWrite",
"db" : "ais_mlm"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
{
"_id" : "ais_mlm.ko",
"userId" : UUID("bec76659-235a-44b8-8816-08a1de983c80"),
"user" : "ko",
"db" : "ais_mlm",
"roles" : [
{
"role" : "readWrite",
"db" : "ais_mlm"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
but cannot connect to mongodb what is error why it happen I tried already 2 days so frequent errors Is this string url method deprecated or not How can connect to mongodb by admin access thanks