LATEST VERSION: 2.2 - CHANGELOG

Using MySQL for PCF v2

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

These procedures use the Cloud Foundry Command Line Interface (cf CLI). For more information, see Managing Service Instances with the cf CLI.

You can also use Apps Manager to do the same tasks using a graphical UI.

Prerequisites

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

The Process for Using MySQL for PCF in Your App

To use MySQL in a PCF app:

  1. Check the service availability in the Marketplace, and see if there is an existing instance of MySQL for PCF in your space.

    See Confirm the MySQL for PCF v2 Service Availability, below.

  2. If there is no existing instance or you want to use a different one, create an instance of the MySQL for PCF service in the same space as the app.

    See Create a Service Instance, below.

  3. Push your app into the same space as the MySQL for PCF service instance, using cf push.

    For information about cf push, see Push.

  4. Bind the app to the MySQL for PCF service instance, to enable the app to use MySQL.

    See Bind a Service Instance to Your App, below.

  5. Call the MySQL for PCF service in your app code, and then re-push your app into the space.

    See Use the MySQL Service in Your App, below.

Confirm the MySQL for PCF v2 Service Availability

For an app to use the MySQL for PCF v2 service, both of the following must be true:

  • The service must be available in the Marketplace for its space.
  • An instance of the service must exist in its space.

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

Check Service Availability in the Marketplace

To find out if a MySQL for PCF v2 service is available in the Marketplace, do the following:

  1. Enter cf marketplace.
  2. If the output lists p.mysql in the service column, MySQL for PCF v2 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
    [...]
    

Check that an Instance is Running in the Space

To confirm that a MySQL for PCF v2 instance is running in the space, do the following:

  1. Use the cf CLI or Apps Manager to log in to the org and space that contains the app.
  2. Enter cf services.
  3. Any p.mysql listings in the service column are service instances of MySQL for PCF v2 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 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, do the following:

  1. Run the command cf create-service p.mysql PLAN SERVICE-INSTANCE

    Where:

    • PLAN is the name of the MySQL for PCF v2 plan you want to use.
    • SERVICE-INSTANCE is a name you choose to identify the service instance. This name appears under service 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 the app to a service instance. Do this after you push or re-push the app using cf push.

To bind an app to a MySQL for PCF instance run the command cf bind-service APP SERVICE-INSTANCE

Where:
APP is the app you want to use the MySQL service instance.
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.

Manage Service Instances

This section describes tasks you do over the life cycle of your apps and data:

  • Moving your data to a different plan.
  • Removing an app’s access to a service it no longer needs.
  • Deleting a service instance that is not used.

Update a Service Instance to a Larger Plan

As apps and their databases grow, it may be necessary to update the service instance to a larger plan. 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.

To update a service instance to a larger plan, run the commannd cf update-service SERVICE-INSTANCE -p PLAN

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

$ cf update-service my-instance -p db-large

Unbind an App from a Service Instance

To stop an app from using a service it no longer needs, run the following command to unbind the app from the service:

cf unbind-service APP SERVICE-INSTANCE

Where:
APP is the app you want to stop using the MySQL service instance.
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

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

To delete a service instance, do the following:

  1. Run the command 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.

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.

cf CLI MySQL Plugin

To connect to your MySQL for PCF databases from a command line, use the 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.

Example App

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

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