简体   繁体   中英

Can connect mongodb remotely but not locally with nodejs

I have a ubuntu(20.0.4) instance on GCP with mongodb (v4.4.7) installed. Mongodb is configured to be able to connect remotely using a newly created user. I have a node js application and also a laravel app on my local machine (Windows 10). Both the apps(running on local machine) can connect to mongodb instance serving from GCP. Mongodb node js version is 4.0.0 and laravel version is 5.6 .Both are completely working fine. I can perform CRUD operations. Now I want to test the same applications on the GCP instance. The laravel app works perfect in there also but nodejs is having problem connecting to mongodb. It always throws the following error -

MongoDB client failed!
{ MongoServerError: Authentication failed.
at MessageStream.messageHandler (/var/www/api/node_modules/mongodb/lib/cmap/connection.js:470:30)
at MessageStream.emit (events.js:198:13)
at MessageStream.EventEmitter.emit (domain.js:466:23)
at processIncomingData (/var/www/api/node_modules/mongodb/lib/cmap/message_stream.js:108:16)
at MessageStream._write (/var/www/api/node_modules/mongodb/lib/cmap/message_stream.js:28:9)
at doWrite (_stream_writable.js:415:12)
at writeOrBuffer (_stream_writable.js:399:5)
at MessageStream.Writable.write (_stream_writable.js:299:11)
at Socket.ondata (_stream_readable.js:710:20)
at Socket.emit (events.js:198:13)
at Socket.EventEmitter.emit (domain.js:466:23)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) ok: 0, code: 18, codeName: 'AuthenticationFailed' }

Nodejs partial code to connect to Mongodb

const MongoClient = require('mongodb').MongoClient
const user = encodeURIComponent(process.env.MONGO_USER);
const password = encodeURIComponent(process.env.MONGO_PASS);
const address = encodeURIComponent(process.env.MONGO_DB_ADDRESS);

const authMechanism = 'SCRAM-SHA-1';

const authdb = encodeURIComponent(process.env.MONGO_DB_AUTHDB || process.env.MONGO_DB_NAME);
const db = encodeURIComponent(process.env.MONGO_DB_NAME);

const uri = `mongodb://${user}:${password}@${address}/${db}?authMechanism=${authMechanism}&authSource=${authdb}&w=1`;

let client = new MongoClient(uri); 

client.connect()
.then( (resp) => {
    console.log('Mongo Connected')
    next();
})
.catch( (err) => {
    console.log("MongoDB client failed!")
    next(err);
});

Tried changing authMechanism to SCRAM-SHA-256 also but no luck.

To summarize the problem in short. I have a GCP instance with mongodb server installed. Mongodb authentication -

  1. works with nodejs app on local machine
  2. works with laravel app on local machine
  3. works with laravel app on hosted on GCP (same mongodb instance)
  4. doesn't work with nodejs app on hosted on GCP (same mongodb instance)
  5. works with cli

On both local and GCP instance nodejs - v15.0.0 npm - v6.14.13

This is due to env vars not getting loaded. Try console logging the env vars and check logs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM