您好,登錄后才能下訂單哦!
本文實例講述了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=ON slow_query_log_file=/usr/local/mysql/data/slow.log long_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.sock Time 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: 0 SET 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)
更多關于MySQL相關內容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數大匯總》、《MySQL日志操作技巧大全》、《MySQL事務操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數據庫鎖相關技巧匯總》
希望本文所述對大家MySQL數據庫計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。