Halley
Halley

Reputation: 531

Configuring DataSource.groovy for Openshift for Grails

I'm trying to deploy a Grails application on Openshift. I'm deploying the app using a locally built war file. Right now, I am using the url given by the rhc-app-show command in the DataSource.groovy file for my Database configurations. Whenever I try with the environment variables of Openshift (eg. $OPENSHIFT_MYSQL_DB_HOST), it crashes. Any idea or pointers on how to use those in the config file?

Thanks.

Upvotes: 4

Views: 2627

Answers (3)

Samuel Seda
Samuel Seda

Reputation: 2912

In Grails 3.0.11, File application.yml

System.getenv doesn't work, so i had to configure manually the url connection for database in mysql.... Use the command of documentation https://developers.openshift.com/en/managing-port-forwarding.html

dataSource:
        dbCreate: update
        url : jdbc:mysql://thisIsTheUrlGetWithPort-ForwardCommandAndThePort:48381/server?verifyServerCertificate=false&autoReconnect=true&useSSL=false&requireSSL=false
        driverClassName: com.mysql.jdbc.Driver
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        username : admin9CzsS #this is a username example
        password : RR7y9uKw3t #this is a password example
        properties:
            jmxEnabled: true
            initialSize: 5
            maxActive: 50
            minIdle: 5
            maxIdle: 25
            maxWait: 10000
            maxAge: 600000
            timeBetweenEvictionRunsMillis: 5000
            minEvictableIdleTimeMillis: 60000
            validationQuery: SELECT 1
            validationQueryTimeout: 3
            validationInterval: 15000
            testOnBorrow: true
            testWhileIdle: true
            testOnReturn: false
            jdbcInterceptors: ConnectionState
            defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

Upvotes: 0

Burt Beckwith
Burt Beckwith

Reputation: 75671

I haven't used OpenShift but Google led me to the FAQ which shows these environment variables:

OPENSHIFT_MYSQL_DB_HOST
OPENSHIFT_MYSQL_DB_PASSWORD
OPENSHIFT_MYSQL_DB_USERNAME
OPENSHIFT_MYSQL_DB_URL
OPENSHIFT_MYSQL_DB_PORT

so it looks like this would work:

production {
   dataSource {
      driverClassName = 'com.mysql.jdbc.Driver'
      dialect = org.hibernate.dialect.MySQL5InnoDBDialect

      String host = System.getenv('OPENSHIFT_MYSQL_DB_HOST')
      String port = System.getenv('OPENSHIFT_MYSQL_DB_PORT')
      String dbName = System.getenv('OPENSHIFT_APP_NAME')
      url = "jdbc:mysql://$host:$port/$dbName"
      username = System.getenv('OPENSHIFT_MYSQL_DB_USERNAME')
      password = System.getenv('OPENSHIFT_MYSQL_DB_PASSWORD')

      properties {
         ...
      }
   }
}

The missing bit is the database name - is that something that you would have available? I'm not sure of the format of OPENSHIFT_MYSQL_DB_URL but it looks like you might just be able to use url = "jdbc:${System.getenv('OPENSHIFT_MYSQL_DB_URL')}"

Upvotes: 5

Daniel Wondyifraw
Daniel Wondyifraw

Reputation: 7713

POSTGRES SQL ALSO ...there is a nice , tutorial for that on this link ...but the datasource configuration must be configured like this ....

 production {
       dataSource {
        dbCreate = "update"
        driverClassName = "org.postgresql.Driver"
        dialect = org.hibernate.dialect.PostgreSQLDialect   
        uri = new URI(System.getenv('OPENSHIFT_POSTGRESQL_DB_URL'))
        url = "jdbc:postgresql://"+uri.host+uri.path+"/"+System.getenv('OPENSHIFT_APP_NAME')
        username = System.getenv('OPENSHIFT_POSTGRESQL_DB_USERNAME')
        password = System.getenv('OPENSHIFT_POSTGRESQL_DB_PASSWORD')
    }

second Alternative . . .

 production {
       dataSource {
        dbCreate = "update"
        driverClassName = "org.postgresql.Driver"
        dialect = org.hibernate.dialect.PostgreSQLDialect   
        uri = new URI(System.getenv('OPENSHIFT_POSTGRESQL_DB_URL').toString())
        appname = System.getenv('OPENSHIFT_APP_NAME').toString()
        url = "jdbc:postgresql://"+uri.host.toString()+uri.path.toString()+"/"+appname
        username = System.getenv('OPENSHIFT_POSTGRESQL_DB_USERNAME').toString()
        password = System.getenv('OPENSHIFT_POSTGRESQL_DB_PASSWORD').toString()
    }

Upvotes: 0

Related Questions