Home Google CloudHow to Set Up Cloud SQL for MySQL on Google Cloud

How to Set Up Cloud SQL for MySQL on Google Cloud

by Knowledge Base Linux
How to Set Up Cloud SQL — How to Set Up Cloud SQL for MySQL on Google Cloud

Running your own MySQL server means patching, backups, replication and 3 a.m. disk failures are all your problem. With cloud sql mysql, Google operates the database for you: automated backups, point-in-time recovery, optional high availability and managed updates, while you keep full MySQL compatibility. This guide walks through a complete cloud sql mysql setup, from enabling the API to connecting securely from a Compute Engine VM. This article covers How to Set Up Cloud SQL in detail.

If you are migrating an existing database, our tutorial on how to import and export databases in MySQL or MariaDB covers the dump and restore workflow, and if you are comparing engines, see how to install PostgreSQL on Ubuntu.

Prerequisites for Cloud SQL MySQL – How to Set Up Cloud SQL

  • A Google Cloud project with billing enabled
  • The gcloud CLI installed and authenticated
  • Cloud SQL Admin or Owner permissions on the project
  • A VPC network if you plan to use a private IP connection

Step 1: Enable the Cloud SQL Admin API – How to Set Up Cloud SQL

Nothing works until the API is enabled. Do it once per project:

gcloud services enable sqladmin.googleapis.com
gcloud config set project YOUR_PROJECT_ID

Step 2: Create a Cloud SQL MySQL Instance

You can create the instance in the Console under SQL, Create instance, choosing MySQL 8.0, an instance ID, a region and a machine tier. The same result with gcloud is faster and repeatable. Use db-f1-micro for a small development database and db-n1-standard-1 or a modern equivalent for light production workloads.

gcloud sql instances create my-mysql \
  --database-version=MYSQL_8_0 \
  --tier=db-n1-standard-1 \
  --region=us-central1 \
  --root-password=YOUR_STRONG_PASSWORD

Creation takes several minutes. Pick the region closest to your application servers, because every extra millisecond of database latency is multiplied by the number of queries per page load. Choosing the right tier matters too: a well-sized cloud sql mysql instance with enough RAM to hold the working set in the buffer pool will outperform a larger instance with a poorly indexed schema.

cloud sql mysql - backups and high availability illustration

Step 3: Create Databases and Users in Cloud SQL MySQL

Never let applications connect as root. Create one database and one dedicated user per application, with a strong unique password.

gcloud sql databases create appdb --instance=my-mysql

gcloud sql users create appuser \
  --instance=my-mysql \
  --password=APP_USER_PASSWORD \
  --host=%

Rotate these passwords like any other secret, store them in a secret manager rather than in code, and remove users promptly when a service is decommissioned.

Step 4: Choose How to Connect to Cloud SQL MySQL

Public IP with authorized networks

The simplest option is the instance’s public IP, restricted by an authorized networks list. Only the IPs you list can even attempt a connection. This is acceptable for development, but the list must be maintained as addresses change.

Private IP through your VPC

For production, use a private IP so traffic never leaves Google’s network. The instance receives an address inside your VPC, and your existing firewall rules control which VMs can reach port 3306.

Cloud SQL Auth Proxy

The Auth Proxy is the recommended option for laptops and external services. It creates an encrypted, identity-checked tunnel, so you do not need to whitelist IPs or manage SSL certificates yourself.

curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.14.3/cloud-sql-proxy.linux.amd64
chmod +x cloud-sql-proxy

./cloud-sql-proxy PROJECT:REGION:my-mysql &

mysql -u appuser -p --host=127.0.0.1 --port=3306 appdb

Step 5: Configure Backups and High Availability for Cloud SQL MySQL

Automated backups are your safety net, and point-in-time recovery lets you rewind the database to just before a bad deployment or an accidental delete. Enable both, and choose a retention window that matches your recovery requirements.

gcloud sql instances patch my-mysql \
  --backup-start-time=02:00 \
  --enable-point-in-time-recovery \
  --retained-backups-count=7

For production, also enable the regional high availability option. A regional instance keeps a standby in another zone in the same region and fails over automatically, which turns a zonal outage from downtime into a brief reconnect. Test your restore procedure at least once; an untested backup is a hope, not a plan. A well-run cloud sql mysql deployment also monitors connections, storage growth and slow queries, and alerts before the disk fills up.

Step 6: Connect from a Compute Engine VM

On a VM in the same VPC, install the MySQL client and connect using the private IP, or run the Auth Proxy as a service for the same encrypted tunnel you used locally.

sudo apt update && sudo apt install -y default-mysql-client
mysql -u appuser -p --host=PRIVATE_IP --port=3306 appdb

Restrict port 3306 with firewall rules so only your application servers can reach the database, even inside the VPC.

Troubleshooting Cloud SQL MySQL Connections

Connection refused

Check that the client IP is in authorized networks for public IP connections, that the Auth Proxy is actually running for proxy connections, and that the instance has finished creating or restarting.

Unknown host or timeout

Verify the connection name (project, region and instance) is copied exactly, and that a private-IP instance is being reached from a VM inside the same VPC.

SSL errors

Cloud SQL can require SSL for public connections. The Auth Proxy handles encryption automatically, which removes this whole class of error, one more reason it is the preferred method. Full details are in the official Cloud SQL for MySQL documentation.

Conclusion

A cloud sql mysql instance gives you a production-grade MySQL database without the operational burden of self-hosting: enable the API, create the instance, add least-privilege users, connect through the Auth Proxy or a private IP, and switch on backups, point-in-time recovery and high availability. Start small, monitor real usage, and scale the tier as your data grows.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.