Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP & PDO: Connect to MySQL using IPv6 address

Tags:

php

mysql

pdo

ipv6

I want to connect to a remote MySQL instance (a Google Cloud SQL one) by using its IPv6 address.

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] No route to host

From the terminal I can connect to the MySQL instance, without any issue, like this:

mysql --host=<ipv6-address> --user=<username> --<password>

Any help will be really appreciated.

Thanks

like image 449
Johann Fradj Avatar asked May 22 '15 13:05

Johann Fradj


People also ask

What is PHP is used for?

PHP is an open-source server-side scripting language that many devs use for web development. It is also a general-purpose language that you can use to make lots of projects, including Graphical User Interfaces (GUIs).

Is PHP a coding?

Learn PHP. PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.

Which is better Python or PHP?

Python is better than PHP in long term project. PHP has low learning curve, it is easy to get started with PHP. Compare to PHP Python has lower number of Frameworks. Popular ones are DJango, Flask.

Is PHP coding or programming?

PHP is an open-source, server-side programming language that can be used to create websites, applications, customer relationship management systems and more. It is a widely-used general-purpose language that can be embedded into HTML.


2 Answers

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.

See: https://github.com/php/php-src/blob/master/main/streams/xp_socket.c#L568

e.g.

$pdo = new PDO("mysql:host=[1234:5678::42];port=3306;dbname=foo", ...);
like image 78
Paul Clark Avatar answered Nov 03 '22 07:11

Paul Clark


Reading this https://www.saotn.org/php-mysql-and-ipv6-still-slow/ gives the following idea:

Knowing that, normally, IPv6 takes precedence over IPv4 (which is configurable), users are left with a slow responding website and database operations, only because connecting to an IPv6 address in PHP is refused, and the connection refused isn’t handled correctly, making the fallback to IPv4 slow. It takes mysql.connect_timeout seconds

Note: the source does seem credible

Also, this is a good read: http://dev.mysql.com/worklog/task/?id=798

Support should be added for MySQL to work over IPv6
("Internet Protocol Version 6").
This means:
- users can connect with IPv6. this is partly a connector problem.
- storage of user address information, e.g. in mysql.user, can be in IPv6 format
- the proposed new data types CIDR and INET allow IPv6 format as described in WL#2037 "Add CIDR and INET data types"
- functions like inet_ntoa() need revision

like image 4
Alex Tartan Avatar answered Nov 03 '22 07:11

Alex Tartan