title: CertPathValidatorException Java trying to connect to server database using SSL
body: <p>I have a Java application (with hibernate) that I'm trying to use in a project with Google Cloud SQL + Compute Engine, without success. </p>
  
  <p>Google Cloud SQL requires me to connect to the database through SSL, using <code>server-ca.pem</code>, <code>client-cert.pem</code> and <code>client-key.pem</code>.</p>
  
  <p>Before that, I connected to my database using properties:</p>
  
  <pre><code>hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver
  hibernate.connection.url=jdbc:mysql://localhost/dbname?autoReconnect=true&amp;useSSL=false
  hibernate.connection.username=root
  hibernate.connection.password=pass
  
  hibernate.dialect=org.hibernate.dialect.MySQLDialect
  </code></pre>
  
  <p>How I can change this to work with SSL?</p>
  
  <p>Thank you!</p>
  
  <p>[EDIT]</p>
  
  <p>Tried Pooja Aggarwal answer but didn't work.</p>
  
  <p>Using <code>-Djavax.net.debug=all</code> I'm getting the exception:</p>
  
  <pre><code>Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors
  </code></pre>
  
  <p>[EDIT 2]</p>
  
  <p>Commands that i run for generating certs:</p>
  
  <pre><code>openssl pkcs12 -export -in client-cert.pem -inkey client-key.pem \
    -name "mysqldb" -passout pass:MYPASS -out client-keystore.p12
  
  
  keytool -importkeystore -srckeystore client-keystore.p12 -srcstoretype pkcs12 \
   -srcstorepass MYPASS -destkeystore keystore -deststoretype JKS -deststorepass MYPASS
  </code></pre>
  
  <p>And then executed:</p>
  
  <pre><code>java -Djavax.net.ssl.keyStore=/home/user/cert/keystore -Djavax.net.ssl.keyStorePassword=MYPASS -jar MyApp.jar
  </code></pre>
  
  <p>PS:
  I can connect using:</p>
  
  <pre><code> mysql -uroot -p -h 192.00.00.00 \
      --ssl-ca=server-ca.pem --ssl-cert=client-cert.pem \
      --ssl-key=client-key.pem
  </code></pre>