MySQL的通用日誌: 用來記錄對資料庫的通用操作,包括錯誤的sql語句等信息。 通用日誌可以保存在:file(預設值)或 table(mysql.general_log表) mysql通用日誌的設置: general_log=ON|OFF 是否啟用通用日誌 general_log_file=HOS ...
MySQL的通用日誌:
用來記錄對資料庫的通用操作,包括錯誤的sql語句等信息。
通用日誌可以保存在:file(預設值)或 table(mysql.general_log表)
mysql通用日誌的設置:
general_log=ON|OFF --- 是否啟用通用日誌
general_log_file=HOSTNAME.log ---#通用日誌存放的文件路徑
log_output=TABLE|FILE|NONE --- 通用日誌的存放方式
範例:
#查看是否啟用通用日誌:
mysql> select @@general_log;
+---------------+
| @@general_log |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec)
mysql> show variables like "general_log";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log | OFF |
+---------------+-------+
1 row in set (0.00 sec)
general_log是一個全局變數。
#查看通用日誌的存放路徑
mysql> select @@general_log_file;
+-------------------------+
| @@general_log_file |
+-------------------------+
| /data/mysql/CentOS8.log |
+-------------------------+
1 row in set (0.00 sec)
hostname.log
#啟用通用日誌
mysql> set general_log=1;
ERROR 1229 (HY000): Variable 'general_log' is a GLOBAL variable and should be set with SET GLOBAL
mysql> set global general_log=1;
Query OK, 0 rows affected (0.00 sec)
#通用日誌預設村存放在file中
mysql> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
1 row in set (0.00 sec)
mysql> show global variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
1 row in set (0.00 sec)
#修改通用日誌的預設存放
將通用日誌存放在一張表中
mysql> set global log_output="table";
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW GLOBAL VARIABLES LIKE 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | TABLE |
+---------------+-------+
1 row in set (0.00 sec)
#改為存放到表以後,通用日誌是存放在mysql資料庫的gerneral_log這張表中的。
mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
28 rows in set (0.00 sec)
#查看general_log表
mysql> select * from general_log;
+---------------------+---------------------------+-----------+-----------+--------------+---------------------------+
| event_time | user_host | thread_id | server_id | command_type | argument |
+---------------------+---------------------------+-----------+-----------+--------------+---------------------------+
| 2022-09-17 12:41:03 | root[root] @ localhost [] | 2 | 0 | Query | select * from general_log |
+---------------------+---------------------------+-----------+-----------+--------------+---------------------------+
1 row in set (0.00 sec)
mysql> show table status like 'general_log'\G
general_log這張表就是一個CSV(文本文件)中,
文件路徑:mysql數據文件存放位置(預設/var/lib/mysql)裡面的mysql目錄中。
[root@CentOS8 mysql]# pwd
/data/mysql/mysql
[root@CentOS8 mysql]# cat general_log.CSV
"2022-09-17 12:41:03","root[root] @ localhost []",2,0,"Query","select * from general_log"
"2022-09-17 12:41:52","root[root] @ localhost []",2,0,"Query","show table status like 'general_log'"
"2022-09-17 12:41:56","root[root] @ localhost []",2,0,"Query","show table status like 'general_log'"
"2022-09-17 12:43:27","root[root] @ localhost []",2,0,"Query","select * from general_log"
"2022-09-17 12:45:57","root[root] @ localhost []",2,0,"Query","show table status like 'general_log'"
通用日誌的作用:用來觀察資料庫發生的事件。
MySQL的慢查詢日誌
記錄執行查詢時長超出指定時長的操作
慢查詢日誌的相關設置;
#預設沒有啟用慢查詢日誌
slow_query_log=ON|OFF #開啟或關閉慢查詢,支持全局和會話,只有全局設置才會生成慢查詢文件
long_query_time=N #慢查詢的閥值,單位秒,預設為10s,超過這個時間就叫慢查詢
slow_query_log_file=HOSTNAME-slow.log #記錄慢查詢日誌的文件
log_slow_filter = admin,filesort,filesort_on_disk,
full_join,full_scan,
query_cache,query_cache_miss,
tmp_table,tmp_table_on_disk
#上述查詢類型且查詢時長超過long_query_time,則記錄日誌 慢查詢記錄的相關行為
log_queries_not_using_indexes=ON #執行sql語句的時候沒有利用索引或者使用全索引掃描,不管是否超過閾值都要記錄在慢查詢日誌中。(預設OFF,即不記錄)
log_slow_rate_limit = 1 #多少次查詢才記錄,mariadb特有
log_slow_verbosity= Query_plan,explain #記錄內容
log_slow_queries = OFF #同slow_query_log,MariaDB 10.0/MySQL 5.6.1 版後已刪除
#開啟慢查詢
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@slow_query_log;
+------------------+
| @@slow_query_log |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
#修改慢查詢的預設值
mysql> set long_query_time=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@long_query_time;
+-------------------+
| @@long_query_time |
+-------------------+
| 1.000000 |
+-------------------+
1 row in set (0.00 sec)