Accessing an External Database Storage from a Bluemix-Based Ruby App

by Cristian MolinaMarch 22, 2016
One of the many benefits that a PaaS provides is how easily you can extend its functionality with external services.


In this article, we add a ClearDB connection to a sample Sinatra application running on Bluemix, addressing a very common need to read and save data dynamically.


Adding the ClearDB service

First, let’s go to the Bluemix services catalog. In the Data and Analytics section, several storage facilities are available both from IBM and third parties.

Some of the storage options are well-known RDBMS like PostgreSQL, MySQL, and DB2. There are NoSQL options as well, including Redis, MongoDB, and Cloudant (compatible with CouchDB).

I use the megatux-cf-sinatra-db application and select a third-party data service that supports MySQL—ClearDB.


Then, I assign the service to the application.


The free plan is very limited, but it is enough for our demonstration purposes.


If you run the cf services command, you will see the list of the application services similar to this:

$ cf services
Getting services in org / space dev as

name      service   plan    bound apps              last operation   
p-mysql   cleardb   spark   megatux-cf-sinatra-db   create succeeded


Obtaining and using database credentials

Now, we need to obtain the database connection settings for using the service in the application. Cloud Foundry shares this information through the VCAP-SERVICES environment variable.

VCAP-SERVICES is in the JSON format and contains all information about the services that the application is associated with.

Find more information at IBM Bluemix docs and the SQL Database tutorial.

You can see these values in your Bluemix application console.


Alternatively, you can use the cf env command:

$ cf env megatux-cf-sinatra-db

As you can see in the JSON example, there is the cleardb array that contains the object with the credentials we need:

   "cleardb": [
         "name": "p-mysql",
         "label": "cleardb",
         "plan": "spark",
         "credentials": {
            "jdbcUrl": "jdbc:mysql://***********?user=******&password=******",
            "uri": "mysql://******?reconnect=true",
            "name": "ad***********",
            "hostname": "",
            "port": "3306",
            "username": "*********",
            "password": "********"

We can fetch these values in Ruby and use them for accessing the database:


It is simple code with no error checking intended for the first steps with the database service. A sample application with similar code is available on GitHub, in the bluemix-cleardb branch. You can clone it and push to your Bluemix:

$ cf push megatux-cf-sinatra-db



With its marketplace of very compelling options, Bluemix gives users the flexibility in choosing a database service as well as relieves them from maintaining this infrastructure. In addition, you have well-documented open standards and tools like OpenStack and Cloud Foundry.


Related reading