MongoServerError: Authentification Failed, Code 18

Hello MongoDB people,

Im trying to connect to a MongoDB hosted on Railway, but I keep getting this error:

MongoServerError: Authentication failed.
    at Connection.onMessage (C:\SubMAN\Backend-2\railway_test\node_modules\mongodb\lib\cmap\connection.js:201:30)    
    at MessageStream.<anonymous> (C:\SubMAN\Backend-2\railway_test\node_modules\mongodb\lib\cmap\connection.js:59:60)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (C:\SubMAN\Backend-2\railway_test\node_modules\mongodb\lib\cmap\message_stream.js:124:16) 
    at MessageStream._write (C:\SubMAN\Backend-2\railway_test\node_modules\mongodb\lib\cmap\message_stream.js:33:9)  
    at writeOrBuffer (node:internal/streams/writable:391:12)
    at _write (node:internal/streams/writable:332:10)
    at MessageStream.Writable.write (node:internal/streams/writable:336:10)
    at Socket.ondata (node:internal/streams/readable:754:22)
    at Socket.emit (node:events:513:28) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  connectionGeneration: 0,
  [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}

Here is my code:

import  mongoose from "mongoose";


const connect = async () => {
    mongoose.connect('mongodb://mongo:<password>@containers-us-west-85.railway.app:6949')
        .then(() => console.log('Connected to database movieDB'))
        .catch((err: any) => console.log(err));
}

connect();

The connection uri should be right, since I was able to connect to the DB using MongoDBCompass and the built in IntelliJ Database too.

Any help would be awesome,
Thanks in advance!

Hello :wave: @Splatted_I0I,

Welcome to the MongoDB Community forums :sparkles:

I ran the following code on my system using node v18.7.0:

import mongoose from "mongoose";

const connect = async () => {
    mongoose.connect('mongodb://mongo:<password>@containers-us-west-85.railway.app:6949')
        .then(() => console.log('Connected to database movieDB'))
        .catch((err) => console.log(err));
}

connect();

Initially, there was an error in the .catch(...) parameter,

which I modified to the above code, and it ran successfully with the output:

[Running] node "/node-mongoDB/src/test.js"
Connected to database movieDB

Although the code seems fine, based on the error message you provided, it may be an incorrect combination of user-id/password or might be an issue with the built-in role. Please double-check the code you are running and the role of your database user, if it still does not work as expected, please provide more details about the error and your workflow.

For more information, please check the documentation on how to manage users and role

Also, please note that you should not share the URI string with the user-id and password on public forums or anywhere else to avoid any potential issues.

I hope this information helps you!

Let me know if you have any further questions.

Best,
Kushagra

Thanks for the Fix! My connection also works now :slight_smile:

1 Like