大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说MySQL日志——通用日志和慢查询日志[通俗易懂],希望您对编程的造诣更进一步.
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)
慢查询分析工具mysqldumpslow
原文地址:https://www.cnblogs.com/heyongshen/archive/2022/09/17/16702455.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/4758.html