Adding Persistence to Project via Openshift MySQL

Openshift container platform is a cloud application deployment and hosting platform. Most of the customers have been using Openshift to run stateless applications. However the real challenge for Openshift is applications that run stateful. To exceed that challenge, Openshift provides storage solutions. One of these solutions includes a container image for running MySQL. This image can provide database services based on username,password and database name. These settings can be reachable via configuration.

In this article, creating a database and connecting that database with an application will be shown. First of all, to create a mysql database, click mysql on openshift web console.

Openshift MySQL

In information part, there is nothing to change or create. Therefore click next. On this page, the essential part is the database service name, mysql connection username, mysql connection password mysql root user password, mysql database name. These parts can be empty. If they are empty, the system is automatically generated them as environment values which can be seen at the pod terminal with shell command “printenv”.

Openshift MySQL

To create a connection between the database and the application, there are some codes that have to be written in application properties file in the spring boot project. These are shown as follows.

Openshift MySQL

In first line, spring boot choeses a default value to based on whether that thinks the database is embedded. This defaults to create-drop if no schema manager has been detected or non in all other cases.

In second line, the URL has to be specified. Otherwise, spring boot tries to auto-configure an embedded database.

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect , this code helps to run quiers for mysql.

To dump queries to standard out, add the following spring.jpa.show-sql=true

To deploy the application on openshift, open Red Hat OpenJDK 8 and click next.

Openshift MySQL

Give name that project. Then give the address of git repository that have the application. After that, press create button and open the project overview.

Openshift MySQL

As can bee seen from the figure, the application is added and the route is ready.

Openshift MySQL