Connecting from Google 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.

For information about the various options for connecting to Cloud SQL, see Connection Options for External Applications.

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 to open its Overview page.
    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 [INSTANCE_NAME]
      
    2. Use the sql instances patch command to provide the names for all applications that can access this instance. Use commas to separate multiple application names.
      gcloud sql instances patch [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 to open its Overview page.
    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

    Python

    PHP

    Go

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 Projects page in the Google Cloud Platform Console.

      Go to the IAM & Admin Projects page

    4. Select the project that contains the Cloud SQL instance.
    5. Search for the service account name.
    6. If the service account is already there with the Editor role, 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 Project > Editor for the role.
    9. Click Add.

      You should now see the service account listed 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 to open its Overview page.
    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

    Python

    PHP

    Go

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 Projects page in the Google Cloud Platform Console.

      Go to the IAM & Admin Projects page

    4. Select the project that contains the Cloud SQL instance.
    5. Search for the service account name.
    6. If the service account is already there with the Editor role, you can proceed to step 3.
    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 Project > Editor for the role.
    9. Click Add.

      You should now see the service account listed 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 to open its Overview page.
    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

    To access Cloud SQL from a program written in the Java programming language, use the MySQL socket factory. The MySQL socket factory requires the Cloud SQL API to be enabled. It authenticates with Cloud SDK credentials, so the Cloud SDK must be installed and authenticated. For more information, see the Java MySQL socket factory GitHub page.

    Python

    Using Cloud SQL with Python in the App Engine flexible environment

    Go

    Using Cloud SQL with Go in the App Engine flexible environment

What's next

Send feedback about...

Cloud SQL Documentation