PHP& PDO:使用IPv6地址连接到MySQL [英] PHP & PDO: Connect to MySQL using IPv6 address
问题描述
我想使用其IPv6地址连接到远程MySQL实例(Google Cloud SQL实例).
I want to connect to a remote MySQL instance (a Google Cloud SQL one) by using its IPv6 address.
我正在这样使用PHP PDO:
I'm using PHP PDO like that:
$db = new \PDO('mysql:host=<ipv6-address>;port=3306;dbname=<database-name>',
'<username>',
'<password>'
);
但是它总是失败,并显示以下异常消息:
But it always fails with the following exception message:
PDOException:SQLSTATE [HY000] [2002]没有路由到主机
PDOException: SQLSTATE[HY000] [2002] No route to host
我可以从终端连接到MySQL实例,而不会出现任何问题,如下所示:
From the terminal I can connect to the MySQL instance, without any issue, like this:
mysql --host=<ipv6-address> --user=<username> --<password>
任何帮助将不胜感激.
谢谢
推荐答案
如果其他任何人遇到相同的问题,并且为了节省他们2小时的PHP源代码研究时间,请在方括号内加上方括号,PDO MySQL IPv6连接将起作用地址.
In case anyone else stumbles over the same problem, and to save them 2 hours delving through PHP source, PDO MySQL IPv6 connections work if you put square brackets around the address.
请参阅: https://github.com. com/php/php-src/blob/master/main/streams/xp_socket.c#L568
例如
$pdo = new PDO("mysql:host=[1234:5678::42];port=3306;dbname=foo", ...);
这篇关于PHP& PDO:使用IPv6地址连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!