title: MongoError: self signed certificate in certificate chain
body: <p>I am trying to connect my node server with mongodb with ssl options enabled.</p>
  
  <h1>mongod.conf</h1>
  
  <pre><code>storage:
    dbPath: /var/lib/mongodb
    journal:
    enabled: true
  
  # where to write logging data.
  systemLog:
    destination: file
    logAppend: true
    path: /var/log/mongodb/mongod.log
  
  # network interfaces
  net:
    port: XX
    bindIp: 127.0.0.1,XXX.XX.XX.XXX
    ssl:
      mode: requireSSL
      PEMKeyFile: /etc/ssl/PEMKEYFILE.pem
      PEMKeyPassword: "PASSWORDFORPEM"
      CAFile: /etc/ssl/CAFILE.pem
  
  security:
    authorization: enabled
  
  replication:
    oplogSizeMB: 1024
    replSetName: "REPLSET"
  </code></pre>
  
  <h1>node server conf file</h1>
  
  <pre><code>MONGO_URL: '&lt;pathToMongoDb&gt;/&lt;db&gt;',
          MONGO_REPLICA_SET_READ_PREFERENCE: 'nearest',
          MONGO_REPLICA_SET: {
              ssl: true,
              sslValidate: true,
              sslKey: fs.readFileSync('/etc/ssl/mongodb.pem'),
              sslCert: fs.readFileSync('/etc/ssl/mongodb.pem'),
              sslCa: fs.readFileSync('/etc/ssl/CAFILE.pem'),
              replicaSet: 'REPLSET',
              auto_reconnect: false,
              poolSize: 24,
              socketOptions: {
                  keepAlive: 1000,
                  connectTimeoutMS: 30000
              }
          },
          MONGO_SERVER: {
              ssl: true,
              sslValidate: false,
              checkServerIdentity: false,
              sslKey: fs.readFileSync('/etc/ssl/mongodb.pem'),
              sslCert: fs.readFileSync('/etc/ssl/mongodb.pem'),
              sslCA: fs.readFileSync('/etc/ssl/CAFILE.pem'),
              poolSize: 24,
              socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 },
              reconnectTries: Number.MAX_VALUE,
              reconnectInterval: 1000
          },
          MONGO_CHECK_SERVER_IDENTITY: false,
  </code></pre>
  
  <h1>mongoose file</h1>
  
  <pre><code>mongoose.connect(config.MONGO_URL,{
    db: {
        native_parser: true,
        readPreference: config.MONGO_REPLICA_SET_READ_PREFERENCE
    },
    server: config.MONGO_SERVER,
    replset: config.MONGO_REPLICA_SET,
    user: config.MONGO_USER,
    pass: config.MONGO_PASSWORD,
    auth: {
        authdb: config.MONGO_AUTHDB
    }
  </code></pre>
  
  <p>})</p>
  
  <p>I have created the CA certificate and and signing certificate but the node server is returning me with the error : <code>MongoError: failed to connect to server [XX.XXX.XXX.XX:port] on first connect MongoError: self signed certificate in certificate chain</code> I am able to connect to the mongo using shell using the following command </p>
  
  <p><code>mongo XX.XX.XX.XXX:XX/&lt;db&gt; -u &lt;user&gt; -p &lt;pass&gt; --authenticationDatabase=admin --ssl --sslPEMKeyFile=/etc/ssl/PEMKEYFILE.pem --sslCAFile /etc/ssl/CAFILE.pem --sslAllowInvalidHostnames</code> </p>
  
  <p>Please suggest me where I am going wrong.  Thanks</p>