How to fix error: self-signed certificate in certificate chain when connect to Azure Cosmos DB for MongoDB account from local

Mattanapol Konguthaikul (TH) 20 Reputation points
2025-06-12T04:05:03.15+00:00

Hi,

I try to connect to Azure Cosmos DB for MongoDB account from local machine.

I use typescript library mongoose to connect to it.

This is an error

TopologyDescription {
  type: 'Unknown',
  servers: Map(1) {
    'seztpedcadcos002.mongo.cosmos.azure.com:10255' => ServerDescription {
      address: 'seztpedcadcos002.mongo.cosmos.azure.com:10255',
      type: 'Unknown',
      hosts: [],
      passives: [],
      arbiters: [],
      tags: {},
      minWireVersion: 0,
      maxWireVersion: 0,
      roundTripTime: -1,
      minRoundTripTime: 0,
      lastUpdateTime: 689051642,
      lastWriteDate: 0,
      error: [MongoNetworkError: self-signed certificate in certificate chain] {
        errorLabelSet: Set(1) { 'ResetPool' },
        beforeHandshake: false,
        [cause]: [Error: self-signed certificate in certificate chain] {
          code: 'SELF_SIGNED_CERT_IN_CHAIN'
        }
      },
      topologyVersion: null,
      setName: null,
      setVersion: null,
      electionId: null,
      logicalSessionTimeoutMinutes: null,
      maxMessageSizeBytes: null,
      maxWriteBatchSize: null,
      maxBsonObjectSize: null,
      primary: null,
      me: null,
      '$clusterTime': null,
      iscryptd: false
    }
  },
  stale: false,
  compatible: true,
  heartbeatFrequencyMS: 10000,
  localThresholdMS: 15,
  setName: null,
  maxElectionId: null,
  maxSetVersion: null,
  commonWireVersion: 0,
  logicalSessionTimeoutMinutes: null
}
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,902 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Vijayalaxmi Kattimani 3,250 Reputation points Microsoft External Staff Moderator
    2025-06-13T03:43:07.5433333+00:00

    Hi Mattanapol Konguthaikul (TH)

    Since you have already installed the certificate from the emulator but are still facing the error, here are a few additional things you can try:

    Double-check that the certificate is correctly installed in your OS's trust store. If you're on Windows, ensure it's listed under the "Trusted Root Certification Authorities". You can use the certlm.msc tool to verify this.

    Make sure you’re running an up-to-date version of Node.js. If you're using an older version, consider updating it, as there may be fixes related to SSL/TLS connections in newer releases.

    In your mongoose connection string, make sure you include the necessary options as follows:

    mongoose.connect('mongodb://your-connection-string', {
        ssl: true,
        sslValidate: true,
        sslCA: [fs.readFileSync('path_to_your_cert.pem')],
        useNewUrlParser: true,
        useUnifiedTopology: true,
    });
    

    Adjust the path to your PEM certificate as necessary.

    If you are still having trouble, you might want to set environment variables which may help with certificate validation:

    export NODE_TLS_REJECT_UNAUTHORIZED=0
    

    (Note: This disables SSL validation and is not recommended for production environments. Only use for testing purposes.)

    Ensure that there's no firewall or proxy blocking your connection. Sometimes, network configurations can interfere with SSL handshakes.

    I request you click on this link for additional information https://learn.microsoft.com/en-us/azure/cosmos-db/emulator?tabs=ssl-netstd21#import-certificate

    I hope this information helps. Please do let us know if you have any further queries.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.