What’s that you say? You got your hands on a shiny new OpenShift Online Next Gen Developer Preview account and need to know how to add a database to your project. Never fear my friends, I have you covered in this blog post.
In this post I will cover how to add a database (mysql) to your project using the web console. I will be using the following application as an example to confirm that database is probably configured and working:
Step 1: Create a new application
Creating a new application is a fairly easy process using either the command line tool (oc) or the web console. When using the web console, click on add to project as shown in the following image:
On the next screen, select to use the php:latest builder image and then provide the name of your application, the git repository and finally click the create button:
Once your application has been created, verify that it is working by visiting the URL in your web browser, you should be greeted with the following message:
Welcome to OpenShift Online Developer Preview
To test the database, hit the dbtest.php URL
Step 2: Add a Database
Now that we have a small application that is running, we can add a database. At the current time, we provide default docker images for MongoDB, MySQL, and PostgreSQL but you are free to use any database that you enjoy. For this example, I will using the mysql database. Go to your project overview page and click on Add to Project button just as we did during step 1 of the post.
On the following page, filter for mysql and select the mysql-persistent image as shown in the following image:
On the following screen, you will be able to specify some configuration items that determines things such as the amount of memory your database can consume, authentication information, and the volume capacity for the disk. Leave everything the default except for the following fields:
|MySQL Database Name||mydatabase|
Note: If you don’t specify authorization credentials, some will be generated for you. This is considered the best practice versus hard coding them. However, for simplification to demonstrate how to add a database, I have created static ones.
Once you have added your username, password and database name, click on the Create button.
Step 3: Linking the database to the application
When we added the database, the platform stored all of the information needed to connect to the database as system environment variables initially only accessible to the database pods. The following variables were created in this example:
In order to be able to communicate to the database using these environment variables, we need to add them to the DeploymentConfig of our application. Adding the environment variables to the DeploymentConfig instead of directly in the running pod ensures that any new pod will be started with the variables it needs to connect. You could certainly just hard code these values in your application code but that is not a best practice! This can be done using the following command:
<span>oc env dc phpdatabase -e MYSQL_USER=myuser -e MYSQL_PASSWORD=mypassword -e MYSQL_DATABASE=mydatabase </span>
Once you enter in the above command, you will notice that your application starts a redeployment. This is because the current running state of the application does not match we have specified in the DeploymentConfig. Due to this mismatch, OpenShift updates the running application to match what we have defined as the “truth” in the DeploymentConfig.
Now that you have added your authentication information as environment variables, you can verify the connection was successful by visiting the /dbtest.php URL of your application. You will see the following message:
<span>Connected to the database</span>
Congratulations! You have just linked a database to an application using OpenShift Online Next Gen.
OpenShift Online Next Gen is now in developer preview! For more information on how to use the platform or how to sign up, check out the following: