Mysql tutorial-slow connection to local MySQL in Windows 7/8

Source: Internet
Author: User
Tags mysql tutorial domain name lookup

Mysql tutorial-slow connection to local MySQL in Windows 7/8

Recently, some programs such as phpcms had encountered this problem during local website debugging, and the speed was very slow. The following article occasionally reminded me of the problem...

I recently found an interesting problem when developing applications on windows: it takes about one second for my local program to connect to the local mysql database.

Problem Analysis

Considering that it may be a program performance problem, I used the xdebug performance log function to output the program running log and found that the function time connecting to mysql actually took nearly 95% of the execution time.

After confirming that it was a mysql performance problem, I was disappointed that the mysql Domain Name Lookup function affected the speed.

When I was puzzled, I suddenly wanted to change the server address in the code from localhost to 127.0.0.1, And the magic 1 second delay actually disappeared.

After changing to an ip address, it's okay. What's wrong with localhost? Open cmd and enter ping localhost. I was surprised by the ping output:

Yes, the reason for the delay of the second is also easy to understand. When using localhost to connect, the address parsed by the program is ipv6 address: 1, then try to connect to the mysql instance listening for this address. Obviously, mysql listens for the ipv4 address 127.0.0.1. After ipv6 fails to connect to the ipv4 address 127.0.0.1, this is the reason for the 1 second delay.

Open the windows hosts file and you will see the following comments

# Localhost name resolution is handled within DNS itself.

#127.0.0.1 localhost

#: 1 localhost

Solution

Solution 1. Use 127.0.0.1 instead of localhost to connect to mysql.

Solution 2. Edit the hosts file to forcibly bind 127.0.0.1 to localhost.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.