0

I have three files (private key, cert and CAcert) and I need to create a keystore to connect to a mysql database that requires SSL connection. So far my steps:

# Create PKCS12 keystore from private key and public certificate and CA like this.
sudo openssl pkcs12 -export -name mycerts -out mycerts.pfx -inkey domain.com.key -in comain.com.crt -certfile domain.com.cabundle

# Convert PKCS12 keystore into a JKS keystore
sudo keytool -importkeystore -destkeystore mykeystore.jks -srckeystore mycerts.pfx -srcstoretype PKCS12 -alias mycerts -deststorepass changeit -destkeypass changeit -srcstorepass changeit

Everything seems to be fine and I have a mykeystore.jks file in the root folder of my application. At this point I try to connect with:

public class TestConnection {

    public static void main(String[] args) {
        System.setProperty("javax.net.ssl.keyStore", "mykeystore.jks");
        System.setProperty("javax.net.ssl.keyStorePassword", "changeit");

        Connection con = null;
        try {
            String url = "jdbc:mysql://remoteIP:3306/my_db"
                + "?verifyServerCertificate=true"
                + "&useSSL=true"
                + "&requireSSL=true";
            String user = "user";
            String password = "pwd";

            Class dbDriver = Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, password);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception e) {
                    e.printstacktrace();
                }
            }
        }
    }
}

but I get an exception:

java.sql.SQLException: null,  message from server: "Host '93-45-149-182.ip102.fastwebnet.it' is not allowed to connect to this MySQL server"
t com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1112)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2506)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at ekovianetworkexplorer.TestConnection.main(TestConnection.java:28)

Is there anything I am missing in my settings? Thanks in advance.

user299791
  • 2,021
  • 3
  • 31
  • 57
  • 1
    You need to grant access permissions first: http://stackoverflow.com/questions/6239131/how-to-grant-remote-access-permissions-to-mysql-server-for-user – Alex Chernyshev Aug 04 '14 at 10:09
  • thanks Alex, I am receiving certificates from the db admin, so I think he already did this... but I will double-check – user299791 Aug 04 '14 at 10:11
  • Then this is your next step: http://dev.mysql.com/doc/connector-j/en/connector-j-reference-using-ssl.html – Alex Chernyshev Aug 04 '14 at 10:13
  • What exception you have? Have you tried to connect from console mysql client? – Alex Chernyshev Aug 04 '14 at 10:53
  • just followed the instruction, created a keystore and a truststore, but still I have the SQLException as in the question... I haven't tried to connect from console mysql client... – user299791 Aug 04 '14 at 11:01
  • Possible duplicate of [Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server](http://stackoverflow.com/questions/1559955/host-xxx-xx-xxx-xxx-is-not-allowed-to-connect-to-this-mysql-server) – jww Aug 04 '14 at 16:24

0 Answers0