日志类型 | 说明 |
---|---|
慢查询日志 | 记录所有执行时间超过long_query_time的所有查询,方便我们对查询进行优化。 |
事务日志 | 包括 redo 日志和 undo 日志。redo log 称为 重做日志,提供再写入操作,恢复提交事务修改的页操作,用来保证事务的持久性;undo log 称为 回滚日志,回滚行记录到某个特定版本,用来保证事务的原子性、一致性。 |
通用查询日志 | 记录所有连接的起始时间和终止时间,以及连接发送给数据库服务器的所有指令,对我们复原操作的实际场景、发现问题,甚至是对数据库操作的审计都有很大的帮助。 |
错误日志 | 记录MySQL服务的启动、运行或停止MySQL服务时出现的问题,方便我们了解服务器的状态,从而对服务器进行维护。 |
二进制日志 | 记录所有更改数据的语句,可以用于主从服务器之间的数据同步,以及服务器遇到故障时数据的无损失恢复。 |
中继日志 | 用于主从服务器架构中,从服务器用来存放主服务器二进制日志内容的一个中间文件。从服务器通过读取中继日志的内容,来同步主服务器上的操作。 |
数据定义语句日志 | 记录数据定义语句执行的元数据操作。 |
mysql> show variables like '%general%';
+------------------+---------------------------------+
| Variable_name | Value |
+------------------+---------------------------------+
| general_log | OFF | # 通用查询日志处于关闭状态
| general_log_file | /var/lib/mysql/5157698acdb3.log | # 通用查询日志文件的名称
+------------------+---------------------------------+
2 rows in set (0.01 sec)
mysql> set global general_log = on;
Query OK, 0 rows affected (0.11 sec)
mysql>
root@5157698acdb3:/var/lib/mysql# more 5157698acdb3.log
/usr/sbin/mysqld, Version: 8.0.27 (MySQL Community Server - GPL). started with:
Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock
Time Id Command Argument
2022-04-03T08:12:30.884376Z 59 Query show variables like '%general%'
2022-04-03T08:14:49.776896Z 59 Query SELECT DATABASE()
2022-04-03T08:14:49.777140Z 59 Init DB test
2022-04-03T08:14:52.839826Z 59 Query show tables
2022-04-03T08:14:58.829493Z 59 Query select * from student
2022-04-03T08:15:19.392348Z 59 Query insert into student values(2, '', '')
2022-04-03T08:15:37.909894Z 59 Query update student set name = '李四' where id = 2
2022-04-03T08:15:50.862257Z 59 Query delete from student where name = '李四'
root@5157698acdb3:/var/lib/mysql#
在MySQL数据库中,错误日志功能是
默认开启
的。而且,错误日志无法被禁止
。
Linux 默认文件位置为
/var/log/mysqld.log
,但容器的话会打印到控制台由 Dokcer 进行收集并通过docker logs container-name
来查看。
mysql> show variables like '%log_error%';
+----------------------------+----------------------------------------+
| Variable_name | Value |
+----------------------------+----------------------------------------+
| binlog_error_action | ABORT_SERVER |
| log_error | stderr |
| log_error_services | log_filter_internal; log_sink_internal |
| log_error_suppression_list | |
| log_error_verbosity | 2 |
+----------------------------+----------------------------------------+
5 rows in set (0.00 sec)
mysql>
root@simwor:~# docker logs mysql33060 --tail=15
2022-03-28T01:15:10.545649Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-03-28T01:15:11.784511Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-03-28T01:15:32.150263Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2022-03-28T01:15:32.151975Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2022-03-28T01:15:32.169335Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-03-28T01:15:32.169524Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-03-28T01:15:32.185872Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2022-03-28T01:15:32.326567Z 6 [Warning] [MY-013360] [Server] 'utf8mb3' is deprecated and will be removed in a future release. Please use utf8mb4 instead
2022-03-28T01:15:32.326744Z 6 [Warning] [MY-013360] [Server] 'utf8mb3' is deprecated and will be removed in a future release. Please use utf8mb4 instead
2022-03-28T01:15:32.326809Z 6 [Warning] [MY-013360] [Server] 'utf8mb3' is deprecated and will be removed in a future release. Please use utf8mb4 instead
2022-03-28T01:15:32.329870Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.27' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
2022-03-28T01:15:32.331423Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
mbind: Operation not permitted
mbind: Operation not permitted
mbind: Operation not permitted
root@simwor:~#
事件形式
记录并保存在 二进制文件
中,通过这些信息可以再现数据更新操作的全过程。数据恢复
:如果 MySQL 数据库意外停止,可以通过二进制日志文件来查看用户执行了哪些操作,对数据库服务器分拣做了哪些修改,然后根据二进制日志文件中的记录来恢复数据库服务器。数据复制
:由于日志的延续性和时效性,master 把它的二进制日志传递给 slave 来达到 master-slave 数据一致的目的。物理日志
,记录内容是“在某个数据页上做了什么修改”,属于 InnoDB 存储引擎层产生的。逻辑日志
,记录内容是语句的原始逻辑,类似于“给 ID=2 这一行的 c 字段加 1”,属于 MySQL Server 层。mysql> show variables like '%log_bin%';
+---------------------------------+-----------------------------+
| Variable_name | Value |
+---------------------------------+-----------------------------+
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/binlog |
| log_bin_index | /var/lib/mysql/binlog.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+-----------------------------+
6 rows in set (0.00 sec)
mysql>
参数 | 说明 |
---|---|
log_bin_basename | 日志的基本文件名,后面会追加标识来表示每一个文件。 |
log_bin_index | 日志文件的索引文件,这个文件管理了所有的 binlog 文件的目录。 |
log_bin_trust_function_creators | 限制存储过程及存储函数。二进制日志的一个重要功能是用于主从复制,而存储函数如 NOW() 有可能导致主从的数据不一致。 |
log_bin_use_v1_row_events | 弃用。 |
记录格式 | 模式 | 说明 |
---|---|---|
statement | SBR | 语句模式记录日志,做什么命令,记录什么命令(可读性强,日志量相对较少,但可能会出现记录不准确的情况例如插入字段时使用now()函数) |
row | RBR | 行模式,记录数据行的变化(记录更加准确,高可用环境中的新特性要依赖于RBR) |
mixed | MBR | 混合模式,由MySQL自行选择 |
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 156 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
mysql> show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];
参数 | 说明 |
---|---|
IN ‘log_name’ | 指定要查询的binlog文件名(不指定就是第一个binlog文件) |
FROM pos | 指定从哪个pos起始点开始查起(不指定就是从整个文件首个pos点开始算) |
LIMIT [offset] | 偏移量(不指定就是0) |
row_count | 查询总条数(不指定就是所有行) |
mysql> create database binlog;
Query OK, 1 row affected (0.00 sec)
mysql> use binlog;
Database changed
mysql> create table t1(id int);
Query OK, 0 rows affected (0.01 sec)
mysql> insert t1 values(1);
Query OK, 1 row affected (0.06 sec)
mysql>
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 | 6 | 125 | Server ver: 8.0.20, Binlog ver: 4 |
| mysql-bin.000004 | 125 | Previous_gtids | 6 | 156 | |
| mysql-bin.000004 | 156 | Anonymous_Gtid | 6 | 233 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 233 | Query | 6 | 347 | create database binlog /* xid=15 */ |
| mysql-bin.000004 | 347 | Anonymous_Gtid | 6 | 424 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 424 | Query | 6 | 539 | use `binlog`; create table t1(id int) /* xid=20 */ |
+------------------+-----+----------------+-----------+-------------+----------------------------------------------------+
6 rows in set (0.00 sec)
mysql>
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
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 | 6 | 125 | Server ver: 8.0.20, Binlog ver: 4 |
| mysql-bin.000004 | 125 | Previous_gtids | 6 | 156 | |
| mysql-bin.000004 | 156 | Anonymous_Gtid | 6 | 233 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 233 | Query | 6 | 347 | create database binlog /* xid=15 */ |
| mysql-bin.000004 | 347 | Anonymous_Gtid | 6 | 424 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 424 | Query | 6 | 539 | use `binlog`; create table t1(id int) /* xid=20 */ |
| mysql-bin.000004 | 539 | Anonymous_Gtid | 6 | 618 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 618 | Query | 6 | 695 | BEGIN |
| mysql-bin.000004 | 695 | Table_map | 6 | 745 | table_id: 89 (binlog.t1) |
| mysql-bin.000004 | 745 | Write_rows | 6 | 785 | table_id: 89 flags: STMT_END_F |
| mysql-bin.000004 | 785 | Xid | 6 | 816 | COMMIT /* xid=21 */ |
+------------------+-----+----------------+-----------+-------------+----------------------------------------------------+
11 rows in set (0.00 sec)
mysql>
root@5157698acdb3:/var/lib/mysql# mysqlbinlog -v binlog.000008 | tail -15
tldJYiABAAAAMAAAAPwQAAAAAJQAAAAAAAEAAgAD/wACAAAABuadjuWbmwAnVW+c
'/*!*/;
### DELETE FROM `test`.`student`
### WHERE
### @1=2
### @2='李四'
### @3=''
# at 4348
#220403 8:15:50 server id 1 end_log_pos 4379 CRC32 0x77b5dc8b Xid = 256
COMMIT/*!*/;
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*/;
root@5157698acdb3:/var/lib/mysql#
745 - 785
选项 ‘-d’ 可以指定过滤某个库的日志
[root@mysql01 ~]# mysqlbinlog --base64-output=decode-rows -vvv /data/mysql/binlog/mysql-bin.000004 | sed -n "/at 745/,/at 785/p"
# at 745
#200924 13:53:18 server id 6 end_log_pos 785 CRC32 0x9ed03402 Write_rows: table id 89 flags: STMT_END_F
### INSERT INTO `binlog`.`t1`
### SET
### @1=1 /* INT meta=0 nullable=1 is_null=0 */
# at 785
[root@mysql01 ~]#
233 - 347
[root@mysql01 ~]# mysqlbinlog --start-position=233 --stop-position=347 /data/mysql/binlog/mysql-bin.000004
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 156
#200924 13:50:41 server id 6 end_log_pos 125 CRC32 0xc637d02c Start: binlog v 4, server v 8.0.20 created 200924 13:50:41
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
sTNsXw8GAAAAeQAAAH0AAAABAAQAOC4wLjIwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEwANAAgAAAAABAAEAAAAYQAEGggAAAAICAgCAAAACgoKKioAEjQA
CigBLNA3xg==
'/*!*/;
# at 233
#200924 13:52:53 server id 6 end_log_pos 347 CRC32 0x74cb0e8a Query thread_id=9 exec_time=0 error_code=0 Xid = 15
SET TIMESTAMP=1600926773/*!*/;
SET @@session.pseudo_thread_id=9/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1168113696/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=255,@@session.collation_connection=255,@@session.collation_server=255/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
/*!80011 SET @@session.default_collation_for_utf8mb4=255*//*!*/;
/*!80016 SET @@session.default_table_encryption=0*//*!*/;
create database binlog
/*!*/;
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*/;
[root@mysql01 ~]#
mysql> create database test2;
Query OK, 1 row affected (0.01 sec)
mysql> use test2
Database changed
mysql> create table t1(id int);
Query OK, 0 rows affected (0.00 sec)
mysql> insert t1 values(1);
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> drop database test2;
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 1653 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
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 | 6 | 125 | Server ver: 8.0.20, Binlog ver: 4 |
| mysql-bin.000004 | 125 | Previous_gtids | 6 | 156 | |
| mysql-bin.000004 | 156 | Anonymous_Gtid | 6 | 233 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 233 | Query | 6 | 347 | create database binlog /* xid=15 */ |
| mysql-bin.000004 | 347 | Anonymous_Gtid | 6 | 424 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 424 | Query | 6 | 539 | use `binlog`; create table t1(id int) /* xid=20 */ |
| mysql-bin.000004 | 539 | Anonymous_Gtid | 6 | 618 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 618 | Query | 6 | 695 | BEGIN |
| mysql-bin.000004 | 695 | Table_map | 6 | 745 | table_id: 89 (binlog.t1) |
| mysql-bin.000004 | 745 | Write_rows | 6 | 785 | table_id: 89 flags: STMT_END_F |
| mysql-bin.000004 | 785 | Xid | 6 | 816 | COMMIT /* xid=21 */ |
| mysql-bin.000004 | 816 | Anonymous_Gtid | 6 | 893 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 893 | Query | 6 | 1004 | create database test2 /* xid=27 */ |
| mysql-bin.000004 | 1004 | Anonymous_Gtid | 6 | 1081 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 1081 | Query | 6 | 1194 | use `test2`; create table t1(id int) /* xid=32 */ |
| mysql-bin.000004 | 1194 | Anonymous_Gtid | 6 | 1273 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 1273 | Query | 6 | 1349 | BEGIN |
| mysql-bin.000004 | 1349 | Table_map | 6 | 1398 | table_id: 90 (test2.t1) |
| mysql-bin.000004 | 1398 | Write_rows | 6 | 1438 | table_id: 90 flags: STMT_END_F |
| mysql-bin.000004 | 1438 | Xid | 6 | 1469 | COMMIT /* xid=33 */ |
| mysql-bin.000004 | 1469 | Anonymous_Gtid | 6 | 1546 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 1546 | Query | 6 | 1653 | drop database test2 /* xid=35 */ |
+------------------+------+----------------+-----------+-------------+----------------------------------------------------+
22 rows in set (0.00 sec)
mysql>
[root@mysql01 ~]# mysqlbinlog --start-position=893 --stop-position=1546 /data/mysql/binlog/mysql-bin.000004 > /tmp/restore.sql
[root@mysql01 ~]#
mysql> select @@sql_log_bin;
+---------------+
| @@sql_log_bin |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
mysql> set @@sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> source /tmp/restore.sql
Query OK, 0 rows affected (0.00 sec)
....
Charset changed
....
Database changed
....
mysql> select * from test2.t1;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> set @@sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> select @@sync_binlog;
+---------------+
| @@sync_binlog |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
mysql>
参数值 | 说明 |
---|---|
0 | 每次提交事务都只 write,由系统自行判断什么时候执行fsync。 |
1 | 示每次提交事务都会执行fsync,就如同 redo log 刷盘流程一样。 |
N(N>1) | 每次提交事务都write,但累积N个事务后才fsync。 |
sync_binlog = 0
sync_binlog = 2
[root@mysql01 ~]# cat /data/mysql/data/auto.cnf
[auto]
server-uuid=95a35387-fa2c-11ea-8932-000c29b46d5f
[root@mysql01 ~]#
mysql> show variables like '%gtid%';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | OFF |
| gtid_executed | |
| gtid_executed_compression_period | 1000 |
| gtid_mode | OFF |
| gtid_next | AUTOMATIC |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+-----------+
9 rows in set (0.00 sec)
mysql>
[root@mysql01 ~]# sed '/log_bin/a gtid_mode=on' /etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=6
port=3306
autocommit=0
log_error=./mysql01.err
log_bin=/data/mysql/binlog/mysql-bin
gtid_mode=on
[mysql]
socket=/tmp/mysql.sock
[root@mysql01 ~]# sed -i '/log_bin/a gtid_mode=on' /etc/my.cnf
[root@mysql01 ~]# sed -i '/gtid_mode/a enforce_gtid_consistency=true' /etc/my.cnf
[root@mysql01 ~]# systemctl restart mysqld
[root@mysql01 ~]# mysql -uroot -p
....
mysql> show variables like '%gtid%';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed | |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_next | AUTOMATIC |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+-----------+
9 rows in set (0.01 sec)
mysql>
幂等性:如果拿有GTID的日志去恢复时,检查当前系统中是否有相同GTID号,有相同的就自动跳过
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 156 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> create database gtid;
Query OK, 1 row affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+----------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+----------------------------------------+
| mysql-bin.000005 | 341 | | | 95a35387-fa2c-11ea-8932-000c29b46d5f:1 |
+------------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec)
mysql> use gtid;
Database changed
mysql> create table t1(id int);
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000005 | 529 | | | 95a35387-fa2c-11ea-8932-000c29b46d5f:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
mysql> insert t1 values(1);
Query OK, 1 row affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000005 | 529 | | | 95a35387-fa2c-11ea-8932-000c29b46d5f:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000005 | 802 | | | 95a35387-fa2c-11ea-8932-000c29b46d5f:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql> drop database gtid;
Query OK, 1 row affected (0.02 sec)
mysql>
mysql> show binlog events in 'mysql-bin.000005';
+------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------+
| mysql-bin.000005 | 4 | Format_desc | 6 | 125 | Server ver: 8.0.20, Binlog ver: 4 |
| mysql-bin.000005 | 125 | Previous_gtids | 6 | 156 | |
| mysql-bin.000005 | 156 | Gtid | 6 | 233 | SET @@SESSION.GTID_NEXT= '95a35387-fa2c-11ea-8932-000c29b46d5f:1' |
| mysql-bin.000005 | 233 | Query | 6 | 341 | create database gtid /* xid=5 */ |
| mysql-bin.000005 | 341 | Gtid | 6 | 418 | SET @@SESSION.GTID_NEXT= '95a35387-fa2c-11ea-8932-000c29b46d5f:2' |
| mysql-bin.000005 | 418 | Query | 6 | 529 | use `gtid`; create table t1(id int) /* xid=11 */ |
| mysql-bin.000005 | 529 | Gtid | 6 | 608 | SET @@SESSION.GTID_NEXT= '95a35387-fa2c-11ea-8932-000c29b46d5f:3' |
| mysql-bin.000005 | 608 | Query | 6 | 683 | BEGIN |
| mysql-bin.000005 | 683 | Table_map | 6 | 731 | table_id: 89 (gtid.t1) |
| mysql-bin.000005 | 731 | Write_rows | 6 | 771 | table_id: 89 flags: STMT_END_F |
| mysql-bin.000005 | 771 | Xid | 6 | 802 | COMMIT /* xid=15 */ |
+------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------+
11 rows in set (0.00 sec)
mysql>
[root@mysql01 ~]# mysqlbinlog --include-gtids='95a35387-fa2c-11ea-8932-000c29b46d5f:1-3' /data/mysql/binlog/mysql-bin.000005 > /tmp/gtid.sql
[root@mysql01 ~]#
mysql> set @@sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> source /tmp/gtid.sql
....
ERROR 1049 (42000): Unknown database 'gtid'
....
mysql> set @@sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql>
因为幂等性的检查,1-3事务已经做过了
--skip-gtids
[root@mysql01 ~]# mysqlbinlog --skip-gtids --include-gtids='95a35387-fa2c-11ea-8932-000c29b46d5f:1-3' /data/mysql/binlog/mysql-bin.000005 > /tmp/gtid2.sql
[root@mysql01 ~]#
mysql> set @@sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> source /tmp/gtid2.sql
....
Database changed
....
mysql> set @@sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from gtid.t1;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql>
mysql> show variables like '%relay_log%';
+---------------------------+---------------------------------------------+
| Variable_name | Value |
+---------------------------+---------------------------------------------+
| max_relay_log_size | 0 |
| relay_log | 5157698acdb3-relay-bin |
| relay_log_basename | /var/lib/mysql/5157698acdb3-relay-bin |
| relay_log_index | /var/lib/mysql/5157698acdb3-relay-bin.index |
| relay_log_info_file | relay-log.info |
| relay_log_info_repository | TABLE |
| relay_log_purge | ON |
| relay_log_recovery | OFF |
| relay_log_space_limit | 0 |
| sync_relay_log | 10000 |
| sync_relay_log_info | 10000 |
+---------------------------+---------------------------------------------+
11 rows in set (0.00 sec)
mysql>
一般设置两个备份周期加一天,例如每周一次备份,则设置为15 = 7 x 2 + 1
mysql> select @@expire_logs_days;
+--------------------+
| @@expire_logs_days |
+--------------------+
| 0 |
+--------------------+
1 row in set, 1 warning (0.00 sec)
mysql>
mysql> help purge;
Many help items for your request exist.
To make a more specific request, please type 'help - '
,
where <item> is one of the following
topics:
PURGE BINARY LOGS
PURGE MASTER LOGS
mysql> help purge binary logs;
....
Examples:
PURGE BINARY LOGS TO 'mysql-bin.010';
PURGE BINARY LOGS BEFORE '2019-04-02 22:46:26';
mysql>
mysql> show binary logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 | 179 | No |
| mysql-bin.000002 | 179 | No |
| mysql-bin.000003 | 203 | No |
| mysql-bin.000004 | 1676 | No |
| mysql-bin.000005 | 1006 | No |
| mysql-bin.000006 | 219 | No |
| mysql-bin.000007 | 196 | No |
+------------------+-----------+-----------+
7 rows in set (0.01 sec)
mysql> purge binary lomysql-bin.000003';
Query OK, 0 rows affected (0.00 sec)
mysql> show binary logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000003 | 203 | No |
| mysql-bin.000004 | 1676 | No |
| mysql-bin.000005 | 1006 | No |
| mysql-bin.000006 | 219 | No |
| mysql-bin.000007 | 196 | No |
+------------------+-----------+-----------+
5 rows in set (0.00 sec)
mysql>
[root@mysql01 ~]# ll /data/mysql/binlog/ --time-style=full-iso
total 24
-rw-r-----. 1 mysql mysql 203 2020-09-24 13:50:41.249949928 +0800 mysql-bin.000003
-rw-r-----. 1 mysql mysql 1676 2020-09-24 15:02:38.237774262 +0800 mysql-bin.000004
-rw-r-----. 1 mysql mysql 1006 2020-09-24 15:50:46.312656741 +0800 mysql-bin.000005
-rw-r-----. 1 mysql mysql 219 2020-09-24 15:56:16.500643305 +0800 mysql-bin.000006
-rw-r-----. 1 mysql mysql 196 2020-09-24 15:56:18.390643228 +0800 mysql-bin.000007
-rw-r-----. 1 mysql mysql 180 2020-09-24 16:28:45.934563979 +0800 mysql-bin.index
[root@mysql01 ~]# mysql -uroot -p
....
mysql> purge binary logs before '2020-09-24 15:56:16';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@mysql01 ~]# ll /data/mysql/binlog/ --time-style=full-iso
total 12
-rw-r-----. 1 mysql mysql 219 2020-09-24 15:56:16.500643305 +0800 mysql-bin.000006
-rw-r-----. 1 mysql mysql 196 2020-09-24 15:56:18.390643228 +0800 mysql-bin.000007
-rw-r-----. 1 mysql mysql 72 2020-09-24 16:34:11.906550715 +0800 mysql-bin.index
[root@mysql01 ~]#
mysql> show binary logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000006 | 219 | No |
| mysql-bin.000007 | 196 | No |
+------------------+-----------+-----------+
2 rows in set (0.00 sec)
mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)
mysql> show binary logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000006 | 219 | No |
| mysql-bin.000007 | 243 | No |
| mysql-bin.000008 | 196 | No |
+------------------+-----------+-----------+
3 rows in set (0.00 sec)
mysql>
mysql> show binary logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000006 | 219 | No |
| mysql-bin.000007 | 243 | No |
| mysql-bin.000008 | 196 | No |
+------------------+-----------+-----------+
3 rows in set (0.00 sec)
mysql> quit
Bye
[root@mysql01 ~]# systemctl restart mysqld
[root@mysql01 ~]# mysql -uroot -p
....
mysql> show binary logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000006 | 219 | No |
| mysql-bin.000007 | 243 | No |
| mysql-bin.000008 | 219 | No |
| mysql-bin.000009 | 196 | No |
+------------------+-----------+-----------+
4 rows in set (0.00 sec)
mysql>
mysql> select @@max_binlog_size;
+-------------------+
| @@max_binlog_size |
+-------------------+
| 1073741824 |
+-------------------+
1 row in set (0.00 sec)
mysql>