一路走来,所有遇到的人,帮助过我的、伤害过我的都是朋友,没有一个是敌人,如有侵权请留言,我及时删除。
一、mysql相关日志
首先,我们能接触到的,一般我们排查慢查询时,会去看慢查询日志。如果做过数据备份会恢复的,可能接触或用过BinLog。那还有其他的吗?对MySQL原理比较了解的,应该知道还有Redo Log和Undo Log。这些都是比较常见:慢查询日志、Binlog、Redo Log、Undo Log。
其实,MySQL中还有:错误日志(errorlog)、一般查询日志(general log)、中继日志(relay log),只是这些大家接触少点。
总结下,MySQL中一共七种日志,分别为:慢查询日志、Binlog、Redo Log、Undo Log、错误日志(errorlog)、一般查询日志(general log)、中继日志(relay log)。

1、错误日志 log_error

mysql> show variables like '%log_error%';

2、二进制日志 BINLOG

mysql> show variables like '%bin_log%';

2.1、使用mysqlbinlog 查看MySQL默认的二进制格式

mysql binlog参数详解_mysqlbinlog参数-CSDN博客
mysql> show variables like '%binlog_format%';

使用mysqlbinlog 查看MySQL binlog ROW格式的二进制日志
示例:创建一张销售表,并在binlog日志中找到SQL执行记录
CREATE TABLE sales (
id INT PRIMARY KEY AUTO_INCREMENT,
product VARCHAR(50),
quantity INT,
price DECIMAL(10,2)
);
INSERT INTO sales (product, quantity, price) VALUES ('产品A', 10, 9.99);
INSERT INTO sales (product, quantity, price) VALUES ('产品B', 5, 4.99);
INSERT INTO sales (product, quantity, price) VALUES ('产品C', 8, 6.75);
[root@mysql8 data]# mysqlbinlog -v binlog.000007
2.2、使用mysqlbinlog 查看MySQL binlog STATEMENT格式的二进制日志
配置文件/etc/my.cnf 新增 binlog_format=STATEMENT 重启MySQL
vi /etc/my.cnf
binlog_format=STATEMENT

插入一条语句,并在binlog日志中查看执行的SQL
INSERT INTO sales (product, quantity, price) VALUES ('??D', 9, 8.75)

2.3、清理MySQL-binlog日志文件

示例一:删除binlog.000003之前的日志
mysql> purge master logs to 'binlog.000003';


2.4、MySQL数据设置binlog日志过期时间
mysql> show variables like '%binlog_expire_logs%';

3、MySQL 查询日志

3.1、查询日志默认状态
mysql> show variables like '%general%';

3.2、开启MySQL查询日志,修改my.cnf文件,并重启MySQL服务
vi /etc/my.cnf
---新增如下配置-------
general_log=1
general_log_file=/mysql/data/mysql8.log
[root@mysql8 data]# systemctl restart mysqld
查看日志状态
mysql> show variables like '%general%';

3.3、查询日志记录的信息



4、慢日志

4.1、查看慢日志的默认配置

4.2、开启慢日志,并修改慢SQL记录时间,重启MySQL生效
[root@mysql8 data]# vi /etc/my.cnf
----新增配置-----
slow_query_log=1
long_query_time=1
[root@mysql8 data]# systemctl restart mysqld

4.3、准备测试环境,创建一个1千万数据的test 表

【MySQL】生成1000万条测试数据_生成1000w条mysql数据-CSDN博客
