Learn how to deploy a sample app on your Linux, macOS, or Windows-based local computer 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).
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.Make sure 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.Make sure that billing is enabled for your Google Cloud project.
Click the Enable APIs button to enable the APIs required for this quickstart.
This enables the following APIs:
Install the gcloud CLI which provides command-line access to your Google Cloud resources. The gcloud CLI is used to run the gcloud CLI
commands presented throughout this quickstart. All the commands are formatted to be run in a Linux/macOS terminal or in Windows Powershell.
Open the terminal and run the following gcloud
command:
gcloud services enable sqladmin.googleapis.com iam.googleapis.com
This command enables the following APIs:
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
for Instance ID.Click Create Instance and wait until the instance initializes and starts.
Before running the gcloud sql instances create
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. See the full list of regions.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_PASSWORDCreate a database Console
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
.quickstart_db
as the name of the database, and optionally 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.
Replace the following:
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.
Set up a service accountCreate and configure a Google Cloud service account that has the
Cloud SQL Clientrole with permissions to connect to Cloud SQL. After you create a service account, you might need to wait for 60 seconds or more before you use the service account.
Console Create a service accountquickstart-service-account
as the service account name.quickstart-service-account
service account that you want to create a key for.Clicking Create downloads a service account key file. After you download the key file, you cannot download it again.
Make sure to store the key file securely, because it can be used to authenticate as your service account. You can move and rename this file however you would like.
gcloud Create a service accountgcloud iam service-accounts create
command:gcloud iam service-accounts create quickstart-service-account --description="DESCRIPTION" --display-name="quickstart-service-account"
Replace the following value:
DESCRIPTION
: An optional description of the service accountgcloud projects add-iam-policy-binding
command. Replace PROJECT_ID with your Google Cloud project ID:
gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:quickstart-service-account@PROJECT_ID.iam.gserviceaccount.com" --role="roles/cloudsql.client"
To use the service account you just created from your local computer you need a service account key file to authenticate the sample application as the service account. To create and download the service account key file, run the gcloud iam service-accounts keys create
command:
gcloud iam service-accounts keys create KEY_FILE --iam-account=quickstart-service-account@PROJECT_ID.iam.gserviceaccount.com
Replace the following values:
KEY_FILE
: The path to a new output file for the private key — for example, ~/sa-private-key.json
.PROJECT_ID
: Your Google Cloud project ID.The service account key file is now downloaded to your local computer. After you download the key file, you cannot download it again.
Make sure to store the key file securely, because anyone can use it to authenticate as your service account. You can move and rename this file however you would like.
Setup development environment for programming languageSet up your local computer's development environment for your preferred programming language.
GoComplete the following steps to set up your development environment to run the Go sample app.
Complete the following steps to set up your development environment to run the Java sample app.
Complete the following steps to set up your development environment to run the Node.js sample app.
Complete the following steps to set up your development environment to run the Python sample app.
Install Git, an open source version control system.
LinuxFollow the official Git installation documentation for Linux.
macOSFollow the official Git installation documentation for macOS.
WindowsFollow the official Git installation documentation for Windows.
Clone a sample appClone a sample app to your local computer using the git clone
command.
Run the following commands to clone the Go sample app to your local computer and change directory to the directory containing the sample app.
git clone https://github.com/GoogleCloudPlatform/golang-samples
cd golang-samples/cloudsql/mysql/database-sql
Run the following commands to clone the Java sample app to your local computer and change directory to the directory containing the sample app.
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/cloud-sql/mysql/servlet
Run the following commands to clone the Node.js sample app to your local computer and change directory to the directory containing the sample app.
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
cd nodejs-docs-samples/cloud-sql/mysql/mysql
Run the following commands to clone the Python sample app to your local computer and change directory to the directory containing the sample app.
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/cloud-sql/mysql/sqlalchemy
Configure and run the sample app.
Linux/macOS GoOpen the terminal and run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:
/path/to/service/account/key.json
.quickstart-user
that you created in the previous Create a user quickstart step.export GOOGLE_APPLICATION_CREDENTIALS=CREDENTIALS_JSON_FILE export INSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME' export DB_PORT='3306' export DB_NAME='quickstart_db' export DB_USER='quickstart-user' export DB_PASS='YOUR_DB_PASSWORD'
Run the following commands to get the Go sample app's dependencies on your local computer and run the sample app.
go get ./...
go run cmd/app/main.go
http://127.0.0.1:8080
.
Run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:
/path/to/service/account/key.json
.quickstart-user
that you created in the previous Create a user quickstart step.export GOOGLE_APPLICATION_CREDENTIALS=CREDENTIALS_JSON_FILE export INSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME' export DB_PORT='3306' export DB_NAME='quickstart_db' export DB_USER='quickstart-user' export DB_PASS='YOUR_DB_PASSWORD'
Run the following command to get the Java sample app's dependencies on your local computer and run the sample app.
mvn jetty:run
http://127.0.0.1:8080
.
Run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:
/path/to/service/account/key.json
.quickstart-user
that you created in the previous Create a user quickstart step.export GOOGLE_APPLICATION_CREDENTIALS=CREDENTIALS_JSON_FILE export INSTANCE_HOST='127.0.0.1' export DB_PORT='3306' export DB_NAME='quickstart_db' export DB_USER='quickstart-user' export DB_PASS='YOUR_DB_PASSWORD'
curl -o cloud-sql-proxy \ https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.0.0/cloud-sql-proxy.linux.amd64
./cloud-sql-proxy \ --credentials-file $GOOGLE_APPLICATION_CREDENTIALS \ INSTANCE_CONNECTION_NAME &
fg
command in your Terminal window where you started the sample app. This should bring the running Cloud SQL Auth Proxy job to the terminal foreground. Then press Control+C to stop the proxy job.
Run the following commands to get the required Node.js packages on to your local computer and run the sample app.
npm install
npm start
http://127.0.0.1:8080
.
Run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:
/path/to/service/account/key.json
.quickstart-user
that you created in the previous Create a user quickstart step.export GOOGLE_APPLICATION_CREDENTIALS=CREDENTIALS_JSON_FILE export INSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME' export DB_PORT='3306' export DB_NAME='quickstart_db' export DB_USER='quickstart-user' export DB_PASS='YOUR_DB_PASSWORD'
Run the following commands to get the Python sample app's requirements on to your local computer and run the sample app.
python3 -m venv env source env/bin/activate pip install -r requirements.txt
python app.py
http://127.0.0.1:8080
.
Run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:
C:\Users\Your-Username\Downloads\key.json
.quickstart-user
that you created in the previous Create a user quickstart step.$env:GOOGLE_APPLICATION_CREDENTIALS="CREDENTIALS_JSON_FILE" $env:INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME" $env:DB_PORT="3306" $env:DB_NAME="quickstart_db" $env:DB_USER="quickstart-user" $env:DB_PASS="YOUR_DB_PASSWORD"
Run the following commands to get the Go sample app's dependencies on your local computer and run the sample app.
go get ./...
go run cmd\app\main.go
http://127.0.0.1:8080
.
Run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:
C:\Users\Your-Username\Downloads\key.json
.quickstart-user
that you created in the previous Create a user quickstart step.$env:GOOGLE_APPLICATION_CREDENTIALS="CREDENTIALS_JSON_FILE" $env:INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME" $env:DB_PORT="3306" $env:DB_NAME="quickstart_db" $env:DB_USER="quickstart-user" $env:DB_PASS="YOUR_DB_PASSWORD"
Run the following command to get the Java sample app's dependencies on your local computer and run the sample app.
mvn jetty:run
http://127.0.0.1:8080
.
Run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:
C:\Users\Your-Username\Downloads\key.json
.quickstart-user
that you created in the previous Create a user quickstart step.$env:GOOGLE_APPLICATION_CREDENTIALS="CREDENTIALS_JSON_FILE" $env:INSTANCE_HOST="127.0.0.1" $env:DB_PORT="3306" $env:DB_NAME="quickstart_db" $env:DB_USER="quickstart-user" $env:DB_PASS="YOUR_DB_PASSWORD"
wget https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.0.0/cloud-sql-proxy.x64.exe -O cloud-sql-proxy.exe
Start-Process -filepath ".\cloud-sql-proxy.exe" -ArgumentList ` "--credentials-file $env:GOOGLE_APPLICATION_CREDENTIALS" INSTANCE_CONNECTION_NAME
Start-Process
command. Close the window to stop the proxy job.
Run the following commands to get the required Node.js packages on your local computer and run the sample app.
npm install
npm start
http://127.0.0.1:8080
.
Run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:
C:\Users\Your-Username\Downloads\key.json
.quickstart-user
that you created in the previous Create a user quickstart step.$env:GOOGLE_APPLICATION_CREDENTIALS="CREDENTIALS_JSON_FILE" $env:INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME" $env:DB_PORT="3306" $env:DB_NAME="quickstart_db" $env:DB_USER="quickstart-user" $env:DB_PASS="YOUR_DB_PASSWORD"
Run the following commands to get the Python sample app's requirements on your local computer and run the sample app.
virtualenv --python python3 env .\env\Scripts\activate pip install -r requirements.txt
python app.py
http://127.0.0.1:8080
.
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.If you're not using the Cloud SQL client role that you assigned to the quickstart-service-account
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