mysql/mysql-MD/第四章:数据库日志管理.md

136 lines
3.6 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1><center>数据库日志管理</center></h1>
**作者:行癫(盗版必究)**
------
## 一:日志管理
<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220925214046253.png" alt="image-20220925214046253" style="zoom:50%;" />
#### 1.日志分类
错误日志 启动停止关闭失败报错。rpm安装日志位置 /var/log/mysqld.log
通用查询日志:所有的查询都记下来
二进制日志实现备份增量备份。只记录改变数据除了select都记
中继日志读取主服务器的binlog在本地回放。保持一致
slow log慢查询日志指导调优定义某一个查询语句定义超时时间通过日志提供调优建议给开发人员
DDL log 定义语句的日志
#### 2.Error Log
```shell
log-error=/var/log/mysqld.log
```
#### 3.Binary Log
```
log-bin=/var/log/mysql-bin/slave2
server-id=2
[root@slave2 ~]# mkdir /var/log/mysql-bin
[root@slave2 ~]# chown mysql.mysql /var/log/mysql-bin/
[root@slave2 ~]# systemctl restart mysqld
```
注意:
需要提前开启
查看binlog日志
```shel
[root@slave2 ~]# mysqlbinlog slave2-bin.000001 -v --base64-output=decode-rows
时间点 141126 14:04:49
位置点 : at 106
注:
1. 重启mysqld 会截断
2. flush logs 会截断
3. reset master 删除所有binlog rm -rf /var/lib/mysql/*.000001
4. 删除部分
PURGE BINARY LOGS TO 'mysql-bin.010';
PURGE BINARY LOGS BEFORE '2019-04-02 22:46:26';
截取binlog
all
# mysqlbinlog mysql.000002
datetime
# mysqlbinlog mysql.000002 --start-datetime="2018-12-05 10:02:56"
# mysqlbinlog mysql.000002 --stop-datetime="2018-12-05 11:02:54"
# mysqlbinlog mysql.000002 --start-datetime="2018-12-05 10:02:56" --stop-datetime="2018-12-05 11:02:54"
position
# mysqlbinlog mysql.000002 --start-position=260
# mysqlbinlog mysql.000002 --stop-position=260
# mysqlbinlog mysql.000002 --start-position=260 --stop-position=930
```
#### 4.Slow Query Log
开启慢查询日志:
```shell
[root@xingdian ~]# vim /etc/my.cnf
slow_query_log=1
slow_query_log_file=/var/log/mysql-slow/slow.log
long_query_time=3 设置慢查询超时时间 单位是:秒
```
创建对应目录:
```shell
[root@xingdian ~]# mkdir /var/log/mysql-slow
[root@xingdian ~]# chown mysql.mysql mysql-slow
```
重启服务:
```shell
[root@xingdian ~]# systemctl restart mysqld
```
验证:
```shell
[root@xingdian ~]# mysql -uroot -pQianFeng@123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select sleep(6);
+----------+
| sleep(6) |
+----------+
| 0 |
+----------+
1 row in set (6.00 sec)
mysql> exit
Bye
[root@xingdian ~]# cat /var/log/mysql-slow/slow.log
/usr/sbin/mysqld, Version: 5.7.39-log (MySQL Community Server (GPL)). started with:
Tcp port: 0 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
# Time: 2022-09-25T06:58:05.496205Z
# User@Host: root[root] @ localhost [] Id: 2
# Query_time: 6.007094 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1664089085;
select sleep(6);
```