Learn how to deploy a sample app on Cloud Run connected to a MySQL instance by using the Google Cloud console and a client application.
Assuming that you complete all the steps in a timely manner, the resources created in this quickstart typically cost less than one dollar (USD).
To follow step-by-step guidance for this task directly in the Google Cloud console, click Guide me:
Before you begin Note: The name you use for your project must be between 4 and 30 characters. When you type the name, the form suggests a project ID, which you can edit. The project ID must be between 6 and 30 characters, with a lowercase letter as the first character. You can use a dash, lowercase letter, or digit for the remaining characters, but the last character cannot be a dash.In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Verify that billing is enabled for your Google Cloud project.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Verify that billing is enabled for your Google Cloud project.
Click Enable APIs to enable the APIs required for this quickstart.
This enables the following APIs:
Click the following button to open Cloud Shell, which provides command-line access to your Google Cloud resources directly from the browser. Cloud Shell can be used to run the gcloud
commands presented throughout this quickstart.
Run the following gcloud
command using Cloud Shell:
gcloud services enable compute.googleapis.com sqladmin.googleapis.com run.googleapis.com \ containerregistry.googleapis.com cloudbuild.googleapis.com servicenetworking.googleapis.com
This command enables the following APIs:
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
.Before running the command as follows, replace DB_ROOT_PASSWORD with the password of your database user.
Optionally, modify the values for the following parameters:
us-central
is used.Run the gcloud sql instances create
command to create a Cloud SQL instance.
gcloud sql instances create quickstart-instance \ --database-version=MYSQL_8_0 \ --cpu=1 \ --memory=4GB \ --region=us-central1 \ --root-password=DB_ROOT_PASSWORDPrivate IP Console Allocate an IP address range and create a private connection to configure private services access for Cloud SQL
In the Google Cloud console, go to the VPC networks page.
default
VPC network.google-managed-services-default
.16
.default
VPC network.google-managed-services-default
.In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
.Run the gcloud compute addresses create
command to allocate an IP address range.
gcloud compute addresses create google-managed-services-default \ --global --purpose=VPC_PEERING --prefix-length=16 \ --description="peering range for Google" --network=default
Run the gcloud services vpc-peerings connect
command to create a private connection to the allocated IP address range. Replace YOUR_PROJECT_ID with your project's project ID.
gcloud services vpc-peerings connect --service=servicenetworking.googleapis.com \ --ranges=google-managed-services-default --network=default \ --project=YOUR_PROJECT_ID
Before running the command as follows, replace DB_ROOT_PASSWORD with the password of your database user.
Optionally, modify the values for the following parameters:
us-central1
is used. See the full list of regions.Run the gcloud sql instances create
command to create a Cloud SQL instance with a Private IP address.
gcloud sql instances create quickstart-instance \ --database-version=MYSQL_8_0 \ --cpu=1 \ --memory=4GB \ --region=us-central1 \ --root-password=DB_ROOT_PASSWORD \ --no-assign-ip \ --network=default
Run the gcloud sql instances patch
command to enable only allow SSL connections for the instance.
gcloud sql instances patch quickstart-instance --require-ssl
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
.quickstart-db
. Leave the values for the character set and collation.Run the gcloud sql databases create
command to create a database.
gcloud sql databases create quickstart-db --instance=quickstart-instanceCreate a user Console
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-user
Optionally, select Restrict host by IP address or address range and enter an IP address or address range in the Host section. The user can then connect only from the IP address or addresses specified.
Before running the following command, make the following replacements:
Run the gcloud sql users create
command to create the user.
gcloud sql users create quickstart-user \ --instance=quickstart-instance \ --password=PASSWORD
User name length limits are the same for Cloud SQL as for on-premises MySQL; 32 characters for MySQL 8.0 and later, 16 characters for earlier versions.
Deploy sample app to Cloud Run Configure a Cloud Run service accountConfigure the service account used by Cloud Run so that it has the
Cloud SQL Clientrole with permissions to connect to Cloud SQL.
ConsoleIn the Google Cloud console, go to the IAM page.
gcloud
command to get a list of your project's service accounts:
gcloud iam service-accounts list
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \ --role="roles/cloudsql.client"
With a Cloud SQL instance, database, and service account with client permissions, you can now configure a sample application to connect to your Cloud SQL instance.
Public IP For public IP paths, Cloud Run provides encryption and connects using the Cloud SQL Auth Proxy through Unix sockets. GoIn Cloud Shell Editor, open the sample app's source code.
Open Cloud Shell Editorgcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
mvn clean package com.google.cloud.tools:jib-maven-plugin:2.8.0:build \ -Dimage=gcr.io/YOUR_PROJECT_ID/run-sql -DskipTests
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
to see its Overview pagequickstart-key
as the name and click Create.In the New SSL certificate created dialog, click each download link to download the certificates. Then, click Close.
Important: Store this private key securely. If you lose it, you must create a new client certificate.In the Google Cloud console, go to the Serverless VPC access - Create connector page.
quickstart-connector
for the Name.10.8.0.0
in the IP range input boxIn Cloud Shell Editor, open the sample app's source code.
Open Cloud Shell EditorOpen in Cloud Shell
dialog, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.client-key.pem
client-cert.pem
server-ca.pem
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
For Java users, the connector already provides a secure connection so that creating and downloading SSL server and client certificates is unnecessary.
Create a Serverless VPC Connection for connections to the instance via Private IPIn the Google Cloud console, go to the Serverless VPC access - Create connector page.
quickstart-connector
for the Name.10.8.0.0
in the IP range input boxmvn clean package com.google.cloud.tools:jib-maven-plugin:2.8.0:build \ -Dimage=gcr.io/YOUR_PROJECT_ID/run-sql -DskipTests
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
to see its Overview pagequickstart-key
as the name and click Create.In the New SSL certificate created dialog, click each download link to download the certificates. Then, click Close.
Important: Store this private key securely. If you lose it, you must create a new client certificate.In the Google Cloud console, go to the Serverless VPC access - Create connector page.
quickstart-connector
for the Name.10.8.0.0
in the IP range input boxclient-key.pem
client-cert.pem
server-ca.pem
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
to see its Overview pagequickstart-key
as the name and click Create.In the New SSL certificate created dialog, click each download link to download the certificates. Then, click Close.
Important: Store this private key securely. If you lose it, you must create a new client certificate.In the Google Cloud console, go to the Serverless VPC access - Create connector page.
quickstart-connector
for the Name.10.8.0.0
in the IP range input boxclient-key.pem
client-cert.pem
server-ca.pem
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
The steps to deploy the sample to Cloud Run depend on the type of IP address you assigned to your Cloud SQL instance.
The image connection method varies based on which environment variables are set.
INSTANCE_HOST
environment variable. This connection method honors the certificates and ports configured as environment variables.INSTANCE_UNIX_SOCKET
environment variable.
Don't set the value for the INSTANCE_HOST
environment variable.
Unix sockets aren't natively supported in Java. If your application is written using Java, you must use the Cloud SQL Java Connector.
INSTANCE_CONNECTION_NAME
and DB_IAM_HOST
environment variables.
Don't set the values for the following environment variables:
INSTANCE_HOST
INSTANCE_UNIX_SOCKET
DB_PORT
DB_ROOT_CERT
DB_CERT
DB_KEY
3307
.In the Google Cloud console, go to the Cloud Run page.
Click Create container and select Service to display the Create service form.
gcr.io/YOUR_PROJECT_ID/run-sql
container image you created in the previous step.quickstart-service
for the Service name.Allow unauthenticated invocations
option. If you don't have permissions (Cloud Run Admin role) to select this, the service will deploy and require authentication.Environment variables
by clicking Add Variable under Environment variables. Set the values for the environment variables, as follows:/cloudsql/YOUR_PROJECT_ID:us-central1:quickstart-instance
.quickstart-db
.quickstart-user
.quickstart-user
that you specified in the previous Create a user quickstart step.Enable connecting to Cloud SQL:
quickstart-instance
you previously created from the drop-down menu.To create the Cloud Run service, click Create.
gcloud run deploy
, and have the required roles granted to you.
Before running the following command, make the following replacements:
INSTANCE_CONNECTION_NAME with your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.
quickstart-user
that you created in the previous Create a user quickstart step.Run the gcloud run deploy
command as follows to create the Cloud Run service. Environment variables vary depending on the connection method you want to use:
gcloud run deploy run-sql --image gcr.io/YOUR_PROJECT_ID/run-sql \ --add-cloudsql-instances INSTANCE_CONNECTION_NAME \ --set-env-vars INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME" \ --set-env-vars DB_NAME="quickstart-db" \ --set-env-vars DB_USER="quickstart-user" \ --set-env-vars DB_PASS="DB_PASS"
Enter the numeric choice provided for us-central1
when prompted to specify a region.
In the Google Cloud console, go to the Cloud Run page.
Click Create container and select Service to display the Create service form.
gcr.io/YOUR_PROJECT_ID/run-sql
container image you created in the previous step.quickstart-service
for the Service name.Allow unauthenticated invocations
option. If you don't have permissions (Cloud Run Admin role) to select this, the service will deploy and require authentication.DB_NAME
: Set to quickstart-db
.DB_USER
: Set to quickstart-user
.DB_PASS
: Set to the password of the quickstart-user
that you specified in the previous Create a user quickstart step.INSTANCE_CONNECTION_NAME
: Set to your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.DB_PORT
: Set to 3306
.DB_ROOT_CERT
: Set to certs/server-ca.pem
.DB_CERT
: Set to certs/client-cert.pem
.DB_KEY
: Set to certs/client-key.pem
.PRIVATE_IP
: Set to TRUE
.Enable connecting to Cloud SQL:
quickstart-instance
Cloud SQL instance that you previously created.default: Serverless VPC Access Connector "quickstart-connector"
from the VPC Network drop-down menu.Click Create to create the Cloud Run service.
gcloud run deploy
, and have the required roles granted to you.
Before running the following command, make the following replacements:
INSTANCE_CONNECTION_NAME with your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.
quickstart-user
that you created in the previous Create a user quickstart step.Run the gcloud run deploy
command as follows to create the Cloud Run service. Environment variables vary depending on the connection method you want to use:
gcloud run deploy run-sql --image gcr.io/YOUR_PROJECT_ID/run-sql \ --add-cloudsql-instances INSTANCE_CONNECTION_NAME \ --vpc-connector="quickstart-connector" --vpc-egress=all-traffic \ --set-env-vars DB_NAME="quickstart-db" \ --set-env-vars DB_USER="quickstart-user" \ --set-env-vars DB_PASS="DB_PASS" \ --set-env-vars INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME" \ --set-env-vars DB_ROOT_CERT="certs/server-ca.pem" \ --set-env-vars DB_CERT="certs/client-cert.pem" \ --set-env-vars DB_KEY="certs/client-key.pem" \ --set-env-vars PRIVATE_IP="TRUE"
gcloud run deploy run-sql --image gcr.io/YOUR_PROJECT_ID/run-sql \ --add-cloudsql-instances INSTANCE_CONNECTION_NAME \ --vpc-connector="quickstart-connector" --vpc-egress=all-traffic \ --set-env-vars DB_NAME="quickstart-db" \ --set-env-vars DB_USER="quickstart-user" \ --set-env-vars DB_PASS="DB_PASS" \ --set-env-vars INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME"
Enter the numeric choice provided for us-central1
when prompted to specify a region.
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
instance to open the Instance details page.quickstart-instance
, and then click Delete to delete the instance.In the Google Cloud console, go to the Cloud Run page.
quickstart-service
service name.If you're not using the Cloud SQL client role that you assigned to the Compute Engine service account, you can remove it.
In the Google Cloud console, go to the IAM page.
If you're not using the APIs that were enabled as part of this quickstart, you can disable them.
In the Google Cloud console, go to the APIs page.
Select any API that you would like to disable and then click the Disable API button.
You also can learn about creating MySQL users and databases for your Cloud SQL instance.
For more information about pricing, see Cloud SQL for MySQL pricing.
Learn more about:
Additionally, you can learn about connecting to a Cloud SQL instance from other Google Cloud applications:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4