go to bug id or search bugs for
(tested on PHP 7.0.15-0ubuntu0.16.04.4 but verified from git master source)
Attempting to connect to an IPv6 MySQL server using its literal IPv6 address fails unless you put square brackets [ ] around the address in the hostname.
Square brackets are required in the URL form if there is a port - e.g.
but are not normally used for hostnames by themselves.
Although putting [ ] around the hostname makes it work - e.g.
$pdo = new PDO("mysql:host=[1234:5678::42];port=3306;dbname=foo", ...);
this is not expected, or documented.
The problem arises because the mysqlnd driver reconstructs a stream URL with the hostname and port:
transport.l = mnd_sprintf(&transport.s, 0, "tcp://%s:%u", hostname.s, port);
( https://github.com/php/php-src/blob/master/ext/mysqlnd/mysqlnd_connection.c#L581 )
This does not take into account that the hostname could already contain colons in an IPv6 address. The IP address parser ( https://github.com/php/php-src/blob/master/main/streams/xp_socket.c#L573 ) has code to work this out if there are square brackets around the IPv6 address, but fails otherwise. This is why putting square brackets around the hostname in the PDO string works.
The fix would be to check for ':' in the hostname and surround it with [ ] if present - e.g.
if (strchr(hostname.s, ':'))
transport.l = mnd_sprintf(&transport.s, 0, "tcp://[%s]:%u",
transport.l = mnd_sprintf(&transport.s, 0, "tcp://%s:%u",
(or something like that, PHP-style C is not my language, I'm afraid!)
Add a Patch
Add a Pull Request
Putting s around IPv6 addresses is standard.
Square brackets are only standard in URLs (as per RFC2732), not where hostnames stand alone as in the PDO string. Having to put them in the hostname field is a workround (which could be usefully documented, agreed!).