Java Connection to MariaDB/MySQL/Percona
MariaDB, MySQL, and Percona are highly popular open source databases, used by developers all over the world. In this instruction we’ll show you how to connect your Java application to these databases, standalone server and clustered solution.
1. Log into your PaaS account and create an environment with the MariaDB (or MySQL) database server (available within the SQL wizard section):
for standalone database server
for Auto-Clustering solution
We’ve also added Tomcat node to provide an example of database connection from application server.
2. Check your email inbox - it should contain a message with administration details for the created MariaDB (or MySQL) server.
In case of database cluster, the Entry Point for Connecting is referred to a ProxySQL load balancer.
3. Switch back to the dashboard and click the Open in Browser button for your MariaDB/MySQL node.
If you have clustered solution, press on Open in Browser next to the master database node (marked as M).
Log into the opened admin panel using credentials from the above-mentioned emails.
4. Use an existing database (e.g. test) or Create a new one.
5. Return to dashboard and click the Config button next to the application server (Tomcat, in our case) to access configuration file manager.
6. Navigate to the /opt/tomcat/temp folder, create a new mydb.cfg file.
For standalone database connection, add the following data in the mydb.cfg file:
|
|
All the required info can be found within the MariaDB/MySQL node email:
- {host} - link to your DB node without protocol part
- {db_name} - name of the database (test in our case)
- {user} and {password} - database admin credentials (for production usage, it’s recommended to create a dedicated account with the appropriate permissions)
For connection to the cluster, ProxySQL load balancer is used as the entry point, and each type of databases has its own connector. So add the following data to the mydb.cfg file:
For MariaDB:
|
|
- {hostname} - link to your DB cluster load balancer (i.e. ProxySQL layer)
- {db_name} - name of the database. We chose test in the first step
- usePipelineAuth - if activated different queries are executed using pipeline (all queries are sent, only then all results are read), permitting faster connection creation. This value should be set to false, as such implementation doesn’t work with the ProxySQL in front of the cluster
- {user} and {password} - database credentials received in the email
For MySQL:
|
|
- {hostname} - link to your DB cluster load balancer (i.e. ProxySQL layer)
- {db_name} - name of the database (test in our case)
- {user} and {password} - database admin credentials (for production usage, it’s recommended to create a dedicated account with the appropriate permissions)
In such a way, all connection settings are saved in a single file, which, subsequently, will be read by the application.
7. For deployment and further connection, we are going to use the following sample application:
|
|
8. Deploy our example application to your Tomcat server using the following link:
https://download.jelastic.com/public.php?service=files&t=b2c6e4e01d487dfd5af953ba31dac848&download
Notes:
- Our example application already contains the jdbc-connectors for MariaDB/MySQL database access. However, to connect your own project, you need to manually upload them to the webapps/{app_context}/WEB-INF/lib folder on your application server.
- Don’t forget to restart your application server to apply mydb.cfg changes, by pressing Restart Node button.
9. Once deployment is finished, click Open in Browser in popup window or next to your application server.
10. In the opened browser tab, click on the Create test table in your database button.
11. Now, in order to ensure everything works fine, return to the phpMyAdmin panel and navigate to the test database.
You’ll see that the newly created table appeared with the name {date-time of creation}, that means the DB has been successfully accessed and modified from your Java application. It’s that easy!
Useful to Know
The platform also lets you extend your MariaDB/MySQL servers functionality by following the appropriate instructions in our documentation:
- set up the preferred database replication type with embedded Auto-Clustering feature in order to get the increased DB performance and data loss protection
- adjust Backups Scheduling for ensuring safety of the information inside your DBs in the case of unexpected server failure
- see the Remote Access instruction and learn how to access your database remotely via the preferred desktop MySQL client
- use the Dump Files Import/Export guide to find out how to manually backup and restore your data from the previously created dumps
- see the Database connection strings to get more info how to configure connection to different db types
Video Tutorial on MySQL Connection to Java Application
You can use the video tutorial version to see how to create a new environment, configure a MySQL database server, connect to it from your application, and finally, deploy the application to the platform.