简体   繁体   中英

How can I set TLS for Mongoose connection

I'm trying to migrate my mongo database from Compose to IBM Cloud Databases for Mongo and in their documnetations ( https://www.compose.com/articles/exporting-databases-from-compose-for-mongodb-to-ibm-cloud/ ): "With a new Databases for MongoDB deployment, you'll be provided with a replica set of two endpoints to connect to your database. Databases for MongoDB also uses a TLS certificate, so you'll need to configure your MongoDB application driver to accept two hosts and a TLS certificate"

How can I set the TLS certificate provided by IBM Cloud in Mongoose connection? Nothing I've tried worked:(

I can see my database if I'm using the IBM cli but from my node.js application I cannot connect to it

var mongoose = require('mongoose');
mongoose.Promise = Promise;
var uri="mongodb://admin:passSftgdsdfvrrdfs@host1-1231243242.databases.appdomain.cloud:32605,host2-1231243242,host1-1231243242/testDatabaseName?authSource=admin&replicaSet=replset"
myDb.db = mongoose.createConnection(uri, {
  tls: true,
  tlsCAFile:`076baeec-1337-11e9-8c9b-ae5t6r3d1b17` (this is the name of the certificate and is placed in the root)
  // tlsCAFile: require('fs').readFileSync('041baeec-1272-11e9-8c9b-ae2e3a9c1b17') // I have also tried something like this

absolute nothing is working even the database is there Please help me

I'm also facing same problem this works for me

 mongoose.connect(‘mongodb+srv://username:password@host/db_name?authSource=admin&replicaSet=repliasetname&tls=true&tlsCAFile=/root/ca-certificate.crt’,{some config})

Try the following:


var key = fs.readFileSync('/home/node/mongodb/mongodb.pem');
var ca = [fs.readFileSync('/home/node/mongodb/ca.pem')];

var o = {
    server: {
        ssl: true,
        sslValidate:true,
        sslCA: ca,
        sslKey: key,
        sslCert:key
    },
    user: '****',
    pass: '****'
};

m.connect('mongodb://dbAddr/dbName', o)```

I did it locally, you need to install the tunnel first

$ ssh -i "IF YOU HAVE PEM.pem" -L <27017:YOUR_AMAZON_HOST:27017> <server_user_name@server_ip_OR_server_url> -N

I managed to implement it as follows

const CERTIFICATE_PATH = 'rds-combined-ca-bundle.pem'
const certificateCA = CERTIFICATE_PATH && [fs.readFileSync(CERTIFICATE_PATH)];
    
const sslOptions = certificateCA
  ? ({
      ssl: true,
      tlsAllowInvalidHostnames: true,
      sslCA: certificateCA,
      user: MONGODB_USER,
      pass: MONGODB_PASSWORD,
    } as ConnectionOptions)
  : {};

const options: ConnectionOptions = {
  ...sslOptions,
};

export const connectMongoDb = async (): Promise<void> => {
  await mongoose.connect('mongodb://localhost:27017/test', options);
  console.log('📊 Successfully connected to the database');
};

You need to set

MONGODB_USER

MONGODB_PASSWORD

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