目录
一、 日志分类
1-1 错误日志
1-2 通用查询日志
1-3 二进制日志
1-4 中继日志
1-5 slow log
1-6 DDL log
二、Error Log
2-1进入主配置文件,观察日志是否启动
2-2 日志启动信息 以及位置
三、 Binary Log
3-1 配置文件
3-2 添加配置信息
3-3 测试
四、 Slow Query Log
4-1 开启慢查询日志功能
4-2 查询慢查询日志
4-3 模拟慢查询
4-4 验证慢查询日志
一、 日志分类
1-1 错误日志
启动,停止,关闭失败报错。rpm安装日志位置 /var/log/mysqld.log
1-2 通用查询日志
所有的查询都记下来。
1-3 二进制日志
实现备份,增量备份。只记录改变数据,除了select都记。 默认关闭
1-4 中继日志
读取主服务器的binlog,在本地回放。保持一致。
1-5 slow log
慢查询日志,指导调优,定义某一个查询语句,定义超时时间,通过日志提供调优建议给开发人员。
1-6 DDL log
定义语句的日志
二、Error Log
2-1进入主配置文件,观察日志是否启动
vim /etc/my.cnf
2-2 日志启动信息 以及位置
log-error=/var/log/mysqld.log
tail /var/log/mysqld.log
2022-05-10 13:46:12 6763 [Note] InnoDB: Waiting for purge to start
2022-05-10 13:46:12 6763 [Note] InnoDB: 5.6.51 started; log sequence number 1635160
2022-05-10 13:46:12 6763 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work.
2022-05-10 13:46:12 6763 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
2022-05-10 13:46:12 6763 [Note] Server hostname (bind-address): '*'; port: 3306
三、 Binary Log
3-1 配置文件
vim /etc/my.cnf
3-2 添加配置信息
vim /etc/my.cnf
2-2 日志启动信息 以及位置
log-error=/var/log/mysqld.log
tail /var/log/mysqld.log
2022-05-10 13:46:12 6763 [Note] InnoDB: Waiting for purge to start
2022-05-10 13:46:12 6763 [Note] InnoDB: 5.6.51 started; log sequence number 1635160
2022-05-10 13:46:12 6763 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work.
2022-05-10 13:46:12 6763 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
2022-05-10 13:46:12 6763 [Note] Server hostname (bind-address): '*'; port: 3306
三、 Binary Log
3-1 配置文件
vim /etc/my.cnf
3-2 添加配置信息
3-1 配置文件
vim /etc/my.cnf
3-2 添加配置信息
文件搜索/log_bin 并log_bin 添加该字段
并重启查询
vim /etc/my.cnf [root@localhost ~]# ls /var/lib/mysql auto.cnf company ibdata1 ib_logfile0 ib_logfile1 localhost.localdomain.pid mysql mysql.sock performance_schema systemctl restart mysql [root@localhost ~]# ls /var/lib/mysql
3-3-2 进入数据库,进行数据操作
创库 创表 创数据
mysql> create table yifan.0011111111111111111(id int); Query OK, 0 rows affected (0.01 sec) ysql> insert into yifan.0011111111111111111 values(1); Query OK, 1 row affected (0.01 sec) mysql> insert into yifan.0011111111111111111 values(2); Query OK, 1 row affected (0.01 sec) mysql> insert into yifan.0011111111111111111 values(3); Query OK, 1 row affected (0.00 sec) mysql> select *from 0011111111111111111; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0011111111111111111' at line 1 mysql> select *from yifan.0011111111111111111; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec)
3-3-3 完成二进制文件日志启动,查询测试
mysqlbinlog -v /var/lib/mysql/localhost-bin.000001
mysqlbinlog -v /var/lib/mysql/localhost-bin.000001 ; ; ; DELIMITER ; # at 4 #220510 20:28:51 server id 1 end_log_pos 120 CRC32 0x3b924082 Start: binlog v 4, server v 5.6.51-log created 220510 20:28:51 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK; BINLOG ' g1p6Yg8BAAAAdAAAAHgAAAABAAQANS42LjUxLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAACDWnpiEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAYJA kjs= '; # at 120 #220510 20:31:45 server id 1 end_log_pos 217 CRC32 0x00138988 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1652185905; SET @@session.pseudo_thread_id=2; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1; SET @@session.sql_mode=1075838976; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1; ; SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8; SET @@session.lc_time_names=0; SET @@session.collation_database=DEFAULT; create database yifan ; # at 217 #220510 20:35:55 server id 1 end_log_pos 334 CRC32 0xa6f386ea Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1652186155; create table yifan.0011111111111111111(id int) ; # at 334 #220510 20:39:38 server id 1 end_log_pos 410 CRC32 0xc2c7de40 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1652186378; BEGIN ; # at 410 #220510 20:39:38 server id 1 end_log_pos 528 CRC32 0xdf114845 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1652186378; insert into yifan.0011111111111111111 values(1) ; # at 528 #220510 20:39:38 server id 1 end_log_pos 559 CRC32 0xc5d1520e Xid = 26 COMMIT;
四、 Slow Query Log
默认慢查询未开起
4-1 开启慢查询日志功能
进入主配置文件 /etc/my.cnf
slog_query_log=1
这个参数设置为ON,可以捕获执行时间超过一定数值的SQL语句。
long_query_time=3
//当SQL语句执行时间超过此数值时,就会被记录到日志中,建议设置为1或者更短。//
重启msyqld
4-2 查询慢查询日志
ll /var/lib/mysql
4-3 模拟慢查询
mysql>SELECT BENCHMARK(500000000,2*3);
4-4 验证慢查询日志
#tail /var/lib/mysql/localhost-slow.log
//
mysql>SELECT BENCHMARK(500000000,2*3);