本教學使用環境介紹
伺服器端:Ubuntu 18.04 LTS
資料庫:Mariadb 10.1.34(Mysql)
語言版本:php 7.3
本機端:MacOS High Sierraphp
網路上寫的設定檔是 /etc/mysql/my.cnf
但我實際去找是在 mariadb.conf.d 底下的 50-server.cnf 纔是(多是 Mysql 跟 MariaDB 的差異?)html
$ cd /etc/mysql/mariadb.conf.d
使用 nano 開啓mysql
$ nano 50-server.cnf
進入後會看到 [mysqld] ,直接在下面加上 slow_query 變成:sql
[mysqld] # 開啓慢日誌功能 slow_query_log = 1 # 查詢時間超過 2 秒則定義爲慢查詢(可自行改秒數) long_query_time = 2 # 將產生的 slow query log 放到你指定的地方 slow_query_log_file = /var/www/slow_query.log
保存後別忘了重啓資料庫rest
systemctl restart mariadb.service
驗證是否成功開啓
先透過 CLI 進入 Mysql (Mariadb)日誌
$ mysql -u root -p
進入後輸入指令
如下 MariaDB [(none)]>
簡稱爲 >
code
> show variables like '%quer%';
會出現如下表格server
+---------------------------------+--------------- ----------+ | 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
有無符合htm
slow_query_log 爲 ON long_query_time 爲剛剛設定的 2 秒 slow_query_log_file 爲你剛剛指定的路徑 /var/www/slow_query.log
符合表示已經設定成功,便可退出 MariaDB
ip
> exit;
參考資料:https://www.itread01.com/p/11...
Line ID:ianmacQQ:1258554508