Working fine for me on Mongodb 4.2 and Mongoose 5.7.13
Node.js
const Connect = async () => {
let url = "mongodb://localhost:27017/test_db";
try {
let client = await Mongoose.connect( url, {
poolSize: 10,
authSource: "admin",
user: "root",
pass: "root123",
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
} );
log( "Database is connected!" );
} catch ( error ) {
log( error.stack );
process.exit( 1 );
}
}
Connect();
/etc/mongod.conf
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0
setParameter:
enableLocalhostAuthBypass: false
security:
authorization: enabled
Database User
use admin;
db.createUser(
{
user: "root",
pwd: "root123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
show users;
{
"_id": "admin.root",
"userId": UUID( "5db3aafd-b1fd-4bea-925e-8a4bfb709f22" ),
"user": "root",
"db": "admin",
"roles": [ {
"role": "userAdminAnyDatabase",
"db": "admin"
},
{
"role": "readWriteAnyDatabase",
"db": "admin"
}
],
"mechanisms": [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}