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.
To use MySQL for PCF v2 with your PCF apps, you need:
- A PCF installation with MySQL for PCF installed and listed in the Marketplace
- A Space Developer or Admin account on the PCF installation
- A local machine with the following installed:
- To log into the org and space containing your app
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:
Make sure an instance of the MySQL for PCF service exists in the same space as the app.
Bind the app to the MySQL for PCF service instance, to enable the app to use MySQL.
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.
To find out if a MySQL for PCF v2 service is available in the Marketplace:
- If the output lists
servicecolumn, 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 email@example.com... OK service plans description [...] p.mysql db-small Dedicated instances of MySQL service to provide a relational database [...]
To confirm that a MySQL for PCF v2 instance is running in the space
p.mysqllistings in the
servicecolumn are service instances of on-demand MySQL in the space.
$ cf services Getting services in org my-org / space my-space as firstname.lastname@example.org... OK name service plan bound apps last operation my-instance p.mysql db-small create succeeded
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 p.mysql db-small SERVICE_INSTANCE
SERVICE_INSTANCEis a name you choose to identify the service instance. This name will appear under
service[sic] in output from
watch cf servicesand wait for the
last operationfor your instance to show as
$ cf create-service p.mysql db-small my-instance
Creating service my-instance in org my-org / space my-space as email@example.com... OK
$ watch cf services
Getting services in org my-org / space my-space as firstname.lastname@example.org... OK name service plan bound apps last operation my-instance p.mysql db-small create succeeded
If you get an error, see Troubleshooting Instances.
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
To bind an app to a MySQL instance run
$ cf bind-service.
cf bind-service APP SERVICE_INSTANCE
APPis the app you want to use the MySQL service instance and
SERVICE_INSTANCEis the name you supplied when you ran
$ cf bind-service my-app my-instance
Binding service mydb to my-app in org my-org / space test as email@example.com... OK TIP: Use 'cf push' to ensure your env variable changes take effect
To access the MySQL service from your app:
cf env APP_NAMEwith the name of the app bound to the MySQL for PCF instance.
In the output, note the connection strings listed in the
credentialsobject for the app.
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.
As apps and their databases grow, it may be necessary to update the service instance to a larger plan.
cf update-service -p PLAN
PLANis 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.
To stop an app from using a service it no longer needs, unbind it from the service instance using
cf unbind-service APP SERVICE_INSTANCE
APPis the app you want to stop using the MySQL service instance and
SERVICE_INSTANCEis the name you supplied when you ran
$ cf unbind-service my-app my-instance
Unbinding app my-app from service my-instance in org my-org / space my-space as firstname.lastname@example.org... OK
To delete a service instance, run
cf delete-service SERVICE_INSTANCE
SERVICE_INSTANCEis 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 email@example.com... OK
watch cf service SERVICE_INSTANCEand wait for a
Service instance not founderror indicating that the instance no longer exists.
You cannot delete a service instance that an app is bound to.
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.
The following tools let developers access their MySQL for PCF databases.
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.
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.
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.