Solve the problem of slow remote connection to MySQL (mysql_connect slow connection)

Source: Internet
Author: User
Tags mysql host

Introduction: This is a detailed page to solve the problem of slow remote connection to MySQL (mysql_connect opens slow connection). It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 339668 'rolling = 'no'>
My colleague once proposed that the MySQL database used for development is slow because our MySQL development database is deployed on a separate machine, which may be caused by network connection problems. In progress
After Ping and route, it is found that network communication is normal, and local connection on the MySQL machine is quickly discovered, so the network problem is basically ruled out. I used to have such a problem.
Problem, but I don't know how to suddenly get better. This time I encountered such a problem, so I want to see if it is a problem with MySQL configuration. After querying MySQL related documents and network searches, a configuration is found.
It seems that this problem can be solved by adding the following configuration parameters to the MySQL configuration file:

[Mysqld]

Skip-name-resolve



In Linux, the configuration file is/etc/My. CNF, and in windows, the configuration file is the my. ini file under the MySQL installation directory. Note that this configuration is added
Under [mysqld], after changing the configuration and saving it, restart MySQL and remotely test the connection. The official explanation of this parameter is as follows: (http://www.my400800.cn)

How MySQL uses DNS

When
A new thread connects to mysqld, mysqld will spawn a new thread
Handle the request. This thread will first check if the hostname is in
The hostname cache. If not the thread will call gethostbyaddr_r () and
Gethostbyname_r () to resolve the hostname.

If
Operating System doesn' t support the above thread-safe CILS, the thread
Will lock a mutex and call gethostbyaddr () and gethostbyname () instead.
Note that in this case no other thread can resolve other hostnames that
Is not in the hostname cache until the first thread is ready.

You
Can disable DNS host Lookup by starting mysqld with-Skip-name-resolve.
In this case you can however only use IP names in the MySQL privilege
Tables.

If you have a very slow DNS and have hosts, you
Can get more performance by either disabling DNS lookop
-Skip-name-resolve or by increasing the host_cache_size define (default:
128) and recompile mysqld.

You can disable the hostname
Cache with-Skip-host-Cache. You can clear the hostname cache with flush
Hosts or mysqladmin flush-hosts.

If you don't want to allow connections over TCP/IP, you can do this by starting mysqld with-Skip-networking.



According to the instructions in the document, if your MySQL host queries DNS slowly or if there are many client hosts, the connection will be slow. Because our development machine cannot connect to the Internet, the DNS resolution is
This is not possible, so you can understand why the connection is so slow. At the same time, note that after this configuration parameter is added, the host field in the MySQL authorization table cannot use the domain name but can only use
The IP address, because this is the result of disabling domain name resolution.

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/339668.html pageno: 7.

Related Article

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.