Search code examples
node.jsmongodbmongoosemongodb-replica-set

Different SSL key for mongodb replica set members


I have 3 AWS EC2 instances. 2 for mongodb replica and 1 as my server. I have followed this article to set up my replica set but there is no information on how to connect my nodejs server using mongoose.

I searched and got this as results

mongoose.connect('mongodb://replica1.example.com:27017,replica2.example.com:27017,replica3.example.com:27017/mydatabase?ssl=true', {
  sslValidate: true,
  sslCA: fs.readFileSync('/path/to/ca.pem'),
  sslKey: fs.readFileSync('/path/to/client.pem'),
  sslCert: fs.readFileSync('/path/to/client.pem')
});

As per the article I have different sslKey and Cert for my replica member host 1 and replica member host 2

I am unable to connect to the database as it gives this as error

Could not connect to database: MongoNetworkError: failed to connect to server [replica2.example.com:27017] on first connect [Error: getaddrinfo ENOTFOUND replica2.example.com

I believe this is because I did not specify the ssl key for 2nd member. But how can I do that?


Solution

  • I did not specify the host names in the /etc/hosts path as I did in the replica members EC2 instances like this

    35.xxx.111.yyy          host1.mongo.replset
    yy.231.xxx.182          host2.mongo.replset
    3.xxx.250.yy            host2.mongo.replset
    

    Since the host name and respective IP was not defined in the server instance it was unable to find the address