Connecting to an RDS for MariaDB Instance Through JDBC
If you are connecting to an instance through JDBC, an SSL certificate is optional, but using an SSL certificate can improve the security of your data. SSL is disabled by default for RDS for MariaDB instances. It encrypts connections to databases but increases the connection response time and CPU usage. For this reason, you are advised not to enable SSL.
Prerequisites
You are familiar with:
- Computer basics.
- Java.
- JDBC.
Connection with the SSL Certificate
Download the SSL certificate and verify it before connecting to your instance.
- Download the CA certificate or certificate bundle.
- On the Instances page, click the instance name to go to the Basic Information page.
- In the DB Information area, click on the right of the SSL switch.
- Use keytool to generate a truststore file using the CA certificate.
<keytool_installation_path> ./keytool.exe -importcert -alias <MariaDBCACert> -file <ca.pem> -keystore <truststore_file> -storepass <password>
Table 1 Parameter description Parameter
Description
<keytool installation path>
Bin directory in the JDK or JRE installation path, for example, C:\Program Files (x86)\Java\jdk11.0.7\bin.
<MariaDBCACert>
Name of the truststore file. Set it to a name specific to the service for future identification.
<ca.pem>
Name of the CA certificate downloaded and decompressed in 1, for example, ca.pem.
<truststore_file>
Path for storing the truststore file.
<password>
Password of the truststore file.
Code example (using keytool in the JDK installation path to generate the truststore file):
Owner: CN=MySQL_Server_5.7.17_Auto_Generated_CA_Certificate Issuer: CN=MySQL_Server_5.7.17_Auto_Generated_CA_Certificate Serial number: 1 Valid from: Thu Feb 16 11:42:43 EST 2017 until: Sun Feb 14 11:42:43 EST 2027 Certificate fingerprints: MD5: 18:87:97:37:EA:CB:0B:5A:24:AB:27:76:45:A4:78:C1 SHA1: 2B:0D:D9:69:2C:99:BF:1E:2A:25:4E:8D:2D:38:B8:70:66:47:FA:ED SHA256:C3:29:67:1B:E5:37:06:F7:A9:93:DF:C7:B3:27:5E:09:C7:FD:EE:2D:18:86:F4:9C:40:D8:26:CB:DA:95: A0:24 Signature algorithm name: SHA256withRSA Subject Public Key Algorithm: 2048-bit RSA key Version: 1 Trust this certificate? [no]: y Certificate was added to keystore
- Connect to your RDS for MariaDB instance through JDBC.
jdbc:mysql://<instance_ip>:<instance_port>/<database_name>?param1=value1¶m2=value2
Table 2 Parameter description Parameter
Description
<instance_ip>
IP address of the DB instance.
NOTE:- If you are accessing the DB instance through an ECS, instance_ip is the floating IP address of the instance. You can view the floating IP address in the Connection Information area on the Basic Information or Connectivity & Security page.
- If you are accessing the DB instance through a public network, instance_ip indicates the EIP that has been bound to the instance. You can view the EIP in the Connection Information area on the Connectivity & Security page.
<instance_port>
Database port of the DB instance. The default port is 3306.
NOTE:You can view the database port in the Connection Information area on the Connectivity & Security page.
<database_name>
Database name used for connecting to the DB instance. The default value is MariaDB.
<param1>
requireSSL, indicating whether the server supports SSL. Its value can be either of the following:
- true: The server supports SSL.
- false: The server does not support SSL.
NOTE:For details about the relationship between requireSSL and sslmode, see Table 3.
<param2>
useSSL, indicating whether the client uses SSL to connect to the server. Its value can be either of the following:
- true: The client uses SSL to connect to the server.
- false: The client does not use SSL to connect to the server.
NOTE:
For details about the relationship between useSSL and sslmode, see Table 3.
<param3>
verifyServerCertificate, indicating whether the client verifies the server certificate. Its value can be either of the following:
- true: The client verifies the server certificate.
- false: The client does not verify the server certificate.
NOTE:
For details about the relationship between verifyServerCertificate and sslmode, see Table 3.
<param4>
trustCertificateKeyStoreUrl. Its value is file:<truststore_file>.
<truststore_file> is the path for storing the truststore file set in 2.
<param5>
trustCertificateKeyStorePassword. Its value is the password of the truststore file set in 2.
Table 3 Relationship between connection parameters and sslmode useSSL
requireSSL
verifyServerCertificate
sslMode
false
N/A
N/A
DISABLED
true
false
false
PREFERRED
true
true
false
REQUIRED
true
N/A
true
VERIFY_CA
Code example (Java code for connecting to an RDS for MariaDB instance):
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.sql.SQLException; // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables. // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed. public class JDBCTest { String USER = System.getenv("EXAMPLE_USERNAME_ENV"); String PASS = System.getenv("EXAMPLE_PASSWORD_ENV"); public static void main(String[] args) { Connection conn = null; Statement stmt = null; // Set the required parameters in the URL based on the site requirements. String url = "jdbc:mysql://<instance_ip>:<instance_port>/<database_name>?param1=value1¶m2=value2"; try { Class.forName("com.MariaDB.cj.jdbc.Driver"); conn = DriverManager.getConnection(url, USER, PASS); stmt = conn.createStatement(); String sql = "show status like 'ssl%'"; ResultSet rs = stmt.executeQuery(sql); int columns = rs.getMetaData().getColumnCount(); for (int i = 1; i <= columns; i++) { System.out.print(rs.getMetaData().getColumnName(i)); System.out.print("\t"); } while (rs.next()) { System.out.println(); for (int i = 1; i <= columns; i++) { System.out.print(rs.getObject(i)); System.out.print("\t"); } } rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // release resource .... } } }
Connection Without the SSL Certificate
You do not need to download the SSL certificate because certificate verification on the server is not required.
jdbc:mysql://<instance_ip>:<instance_port>/<database_name>?useSSL=false
Parameter |
Description |
---|---|
<instance_ip> |
IP address of the DB instance.
NOTE:
|
<instance_port> |
Database port of the DB instance. The default port is 3306.
NOTE:
You can view the database port in the Connection Information area on the Connectivity & Security page. |
<database_name> |
Database name used for connecting to the DB instance. The default value is MariaDB. |
Code example (Java code for connecting to an RDS for MariaDB instance):
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables. // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed. public class MyConnTest { final public static void main(String[] args) { Connection conn = null; // Set the required parameters in the URL based on the site requirements. String url = "jdbc:mysql://<instance_ip>:<instance_port>/<database_name>?param1=value1¶m2=value2"; String USER = System.getenv("EXAMPLE_USERNAME_ENV"); String PASS = System.getenv("EXAMPLE_PASSWORD_ENV"); try { Class.forName("com.MariaDB.jdbc.Driver"); conn = DriverManager.getConnection(url,USER,PASS); System.out.println("Database connected"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM mytable WHERE columnfoo = 500"); while (rs.next()) { System.out.println(rs.getString(1)); } rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); System.out.println("Test failed"); } finally { // release resource .... } } }
Related Issues
- Symptom
When you use JDK 8.0 or a later version to connect to an RDS for MariaDB instance with an SSL certificate downloaded, an error similar to the following is reported:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) at sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:171) ~[na:1.8.0_292] at sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:98) ~ [na:1.8.0_292] at sun.security.ssl.TransportContext.kickstart(TransportContext.java:220) ~ [na:1.8.0_292] at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:428) ~ [na:1.8.0_292] at com.MariaDB.cj.protocol.ExportControlled.performTlsHandshake(ExportControlled.java:316) ~ [MariaDB-connector-java-8.0.17.jar:8.0.17] at com.MariaDB.cj.protocol.StandardSocketFactory.performTlsHandshake(StandardSocketFactory.java :188) ~[MariaDB-connector-java8.0.17.jar:8.0.17] at com.MariaDB.cj.protocol.a.NativeSocketConnection.performTlsHandshake(NativeSocketConnection. java:99) ~[MariaDB-connector-java8.0.17.jar:8.0.17] at com.MariaDB.cj.protocol.a.NativeProtocol.negotiateSSLConnection(NativeProtocol.java:331) ~ [MariaDB-connector-java8.0.17.jar:8.0.17] ... 68 common frames omitted
- Solution
Specify the corresponding parameter values in the code link of 3 based on the JAR package used by the client. Example:
- MariaDB-connector-java-5.1.xx.jar
In the database connection URL jdbc:mysql://<instance_ip>:<instance_port>/<database_name>?param1=value1¶m2=value2, replace param1=value1 with enabledTLSProtocols=TLSv1.2.
- MariaDB-connector-java-8.0.xx.jar
In the database connection URL jdbc:mysql://<instance_ip>:<instance_port>/<database_name>?param1=value1¶m2=value2, replace param1=value1 with tlsVersions=TLSv1.2.
- MariaDB-connector-java-5.1.xx.jar
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot