Access database in OpenShift

6

I deployed a project in OpenShift , I used a cartridge to create a Mysql database soon after the creation of the database this information appears:

  

MySQL 5.5 database added. Please make note of these credentials:

     

Root User: adminhtK8LZq Root Password: shKekKGKhHEH Database Name:   oracle

     

Connection URL:   mysql: // $ OPENSHIFT_MYSQL_DB_HOST: $ OPENSHIFT_MYSQL_DB_PORT /

     

You can manage your new MySQL database by also embedding phpmyadmin.   The phpmyadmin username and password will be the same as the MySQL   credentials above.

But when I configure my hibernate.cfg with this data the database is not created in OpenShift .

Hibernate.cfg:

<!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/oraculo </property>
        <property name="connection.username">adminhtK8LZq</property>
        <property name="connection.password">shKekKGKhHEH</property>
        ...

I have to pass some value in place of this URL, I already tried to pass the ip that appears in phpMyAdmin but it did not work.

    
asked by anonymous 05.10.2015 / 22:35

1 answer

4

I work with OpenShift. I'll tell you what's going on and how I usually work on that kind of thing.

openshift has some variables to make your life easier. $ OPENSHIFT_MYSQL_DB_HOST , $ OPENSHIFT_MYSQL_DB_PORT and others are defined there in openshift.

Below is an example of how I can retrieve variables and connect using JDBC:

this.openShiftDbHost = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
this.openShiftDbPort = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
this.openShiftDbUser = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
this.openShiftDbPassword = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");
conexao =  DriverManager.getConnection("jdbc:mysql://"+this.openShiftDbHost+":"+this.openShiftDbPort+"/mydb?user="+this.openShiftDbUser+"&password="+this.openShiftDbPassword);

In the case of hibernate I make the following configuration:

<property name="url" value="jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/${OPENSHIFT_APP_NAME}" />
<property name="username" value="${OPENSHIFT_MYSQL_DB_USERNAME}" />
<property name="password" value="${OPENSHIFT_MYSQL_DB_PASSWORD}" />
    
15.10.2015 / 20:56