How to use OpenShift environment variables to configure a MySQL database
<p>So I generated an application with JHipster.</p>

<p>Locally, I would just modify the database info in the application-dev.yml/application-prod.yml and everything works fine.</p>

<p>Now I want to deploy my application on OpenShift. I tried putting the environment variable names in the application-xxx.yml but it returned null. So I modified the <code>DatabaseConfiguration.java</code> to skip reading the info from the .yml file and read them directly from the environment variables.</p>

<pre><code>if(System.getenv("OPENSHIFT_MYSQL_DB_HOST") != null &amp;&amp; System.getenv("OPENSHIFT_MYSQL_DB_PORT") != null){
    config.addDataSourceProperty("url", "jdbc:mysql://"+System.getenv("OPENSHIFT_MYSQL_DB_HOST")+":"+System.getenv("OPENSHIFT_MYSQL_DB_PORT")+"/zetravelcloud?useUnicode=true&amp;characterEncoding=utf8");
}
if (System.getenv("OPENSHIFT_MYSQL_DB_USERNAME") != null) {
    config.addDataSourceProperty("user", System.getenv("OPENSHIFT_MYSQL_DB_USERNAME"));
} else {
    config.addDataSourceProperty("user", ""); // HikariCP doesn't allow null user
}
System.out.println(System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD"));
if (System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD") != null) {
    config.addDataSourceProperty("password", System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD"));
} else {
    config.addDataSourceProperty("password", ""); // HikariCP doesn't allow null password
}
</code></pre>

<p>instead of </p>

<pre><code>config.addDataSourceProperty("url", dataSourceProperties.getUrl());
if (dataSourceProperties.getUsername() != null) {
    config.addDataSourceProperty("user", dataSourceProperties.getUsername());
} else {
    config.addDataSourceProperty("user", ""); // HikariCP doesn't allow null user
}
if (dataSourceProperties.getPassword() != null) {
    config.addDataSourceProperty("password", dataSourceProperties.getPassword());
} else {
    config.addDataSourceProperty("password", ""); // HikariCP doesn't allow null password
}
</code></pre>

<p><strong>Now the question is: How to correctly use the OpenShift environment variables in my configuration class to successfully connect to the database?</strong> </p>

<p>Bonus question: What is a better way to solve this, I don't feel it's right to put the variable names in the <code>DatabaseConfiguration</code> class? </p>