本文實例講述了mysql慢查詢操作。分享給大家供大家參考,具體如下:
mysql有些sql會執行很慢,有可能造成服務器負載飆升
首先查詢 確定影響負載的是mysql ,使用top命令,ps命令等
其次,進入MySQL,使用show full processlist查詢執行中的sql語句,看看問題,使用explain 命令 查看狀態
最后找出sql語句殺死或者優化
centos7上面安裝mariadb服務
yum -y install mariadb-server mariadb-devel
開啟慢查詢
more /etc/my.cnf.d/server.cnf
[mariadb]slow_query_log=ONslow_query_log_file=/usr/local/mysql/data/slow.loglong_query_time=1
啟動mariadb服務
systemctl start mariadb
查詢mysql的慢查詢是否開啟,以及多久的時間以上是慢查詢
MariaDB [(none)]> show variables like '%slow_query%';+---------------------+--------------------------------+| Variable_name | Value |+---------------------+--------------------------------+| slow_query_log | ON || slow_query_log_file | /usr/local/mysql/data/slow.log |+---------------------+--------------------------------+2 rows in set (0.00 sec)MariaDB [(none)]> show variables like 'long_query_time';+-----------------+----------+| Variable_name | Value |+-----------------+----------+| long_query_time | 1.000000 |+-----------------+----------+1 row in set (0.00 sec)
#如果沒用開啟慢查詢,可以在命令行開啟mysql> set global slow_query_log=1;Query OK, 0 rows affected (0.00 sec)
測試慢查詢,以及查看日志
MariaDB [(none)]> select sleep(2);+----------+| sleep(2) |+----------+| 0 |+----------+1 row in set (2.00 sec)
[root@localhost ~]# more /usr/local/mysql/data/slow.log/usr/libexec/mysqld, Version: 5.5.60-MariaDB (MariaDB Server). started with:Tcp port: 0 Unix socket: /var/lib/mysql/mysql.sockTime Id Command Argument# Time: 180930 23:51:07# User@Host: root[root] @ localhost []# Thread_id: 2 Schema: QC_hit: No# Query_time: 2.001017 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0SET timestamp=1538322667;select sleep(2);
確認慢查詢
MariaDB [(none)]> show full processlist; #查看state慢查詢在進行+----+------+-----------+------+---------+------+------------+-----------------------+----------+| Id | User | Host | db | Command | Time | State | Info | Progress |+----+------+-----------+------+---------+------+------------+-----------------------+----------+| 3 | root | localhost | NULL | Query | 9 | User sleep | select sleep(10) | 0.000 || 4 | root | localhost | NULL | Query | 0 | NULL | show full processlist | 0.000 |+----+------+-----------+------+---------+------+------------+-----------------------+----------+2 rows in set (0.00 sec)MariaDB [(none)]> show full processlist; #查看state慢查詢已經結束,但是用戶登陸了+----+------+-----------+------+---------+------+-------+-----------------------+----------+| Id | User | Host | db | Command | Time | State | Info | Progress |+----+------+-----------+------+---------+------+-------+-----------------------+----------+| 3 | root | localhost | NULL | Sleep | 1 | | NULL | 0.000 || 4 | root | localhost | NULL | Query | 0 | NULL | show full processlist | 0.000 |+----+------+-----------+------+---------+------+-------+-----------------------+----------+2 rows in set (0.00 sec)
新聞熱點
疑難解答