I have a WildFly 10 pod and a mysql 5.7 pod running in Openshift V3. I would like to use environment variables in my standalone.xml as I was used in Openshift V2 to configure my mysql datasource.
I followed this guide: https://github.com/openshift-s2i/s2i-wildfly
I added a .s2i folder and created there a file called: environment.
I added to the 'environment' file the following key:
MYSQL_DATABASE=<DATABASE_NAME>
The <DATABASE_NAME>
is of course replaced with the real database name.
In my standalone.xml I have:
<subsystem xmlns="urn:jboss:domain:datasources:4.0">
<datasources>
<datasource jta="false" jndi-name="java:/<DATABASE_NAME>" pool-name="pool_name" enabled="true" use-ccm="false">
<connection-url>jdbc:mysql://MYSQL_SERVICE_HOST:MYSQL_SERVICE_PORT/<DATABASE_NAME>?useSSL=false</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysql</driver>
<security>
<user-name>MYSQL_USER</user-name>
<password>MYSQL_PASSWORD </password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
<background-validation>true</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
</validation>
</datasource>
<drivers>
<driver name="mysql" module="com.mysql.jdbc">
<driver-class>com.mysql.jdbc.Driver</driver-class>
</driver>
</drivers>
</datasources>
</subsystem>
When replacing the environment variables by the real values, the mysql connection works and wildfly boots up with success.
What am I doing wrong?
I also prefixed each environment variable in standalone.xml with the dollar sign but still no luck...
In Openshift (for wildfly) you should use this approach to use environment variables:
${env.DATABASE_NAME}
eg:
First you need to add these variables in the DeploymentConfig:
MYSQL_USER=<mysql_username>
MYSQL_PASSWORD=<mysql_password>
If you have created a service named msyql in the same project/naspace of Wildfly's pod you could skip the settings of this other two environment variables:
MYSQL_SERVICE_HOST=<mysql_host>
MYSQL_SERVICE_PORT=<mysql_port>
This because every pod started will have a set of injected environment variables of the other services created before it (like hosts, ports, etc etc).
Then you can configure the datasource:
<subsystem xmlns="urn:jboss:domain:datasources:4.0">
<datasources>
<datasource jta="false" jndi-name="java:/${env.DATABASE_NAME}" pool-name="pool_name" enabled="true" use-ccm="false">
<connection-url>jdbc:mysql://${env.MYSQL_SERVICE_HOST}:${env.MYSQL_SERVICE_PORT}/<DATABASE_NAME>?useSSL=false</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysql</driver>
<security>
<user-name>${env.MYSQL_USER}</user-name>
<password>${env.MYSQL_PASSWORD}</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
<background-validation>true</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
</validation>
</datasource>
<drivers>
<driver name="mysql" module="com.mysql.jdbc">
<driver-class>com.mysql.jdbc.Driver</driver-class>
</driver>
</drivers>
</datasources>
</subsystem>
NOTE: I suggest to use the DeploymentConfig environments variables instead of using static variables in the enviroments file in .s2i so you can use the same source repository for different Openshift environments/projects/namespaces (dev, test, integration, prod).