From 808c553ac3f3d79278643fb782ae0df6759818ac Mon Sep 17 00:00:00 2001 From: wxin <15253413025@163.com> Date: Wed, 12 Mar 2025 15:46:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E6=97=A5=E5=BF=97=E7=AE=A1=E7=90=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 数据库日志管理.md | 248 +++++++++++++++++++++++----------------------- 1 file changed, 124 insertions(+), 124 deletions(-) diff --git a/数据库日志管理.md b/数据库日志管理.md index 39c0896..c991a10 100644 --- a/数据库日志管理.md +++ b/数据库日志管理.md @@ -1,124 +1,124 @@ -

数据库日志管理

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

数据库日志管理

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