在Windows平台上mysql_connect(localhost/127.0.0.1)缓慢 [英] mysql_connect (localhost / 127.0.0.1) slow on Windows platform

查看:286
本文介绍了在Windows平台上mysql_connect(localhost/127.0.0.1)缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows 7,Apache 2,PHP 5,MySQL 5,它们都在同一台计算机上. 我发现了一个有趣的问题,我有以下代码:

    $sql = "select * from user1";
    $conn = mysql_connect("localhost", "root", "xxxxxxxx");
    mysql_select_db("test1");
    mysql_query("set names utf8");
    $result = mysql_query($sql, $conn);
    while ($row = mysql_fetch_assoc($result)){
        foreach ($row as $key => $value){
            echo $key." => ".$value." || ";
        }
        echo "<br/>";
    }
    mysql_free_result($result);
    mysql_close($conn);

以上代码的运行时间超过1秒.

当我使用127.0.0.1而不是localhost时,运行时间约为10毫秒.

我试图在互联网上找到根本原因,这就是结果:

我最近将开发从XP迁移到Windows 7,发现我开发的网页需要花费5秒钟的时间来加载.这当然是不可接受的,所以我不得不找出问题所在. 我最终找到了令人反感的功能/方法pdo :: construct.我还发现mysql_connect大约需要1秒才能建立连接.经过一番谷歌搜索后,我发现一种解释,说明php的IPv6存在问题,您可以通过禁用IPv6或在建立连接时切换到ipaddress 127.0.0.1来解决此问题.

我想知道PHP上IPv6的问题是什么,只是想更深入地了解.谢谢.

解决方案

PHP试图打开与localhost的连接.由于您的计算机是通过IPv6连接到网络的,因此它首先尝试使用IPv6版本的"localhost",即IP地址为:: 1

http://en.wikipedia.org/wiki/IPv6_address#Special_addresses

:: 1/128 —环回地址是单播localhost地址.如果 主机中的应用程序将数据包发送到该地址,即IPv6堆栈 将这些数据包循环回同一虚拟接口上 (对应于IPv4中的127.0.0.0/8).

您的MySQL服务器似乎没有在监听该地址,而是仅绑定到IPv4地址,因此,一旦PHP无法打开连接,它将回退并尝试通过aka 127.0.0.1的IPv4打开本地主机

我个人更喜欢使用IP地址或使用Windows主机文件或等效于Mac的Mac来定义假"域名,然后在连接到解析为IP地址的MySQL时使用这些域名.无论哪种方式,我都可以确切知道将使用IPv4还是IPv6地址.

MySQL和Apache都支持IPv6,但是您必须告诉它们明确使用IPv6地址.对于MySQL,请参见: http://dev.mysql.com/doc/refman/5.5/zh-CN ipv6-server-config.html

对于Apache配置,请参阅: http://httpd.apache.org/docs/2.2/bind.html

Apache支持多个IP地址,因此,如果机器中的网卡同时具有IPv4和IPv6地址,则可以一次使用这两个IP地址. MySQL仅支持一个地址.

I am using Windows 7, Apache 2, PHP 5, MySQL 5, all are on the same machine. I have found an interesting issue, I have the following code:

    $sql = "select * from user1";
    $conn = mysql_connect("localhost", "root", "xxxxxxxx");
    mysql_select_db("test1");
    mysql_query("set names utf8");
    $result = mysql_query($sql, $conn);
    while ($row = mysql_fetch_assoc($result)){
        foreach ($row as $key => $value){
            echo $key." => ".$value." || ";
        }
        echo "<br/>";
    }
    mysql_free_result($result);
    mysql_close($conn);

The running time for the above code is over 1 second.

When I use 127.0.0.1 instead of localhost, the running time is around 10 ms.

I tried to find the underlying reason on the internet, and this is the result:

I recently moved my development from XP to Windows 7 and found that webpages I had developed were taking 5 seconds long to load. This was unacceptable of course so I had to track down the problem. I eventually tracked down the offending function/method pdo::construct. I also found that mysql_connect was taking about 1 second to make a connection. After a little googling I found an explaination that php had issues with IPv6 and that you could fix the problem by either disabling IPv6 or switching to the ipaddress 127.0.0.1 when making your connection.

I wonder what the issue of IPv6 on PHP is, just want to get a deeper understaning. Thanks.

解决方案

PHP is attempting to open a connection to localhost. Because your computer is connected to your network via IPv6 it's trying the IPv6 version of 'localhost' first, which is which is an IP address of ::1

http://en.wikipedia.org/wiki/IPv6_address#Special_addresses

::1/128 — The loopback address is a unicast localhost address. If an application in a host sends packets to this address, the IPv6 stack will loop these packets back on the same virtual interface (corresponding to 127.0.0.0/8 in IPv4).

It looks like your MySQL server isn't listening to that address, instead it's only bound to an IPv4 address and so once PHP fails to open the connection it falls back and tries to open localhost via IPv4 aka 127.0.0.1

I personally prefer to use either IP addresses or use ether the Windows hosts file or Mac equivalent to define 'fake' domain names and then use those when connecting to MySQL, which resolve to IP addresses. Either way I can know exactly whether an IPv4 or IPv6 address will be used.

Both MySQL and Apache support IPv6 but you have to tell them to use an IPv6 address explicitly. For MySQL see: http://dev.mysql.com/doc/refman/5.5/en/ipv6-server-config.html

For Apache config see: http://httpd.apache.org/docs/2.2/bind.html

Apache supports multiple IP addresses so you can use both at once - if the network card in the machine has both an IPv4 and IPv6 address. MySQL only supports one address.

这篇关于在Windows平台上mysql_connect(localhost/127.0.0.1)缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆