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
- Fully Managed Service: Cloud SQL automates database management tasks such as backups, patch management, and failover.
- Scalability: Easily scale your database's compute and storage resources with minimal downtime.
- Security: Integrated with Google Cloud's security features, including encryption at rest and in transit, and IAM for access control.
- High Availability: Built-in high availability with automatic failover.
- Compatibility: Supports MySQL, PostgreSQL, and SQL Server.
Setting Up Cloud SQL
Step-by-Step Guide
-
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".
-
Configure Database Settings:
- Set up the root password.
- Configure network settings (authorized networks, private IP, etc.).
- Set up automated backups and maintenance windows.
-
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
- Objective: Create a Cloud SQL instance and configure it for a MySQL database.
- 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
- Objective: Connect to your Cloud SQL instance from a Compute Engine instance.
- 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
-
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".
-
Configure Database Settings:
- Set the root password.
- Configure authorized networks to allow your IP address.
-
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
-
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.
-
Install Cloud SQL Proxy:
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy chmod +x cloud_sql_proxy
-
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.
Google Cloud Platform (GCP) Course
Module 1: Introduction to Google Cloud Platform
- What is Google Cloud Platform?
- Setting Up Your GCP Account
- GCP Console Overview
- Understanding Projects and Billing
Module 2: Core GCP Services
Module 3: Networking and Security
Module 4: Data and Analytics
Module 5: Machine Learning and AI
Module 6: DevOps and Monitoring
- Cloud Build
- Cloud Source Repositories
- Cloud Functions
- Stackdriver Monitoring
- Cloud Deployment Manager
Module 7: Advanced GCP Topics
- Hybrid and Multi-Cloud with Anthos
- Serverless Computing with Cloud Run
- Advanced Networking
- Security Best Practices
- Cost Management and Optimization