Pages

Wednesday, April 11, 2012

Slow response from mysql server

I've been working with one of our older applications the last couple days and for some reason my test server database was very slow to respond to each operation.  Pages would take upwards of 6 seconds to generate even though the actual queries ran for less than 0.05 seconds.

I also noticed that the slow-down only happened if the application ran on a different machine than the sql server.  For example I was browsing from my machine ( 192.168.0 .10) to the Apache server on the same machine (localhost) which in turn connected to a MySQL database on another machine in the same LAN (192.168.0.99).  When I connected to Apache on  192.168.0 .99 and it used localhost to connect to mysql everything was smooth.

The solution - as I found out after several hours of Google-ing - is a little configuration option in mysql's my.cnf (or my.ini as your case may be):
skip-name-resolve
This should go in your [mysqld] section, so I just added mine at the bottom.  Here's the what the whole section looks like:
[mysqld]
user = mysql
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
skip-external-locking
skip-name-resolve
While I am not exactly clear on what is causing the slowdown, I believe it is due to the MySQL server attempting to resolve a host name for the server connecting to it if the connection comes through the network and not a local socket.  As such it wastes some time attempting to resolve 192.168.0.10 which would fail.

No comments:

Post a Comment