Accessing Services with SSH

Page last updated:

This page assumes you are using cf CLI v6.15.0 or later.

This topic describes how to gain direct command line access to your deployed service instance. For example, you may need access to your database to execute raw SQL commands to edit the schema, import and export data, or debug application data issues.

To establish direct command line access to a service, you deploy a host app and utilize its SSH and port forwarding features to communicate with the service instance through the app container. The technique outlined below works with any TCP service, such as MySQL or Redis.

Create a Service Instance

In your terminal window, log in to your deployment with cf login.

  1. List the marketplace services available using the cf marketplace command. In this example, we create a MySQL service instance.
    $ cf marketplace
    mysql  100mb MySQL databases on demand
  2. Create your service instance. As part of the create-service command, indicate the service name, the service plan, and the name you choose for your service instance.
    $ cf create-service MySQL 100mb MY-DB  

Push Your Host App

To push an app that will act as the host for the SSH tunnel, push any app that will successfully deploy to Cloud Foundry.

Note: Your app must be prepared before you push it. See the Deploy an Application topic for details on preparing apps for deployment.

  1. Push your app.
    $ cf push YOUR-HOST-APP
  2. Enable SSH for your app.
    $ cf enable-ssh YOUR-HOST-APP

    Note: In order to enable SSH access to your app, SSH access must also be enabled for both the space that contains the app and Cloud Foundry. See the Application SSH Overview topic for further details.

Create Your Service Key

To establish SSH access to your service instance, you need to create a service key that contains critical information for configuring your SSH tunnel.

  1. Create a service key for your service instance using the cf create-service-key command.
    $ cf create-service-key MY-DB EXTERNAL-ACCESS-KEY
  2. Retrieve your new service key using the cf service-key command.
    $ cf service-key MY-DB EXTERNAL-ACCESS-KEY
    Getting key EXTERNAL-ACCESS-KEY for service instance MY-DB as
    "hostname": "",
    "jdbcUrl": "jdbc:mysql://\u0026password=231f435o05",
    "name": "ad_b2fca6t49704585d",
    "password": "231f435o05",
    "port": "3306",
    "uri": "mysql://",
    "username": "b5136e448be920"

Configure Your SSH Tunnel

Configure an SSH tunnel to your service instance using cf ssh. Tailor the example command below with information from your service key.

$ cf ssh -L YOUR-HOST-APP

  • Use any available local port for port forwarding. For example, 63306.
  • Replace with the address provided under hostname in the service key retrieved above.
  • Replace 3306 with the port provided under port above.
  • Replace YOUR-HOST-APP with the name of your host app.

After you enter the command, open another terminal window and perform the steps below in Access Your Service Instance.

Access Your Service Instance

To establish direct command-line access to your service instance, use the relevant command line tool for that service. This example uses the MySQL command line client to access the MySQL service instance.

$ mysql -u b5136e448be920 -h 0 -p -D ad_b2fca6t49704585d -P 63306
  • Replace b5136e448be920 with the username provided under username in your service key.
  • -h 0 indicates to mysql to connect to your local machine.
  • -p indicates to mysql to prompt for a password. When prompted, use the password provided under password in your service key.
  • Replace ad_b2fca6t49704585d with the database name provided under name in your service key.
  • -P 63306 indicates to mysql to connect on port 63306.
Create a pull request or raise an issue on the source for this page in GitHub