Connecting to Cloud SQL instances

This page describes how to configure your fully managed Cloud Run service to allow connections to a Cloud SQL instance. If you are using Cloud Run on GKE, follow the instructions for Private IP under Connecting from GKE.

Note that when a revision is configured to connect to a Cloud SQL instance, the Cloud SQL proxy is automatically enabled and configured for you.

Before you start

Before following the instructions on this page, do the following:

  • For your project: Enable the Cloud SQL Admin API.

    Enable the API

  • Unless you are using an existing instance, create a second generation Cloud SQL MySQL instance or a Cloud SQL PostgresSQL instance, following the instructions in the appropriate link. The instance must have a public IP address, and you don't need to to add any authorized networks. Note down the Cloud SQL instance name: you'll need it later.

Configuring Cloud Run for Cloud SQL

To configure your Cloud Run service to allow connections to a Cloud SQL instance:

Command line

  • If the Cloud SQL instance(s) is in your project and in the same region as your Cloud Run service, run either of the following commands depending on whether you are deploying a new container image or are just updating:

    gcloud beta run deploy --image IMAGE --add-cloudsql-instances INSTANCE-NAME
    gcloud beta run services update --add-cloudsql-instances INSTANCE-NAME
    Replace IMAGE with the image you are deploying and INSTANCE-NAME with the instance name for your Cloud SQL instance or a comma delimited list of instance names.

    You can use other options and parameters, including options to clear and remove Cloud SQL instances. For more details refer to the gcloud deploy or update reference.

  • If the Cloud SQL instance is not in your project or is not in the same region as your Cloud Run service, run either of the following commands depending on whether you are deploying a new container image or are just updating:

    gcloud beta run deploy --image IMAGE --add-cloudsql-instances CONNECTION-NAME
    gcloud beta run services update --add-cloudsql-instances CONNECTION-NAME
    Replace IMAGE with the image you are deploying and CONNECTION-NAME with the instance connection name of your Cloud SQL instance, or a comma delimited list of connection names. To get the instance connection name, you can do either of the following:

    • Use the colon delimited instance connection name format your-project-name:your-zone:your-cloudsql-instance-name, replacing these with your own values.
    • Use the command

      gcloud sql instances describe INSTANCE-NAME
      The connection name will be listed in the top few lines of the description:

      backendType: SECOND_GEN
      connectionName: sample-run-run1:us-central1:boblemon-instance
      

Console

Not available yet.

Connect to the Cloud SQL instance from your code

The container instances are now able to connect to Cloud SQL via the Cloud SQL Proxy that is automatically activated and configured for this service.

In your code, you can connect to the Cloud SQL instance using the /cloudsql/[CONNECTION NAME] Unix socket.

For code examples, refer to connect from Cloud Functions or to connect from App Engine for Python, Java, Node.js and Python 3.

Var denne side nyttig? Giv os en anmeldelse af den:

Send feedback om...

Cloud Run Documentation