PDOException'与消息'SQLSTATE [HY000] [2002]连接超时'当试图连接到谷歌云sql

Hie members
so I was trying to connect to Google cloud sql from an external site

I authorized the ip of the server in google cloud at Instance -> Access Control -> Authorization

and the instance also has been assigned an IP lets say 'X.X.X.X', and a user also has been created with User name 'username' and password 'password'

now the connection code for connecting google cloud sql I used is as follows

$db = new pdo('mysql:host=X.X.X.X;dbname=db','username','password');

and I tried opening the page and I am getting the following error

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Connection timed out' in /home/kannadakavana/public_html/test.php:17 Stack trace: #0 /home/kannadakavana/public_html/test.php(17): PDO->__construct('mysql:host=X....', 'username', '') #1 {main} thrown in /home/kannadakavana/public_html/test.php on line 17

what am I doing wrong ?

douren9077
douren9077 你是如何解决这个问题的?
接近 2 年之前 回复
dqyz48470
dqyz48470 我尝试授权我的本地系统ip并运行脚本,它的工作原理!所以这里的问题实际上是防火墙谢谢你的支持:)
接近 5 年之前 回复

2个回答

By default Cloud SQL instances does not have IPv4 enable.

Did you enable IPv4 address? It is under Access Control > IP address tab.

dpcyx08288
dpcyx08288 实际上问题解决了我用我的系统的本地IP授权工作!! 并且它是我的服务器的防火墙,不让服务器连接到云sql感谢您的支持无论如何先生:)
接近 5 年之前 回复
dongze8698
dongze8698 它使用默认的MySQL端口号 - 您不需要这样做。 当你说mysql:host =“assigned ip”时,我假设它类似于mysql:host =“10.0.0.1”(另一个有效的IP地址)。 你可以使用命令行mysql连接吗?
接近 5 年之前 回复
dongyun7897
dongyun7897 我们必须提供任何端口号吗?
接近 5 年之前 回复
duanchuli5647
duanchuli5647 是的先生我确实启用了IPV4地址,我将它用作mysql:host =“assigned ip”我做得对吗?
接近 5 年之前 回复



如果CloudSQL实例是按需实例,则有时会在实例旋转时首次尝试连接超时。< / p>

如果这是问题所以解决方案是一个永远在线的实例,或者(最好)一个自动连接重试与退避。</ p>
</ div>

展开原文

原文

If the CloudSQL instance is an on-demand instance, you will sometime get a connection timeout on first attempt as the instance spins up.

If that is the problem then the solution is either an always on instance or (preferably) an automated connection retry with back-off.

dtrovwl75780
dtrovwl75780 抱歉先生,我没有让这部分自动连接重试,如果这意味着重新加载页面,我确实尝试每5分钟重新加载页面一小时
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问