title: Unable to connect mongodb with ssl, getting exception com.mongodb.MongoSocketOpenException: Exception opening socket
body: <p>Trying to create a java client to connect to the Mongodb server which is secure(with authentication and self signed certificate.)</p>
  
  <p>It fails with exception "com.mongodb.MongoSocketOpenException: Exception opening socket."</p>
  
  <p>I am able to connect to the server with auth and ssl options by using robomongo client which is installed on the same machine from where I am running the code.</p>
  
  <p>Also I verified java client works fine with there is only authentication in mongodb server, only with ssl it is failing.</p>
  
  <p>My Code looks like this:</p>
  
  <pre><code>String uri = "mongodb://&lt;user&gt;:&lt;password&gt;@&lt;ip&gt;:&lt;port&gt;/admin?ssl=true&amp;sslInvalidHostNameAllowed=true";
  MongoClientURI connectionString = new MongoClientURI(uri);
  MongoClient mongoClient = new MongoClient(connectionString);
  DB db = mongoClient.getDB(connectionString.getDatabase());
  </code></pre>
  
  <p>Versions:</p>
  
  <p>Mongo v3.4.5</p>
  
  <p>Jdk 1.8</p>
  
  <p>Mongo-java-driver 3.2.2</p>
  
  <p>Full stack trace:</p>
  
  <pre><code>com.mongodb.MongoSocketWriteException: Exception sending message
      at com.mongodb.connection.InternalStreamConnection.translateWriteException(InternalStreamConnection.java:462)
      at com.mongodb.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:205)
      at com.mongodb.connection.CommandHelper.sendMessage(CommandHelper.java:89)
      at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
      at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:83)
      at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:43)
      at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
      at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128)
      at java.lang.Thread.run(Unknown Source)
  Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present
      at sun.security.ssl.Alerts.getSSLException(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
      at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
      at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
      at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
      at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
      at sun.security.ssl.Handshaker.processLoop(Unknown Source)
      at sun.security.ssl.Handshaker.process_record(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
      at sun.security.ssl.AppOutputStream.write(Unknown Source)
      at com.mongodb.connection.SocketStream.write(SocketStream.java:75)
      at com.mongodb.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:201)
      ... 7 more
  Caused by: java.security.cert.CertificateException: No subject alternative names present
      at sun.security.util.HostnameChecker.matchIP(Unknown Source)
      at sun.security.util.HostnameChecker.match(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
      ... 17 more
  </code></pre>