LATEST VERSION: 2.0 - CHANGELOG
MySQL for PCF v2.0

Using MySQL for PCF

This topic provides instructions for developers using the MySQL for Pivotal Cloud Foundry v2 service for their Pivotal Cloud Foundry (PCF) apps. MySQL provides a relational database for apps and devices.

These procedures use the Cloud Foundry Command Line Interface (cf CLI). You can also use Apps Manager to perform the same tasks using a graphical UI.

For general information, see Managing Service Instances with the cf CLI.

Prerequisites

To use MySQL for PCF v2 with your PCF apps, you need:

The Create-Bind Process

Because every app and service in PCF is scoped to a space, an app can only use a service if an instance of the service exists in the same space.

To use MySQL in a PCF app:

  1. Use the cf CLI or Apps Manager to log into the org and space that contains the app.

  2. Make sure an instance of the MySQL for PCF service exists in the same space as the app.

    • If the space does not already have a MySQL for PCF instance, create one.
    • If the space already has a MySQL for PCF instance, you can bind your app to the existing instance or create a new instance to bind to your app.
  3. Bind the app to the MySQL for PCF service instance, to enable the app to use MySQL.

Confirm Service Availability

For an app to use a service, 1) the service must be available in the Marketplace for its space and 2) an instance of the service must exist in its space.

You can confirm both of these using the cf CLI as follows.

  1. To find out if a MySQL for PCF v2 service is available in the Marketplace:

    1. Enter cf marketplace
    2. If the output lists p.mysql in the service column, on-demand MySQL for PCF is available. If it is not available, ask your operator to install it.
    $ cf marketplace
    Getting services from marketplace in org my-org / space my-space as user@example.com...
    OK
    service             plans          description
    [...]
    p.mysql             db-small       Dedicated instances of MySQL service to provide a relational database
    [...]
    
  2. To confirm that a MySQL for PCF v2 instance is running in the space

    1. Enter cf services
    2. Any p.mysql listings in the service column are service instances of on-demand MySQL in the space.
    $ cf services
    Getting services in org my-org / space my-space as user@example.com...
    OK
    name          service     plan        bound apps    last operation
    my-instance   p.mysql     db-small                  create succeeded
    

    You can bind your app to an existing instance or create a new instance to bind to your app.

Create, Use, and Manage Service Instances

Create a Service Instance

Unlike pre-provisioned services, on-demand services are created asynchronously, not immediately. The watch command shows you when your service instance is ready to bind and use.

To create an instance of the MySQL for PCF v2 service, run cf create-service:

  1. Enter cf create-service p.mysql db-small SERVICE_INSTANCE

    Where SERVICE_INSTANCE is a name you choose to identify the service instance. This name will appear under service [sic] in output from cf services.

  2. Enter watch cf services and wait for the last operation for your instance to show as create succeeded.

    $ cf create-service p.mysql db-small my-instance
    Creating service my-instance in org my-org / space my-space as user@example.com... OK
    $ watch cf services
    Getting services in org my-org / space my-space as user@example.com... OK name service plan bound apps last operation my-instance p.mysql db-small create succeeded

    If you get an error, see Troubleshooting Instances.

Bind a Service Instance to Your App

For an app to use a service, you must bind it to a service instance. Do this after you push or re-push the app using cf push.

To bind an app to a MySQL instance run $ cf bind-service.

  1. Enter cf bind-service APP SERVICE_INSTANCE

    Where APP is the app you want to use the MySQL service instance and SERVICE_INSTANCE is the name you supplied when you ran cf create-service.

    $ cf bind-service my-app my-instance
    Binding service mydb to my-app in org my-org / space test as user@example.com... OK TIP: Use 'cf push' to ensure your env variable changes take effect

Use the MySQL Service in Your App

To access the MySQL service from your app:

  1. Run cf env APP_NAME with the name of the app bound to the MySQL for PCF instance.

  2. In the output, note the connection strings listed in the VCAP_SERVICES > credentials object for the app.

  3. In your app code, call the MySQL service using the connection strings.

For how to code your app to use MySQL messaging, see About Using Pivotal MySQL > Client Documentation in the MySQL documentation.

Update to a Larger Service Instance

As apps and their databases grow, it may be necessary to update the service instance to a larger plan.

  1. Enter cf update-service -p PLAN

    Where PLAN is the plan you want to upgrade the service instance to.

    $ cf update-service -p db-large
    

This does not require a rebinding of your app. However, while the instance is being migrated to a new service instance, the database will be unavailable for several minutes.

Unbind a Service Instance to Your App

To stop an app from using a service it no longer needs, unbind it from the service instance using cf unbind-service.

  1. Enter cf unbind-service APP SERVICE_INSTANCE

    Where APP is the app you want to stop using the MySQL service instance and SERVICE_INSTANCE is the name you supplied when you ran cf create-service.

    $ cf unbind-service my-app my-instance
    Unbinding app my-app from service my-instance in org my-org / space my-space as user@example.com... OK

Delete a Service Instance

To delete a service instance, run cf delete-service.

  1. Enter cf delete-service SERVICE_INSTANCE

    Where SERVICE_INSTANCE is the name of the service to delete.

    $ cf delete-service my-instance
    Are you sure you want to delete the service my-instance ? y Deleting service my-service in org my-org / space my-space as user@example.com... OK
  2. Enter watch cf service SERVICE_INSTANCE and wait for a Service instance not found error indicating that the instance no longer exists.

You cannot delete a service instance that an app is bound to.

Example Application

To help application developers get started with MySQL for PCF, we have provided an example application, which can be downloaded here. Instructions can be found in the included README.

MySQL for PCF Tools

The following tools let developers access their MySQL for PCF databases.

Pivotal MySQLWeb Database Management App

The Pivotal MySQLWeb app provides a web-based UI for managing MySQL for PCF databases. The free app lets you view and operate on tables, indexes, constraints, and other database structures, and directly execute SQL commands.

Pivotal MySQLWeb

You can run the Pivotal MySQLWeb app in two ways:

  • Standalone on your own machine
  • Deployed to PCF

If you deploy Pivotal MySQLWeb to PCF, you can configure it in the deployment manifest to automatically bind to a specific service instance.

See the Pivotal MySQLWeb code repo and demo video, for how to install and run Pivotal MySQLWeb.

Command-Line Interface MySQL Plugin

To connect to your MySQL for PCF databases from a command line, use the Cloud Foundry Command-Line Interface (cf CLI) MySQL plugin. The plugin lets you:

  • Inspect databases for debugging.
  • Manually adjust database schema or contents in development environments.
  • Dump and restore databases.

To install the cf CLI MySQL plugin, run the following:

$ cf install-plugin -r "CF-Community" mysql-plugin

For more information, see the cf-mysql-plugin repository.

Create a pull request or raise an issue on the source for this page in GitHub