Oracle by Example brandingConnect a Ruby Application to MySQL

section 0Before You Begin

This 15-minute tutorial shows you how to connect a Ruby application to Oracle MySQL Cloud Service in Oracle Application Container Cloud Service.

Background

Oracle Application Container Cloud Service provides a lightweight infrastructure so that you can run Java, PHP, Node.js, Python, and Ruby applications in Oracle Cloud. To develop more robust applications, you can integrate this cloud service with other Oracle services (for example, Oracle MySQL Cloud Service).

Oracle MySQL Cloud Service provides you the ability to deploy MySQL Server in the Cloud, with each deployment containing a single MySQL Server. You have full access to the features and operations available with MySQL Server, with Oracle providing the computing power, physical storage and tooling to simplify maintenance and management operations.

This tutorial provides you a sample application developed with Sinatra and Active Record.

What Do You Need?


part 1Create the Startup Script

  1. Create sinatra-employee-service directory in your local system.
  2. In the sinatra-employee-service directory, extract the content of the sinatra-employee-service.zip file.
  3. Create the start.sh script file and add the following content:
    #Install the dependencies specified in the Gemfile
    bundle install
    #Run the database migration to create the Employee table.
    bundle exec rake db:migrate
    #Run the Sinatra application. Your application must run on the port specified in the PORT environment variable and in the 0.0.0.0 host.
    rackup -p ${PORT} --host 0.0.0.0
  4. Create again the sinatra-employee-service.zip file with the content of the sinatra-employee-service directory. Make sure the zip file doesn't contain a subdirectory.

part 2Prepare Your Application for Cloud Deployment

  1. In your local system, create the deployment.json file and add the following content. Replace the placeholders with the information of your Oracle MySQL Cloud Service instance:
    {
      "services": [{
        "name": "<name-of-your-instance>",
        "type": "MYSQLCS",
        "username": "<your-username>",
        "password": "<your-password>" 
      }]
    }  
  2. Create the manifest.json file and add the following content:
    {
      "runtime":{
        "majorVersion":"2.4.1"
      },
      "command": "sh ./start.sh",
      "release": {},
      "notes": "Employee service developed with Sinatra"
    }

part 3Deploy Your Application to Oracle Application Container Cloud Service

  1. Log in to Oracle Cloud at http://cloud.oracle.com/. Enter your account credentials in the Identity Domain, User Name, and Password fields.
  2. In the Oracle Cloud Services dashboard, click the Action menu Menu, and select Oracle Application Container Cloud Service.
  3. In the Applications list view, click Create Application.
  4. In the Create Application dialog box, click Ruby.
  5. In the Application section, enter EmployeeService for the name of your application and click Browse.
  6. In the File Upload dialog box, select the sinatra-employee-service.zip. file.
  7. Keep the default values in the Instances and Memory fields and click More Options.
  8. Click Browse next to Manifest, select the manifest.json file, and click Open.
  9. Click Browse next to Deployment Configuration, select the deployment.json file, and click Open.
  10. Click Create.
  11. Wait until the application is created. The URL is enabled when the creation is completed. Copy the URL of your application. You'll use it in the next section.

part 4Test the Application

  1. In the Oracle Application Container Cloud Service console, click the URL of your application.
  2. Open a command-line window (or terminal in Linux).
  3. Create an employee record and replace the app_endpoint placeholder with the URL of your application.
    curl -i -X POST -H "Content-Type:application/json" -d "{ \"firstName\" : \"John\",  \"lastName\" : \"Smith\",\"birthDate\":\"1957-10-14\", \"phone\":\"193-754-4112\",\"email\":\"john.smith@example.com\",\"title\" : \"Manager\",\"department\" : \"Sales\" }" app_endpoint/employees
  4. Query all employee entities.
    curl app_endpoint/employees
  5. Get the employee by ID.
    curl app_endpoint/employees/1 
  6. Update the employee.
    curl -i -X PUT -H "Content-Type:application/json" -d "{ \"firstName\" : \"John\",  \"lastName\" : \"Smith\",\"birthDate\":\"1957-12-08\", \"phone\":\"193-124-8212\",\"email\":\"john.smith@example.com\",\"title\" : \"Manager\",\"department\" : \"IT\" }" app_endpoint/employees/1
  7. Delete the employee.
    curl -i -X DELETE app_endpoint/employees/1

more informationWant to Learn More?