Trong thời đại “Big Data” thì các kho dữ liệu (Data Warehouse) truyền thống gặp…
How to connect to Cloud SQL from Compute Engine
For programmers to use Google Cloud (GCP), you may need to connect to a database service such as Google Cloud SQL from a virtual machine instance such as Google Compute Engine. In this article, Gimasys will explore the answer to this common question and provide you with step-by-step instructions on how to set up Connect to Cloud SQL from Compute Engine.
The basic concepts
- Google Cloud SQL is a fully managed relational database service provided by Google. It allows you to deploy, manage, and scale relational databases with ease. With Google Cloud SQL, you can choose between MySQL, PostgreSQL, and SQL Server as your preferred database engine.
- Compute Engine GCP is virtual machine (VM) infrastructure provided by Google. It allows you to create and run virtual machines on Google's infrastructure, giving you the flexibility to deploy and scale your applications as needed.
How to connect to Google Cloud SQL from Compute Engine
Now that we have a basic understanding of both Google Cloud SQL and Google Compute Engine, let's explore how to establish a connection between them.
Step 1: Set up Google Cloud SQL
First, you need to set up a Google Cloud SQL instance. Here are the steps to set it up:
- Go to Google Cloud Console and sign in to your account.
- Create a new project or select an existing project.
- In the left navigation menu, click “SQL” in the “Storage” section.
- Click the “Create instance” button.
- Select the database engine (MySQL, PostgreSQL, or SQL Server) and provide a name for your instance.
- Customize your instance settings configuration, including machine type, memory, and region.
- Set a username and password for your database.
- Click the “Create” button to create your Google Cloud SQL instance.
Step 2: Create a Compute Engine instance
Next, you need to create a Compute Engine instance to connect to your Google Cloud SQL instance. Follow these steps:
- In the left navigation menu of Google Cloud Console, click “Compute Engine” in the “Compute” section.
- Click the “Create instance” button. Provide a name for your Compute Engine instance.
- Select your desired machine configuration, including machine type, CPU, memory, and storage.
- Select the region and region for your instance.
- In the “Networking” section, select the appropriate network for your instance.
- Click the button “Create” to create your Compute Engine instance.
Step 3: Customize firewall rules
To allow your Compute Engine instance to connect to your Google Cloud SQL instance, you need to configure firewall rules as follows:
- In the left navigation menu of Google Cloud Console, click “VPC network” in the “Networking” section.
- Click “Firewall rules”.
- Click the “Create firewall rule” button.
- Provide a name for the rule and specify the network where your Compute Engine instance resides.
- Set the appropriate source IP range.
- In the “Protocols and ports” section, select “Specified protocols and ports” and enter the port number for your database engine (for example, 3306 for MySQL).
- Click the “Create” button to create a firewall rule.
Step 4: Connect to Google Cloud SQL from Compute Engine
Now that you have set up both Google Cloud SQL and Compute Engine and configured the necessary firewall rules, you can connect to your Google Cloud SQL instance from the Compute Engine instance. Follow these steps:
- SSH into your Compute Engine instance using a terminal or SSH client.
- Install the necessary database client software, such as MySQL or PostgreSQL, depending on your database engine.
- Use the appropriate database client command line tool to connect to your Google Cloud SQL instance. For example, if you are using MySQL, you can use the following command:
- mysql –host= –user= –password
- Replace with the connection name of your Google Cloud SQL instance and replace with the username you configured for your database.
- Enter the password for your database when prompted.
- You will now be connected to your Google Cloud SQL instance from the Compute Engine instance
In conclusion
In this article, we have explored the process Connect to Cloud SQL from Compute Engine. Gimasys discussed the basics of both services and provided step-by-step instructions on how to set up the connection. By following these instructions, you can establish a connection between Google Cloud SQL and Google Compute Engine, allowing you to leverage the power of managed database services in your virtual machine environment.
Gimasys - Google's Premier Partner in Vietnam is a provider and consultant on the structure and design of the optimal Cloud solution for you. For technical support, you can contact Gimasys – Premier Partner of Google in Vietnam at the following information:
- Hotline: 0974 417 099 (HCM) | 0987 682 505 (HN)
- Email: gcp@gimasys.com
Source: Gimasys