Linux
mysql執行緒吃掉整個cpu
我的 MySQL 伺服器有問題。幾個小時的一些mysql執行緒耗盡了整個處理器。殺死程序當然有幫助,但是如何跟踪程式碼在裡面執行呢?
我目前的上衣:
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ IO Command 1353 mysql 20 0 340M 70004 7652 S 31.0 1.1 1h34:28 0 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --socket 4344 mysql 20 0 340M 70004 7652 S 3.0 1.1 5:17.75 0 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --socket 5870 mysql 20 0 340M 70004 7652 S 2.0 1.1 1:13.46 0 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --socket mysql> SHOW PROCESSLIST; +------+-------+-----------+---------+---------+------+--------------+--------------- | Id | User | Host | db | Command | Time | State | Info +------+-------+-----------+---------+---------+------+--------------+---------------- | 8731 | sites | localhost | mywebsite | Sleep | 2520 | | NULL | 8734 | sites | localhost | mywebsite | Sleep | 2516 | | NULL | 8737 | sites | localhost | mywebsite | Sleep | 2508 | | NULL | 8741 | sites | localhost | mywebsite | Sleep | 2502 | | NULL ... | 9848 | root | localhost | NULL | Query | 0 | NULL | SHOW PROCESSLIST | 9952 | sites | localhost | mywebsite | Sleep | 2 | | NULL | 9953 | sites | localhost | mywebsite | Query | 2 | Sending data | SELECT user_info.name, | +------+-------+-----------+---------+---------+------+--------------+--------------------------- 150 rows in set (0.00 sec)
好吧,在殺死程序之後(它已經吃掉了整個 cpu)輸出是變化(10 分鐘後仍然沒有空程序):
mysql> SHOW PROCESSLIST; +-----+------+-----------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +-----+------+-----------+------+---------+------+-------+------------------+ | 952 | root | localhost | NULL | Query | 0 | NULL | SHOW PROCESSLIST | +-----+------+-----------+------+---------+------+-------+------------------+ 1 row in set (0.00 sec)
休眠的 MySQL 程序實際上確實會佔用 CPU。
150 個睡眠查詢很多。您是否有數百個(或更多)並發連接?如果沒有,這可能是首先要看的東西。
在您的 Web 應用程序中,確保在完成查詢後關閉 MySQL 連接。mysql_close() 在 PHP 中,但實現基於您目前的設置。