前言
MySQL服务,同样也有很多种类日志,日志在数据库数据备份、数据恢复、排错、服务器调优,都会涉及到日志。接下来,我们讲详细介绍MySQL日志管理
本环境是基于 Centos 7.8 系统构建MySQL-5.7.14
具体构建,请参考 MySQL-5.7.14 环境构建
MySQL 日志
日志文件 | 记录文件中的信息类型 |
---|---|
错误日志 | 记录启动、运行、或停止时出现的问题 |
查询日志 | 记录建立的客户端连接和执行的语句 |
二进制日志 | 记录所有更改数据语句。主要用于复制和即时点恢复 |
慢日志 | 记录所有执行时间超过log_query_time秒的所有查询或不得使用索引的查询 |
事物日志 | 记录InnoDB等支持的存储引擎执行时产生的日志 |
一、错误日志
错误日志主要记录如下几种日志
- 服务器启动和关闭过程中的信息
- 服务器运行过程中的错误信息
- 事件调度器运行一个时间产生的信息
- 在从服务器上启动从服务器进程时产生的信息
管理错误日志
MySQL 错误日志默认情况下,存放在datadir(数据目录)目录下,
但是我们也可以手工设定日志文件存放路径
1、日志默认存放路径路径
[root@mysql-yum ~]# vim /etc/my.cnf
log-error=/var/log/mysqld.log
[root@mysql-yum ~]# systemctl restart mysqld
mysql> show global variables like '%log_error%';
+---------------------+---------------------+
| Variable_name | Value |
+---------------------+---------------------+
| binlog_error_action | ABORT_SERVER |
| log_error | /var/log/mysqld.log |
| log_error_verbosity | 3 |
+---------------------+---------------------+
3 rows in set (0.00 sec)
2、未指定日志存放路径
[root@mysql-yum ~]# vim /etc/my.cnf
log-error
[root@mysql-yum ~]# systemctl restart mysqld
mysql> show global variables like '%log_error%';
+---------------------+----------------------------+
| Variable_name | Value |
+---------------------+----------------------------+
| binlog_error_action | ABORT_SERVER |
| log_error | /var/run/mysqld/mysqld.err |
| log_error_verbosity | 3 |
+---------------------+----------------------------+
3 rows in set (0.00 sec)
3、设定存放路径
[root@mysql-yum ~]# vim /etc/my.cn
log-error=mysql-yum
[root@mysql-yum ~]# systemctl restart mysqld
mysql> show global variables like '%log_error%';
+---------------------+-----------------+
| Variable_name | Value |
+---------------------+-----------------+
| binlog_error_action | ABORT_SERVER |
| log_error | ./mysql-yum.err |
| log_error_verbosity | 3 |
+---------------------+-----------------+
3 rows in set (0.00 sec)
注:MySQL 错误日志,存放在数据目录下
二、查询日志
查询日志:记录建立的客户端连接和执行的语句
开启查询日志
[root@mysql-yum ~]# vim /etc/my.cnf
general_log=on
[root@mysql-yum ~]# systemctl restart mysqld
查看日志状态
mysql> show global variables like '%general_log%';
+------------------+------------------------------+
| Variable_name | Value |
+------------------+------------------------------+
| general_log | ON |
| general_log_file | /var/lib/mysql/mysql-yum.log |
+------------------+------------------------------+
2 rows 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> show databases;
mysql> select database();
查看日志记录
三、慢查询日志
慢日志:记录所有执行时间超过log_query_time秒的所有查询或不得使用索引的查询
作用:主要用于服务器调优
设置慢日志开启、查询时间
mysql> set global slow_query_log=on;
Query OK, 0 rows affected (0.22 sec)
mysql> set global long_query_time=3;
Query OK, 0 rows affected (0.00 sec)
# 查看日志设定
mysql> show global variables like '%slow_query_log%';
+---------------------+-----------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------+
| slow_query_log | ON |
| slow_query_log_file | /var/lib/mysql/mysql-yum-slow.log |
+---------------------+-----------------------------------+
2 rows in set (0.00 sec)
mysql> show global variables like '%long_query_time%';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 3.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
四、二进制日志
什么是二进制日志?
- 二进制日志包含了所有更新了数据或者已经潜在更新了数据(例如,没有匹配任何行的一个DELETE)的所有语句
- 语句以“事件”的形式保存,它描述数据更改。二进制日志还包含关于每个更新数据库的语句的执行时间信息。它不包含没有修改任何数据的语句。
二进制日志的的作用?
- 进制日志的主要目的是在数据库存在故障时,恢复时能够最大可能地更新数据库(即时点恢复),因为二进制日志包含备份后进行的所有更新
- 二进制日志还用于在主复制服务器上记录所有将发送给从服务器的语句
生产环境中,我们常常采:执行语句和记录执行结果方式,记录二进制日志
开启log_bin
[root@mysql-yum ~]# systemctl restart mysqld
log_bin=mysql-bin
server_id=10
[root@mysql-yum ~]# systemctl restart mysqld
查看log_bin开启状态
mysql> show global variables like '%log_bin%';
+---------------------------------+--------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------+
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/mysql-bin |
| log_bin_index | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+--------------------------------+
5 rows in set (0.00 sec)
# 查看二进制日志文件
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 154 |
+------------------+-----------+
1 row in set (0.00 sec)
# 查看二进制日志文件内容
mysql> mysql> show binlog events in 'mysql-bin.000001';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 10 | 123 | Server ver: 5.7.14-log, Binlog ver: 4 |
| mysql-bin.000001 | 123 | Previous_gtids | 10 | 154 | |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
2 rows in set (0.00 sec)
查看log_bin内容
[root@mysql-yum ~]# mysqlbinlog /var/lib/mysql/mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#210129 11:06:37 server id 10 end_log_pos 123 CRC32 0x323c980c Start: binlog v 4, server v 5.7.14-log created 210129 11:06:37 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
vXsTYA8KAAAAdwAAAHsAAAABAAQANS43LjE0LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAC9exNgEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AQyYPDI=
'/*!*/;
# at 123
#210129 11:06:37 server id 10 end_log_pos 154 CRC32 0xb271a2b4 Previous-GTIDs
# [empty]
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
主:MySQL较高版本,已经讲log_bin中的语句加密,需要解码才能查看
开启log_bin时,还需要指定server_id 编号,且该值在MySQL服务器集群中唯一
删除log_bin
# 多西刷新log_bin
mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)
# 查看所有的log_bin
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 201 |
| mysql-bin.000002 | 201 |
| mysql-bin.000003 | 201 |
| mysql-bin.000004 | 201 |
| mysql-bin.000005 | 154 |
+------------------+-----------+
5 rows in set (0.00 sec)
# 删除日志
---方法一:按照日志名字删除
mysql> purge binary logs to 'mysql-bin.000003';
Query OK, 0 rows affected (0.00 sec)
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000003 | 201 |
| mysql-bin.000004 | 201 |
| mysql-bin.000005 | 154 |
+------------------+-----------+
3 rows in set (0.00 sec)
---方法二:按照日志删除
mysql> purge binary logs before '2021-01-29 11:24:37';
Query OK, 0 rows affected (0.00 sec)
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000004 | 201 |
| mysql-bin.000005 | 154 |
+------------------+-----------+
2 rows in set (0.00 sec)
---方法三:删除所有日志,重新从第一编号开始,重新记录日志
mysql> reset master;
Query OK, 0 rows affected (0.00 sec)
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 154 |
+------------------+-----------+
1 row in set (0.01 sec)
注:二进制日志文件不能直接删除的,如果使用rm等命令直接删除日志文件,可能导致数据库的崩溃。
1、purge 命令删除
2、reset 命令删除