Ruby and Compose for MySQL

There are a number of MySQL drivers for Ruby. The sample connection code uses MySQL2.

You can connect to MySQL by installing MySQL2 according to the instructions in the driver's Github repository and using the following code:

require "mysql2"  
config = {  
    "hostname": "",
    "port": 15942,
    "database": "compose",
    "username": "admin",
    "password": "mypass",
    "sslCA": "cert.pem",
    "sslverify": true
conn =  
results = conn.query("SHOW DATABASES")  
results.each {|row| puts row["Database"]}


  • The config hash contains all of the connection configuration, including setting up an SSL connection. For details of all the configuration options, see the MySQL2 readme.
  • To set up a secure connection using the self-signed certificate, sslCA and sslverify are set. sslCA is the path of the .pem or .cert file containing the certificate, and sslverify is set to true in order to check for a valid certificate.
  • The constructor initializes a new connection variable, using the connection configuration in config.
  • results contains an array of hashes, which are iterated through and printed to the console.

Still Need Help?

If this article didn't solve things, summon a human and get some help!