Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication failure on MongoDB 6.0

Since I started to use MongoDB 6.0 I can no longer connect to the DB with Mongoose because of "Authentication failure". I kept everything the same as on MongoDB 5 but it no longer works with 6.

MongoServerError: Authentication failed.
    at Connection.onMessage (/home/leemorgan/market/market-api/node_modules/mongodb/lib/cmap/connection.js:207:30)
    at MessageStream.<anonymous> (/home/leemorgan/market/market-api/node_modules/mongodb/lib/cmap/connection.js:60:60)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (/home/leemorgan/market/market-api/node_modules/mongodb/lib/cmap/message_stream.js:132:20)
    at MessageStream._write (/home/leemorgan/market/market-api/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at Socket.ondata (node:internal/streams/readable:766:22)
    at Socket.emit (node:events:513:28) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  [Symbol(errorLabels)]: Set(1) { 'HandshakeError' }
}

Code for connecting with Mongoose:

mongoose.connect("mongodb://127.0.0.1:27017/myDB", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    auth: {authSource: "admin"},
    user: "user",
    pass: process.env.MONGODB_PASS,
    family: 4
});

I checked and re-checked the username/password a million times. I can log in to mongosh with the user, just not through Mongoose. This even happens when authentication is disabled. How do I get past this error?

like image 801
Lee Morgan Avatar asked Nov 01 '25 16:11

Lee Morgan


1 Answers

I don't see auth: {authSource in the documentation? Looks like it should be authSource: "admin",. Try:

mongoose.connect("mongodb://127.0.0.1:27017/myDB", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    user: "user",
    pass: process.env.MONGODB_PASS,
    family: 4
});
like image 85
Ronnie Royston Avatar answered Nov 04 '25 11:11

Ronnie Royston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!