By baronofclubs
I’ve spent three days on this so far, and I’m tearing my hair out. I’m trying to adapt some code I’ve inherited to migrate from AWS to DigitalOcean. Amazon’s DB didn’t require SSL to connect, so the code worked fine before.
I can connect with MySQL Workbench, but not within my code.
I’ve created a truststore and put the CA Certificate in it.
My test code is as follows:
import java.sql.SQLException;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
public class DBTest {
public static void main(String[] args) {
MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
dataSource.setUser("adminusername");
dataSource.setPassword("password");
dataSource.setServerName("db-example-nyc1-01-do-user-123456-0.db.ondigitalocean.com");
dataSource.setPort(25060);
dataSource.setDatabaseName("defaultdb");
// SSL Stuff
dataSource.setTrustCertificateKeyStoreUrl("file:///opt/appname/truststore.store");
dataSource.setTrustCertificateKeyStoreType("PKCS12");
dataSource.setTrustCertificateKeyStorePassword("changeme");
dataSource.setUseSSL(true);
//dataSource.setRequireSSL(true);
try {
dataSource.getConnection();
System.out.println("SUCCESS");
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("FAIL");
e.printStackTrace();
}
}
}
(details changed for security where required)
The output in IntelliJ IDEA Community 2019.3 is:
4:13:13 PM: Executing task 'DBTest.main()'...
> Task :compileJava NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :DBTest.main()
Communications link failure
Last packet sent to the server was 114 ms ago.
FAIL
BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
Picked up JAVA_TOOL_OPTIONS: -Djava.io.tmpdir=/home/baronofclubs/.var/app/com.jetbrains.IntelliJ-IDEA-Community/cache/tmp/
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 114 ms ago.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:422)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:137)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:107)
at DBTest.main(DBTest.java:11)
Caused by: java.lang.IllegalStateException: TrustManagerFactoryImpl is not initialized
at java.base/sun.security.ssl.TrustManagerFactoryImpl.engineGetTrustManagers(TrustManagerFactoryImpl.java:102)
at java.base/javax.net.ssl.TrustManagerFactory.getTrustManagers(TrustManagerFactory.java:313)
at com.mysql.jdbc.ExportControlled.getSSLSocketFactoryDefaultOrConfigured(ExportControlled.java:237)
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:79)
at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4472)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1319)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
... 13 more
4:13:14 PM: Task execution finished 'DBTest.main()'.
I’m unsure what I’m doing wrong here. If anyone can provide any insight it would be extremely helpful.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi @baronofclubs,
You’ll need to change your connection code like so
MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
dataSource.setUser("adminusername");
dataSource.setPassword("password");
dataSource.setServerName("db-example-nyc1-01-do-user-123456-0.db.ondigitalocean.com:25060");
dataSource.setDatabaseName("defaultdb");
The change is having the Port in the setServerName function.
If you additionally, try to connect and get an error like/or something similar as :
The server requested authentication method unknown to the client
Then you’ll need to create a new user with different authentication method that the default. To achieve this, you’ll need to connect via a Terminal to your MySQL server and use the CREATE USER command in there.
If you are having difficulties, you can check the following video here : https://www.youtube.com/watch?v=EAZoXK3i7eA&t=116s
Although it’s for PHP and MySQL Managed Database, the principle should be the same.
Regards, KDSys
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.