mysql_connect (localhost / 127.0.0.1) slow on Windows platform

前端 未结 2 971
耶瑟儿~
耶瑟儿~ 2020-12-02 23:20

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 = \"selec         


        
相关标签:
2条回答
  • 2020-12-03 00:06

    PHP is trying to connect to "localhost" in Windows 7/8/10 it is ::1, but MySQL is not listening on IPv6 sockets, you can apply several fixes:

    1) In your host file (C:/windows/system32/drivers/etc/host) set localhost to 127.0.0.1

    2) In PHP the MySQL server change from localhost to 127.0.0.1

    3) In my.ini, add or edit: bind-address = ::

    If the address is ::, the server accepts TCP/IP connections on all server host IPv4 and IPv6 interfaces. Use this address to permit both IPv4 and IPv6 connections on all server interfaces.

    Suggested option if you have MySQL >= 5.5.3

    0 讨论(0)
  • 2020-12-03 00:16

    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.

    0 讨论(0)
提交回复
热议问题