简体   繁体   English

如何使用php连接到谷歌云SQL实例?

[英]How to connect to Google cloud SQL instance using php?

With the following code am able to connect to my SQL instance from localhost environment , but when i upload the same code to my VM instance and try to connect it from there am getting "connection failed: connection timed out"使用以下代码可以从本地主机环境连接到我的 SQL 实例,但是当我将相同的代码上传到我的 VM 实例并尝试从那里连接它时,我收到“连接失败:连接超时”

Note: I added VM instance's External IP address in Authorized network section.注意:我在授权网络部分添加了虚拟机实例的外部 IP 地址。

<?php
$servername = "SQL INSTANCE IP ADDRESS : port";
$username = "root";
$password = "password";
$dbname="appdb";

$response=array();

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

Please help, Thanks.请帮忙,谢谢。

If you have your PHP outside of GCP (compute VM, App Engine, etc) You can either Install the cloud proxy in your app server and your app would connect like if the DB was in localhost, or you can use the public IP settings (I would suggest using the Proxy this way you don't have to use attach a public IP to your SQL instance)如果您在 GCP 之外拥有 PHP(计算 VM、App Engine 等),您可以在应用服务器中安装云代理,并且您的应用将连接,就像数据库位于 localhost 中一样,或者您可以使用公共 IP 设置(我建议以这种方式使用代理,您不必使用将公共 IP 附加到您的 SQL 实例)

If the PHP app is hosted in GCP you may follow the steps for the service where your app is如果 PHP 应用程序托管在 GCP 中,您可以按照应用程序所在服务的步骤进行操作

How to connect from Compute Engine to Cloud SQL internal ip address (PHP)如何从 Compute Engine 连接到 Cloud SQL 内部 IP 地址 (PHP)

Your Compute Engine instance must be in the same region as your Cloud SQL instance, and on the network configured for a private connection您的 Compute Engine 实例必须与您的 Cloud SQL 实例位于同一区域,并且位于为专用连接配置的网络上

1. Create the Cloud SQL instance with internal ip address . 1. 使用内部 ip 地址创建 Cloud SQL实例。 Create a user, password and database.创建用户、密码和数据库。

2. Getting started with PHP on Compute Engine 2. Compute Engine 上的 PHP 入门

After you copied the startup-script.sh runt this command to create the instance:复制startup-script.sh后,运行此命令以创建实例:

 gcloud compute instances create $MY_INSTANCE_NAME \
--image-family=ubuntu-1804-lts \
--image-project=ubuntu-os-cloud \
--machine-type=g1-small \
--scopes userinfo-email,cloud-platform,sql-admin  \
--metadata-from-file startup-script=scripts/startup-script.sh \
--zone $ZONE \
--tags http-server

On the scopes we added sql-admin , then create the firewall rule.在我们添加的范围上sql-admin ,然后创建防火墙规则。

3.SSH to the instance you just created. 3.SSH 到您刚刚创建的实例。

4.Install mysql . 4.安装mysql You could add this part to the startup script.您可以将此部分添加到启动脚本中。

 sudo apt install mysql-client-core-5.7

5.Test if you can connect to SQL instance using internal ip address. 5.测试是否可以使用内部IP地址连接到SQL实例。

mysql --host=internall-ip-sql  --user=test --password

6.If you are able to connect then : 6.如果您能够连接,则:

cd /opt/app/routes
sudo nano web.php

7.Add your code to router->get('/', function (Request $request) 7.将你的代码添加到router->get('/', function (Request $request)

8.In the browser go to http://compute-engine-instance-externall-ip 8.在浏览器中访问http://compute-engine-instance-externall-ip

Success!成功!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM