一:错误日志
1 错误日志配置
默认是开启的,默认目录在/数据路径下/hostname.err
手动定义目录位置:
[root@db02-52 data]# cat /etc/my.cnf
[mysqld]
........
log_error=/var/log/mysql.log #注意这个目录对mysql用户一定要有读写的权利,不然重启mysql服务器会失败
log_timestamps=system
.......
修改/var/log目录的权限,让mysql用户对其有读写权利
[root@db02-52 data]# chmod -R 777 /var/log/
重启mysql服务器
[root@db02-52 data]# systemctl restart mysqld.service
查看日志目录是否生效
mysql> select @@log_error;
+--------------------+
| @@log_error |
+--------------------+
| /var/log/mysql.log |
+--------------------+
二:binlog二进制日志
2.1 binlog的作用
(1)备份恢复必须依赖二进制日志
(2)主从环境必须依赖二进制日志
2.2 binlog配置
2.2.1 创建日志目录
[root@db02-52 ~] mkdir /data/binlog
[root@db02-52 ~] chown -R mysql.mysql /data/binlog
2.2.2 修改配置文件
[root@db02-52 data]# cat /etc/my.cnf
[mysqld]
....
server_id=10 # 5.6版本中,单机可以不需要此参数
log_bin=/data/binlog/mysql-bin #开启binlog日志功能,并定义binlog名称前缀
binlog_format=row #binlog的记录格式
....
2.2.3 命令查看binlog相关信息
开关:
[(none)]>select @@log_bin;
日志路径及名字
[(none)]>select @@log_bin_basename;
服务ID号:
[(none)]>select @@server_id;
二进制日志格式:
[(none)]>select @@binlog_format;
双一标准之二:
[(none)]>select @@sync_binlog;
2.3 binlog详解
2.3.1 binlog记录的是什么
binlog是SQL层的功能,记录的是变更SQL语句,不记录查询语句
2.3.2 DML三种记录方式
statement(5.6默认) SBR:语句模式原封不动的记录当前DML
ROW(5.7默认) RBR:记录数据行的变化
mixed(混合) MBR:以上两种模式的混合
SBR和RBR的区别
statement:可读性较高,日志量少,但是不够严谨
row:可读性很低,日志量大,足够严谨
为什么row模式严谨
当insert into t1 values(1,'xs',now()),now()记录的时间是当时的录入数据库的是时间,当使用SBR的时候,记录的是SQL语句,恢复的时候,now()时间就会变成恢复数据的时间,所以使用RBR会严谨一点
建议使用row记录模式
2.4 event事件
2.4.1 什么是event
二进制日志的最小记录单元
对应DDL,DCL一个语句就说一个event
对应DML语句来说,只记录已提交的事务
2.4.2 event的组成
查看event
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.26-log, Binlog ver: 4 |
| mysql-bin.000001 | 123 | Previous_gtids | 10 | 154 | |
| mysql-bin.000001 | 154 | Anonymous_Gtid | 10 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 219 | Query | 10 | 293 | BEGIN |
| mysql-bin.000001 | 293 | Table_map | 10 | 340 | table_id: 108 (binlog.t1) |
| mysql-bin.000001 | 340 | Write_rows | 10 | 390 | table_id: 108 flags: STMT_END_F |
| mysql-bin.000001 | 390 | Xid | 10 | 421 | COMMIT /* xid=11 */ |
| mysql-bin.000001 | 421 | Anonymous_Gtid | 10 | 486 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 486 | Query | 10 | 584 | drop database binlog |
+------------------+-----+----------------+-----------+-------------+----------------------------------------+
Log_name:binlog文件名
Pos:开始的position
Event_type:事件类型
Server_id:mysql服务号标识
End_log_pos:事件的结束位置号
Info:事件内容
2.5 binlog详情命令
查看binlog是否开启以及日志文件目录
mysql> show variables like '%log_bin%';
+---------------------------------+------------------------------+
| Variable_name | Value |
+---------------------------------+------------------------------+
| log_bin | ON |
| log_bin_basename | /data/binlog/mysql-bin |
| log_bin_index | /data/binlog/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+------------------------------+
6 rows in set (0.01 sec)
查看一共有多少个binlog
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 1714 |
+------------------+-----------+
1 row in set (0.00 sec)
查看mysql正在使用的日志文件
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1714 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
查看binlog日志文件内容
mysqlbinlog /data/mysql/mysql-bin.000001
2.6 使用position号进行日志截图
一定要在event事件里面找准截取的起点和终点
测试案例:
开启binlog之后,创建一个binlog库,然后创建2个表t1,t2,在t1,t2表里随便插入几行数据
现在删除binlog库,通过binlog日志恢复数据
mysql> drop database binlog;
恢复操作:
1.查询现在使用binlog
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 1076 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2.查看event事件
mysql> show binlog events in 'mysql-bin.000002';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000002 | 4 | Format_desc | 10 | 123 | Server ver: 5.7.26-log, Binlog ver: 4 |
| mysql-bin.000002 | 123 | Previous_gtids | 10 | 154 | |
| mysql-bin.000002 | 154 | Anonymous_Gtid | 10 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000002 | 219 | Query | 10 | 320 | use `binlog`; create table t2(id int) |
| mysql-bin.000002 | 320 | Anonymous_Gtid | 10 | 385 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000002 | 385 | Query | 10 | 459 | BEGIN |
| mysql-bin.000002 | 459 | Table_map | 10 | 506 | table_id: 109 (binlog.t2) |
| mysql-bin.000002 | 506 | Write_rows | 10 | 556 | table_id: 109 flags: STMT_END_F |
| mysql-bin.000002 | 556 | Xid | 10 | 587 | COMMIT /* xid=6 */ |
| mysql-bin.000002 | 587 | Anonymous_Gtid | 10 | 652 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000002 | 652 | Query | 10 | 750 | drop database binlog |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
3.截取begin创建t2表插入数据到commit这段事件的数据
--start-position=219 --stop-position=587
4.截取创建binlog数据库和t1表的这段事件的起点和终点时间
mysql> show binlog events in 'mysql-bin.000001';
+------------------+------+----------------+-----------+-------------+----------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+----------------+-----------+-------------+----------------------------------------+
| mysql-bin.000001 | 649 | Query | 10 | 765 | create database binlog charset utf8mb4 |
| mysql-bin.000001 | 765 | Anonymous_Gtid | 10 | 830 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 830 | Query | 10 | 931 | use `binlog`; create table t1(id int) |
| mysql-bin.000001 | 931 | Anonymous_Gtid | 10 | 996 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 996 | Query | 10 | 1070 | BEGIN |
| mysql-bin.000001 | 1070 | Table_map | 10 | 1117 | table_id: 110 (binlog.t1) |
| mysql-bin.000001 | 1117 | Write_rows | 10 | 1172 | table_id: 110 flags: STMT_END_F |
| mysql-bin.000001 | 1172 | Xid | 10 | 1203 | COMMIT /* xid=30 */ |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
--start-position=649 --stop-position=1203
5.恢复数据操作
[root@db02-52 binlog]# mysqlbinlog --start-position=649 --stop-position=1203 /data/binlog/mysql-bin.000001 >/tmp/bin1.sql
[root@db02-52 binlog]# mysqlbinlog --start-position=219 --stop-position=587 /data/binlog/mysql-bin.000001 >/tmp/bin2.sql
mysql> set sql_log_bin=0;
mysql> source /tmp/bin1.sql
mysql> source /tmp/bin2.sql
mysql> set sql_log_bin=1;
三:binlog日志的GTID新特性
5.6版本新加的特性,5.7中做了加强
5.6中不开启,没有这个功能
5.7中的GTID,即使不开也会有自动生成
GTID是对于一个已提交事务的编号,并且是一个全局唯一的编号
3.1 开启GTID
[root@db02-52 ~]# cat /etc/my.cnf
[mysqld]
.............
gtid-mode=on
enforce-gtid-consistency=true
.............
主要语法参数
--include-gtids 包含的gtid号
--exclude-gtids 排除gtid号
3.2 使用GTID进行binlog恢复
1.创建了一个binlog库,并且创建t1,t2,t3,t4四个表,在不同的binlog日志里面,删除binlog库,通过GTID进行恢复
2.截取删库之前的GTID号
mysql> show binlog events in 'mysql-bin.000004';
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| mysql-bin.000004 | 4 | Format_desc | 10 | 123 | Server ver: 5.7.26-log, Binlog ver: 4 |
| mysql-bin.000004 | 123 | Previous_gtids | 10 | 194 | 2499b92c-9818-11eb-9b9a-000c29248a30:1-7 |
| mysql-bin.000004 | 194 | Gtid | 10 | 259 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:8' |
| mysql-bin.000004 | 259 | Query | 10 | 359 | use `gtid1`; create table t4 (id int) |
| mysql-bin.000004 | 359 | Gtid | 10 | 424 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:9' |
| mysql-bin.000004 | 424 | Query | 10 | 497 | BEGIN |
| mysql-bin.000004 | 497 | Table_map | 10 | 543 | table_id: 111 (gtid1.t4) |
| mysql-bin.000004 | 543 | Write_rows | 10 | 583 | table_id: 111 flags: STMT_END_F |
| mysql-bin.000004 | 583 | Xid | 10 | 614 | COMMIT /* xid=25 */ |
| mysql-bin.000004 | 614 | Gtid | 10 | 679 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:10' |
| mysql-bin.000004 | 679 | Query | 10 | 752 | BEGIN |
| mysql-bin.000004 | 752 | Table_map | 10 | 798 | table_id: 111 (gtid1.t4) |
| mysql-bin.000004 | 798 | Write_rows | 10 | 838 | table_id: 111 flags: STMT_END_F |
| mysql-bin.000004 | 838 | Xid | 10 | 869 | COMMIT /* xid=27 */ |
| mysql-bin.000004 | 869 | Gtid | 10 | 934 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:11' |
| mysql-bin.000004 | 934 | Query | 10 | 1007 | BEGIN |
| mysql-bin.000004 | 1007 | Table_map | 10 | 1053 | table_id: 111 (gtid1.t4) |
| mysql-bin.000004 | 1053 | Write_rows | 10 | 1093 | table_id: 111 flags: STMT_END_F |
| mysql-bin.000004 | 1093 | Xid | 10 | 1124 | COMMIT /* xid=28 */ |
| mysql-bin.000004 | 1124 | Gtid | 10 | 1189 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:12' |
| mysql-bin.000004 | 1189 | Query | 10 | 1284 | drop database gtid1
删库的GTID号是2499b92c-9818-11eb-9b9a-000c29248a30:12,那如果要恢复肯定是截取它前一个GTID号,那就说2499b92c-9818-11eb-9b9a-000c29248a30:11
3.截取创建库的GTID号
mysql> mysql> show binlog events in 'mysql-bin.000003';
+------------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
| mysql-bin.000003 | 4 | Format_desc | 10 | 123 | Server ver: 5.7.26-log, Binlog ver: 4 |
| mysql-bin.000003 | 123 | Previous_gtids | 10 | 154 | |
| mysql-bin.000003 | 154 | Gtid | 10 | 219 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:1' |
| mysql-bin.000003 | 219 | Query | 10 | 332 | create database gtid1 charset utf8mb4 |
| mysql-bin.000003 | 332 | Gtid | 10 | 397 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:2' |
| mysql-bin.000003 | 397 | Query | 10 | 497 | use `gtid1`; create table t1 (id int) |
| mysql-bin.000003 | 497 | Gtid | 10 | 562 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:3' |
| mysql-bin.000003 | 562 | Query | 10 | 662 | use `gtid1`; create table t2 (id int) |
| mysql-bin.000003 | 662 | Gtid | 10 | 727 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:4' |
| mysql-bin.000003 | 727 | Query | 10 | 827 | use `gtid1`; create table t3 (id int) |
| mysql-bin.000003 | 827 | Gtid | 10 | 892 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:5' |
| mysql-bin.000003 | 892 | Query | 10 | 965 | BEGIN |
| mysql-bin.000003 | 965 | Table_map | 10 | 1011 | table_id: 108 (gtid1.t1) |
| mysql-bin.000003 | 1011 | Write_rows | 10 | 1051 | table_id: 108 flags: STMT_END_F |
| mysql-bin.000003 | 1051 | Xid | 10 | 1082 | COMMIT /* xid=17 */ |
| mysql-bin.000003 | 1082 | Gtid | 10 | 1147 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:6' |
| mysql-bin.000003 | 1147 | Query | 10 | 1220 | BEGIN |
| mysql-bin.000003 | 1220 | Table_map | 10 | 1266 | table_id: 109 (gtid1.t2) |
| mysql-bin.000003 | 1266 | Write_rows | 10 | 1306 | table_id: 109 flags: STMT_END_F |
| mysql-bin.000003 | 1306 | Xid | 10 | 1337 | COMMIT /* xid=19 */ |
| mysql-bin.000003 | 1337 | Gtid | 10 | 1402 | SET @@SESSION.GTID_NEXT= '2499b92c-9818-11eb-9b9a-000c29248a30:7' |
| mysql-bin.000003 | 1402 | Query | 10 | 1475 | BEGIN |
| mysql-bin.000003 | 1475 | Table_map | 10 | 1521 | table_id: 110 (gtid1.t3) |
| mysql-bin.000003 | 1521 | Write_rows | 10 | 1561 | table_id: 110 flags: STMT_END_F |
| mysql-bin.000003 | 1561 | Xid | 10 | 1592 | COMMIT /* xid=20 */ |
| mysql-bin.000003 | 1592 | Rotate | 10 | 1639 | mysql-bin.000004;pos=4 |
+------------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
创建库的GTID号是2499b92c-9818-11eb-9b9a-000c29248a30:1
4.恢复数据
[root@db02-52 ~]# cd /data/binlog/
[root@db02-52 binlog]# mysqlbinlog --include-gtids='2499b92c-9818-11eb-9b9a-000c29248a30:1-11' mysql-bin.000003 mysql-bin.000004 >/tmp/gtid.sql
mysql> set sql_log_bin=0;
mysql> source /tmp/gtid.sql
5.查看数据,发现恢复失败
因为GTID的幂等性,重复GTID事务不会在执行,所以需要加--skip-gtids这个参数
[root@db02-52 binlog]# mysqlbinlog --skip-gtids --include-gtids='2499b92c-9818-11eb-9b9a-000c29248a30:1-11' mysql-bin.000003 mysql-bin.000004 >/tmp/gtid.sql
四:binlog日志的其他操作
4.1 自动清理日志
1.默认没有开启自动清理
mysql> show variables like '%expire%';
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| expire_logs_days | 0 |
+--------------------------------+-------+
2.企业建议,至少保留两个全备周期+1的binlog,开启binlog保留时长为15天
[root@db02-52 binlog]# cat /etc/my.cnf
[mysqld]
..........
expire_logs_days=15
..........
4.2手工清理
#清理到某个binlog日志
mysql> purge binary logs to 'mysql-bin.000001';
#清除所有binlog日志,谨慎使用,主从关系里,如果主库执行这个操作,主从环境必崩
mysql> reset master;
4.3 日志的滚动
1.使用命令重新产生一个binlog
msql> flush logs;
2.重启mysql服务也会产生一个binlog
3.系统默认日志达到1G时,也会滚动一个新的
mysql> select @@max_binlog_size;
+-------------------+
| @@max_binlog_size |
+-------------------+
| 1073741824 |
+-------------------+
1 row in set (0.00 sec)