PDOException' with message 'SQLSTATE[HY000] [2002] Connection timed out' when trying to connect to google cloud sql

。_饼干妹妹 提交于 2019-12-12 06:26:25

问题


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 ?


回答1:


By default Cloud SQL instances does not have IPv4 enable.

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




回答2:


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.



来源:https://stackoverflow.com/questions/33522035/pdoexception-with-message-sqlstatehy000-2002-connection-timed-out-when-tr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!