Home >Backend Development >PHP Tutorial >Solution to the slow PHP connection to MySQL in Windows Server 2008 R2 and 2012

Solution to the slow PHP connection to MySQL in Windows Server 2008 R2 and 2012

WBOY
WBOYOriginal
2016-07-28 08:27:171252browse

Compare the speed of the following two scripts:

  • Windows 7 uses localhost to connect to local MySQL, the speed will be very slow.
  • Windows 7 uses 127.0.0.1 to connect to local MySQL, and the speed is normal.

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

is configured in 2.my.ini, the speed of PHP using localhost to connect to MySQL in Win7 and above versions It is normal, but connecting with 127.0.0.1 is not normal.

3. Remove the bind-address configuration, and the speed of connecting to MySQL using localhost or 127.0.0.1 is normal.

So: after configuring

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.

In addition, Windows 2008, 2012 and Windows 7 have the same problem.

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn