In my previous post, I showed how to create a quick Grails app on Cloudfoundry.
It was a very simple ‘Hello World’ type example app. In this post we are going to dig little deeper into cloudfoundry.

At the time of this writing, Cloudfoundry supports 3 different type of data store.
1. MongoDb – No SQL Database
2. redis – A persistent key value database
3. MySQL – Most popular free database

CloudFoundry calls these “Services”. You can create/instantiate these services for your application.
Let’s continue on with our previous example. We will create a MySQL database service and we will call it ‘hcfmysql-db’ (abbreviation to HelloCloudFoundryMySQL)

1. Creating CloudFoundry Service
You can create an instance by invoking the ‘cf-create-service’ command, Lets create ‘hcfmysql-db’

grails cf-create-service mysql hcfmysql-db

You should see an output like upon successful creation.


Service 'hcfmysql-db' provisioned.

You can verify that instance was created by running the following Grails command

grails cf-services

You should see output like follows:

============== System Services ==============

| Service | Version | Description                   |
| mysql   | 5.1     | MySQL database service        |
| redis   | 2.2     | Redis key-value store service |
| mongodb | 1.8     | MongoDB NoSQL store           |

=========== Provisioned Services ============

| Name          | Service |
| hcfmysql-db   | mysql   |

2. Binding the Database to the application
In Cloudfoundry, you will have to “bind” the services you created to the application you are working with.
In our example above, we will have to bind the hcfmysql-db with our HelloCloudFoundry app .
to do so, you can run “cf-bind-service” command.
For instance, in our example we can do:

grails cf-bind-service hcfmysql-db --appname=hellocloudfoundry

You should see the following output

Creating new service binding to 'hcfmysql-db' for 'hellocloudfoundry'.

Application 'hellocloudfoundry' updated
Service 'hcfmysql-db' added

Application 'hellocloudfoundry' stopped.

Trying to start Application: 'hellocloudfoundry'.
Application 'hellocloudfoundry' started.

3. Setting up the app to use the database
When we do the binding to a cloud foundry application, CF will set up the connection settings to the services at runtime. This makes the settings very transparent and intuitive.
Lets configure our app for the data.
Edit the DataSource.groovy found under /grails-app/config

production {
        dataSource {
			dialect = org.hibernate.dialect.MySQLInnoDBDialect
			driverClassName = "com.mysql.jdbc.Driver"
			username = "n/a"
			password = "n/a"
            url = "n/a"
            dbCreate = "update"

Note that i have set up ‘n/a’, under username, password and url. You can put anything you want here as these will be overridden by cloudfoundry at runtime.

This is it! You are done.

4. Putting it all together
Let’s verify that our database is working. To do this we can create a “Domain” Class and we will do a scaffolding.

Lets create a Domain class called “Person” with id and name as two attributes.

grails create-domain-class Person

We will update the class as follows

package hellocloudfoundry

class Person {
    long id
    String name
    static constraints = {

We will create a controller as follows

grails create-controller Person

Update the controller as follows

package hellocloudfoundry

class PersonController {

    def scaffold = Person

Lets the deploy the app on the cloudfoundry.
Since we had already deployed the app before you can do an update

grails prod cf-update

Now you can navigate to your app in my example I deployed my app as
so i can navigate to