Implementing Cloud SQL
Implementing Cloud SQL
SQL
1 hour 30 minutesFree
Rate Lab
Overview
In this lab, you configure a Cloud SQL server and learn how to connect an
application to it via a proxy over an external connection. You also configure a
connection over a Private IP link that offers performance and security benefits.
The app we chose to demonstrate in this lab is Wordpress, but the information
and best practices are applicable to any application that needs SQL Server.
By the end of this lab, you will have 2 working instances of the Wordpress
frontend connected over 2 different connection types to their SQL instance
backend, as shown in this diagram:
Objectives
In this lab, you learn how to perform the following tasks:
Qwiklabs setup
For each lab, you get a new GCP project and set of resources for a fixed time at
no cost.
2. Note the lab's access time (for example, and make sure
you can finish in that time block.
There is no pause feature. You can restart if needed, but you have to start at the
beginning.
Instance ID wordpress-db
Region us-central1
Zone Any
Shared-core machines are good for prototyping, and are not covered by Cloud SLA.
Each vCPU is subject to a 250 MB/s network throughput cap for peak performance. Each
additional core increases the network cap, up to a theoretical maximum of 2000 MB/s.
For performance-sensitive workloads such as online transaction processing (OLTP), a general
guideline is to ensure that your instance has enough memory to contain the entire working set and
accommodate the number of active connections.
11. For this lab, select db-n1-standard-1, and then click Select.
12. Next, choose Storage type and Storage capacity.
A few points to consider:
SSD (solid-state drive) is the best choice for most use cases. HDD (hard-disk drive) offers lower
performance, but storage costs are significantly reduced, so HDD may be preferable for storing
data that is infrequently accessed and does not require very low latency.
There is a direct relationship between the storage capacity and its throughput.
13. Add a few zeros to the storage capacity to see how it affects the
throughput. Reset the slider to 10GB.
Setting your storage capacity too low without enabling an automatic storage increase can cause
your instance to lose its SLA.
14. Click Close.
15. Click Create at the bottom of the page to create the database
instance.
You might have to wait for the Private IP changes to propagate before the Create button becomes
clickable.
Click Check my progress to verify the objective.
Create a Cloud SQL instance
Check my progress
In order to configure the proxy, you need the Cloud SQL instance connection
name.
The lab comes with 2 virtual machines preconfigured with Wordpress and its dependencies. You can
view the startup script and service account access by clicking on a virtual machine name. Notice that
we used the principle of least privilege and only allow SQL access for that VM. There's also a
network tag and a firewall preconfigured to allow port 80 from any host.
1. On the Navigation menu ( ) click Compute Engine.
2. Click SSH next to wordpress-europe-proxy.
3. Download the Cloud SQL Proxy and make it executable:
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O
cloud_sql_proxy && chmod +x cloud_sql_proxy
In order to start the proxy, you need the connection name of the Cloud SQL
instance. Keep your SSH window open and return to the Cloud Console.
export SQL_CONNECTION=[SQL_CONNECTION_NAME]
10. To verify that the environment variable is set, run:
echo $SQL_CONNECTION
The connection name should be printed out.
11. To activate the proxy connection to your Cloud SQL database and
send the process to the background, run the following command:
Property Value
Username root
Password [ROOT_PASSWORD]
By using Private IP, you will increase performance by reducing latency and
minimize the attack surface of your Cloud SQL instance because you can
communicate with it exclusively over internal IPs.
Property Value
Username root
type the [ROOT_PASSWORD] configured when the Cloud SQL instance was
Password
created
Database
[SQL_PRIVATE_IP]
Host
8. Click Submit.
Notice that this time you are creating a direct connection to a Private IP, instead of configuring a
proxy. That connection is private, which means that it doesn't egress to the internet and therefore
benefits from better performance and security.
9. Click Run the installation. An 'Already Installed!' window is displayed,
which means that your application is connected to the Cloud SQL server
over private IP.
10. In your web browser's address bar, remove the text after the IP
address and press ENTER. You'll be presented with a working Wordpress
Blog!
Task 5: Review
In this lab, you created a Cloud SQL database and configured it to use both an
external connection over a secure proxy and a Private IP address, which is more
secure and performant. Remember that you can only connect via Private IP if the
application and the Cloud SQL server are collocated in the same region and are
part of the same VPC network. If your application is hosted in another region,
VPC, or even project, use a proxy to secure its connection over the external
connection.
End your lab
When you have completed your lab, click End Lab. Qwiklabs removes the
resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable
number of stars, type a comment, and then click Submit.
The number of stars indicates the following: