title: Google Cloud SQL with SSL from Compute Engine and/or External Network
body: <p>I'm trying to connect to an Google Cloud SQL (mysql) instance using SSL. I've enabled two IP addresses and a user with remote access permissions from those IP addresses. I have also generated the certificate files from Google's Developer Console.</p>
  
  <pre><code>client-key.pem client-cert.pem server-ca.pem
  </code></pre>
  
  <p>Using this command from each of the two enabled IP addresses I successfully make a connection.</p>
  
  <pre><code>mysql --ssl-ca=server-ca.pem --ssl-cert=client-cert.pem --ssl-key=client-key.pem --host=xxx.xxx.xxx.xxx --user=username --password
  </code></pre>
  
  <p>Since the mysql client works I know my firewall settings, users, certs, etc. are set up correctly.</p>
  
  <p>Now I'd like to make a similar connection with java from within an executable jar file.</p>
  
  <p>Following the steps outlined here: <a href="http://dev.mysql.com/doc/connector-j/en/connector-j-reference-using-ssl.html" rel="nofollow">http://dev.mysql.com/doc/connector-j/en/connector-j-reference-using-ssl.html</a></p>
  
  <pre><code>Step 1: keytool -import -alias mysqlServerCACert -file cacert.pem -keystore truststore  
  </code></pre>
  
  <p>NOTE: I substitute server-ca.pem for cacert.pem used in the instructions</p>
  
  <pre><code>Step 2: openssl x509 -outform DER -in client-cert.pem -out client.cert
  Step 3: keytool -import -file client.cert -keystore keystore -alias mysqlClientCertificate
  </code></pre>
  
  <p>These steps create my keystore and truststore files. I associate distinct passwords for
  each of these two files as they are generated.</p>
  
  <p>Here is the relevant Java code:</p>
  
  <pre><code>public void testConnection() throws ClassNotFoundException, SQLException,
          IllegalAccessException, InstantiationException {
  
      String path = "/home/user/dir/"; // path to keystore and truststore file
  
      System.setProperty("javax.net.ssl.keyStore",path + "keystore");
      System.setProperty("javax.net.ssl.keyStorePassword",keyStorePassword);
      System.setProperty("javax.net.ssl.trustStore",path + "truststore");
      System.setProperty("javax.net.ssl.trustStorePassword",trustStorePassword);
  
      String user = "dbuser";
      String password = "dbUserPassword";
  
      Connection conn = null;
      int i = 0;
      try {
          String url = "jdbc:mysql://&lt;databaseip&gt;:3306/&lt;databaseName&gt;"
              + "?verifyServerCertificate=true"
              + "&amp;useSSL=true"
              + "&amp;requireSSL=true";
  
          Class dbDriver = Class.forName("com.mysql.jdbc.Driver");
          conn = DriverManager.getConnection(url, user, password);
  
          ResultSet rs = conn.createStatement().executeQuery(
                  "SELECT 1 + 1 as val");
  
          while (rs.next()) {
              i = rs.getInt(1);
          }
      } catch (Exception ex) {
          ex.printStackTrace();
      } finally {
          if (conn != null) {
              try {
                  conn.close();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
      System.out.println("The MySQL value is: " + i);
  }
  </code></pre>
  
  <p>Again, from both enabled IP addresses I am able to connect with the MySQL command line client with the same user and password being used in the java code. </p>
  
  <p>I receive the following connection exception with the java. Given that the mysql client works with the same user/passwd credential I am a bit suspect of the error message. Any insights much appreciated. Has anyone else got SSL up and working with Google SQL? Thoughts, advice, tricks and tips appreciated. Thanks!</p>
  
  <pre><code>java.sql.SQLException: Access denied for user 'dbuser'@'xxx.xxx.xxx.xxx' (using password: YES)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:885)
  at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3421)
  at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:3988)
  at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1293)
  at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
  at com.mysql.jdbc.Connection.&lt;init&gt;(Connection.java:1555)
  at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
  at java.sql.DriverManager.getConnection(DriverManager.java:571)
  at java.sql.DriverManager.getConnection(DriverManager.java:215)
  at com.test.UserData.getConnection(UserData.java:81)
  at com.test.UserData.testConnection(UserData.java:52)
  </code></pre>