iT邦幫忙

2

MariaDB 開啟「慢查詢」 Log,找出到底慢在哪個 Query 語句上 (Slow query log、High CPU、MySQL、MariaDB)

網路上寫的設定檔是 /etc/mysql/my.cnf
但我實際去找是在 mariadb.conf.d 底下的 50-server.cnf 才是(可能是 Mysql 跟 MariaDB 的差別?)

$ cd /etc/mysql/mariadb.conf.d

使用 nano 開啟

$ nano 50-server.cnf

進入後會看到 [mysqld] ,直接在下面加上 slow_query 變成:

[mysqld]

# 開啟慢日誌功能
slow_query_log = 1

# 查詢時間超過 2 秒則定義為慢查詢(可自行改秒數)
long_query_time = 2

# 將產生的 slow query log 放到你指定的地方
slow_query_log_file = /var/www/slow_query.log

保存後別忘了重啟資料庫

systemctl restart mariadb.service

驗證是否成功開啟

先透過 CLI 進入 Mysql (Mariadb)

$ mysql -u root -p

進入後輸入指令
以下 MariaDB [(none)]> 簡稱為 >

> show variables like '%quer%'; 

會出現以下表格

+---------------------------------+-------------------------+
| Variable_name                   | Value                   |
+---------------------------------+-------------------------+
| expensive_subquery_limit        | 100                     |
| ft_query_expansion_limit        | 20                      |
| have_query_cache                | YES                     |
| log_queries_not_using_indexes   | OFF                     |
| long_query_time                 | 2.000000                |
| query_alloc_block_size          | 16384                   |
| query_cache_limit               | 1048576                 |
| query_cache_min_res_unit        | 4096                    |
| query_cache_size                | 16777216                |
| query_cache_strip_comments      | OFF                     |
| query_cache_type                | ON                      |
| query_cache_wlock_invalidate    | OFF                     |
| query_prealloc_size             | 24576                   |
| slow_query_log                  | ON                      |
| slow_query_log_file             | /var/www/slow_query.log |
| wsrep_reject_queries            | NONE                    |
| wsrep_sst_donor_rejects_queries | OFF                     |
+---------------------------------+-------------------------+

確認一下 Value 有無符合

slow_query_log 為 ON
long_query_time 為剛剛設定的 2 秒
slow_query_log_file 為你剛剛指定的路徑 /var/www/slow_query.log

符合表示已經設定成功,即可退出 MariaDB

> exit;

參考資料
https://ianakaberlin.medium.com/mysql-mariadb-mariadb-%E9%96%8B%E5%95%9F-%E6%85%A2%E6%9F%A5%E8%A9%A2-log-%E6%89%BE%E5%87%BA%E5%88%B0%E5%BA%95%E6%85%A2%E5%9C%A8%E5%93%AA%E5%80%8B-query-%E8%AA%9E%E5%8F%A5%E4%B8%8A-slow-query-log-high-cpu-mysql-mariadb-%E4%BC%8A%E6%81%A9%E8%B8%A9%E5%9D%91%E7%AD%86%E8%A8%98-58f408c2aa4a


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言