First of all it is a bad practice to include any sensible information like users and passwords in your application, 12 Factor: Configuration, so I strongly advice against using the user/password/db name in the code directly. Also you shouldn't post real credentials anywhere, its a big security risk.
Now to answer your question, Openshift provides environment variables that contains these values. I haven't tried a MySQL database at Openshift, but did use a MongoDB, and it gave me the following variables:
$OPENSHIFT_MONGODB_DB_USERNAME
$OPENSHIFT_MONGODB_DB_PASSWORD
$OPENSHIFT_MONGODB_DB_DATABASE
$OPENSHIFT_MONGODB_DB_HOST
$OPENSHIFT_MONGODB_DB_PORT
So it is probably going to give you the same variables for MySQL, in order to make sure, you can ssh
into your application and inspect the environment variables, using the rhc tool, you can do it like this:
rhc ssh -a <app-name> [-l <username>]
where the <app-name>
represents the name of your application in openshift, the one used when you first created your app.
Optionally, if you didnt configure the rhc tool to use your default openshift credentials, or if you work with multiple accounts, then the account name must be provided with -l
and your Openshift login. After issuing the command, you will be asked to input your password too, if it isn't stored in session.
Once you have a shell to your application, the .env
directory stores the system environment variables, as files, each file represents a variable, the file name is the variable name, and the file content is the variable value.
ls .env
will list all the environment variables, so look for all the files that start with OPENSHIFT_MYSQL_
, because these are containing the values for the environment variables with the credentials to connect to your database.
And finally then you can use these variables with:
System.getenv('OPENSHIFT_MYSQL_DB_USERNAME')
System.getenv('OPENSHIFT_MYSQL_DB_PASSWORD')
// and so on with all the variables that you need
Update:
there is also the rhc env-list
command that lists all the environment variables in your application, which also requires the -a
and -l
parameters.
Edit:
Edited a few parts to remove the confusion and complete the answer a bit more.