Compare the speed of the following two scripts:
my.ini is configured with
bind-address=127.0.0.1
Use localhost to connect to local MySQL: slow
<?php $start = microtime(true); $mysqli = new mysqli('127.0.0.1', 'root', '', 'mysql'); //连接耗时仅为0.0025秒. //$mysqli = new mysqli('localhost', 'root', '', 'mysql'); //连接耗时超过1秒,比正常慢了400倍. echo microtime(true) - $start;
Analysis:
1.my.ini is configured with
bind-address=127.0.0.1
, Win7 and above It takes more than 1 second for system PHP to connect to MySQL using localhost, which is 400 times slower than connecting to 127.0.0.1. When
bind-address=::1
3. Remove the bind-address configuration, and the speed of connecting to MySQL using localhost or 127.0.0.1 is normal.
bind-address=127.0.0.1
In this case, you should use 127.0.0.1 to connect to the local MySQL database.
When installing PHP programs such as WordPress and phpMyAdmin, localhost is used by default to connect to the local MySQL database. At this time, be careful to change the default localhost to 127.0.0.1.
The above introduces the solution to the slow connection of PHP to MySQL in Windows Server 2008 R2 and 2012, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.