Introduction

Cloud SQL is a fully-managed relational database service for MySQL, PostgreSQL, and SQL Server. It allows you to set up, maintain, manage, and administer your relational databases on Google Cloud Platform (GCP). This service is designed to handle the heavy lifting of database management, including backups, replication, and scaling, so you can focus on your applications.

Key Concepts

  1. Fully Managed Service: Cloud SQL automates database management tasks such as backups, patch management, and failover.
  2. Scalability: Easily scale your database's compute and storage resources with minimal downtime.
  3. Security: Integrated with Google Cloud's security features, including encryption at rest and in transit, and IAM for access control.
  4. High Availability: Built-in high availability with automatic failover.
  5. Compatibility: Supports MySQL, PostgreSQL, and SQL Server.

Setting Up Cloud SQL

Step-by-Step Guide

  1. Create a Cloud SQL Instance:

    • Go to the GCP Console.
    • Navigate to the SQL section.
    • Click on "Create Instance".
    • Choose the database engine (MySQL, PostgreSQL, or SQL Server).
    • Configure the instance settings (name, region, zone, machine type, storage, etc.).
    • Click "Create".
  2. Configure Database Settings:

    • Set up the root password.
    • Configure network settings (authorized networks, private IP, etc.).
    • Set up automated backups and maintenance windows.
  3. Connect to Your Cloud SQL Instance:

    • Use the Cloud SQL Proxy, SSL/TLS, or authorized networks to connect securely.
    • Example using Cloud SQL Proxy:
      ./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306
      
    • Connect using a client tool like mysql:
      mysql -u root -p -h 127.0.0.1
      

Example: Creating a MySQL Database

-- Connect to the MySQL instance
mysql -u root -p -h <INSTANCE_IP>

-- Create a new database
CREATE DATABASE my_database;

-- Create a new user and grant privileges
CREATE USER 'my_user'@'%' IDENTIFIED BY 'my_password';
GRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'%';
FLUSH PRIVILEGES;

Practical Exercises

Exercise 1: Create and Configure a Cloud SQL Instance

  1. Objective: Create a Cloud SQL instance and configure it for a MySQL database.
  2. Steps:
    • Follow the step-by-step guide to create a Cloud SQL instance.
    • Configure the instance with a root password and authorized networks.
    • Create a new database and user as shown in the example.

Exercise 2: Connect to Cloud SQL from a Compute Engine Instance

  1. Objective: Connect to your Cloud SQL instance from a Compute Engine instance.
  2. Steps:
    • Create a Compute Engine instance.
    • Install the Cloud SQL Proxy on the Compute Engine instance.
    • Use the Cloud SQL Proxy to connect to your Cloud SQL instance.
    • Verify the connection by listing the databases.

Solution for Exercise 1

  1. Create a Cloud SQL Instance:

    • Navigate to the SQL section in the GCP Console.
    • Click "Create Instance" and select MySQL.
    • Configure the instance settings and click "Create".
  2. Configure Database Settings:

    • Set the root password.
    • Configure authorized networks to allow your IP address.
  3. Create Database and User:

    CREATE DATABASE my_database;
    CREATE USER 'my_user'@'%' IDENTIFIED BY 'my_password';
    GRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'%';
    FLUSH PRIVILEGES;
    

Solution for Exercise 2

  1. Create a Compute Engine Instance:

    • Navigate to the Compute Engine section in the GCP Console.
    • Click "Create Instance" and configure the settings.
    • SSH into the instance.
  2. Install Cloud SQL Proxy:

    wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
    chmod +x cloud_sql_proxy
    
  3. Connect to Cloud SQL:

    ./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306
    mysql -u root -p -h 127.0.0.1
    

Common Mistakes and Tips

  • Firewall Rules: Ensure that your firewall rules allow traffic to and from your Cloud SQL instance.
  • Authorized Networks: Double-check that your IP address is included in the authorized networks.
  • Instance Connection Name: Use the correct instance connection name when using the Cloud SQL Proxy.

Conclusion

In this section, you learned about Cloud SQL, a fully-managed relational database service on GCP. You explored how to create and configure a Cloud SQL instance, connect to it, and perform basic database operations. The practical exercises provided hands-on experience to reinforce the concepts. In the next module, you will dive into other core GCP services, starting with App Engine.

© Copyright 2024. All rights reserved