{"id":15674,"date":"2023-08-21T10:57:42","date_gmt":"2023-08-21T03:57:42","guid":{"rendered":"https:\/\/gcloudvn.com\/?p=15674"},"modified":"2023-08-21T11:06:22","modified_gmt":"2023-08-21T04:06:22","slug":"cach-ket-noi-voi-cloud-sql-tu-compute-engine","status":"publish","type":"post","link":"https:\/\/gcloudvn.com\/en\/kienthuc\/cach-ket-noi-voi-cloud-sql-tu-compute-engine\/","title":{"rendered":"How to connect to Cloud SQL from Compute Engine"},"content":{"rendered":"<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">For programmers to use <a href=\"https:\/\/gcloudvn.com\/en\/google-cloud-platform\/\">Google Cloud<\/a> (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 <\/span><b>Connect to Cloud SQL from Compute Engine.<\/b><\/p>\n<p style=\"text-align: justify;\"><b><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-15676\" src=\"https:\/\/gcloudvn.com\/wp-content\/uploads\/2023\/08\/cloud_sql_from_compute_engine.jpg\" alt=\"How to connect to Cloud SQL from Compute Engine\" width=\"600\" height=\"375\" srcset=\"https:\/\/gcloudvn.com\/wp-content\/uploads\/2023\/08\/cloud_sql_from_compute_engine.jpg 600w, https:\/\/gcloudvn.com\/wp-content\/uploads\/2023\/08\/cloud_sql_from_compute_engine-18x12.jpg 18w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/b><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/gcloudvn.com\/en\/kienthuc\/cach-ket-noi-voi-cloud-sql-tu-compute-engine\/#Cac_khai_niem_co_ban\" >The basic concepts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/gcloudvn.com\/en\/kienthuc\/cach-ket-noi-voi-cloud-sql-tu-compute-engine\/#Cach_ket_noi_voi_Google_Cloud_SQL_tu_Compute_Engine\" >How to connect to Google Cloud SQL from Compute Engine<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/gcloudvn.com\/en\/kienthuc\/cach-ket-noi-voi-cloud-sql-tu-compute-engine\/#Buoc_1_Thiet_lap_Google_Cloud_SQL\" >Step 1: Set up Google Cloud SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/gcloudvn.com\/en\/kienthuc\/cach-ket-noi-voi-cloud-sql-tu-compute-engine\/#Buoc_2_Tao_mot_phien_ban_Compute_Engine\" >Step 2: Create a Compute Engine instance\u00a0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/gcloudvn.com\/en\/kienthuc\/cach-ket-noi-voi-cloud-sql-tu-compute-engine\/#Buoc_3_Tuy_chinh_quy_tac_tuong_lua\" >Step 3: Customize firewall rules\u00a0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/gcloudvn.com\/en\/kienthuc\/cach-ket-noi-voi-cloud-sql-tu-compute-engine\/#Buoc_4_Ket_noi_voi_Google_Cloud_SQL_tu_Compute_Engine\" >Step 4: Connect to Google Cloud SQL from Compute Engine\u00a0<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/gcloudvn.com\/en\/kienthuc\/cach-ket-noi-voi-cloud-sql-tu-compute-engine\/#Tong_ket\" >In conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Cac_khai_niem_co_ban\"><\/span><b>The basic concepts<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><a href=\"https:\/\/gcloudvn.com\/en\/cloud-sql\/\">Google Cloud SQL<\/a> 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.<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-15677 size-full\" src=\"https:\/\/gcloudvn.com\/wp-content\/uploads\/2023\/08\/cloud_sql_from_compute_engine_1.jpg\" alt=\"How to connect to Cloud SQL from Compute Engine 2\" width=\"600\" height=\"259\" srcset=\"https:\/\/gcloudvn.com\/wp-content\/uploads\/2023\/08\/cloud_sql_from_compute_engine_1.jpg 600w, https:\/\/gcloudvn.com\/wp-content\/uploads\/2023\/08\/cloud_sql_from_compute_engine_1-18x8.jpg 18w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><a href=\"https:\/\/gcloudvn.com\/en\/compute-engine\/\"><span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Compute Engine GCP&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:14593,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;11&quot;:4,&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;,&quot;16&quot;:12}\">Compute Engine GCP<\/span><\/a> is virtual machine (VM) infrastructure provided by Google. It allows you to create and run virtual machines on Google&#039;s infrastructure, giving you the flexibility to deploy and scale your applications as needed.<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-15678\" src=\"https:\/\/gcloudvn.com\/wp-content\/uploads\/2023\/08\/cloud_sql_from_compute_engine_2.jpg\" alt=\"How to connect to Cloud SQL from Compute Engine 3\" width=\"600\" height=\"315\" srcset=\"https:\/\/gcloudvn.com\/wp-content\/uploads\/2023\/08\/cloud_sql_from_compute_engine_2.jpg 602w, https:\/\/gcloudvn.com\/wp-content\/uploads\/2023\/08\/cloud_sql_from_compute_engine_2-18x9.jpg 18w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Cach_ket_noi_voi_Google_Cloud_SQL_tu_Compute_Engine\"><\/span><b>How to connect to Google Cloud SQL from Compute Engine<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Now that we have a basic understanding of both Google Cloud SQL and Google Compute Engine, let&#039;s explore how to establish a connection between them.\u00a0<\/span><\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Buoc_1_Thiet_lap_Google_Cloud_SQL\"><\/span><b>Step 1: Set up Google Cloud SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">First, you need to set up a Google Cloud SQL instance. Here are the steps to set it up:\u00a0<\/span><\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Go to Google Cloud Console and sign in to your account.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Create a new project or select an existing project.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In the left navigation menu, click \u201cSQL\u201d in the \u201cStorage\u201d section.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Click the \u201cCreate instance\u201d button.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Select the database engine (MySQL, PostgreSQL, or SQL Server) and provide a name for your instance.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Customize your instance settings configuration, including machine type, memory, and region.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Set a username and password for your database.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Click the \u201cCreate\u201d button to create your Google Cloud SQL instance.<\/span><\/li>\n<\/ul>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Buoc_2_Tao_mot_phien_ban_Compute_Engine\"><\/span><b>Step 2: Create a Compute Engine instance\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Next, you need to create a Compute Engine instance to connect to your Google Cloud SQL instance. Follow these steps:\u00a0<\/span><\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In the left navigation menu of Google Cloud Console, click \u201cCompute Engine\u201d in the \u201cCompute\u201d section.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Click the \u201cCreate instance\u201d button. Provide a name for your Compute Engine instance.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Select your desired machine configuration, including machine type, CPU, memory, and storage.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Select the region and region for your instance.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In the \u201cNetworking\u201d section, select the appropriate network for your instance.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Click the button \u201c<\/span><span style=\"font-weight: 400;\">Create<\/span><span style=\"font-weight: 400;\">\u201d to create your Compute Engine instance.<\/span><\/li>\n<\/ul>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Buoc_3_Tuy_chinh_quy_tac_tuong_lua\"><\/span><b>Step 3: Customize firewall rules\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">To allow your Compute Engine instance to connect to your Google Cloud SQL instance, you need to configure firewall rules as follows:\u00a0<\/span><\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In the left navigation menu of Google Cloud Console, click \u201cVPC network\u201d in the \u201cNetworking\u201d section.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Click \u201cFirewall rules\u201d.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Click the \u201cCreate firewall rule\u201d button.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Provide a name for the rule and specify the network where your Compute Engine instance resides.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Set the appropriate source IP range.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In the \u201cProtocols and ports\u201d section, select \u201cSpecified protocols and ports\u201d and enter the port number for your database engine (for example, 3306 for MySQL).\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Click the \u201cCreate\u201d button to create a firewall rule.<\/span><\/li>\n<\/ul>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Buoc_4_Ket_noi_voi_Google_Cloud_SQL_tu_Compute_Engine\"><\/span><b>Step 4: Connect to Google Cloud SQL from Compute Engine\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">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:\u00a0<\/span><\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">SSH into your Compute Engine instance using a terminal or SSH client.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Install the necessary database client software, such as MySQL or PostgreSQL, depending on your database engine.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">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:\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">mysql \u2013host= \u2013user= \u2013password\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Replace with the connection name of your Google Cloud SQL instance and replace with the username you configured for your database.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Enter the password for your database when prompted.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">You will now be connected to your Google Cloud SQL instance from the Compute Engine instance<\/span><\/li>\n<\/ul>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Tong_ket\"><\/span><b>In conclusion<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In this article, we have explored the process <\/span><b>Connect to Cloud SQL from Compute Engine<\/b><span style=\"font-weight: 400;\">. 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.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Gimasys - Google&#039;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 \u2013 Premier Partner of Google in Vietnam at the following information:<\/span><\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Hotline: <\/b><span style=\"font-weight: 400;\">0974 417 099 (HCM) | 0987 682 505 (HN)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Email: <\/b><span style=\"font-weight: 400;\">gcp@gimasys.com<\/span><\/li>\n<\/ul>\n<p style=\"text-align: right;\"><b>Source: Gimasys<\/b><\/p>","protected":false},"excerpt":{"rendered":"<p>\u0110\u1ed1i v\u1edbi nh\u1eefng nh\u00e0 l\u1eadp tr\u00ecnh s\u1eed d\u1ee5ng Google Cloud (GCP), b\u1ea1n c\u00f3 th\u1ec3 c\u1ea7n k\u1ebft n\u1ed1i v\u1edbi m\u1ed9t d\u1ecbch v\u1ee5 c\u01a1 s\u1edf d\u1eef li\u1ec7u nh\u01b0 Google Cloud SQL t\u1eeb m\u1ed9t phi\u00ean b\u1ea3n m\u00e1y \u1ea3o nh\u01b0 Google Compute Engine. Trong&hellip;<\/p>","protected":false},"author":2,"featured_media":15676,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1,135],"tags":[],"class_list":["post-15674","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kienthuc","category-google-cloud-platform","entry","has-media"],"_links":{"self":[{"href":"https:\/\/gcloudvn.com\/en\/wp-json\/wp\/v2\/posts\/15674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gcloudvn.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gcloudvn.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gcloudvn.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/gcloudvn.com\/en\/wp-json\/wp\/v2\/comments?post=15674"}],"version-history":[{"count":0,"href":"https:\/\/gcloudvn.com\/en\/wp-json\/wp\/v2\/posts\/15674\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gcloudvn.com\/en\/wp-json\/wp\/v2\/media\/15676"}],"wp:attachment":[{"href":"https:\/\/gcloudvn.com\/en\/wp-json\/wp\/v2\/media?parent=15674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gcloudvn.com\/en\/wp-json\/wp\/v2\/categories?post=15674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gcloudvn.com\/en\/wp-json\/wp\/v2\/tags?post=15674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}