2

On my linux machine there was a script which was accessing mysql withing a while(true) loop. It has caused my cpu usage to 99%-100%. But I couldn't find exactly what script was doing this. Following was the script code.

while (true) {
    $con = mysql_connect("localhost","root","root");
    mysql_select_db("test_db", $con);

    $result = mysql_query("SELECT * FROM hs_hr_employee");
    mysql_close($con);
}

I tried to identify the exact process through the top command. The result was as bellow. enter image description here

Even though this shows mysqld is taking 100% pf CPU usage. this didn't showed me what was the exact script which was causing this issue.

Then I tried ps command to find out any hint on this.

ps -aux | grep mysqld

The output was as follows. enter image description here

It also didn't gave any hint about this running php script. In this kind of scenario how we can troubleshoot exactly to identify the trouble making scripts and commands. Please help me on this.

Thanks in advance.

1
  • 1
    Well, mysqld is the process that's using the most CPU. With a command like ps aux --forest (or, more prettily, pstree), you can visualise processes in a tree structure, so you can easily see parentage.
    – Cairnarvon
    Feb 23, 2013 at 22:02

2 Answers 2

1

Tell mysql server to log queries that takes more than X seconds to complete.

Mysql Slow Query Log

2
  • Absolutely - and use "explain" to show how the request is broken down. The OP's problem is not the process consuming the resources - which is correctly identified as MySQL, rather the way the process is doing its calculations.
    – davidgo
    Feb 23, 2013 at 22:37
  • If the OP can identify the query, then he can identify the script that generate it and try to fix the query or the way the script build the query itself Feb 24, 2013 at 9:02
0
sudo apt-get install powertop
sudo powertop

Standard package is version 1.x and gives percentage CPU usage of every process in cool list, newer versions like 2.x show power consumption YAY (only for Intel chips :/)

You must log in to answer this question.