更新 数据库日志管理.md

This commit is contained in:
wxin 2025-03-12 15:46:57 +08:00
parent b1c49e3a1f
commit 808c553ac3

View File

@ -1,124 +1,124 @@
<h2><center>数据库日志管理</center></h2> <h2><center>数据库日志管理</center></h2>
------ ------
## 一:日志管理 ## 一:日志管理
![](accents\images-202503110014.png) ![](http://182.92.143.66:40072/directlink/img/mysql/images-202503110014.png)
### 1. 日志分类 ### 1. 日志分类
- error log错误日志启动停止关闭失败报错。rpm安装日志位置 /var/log/mysqld.log - error log错误日志启动停止关闭失败报错。rpm安装日志位置 /var/log/mysqld.log
- 通用查询日志:所有的查询都记下来 - 通用查询日志:所有的查询都记下来
- bin log二进制日志实现备份增量备份。只记录改变数据除了select都记 - bin log二进制日志实现备份增量备份。只记录改变数据除了select都记
- relay log中继日志读取主服务器的binlog在本地回放。保持一致 - relay log中继日志读取主服务器的binlog在本地回放。保持一致
- slow log慢查询日志指导调优定义某一个查询语句定义超时时间通过日志提供调优建议给开发人员 - slow log慢查询日志指导调优定义某一个查询语句定义超时时间通过日志提供调优建议给开发人员
- DDL log 定义语句的日志 - DDL log 定义语句的日志
### 2. Error log ### 2. Error log
```shell ```shell
log-error=/var/log/mysqld.log log-error=/var/log/mysqld.log
``` ```
### 3. Binary Log ### 3. Binary Log
产生binlog日志修改配置文件,添加如下配置 产生binlog日志修改配置文件,添加如下配置
```bash ```bash
log-bin=/var/log/mysql-bin/slave2 log-bin=/var/log/mysql-bin/slave2
server-id=2 server-id=2
[root@mysql ~]# mkdir /var/log/mysql-bin [root@mysql ~]# mkdir /var/log/mysql-bin
[root@mysql ~]# chown mysql.mysql /var/log/mysql-bin/ [root@mysql ~]# chown mysql.mysql /var/log/mysql-bin/
[root@mysql ~]# systemctl restart mysqld [root@mysql ~]# systemctl restart mysqld
``` ```
注: 注:
```sql ```sql
1. 重启mysqld 会截断旧日志产生新的日志 1. 重启mysqld 会截断旧日志产生新的日志
2. 刷新日志会截断旧日志产生新的日志 2. 刷新日志会截断旧日志产生新的日志
mysql> flush logs mysql> flush logs
3. 删除所有binlog禁用 3. 删除所有binlog禁用
mysql> reset master mysql> reset master
4. 删除部分日志 4. 删除部分日志
mysql> PURGE BINARY LOGS TO 'mysql-bin.010'; mysql> PURGE BINARY LOGS TO 'mysql-bin.010';
mysql> PURGE BINARY LOGS BEFORE '2016-04-02 22:46:26'; mysql> PURGE BINARY LOGS BEFORE '2016-04-02 22:46:26';
5. 暂停binlog日志功能仅对当前会话生效 5. 暂停binlog日志功能仅对当前会话生效
mysql> SET SQL_LOG_BIN=0; mysql> SET SQL_LOG_BIN=0;
mysql> SET SQL_LOG_BIN=1; mysql> SET SQL_LOG_BIN=1;
``` ```
**读取binlog日志** **读取binlog日志**
```bash ```bash
mysqlbinlog mysql.000001 mysqlbinlog mysql.000001
``` ```
**按datetime读取** **按datetime读取**
```bash ```bash
# mysqlbinlog mysql.000001 --start-datetime="2018-12-05 10:02:56" # mysqlbinlog mysql.000001 --start-datetime="2018-12-05 10:02:56"
# mysqlbinlog mysql.000001 --stop-datetime="2018-12-05 11:02:54" # mysqlbinlog mysql.000001 --stop-datetime="2018-12-05 11:02:54"
# mysqlbinlog mysql.000001 --start-datetime="2018-12-05 10:02:56" --stop-datetime="2018-12-05 11:02:54" # mysqlbinlog mysql.000001 --start-datetime="2018-12-05 10:02:56" --stop-datetime="2018-12-05 11:02:54"
``` ```
**按position读取** **按position读取**
```bash ```bash
# mysqlbinlog mysql.000001 --start-position=260 # mysqlbinlog mysql.000001 --start-position=260
# mysqlbinlog mysql.000001 --stop-position=260 # mysqlbinlog mysql.000001 --stop-position=260
# mysqlbinlog mysql.000001 --start-position=260 --stop-position=930 # mysqlbinlog mysql.000001 --start-position=260 --stop-position=930
``` ```
### 4. Slow Query Log ### 4. Slow Query Log
开启慢查询日志: 开启慢查询日志:
```bash ```bash
[root@mysql ~]# vim /etc/my.cnf [root@mysql ~]# vim /etc/my.cnf
slow_query_log=1 slow_query_log=1
slow_query_log_file=/var/log/mysql-slow/slow.log slow_query_log_file=/var/log/mysql-slow/slow.log
long_query_time=3 设置慢查询超时时间 单位是:秒 long_query_time=3 设置慢查询超时时间 单位是:秒
``` ```
创建对应目录: 创建对应目录:
```bash ```bash
[root@mysql ~]# mkdir /var/log/mysql-slow [root@mysql ~]# mkdir /var/log/mysql-slow
[root@mysql ~]# chown mysql.mysql /var/log/mysql-slow/ [root@mysql ~]# chown mysql.mysql /var/log/mysql-slow/
``` ```
重启服务: 重启服务:
```bash ```bash
[root@mysql ~]# systemctl restart mysqld [root@mysql ~]# systemctl restart mysqld
``` ```
验证: 验证:
```SQL ```SQL
[root@mysql ~]# mysql -uroot -p'Wdjsi@313' [root@mysql ~]# mysql -uroot -p'Wdjsi@313'
mysql> select sleep(6); mysql> select sleep(6);
+----------+ +----------+
| sleep(6) | | sleep(6) |
+----------+ +----------+
| 0 | | 0 |
+----------+ +----------+
1 row in set (6.00 sec) 1 row in set (6.00 sec)
mysql> exit mysql> exit
Bye Bye
[root@mysql ~]# cat /var/log/mysql-slow/slow.log [root@mysql ~]# cat /var/log/mysql-slow/slow.log
/usr/sbin/mysqld, Version: 5.7.30-log (MySQL Community Server (GPL)). started with: /usr/sbin/mysqld, Version: 5.7.30-log (MySQL Community Server (GPL)). started with:
Tcp port: 0 Unix socket: /var/lib/mysql/mysql.sock Tcp port: 0 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument Time Id Command Argument
# Time: 2025-03-11T11:20:18.734540Z # Time: 2025-03-11T11:20:18.734540Z
# User@Host: root[root] @ localhost [] Id: 2 # User@Host: root[root] @ localhost [] Id: 2
# Query_time: 6.001048 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0 # Query_time: 6.001048 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1741692018; SET timestamp=1741692018;
select sleep(6); select sleep(6);
``` ```