Connecting to Cloud SQL from App Engine

This page describes how to set up a connection between an App Engine application and a Cloud SQL instance. The steps depend on the types of the App Engine environment and Cloud SQL instance.

  1. App Engine standard environment to First Generation
  2. App Engine standard environment to Second Generation
  3. App Engine flexible environment to Second Generation

App Engine standard environment to First Generation

  1. Grant the application access:

    Console

    1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

      Go to the Cloud SQL Instances page

    2. Click the instance name.
    3. Select Access Control > Authorization.
    4. In the Authorized App Engine applications section, click Add application ID and enter the application ID.
    5. Click Done to exit edit mode.
    6. Click Save to update the instance.

    gcloud

    1. List the existing authorized App Engine applications for the instance.
      gcloud sql instances describe YOUR_INSTANCE_NAME
      
    2. Use the sql instances patch command to provide the App Engine application gae-app-name names for all applications that can access this instance. Use commas to separate multiple application names.
      gcloud sql instances patch YOUR_INSTANCE_NAME --authorized-gae-apps GAE_APP_NAME
      
  2. Update your application:
    1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

      Go to the Cloud SQL Instances page

    2. Click the instance name.
    3. Copy the Instance connection name from the Properties.
    4. Use the following connection string in your application:
      /cloudsql/[INSTANCE_CONNECTION_NAME]
      

      The complete code statement depends on your language and framework. For more details about connecting, see the appropriate language-specific link below.

    Java App Engine Applications

    Python App Engine Applications

    PHP App Engine Applications

    Go App Engine Applications

App Engine standard environment to Second Generation

  1. Grant the application access:
    1. If your application is in the same project as your Cloud SQL instance, you can proceed to step 2.
    2. Otherwise, identify the default App Engine service account for the application's project.
      The default App Engine service account is named '[PROJECT-ID]@appspot.gserviceaccount.com'.
    3. Go to the IAM & Admin page in the Google Cloud Platform Console.

      Go to the IAM & Admin page

    4. Select the project that contains the Cloud SQL instance.
    5. Open the list of accounts with the Editor role.
    6. If the service account is listed there, you can proceed to step 2.
    7. Otherwise, add the service account by clicking Add member.
    8. In the Add members dialog, provide the name of the service account and select Editor for the role.
    9. Click Add.
      You should now see the service account listed with the project members with the Editor role.
  2. Update your application:
    1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

      Go to the Cloud SQL Instances page

    2. Click the instance name.
    3. Copy the Instance connection name from the Properties.
    4. In your application code, use the following string to connect to the instance:
      /cloudsql/[INSTANCE_CONNECTION_NAME]
      

    The complete code statement you use depends on your language and framework. For more details about connection strings, see the appropriate language-specific link below.

    Java App Engine Applications

    Python App Engine Applications

    PHP App Engine Applications

    Go App Engine Applications

App Engine flexible environment to Second Generation

  1. Enable the Cloud SQL Administration API.

    Enable the API

    You do not need to get credentials after enabling the API.

  2. Grant your application access:
    1. If your application is in the same project as your Cloud SQL instance, you can proceed to step 3.
    2. Otherwise, identify the service account associated with the application.
      The service account for the application is named '[PROJECT-ID]@appspot.gserviceaccount.com'.
    3. Go to the IAM & Admin page in the Google Cloud Platform Console.

      Go to the IAM & Admin page

    4. Select the project that contains the Cloud SQL instance.
    5. Open the list of accounts with the Editor role.
    6. If the application's service account is listed there, you can proceed to step 3.
    7. Otherwise, add the service account by clicking Add members.
    8. In the Add members dialog, provide the name of the application's service account and select Editor for the role.
    9. Click Add.
      You should now see the service account listed with the project members with the Editor role.
  3. Update your application:
    1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

      Go to the Cloud SQL Instances page

    2. Click the instance name.
    3. Copy the Instance connection name from the Instance properties.
    4. Add the following key to your app.yaml runtime configuration file :
      beta_settings:
            cloud_sql_instances: [INSTANCE_CONNECTION_NAME]
      
      You can include multiple instance names, separated by commas.
    5. In your application code, connect to the instance by opening a socket with the following name:
      /cloudsql/[INSTANCE_CONNECTION_NAME}
      

    The complete code statement you use depends on your language and framework. For more details about connection strings, see the appropriate language-specific link below.

    Java App Engine Applications

    Python App Engine Applications

    Go App Engine Applications

What's next

Send feedback about...

Cloud SQL