0

I've ran into a strange issue today. I can't seem to connect to my local MongoDB container from my Node.js backend for local testing.

This is how I create my container:

docker start mongo || docker run --rm --name mongo -e MONGO_INITDB_ROOT_USERNAME=\"$MONGODB_USER\" -e MONGO_INITDB_ROOT_PASSWORD=\"$MONGODB_PASSWORD\" -e MONGO_INITDB_DATABASE=\"concept_slide\" -p 27017:27017 -d mongo:latest

I know for a fact that my app and database use the same credentials as they pull from the same environment variables:


export const connectMongoDB = () => {
  console.log({
    user: process.env.MONGODB_USER,
    pass: process.env.MONGODB_PASSWORD,
  });
  mongoose.connect(`mongodb://${process.env.MONGODB_TEST_HOSTNAME}:27017/concept_slide`, {
    user: process.env.MONGODB_USER,
    pass: process.env.MONGODB_PASSWORD,
    hostname: process.env.MONGODB_TEST_HOSTNAME,
  });

  mongoose.connection
    .on('connected', () => {
      console.log('Local database connected');
    })
    .on('error', (err) => {
      console.error(
        'Local database unreachable. Please check database or network access. Error: ',
        err
      );
    });
};

And here is the error message:

Local database unreachable. Please check database or network access. Error:  MongoServerError: Authentication failed.
    at Connection.onMessage (/Users/fareskissoum/Documents/Personal-Projects.tmp/open-concepts/open-concepts-backend/node_modules/mongodb/lib/cmap/connection.js:207:30)
    at MessageStream.<anonymous> (/Users/fareskissoum/Documents/Personal-Projects.tmp/open-concepts/open-concepts-backend/node_modules/mongodb/lib/cmap/connection.js:60:60)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (/Users/fareskissoum/Documents/Personal-Projects.tmp/open-concepts/open-concepts-backend/node_modules/mongodb/lib/cmap/message_stream.js:132:20)
    at MessageStream._write (/Users/fareskissoum/Documents/Personal-Projects.tmp/open-concepts/open-concepts-backend/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)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  [Symbol(errorLabels)]: Set(1) { 'HandshakeError' }

I even tried to print them in the console to make sure whether there was an issue, and I confirm there aren't:

{ user: 'fares', pass: 'password', hostname: 'localhost' } // printed

Credentials stored thru env vars in Docker container

I don't understand where the issue comes from. I feel like I'm missing something.

2
  • What is the value of MONGODB_TEST_HOSTNAME? Are you trying to connect from another docker? Did you try connecting via Compass or another tool and compare results with the connection from your code? Commented Oct 6, 2022 at 20:40
  • Found the solution, adding answer Commented Oct 6, 2022 at 20:48

1 Answer 1

2

I really need to stop coding this late.

It turns out the issue was that the database (concept_slide) didn't exist at the creation of the container/instance, hence there was nothing to connect to.

It's a mix of my ignorance and lack of clearer documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.