Mysql – PHP slow to talk to MySQL on same host

MySQLPHPubuntu-10.04

I have a VPS (OpenVZ) running Ubuntu 10.04:

  • nginx 1.0.5
  • php 5.3.5
  • mysql 5.1.41

I have installed WordPress on it and noticed in Firebug that the initial connection to index.php takes several seconds (from 4 up to 13 seconds at one time).

What makes me think it's an issue with MySQL:

  • if I install a caching plugin that bypasses the database, the lag dissapears
  • the queries themselves execute in a couple of miliseconds

I've tried addding skip-name-resolve or skip-networking to my.cnf as suggested here, with no luck.

One other thing I noticed is that the php-fpm process spikes to 100% CPU load while the page is generated.

I have tried using apache instead and even nginx in front of apache, but now the apache process hogged all the CPU.

One last thing: I run this exact same application on another VPS with similar configuration and there's no delay; index.php loads in under half a second.

Any suggestions on how I might find out what the problem is?

Best Answer

  • try to use IP, instead of hostname when connecting to your MySQL (use dig to diagnose DNS problems),
  • use High Performance MySQL Tuning Script, which will give you the idea about some problems in MySQL server,
  • use strace or tcpdump to debug your issues (here are some examples),
  • upgrade your php & mysql and check if the problem persist,