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.

ibm-bluemix-accessing-cleardb-mysql-db

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.

ibm-bluemix-data-and-analytics-cleardb

Then, I assign the service to the application.

ibm-bluemix-catalog-cleardb-mysql-db-v1

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

ibm-bluemix-adding-cleardb-service-v1

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 some_email@mail.com / space dev as some_email@mail.com...
OK

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.

ibm-bluemix-environment-variables-v1

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://us-cdbr-iron-east-03.cleardb.net/***********?user=******&password=******",
            "uri": "mysql://b90c6d72f4800e:3bab786d@us-cdbr-iron-east-03.cleardb.net:3306/a******?reconnect=true",
            "name": "ad***********",
            "hostname": "us-cdbr-iron-east-03.cleardb.net",
            "port": "3306",
            "username": "*********",
            "password": "********"
         }
      }
   ]
}

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

ibm-bluemix-cleardb-in-ruby-app-v1

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

 

Conclusion

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

  •  
  •  
  •