验证相关参数
binlog_format=row
binlog_row_image=full
binlog_rows_query_log_events=on
先上总结
binlog_format=row
01:直接用"mysqlbinlog PATH/binlogfile"来查看时
02:binlog日志文件中对于DML数据操纵语言的语句是加密的(需要解密才能看到)
03:binlog日志文件中对于DDL数据定义语言的语句是明文的(也就是可以直接看到)
binlog_row_image=full
01:用"mysqlbinlog --base64-output=decode-row -vv PATH/binlogfile"来查看时
02:binlog日志文件中对于DML数据操作语言insert会记录具体插入了什么值(整行的所有字段的值)
03:binlog日志文件中对于DML数据操作语言update会记录更新和更新后的值(整行的所有字段的值,即使你只更新了某一个字段的值)
04:binlog日志文件中对于DML数据操作语言delete会记录具体删除了什么值(整行的所有字段的值)
binlog_rows_query_log_events=on
01:用"mysqlbinlog --base64-output=decode-row -vv PATH/binlogfile"来查看时
02:binlog日志文件中对于DML数据操纵语言语句,会显示具体执行的SQL语句。
03:我们在mysql中用show binlog events in "binlogfile";命令可以看到DDL语句,也可以看到DML语句。
问题一:如何保证主从复制之间数据的一致性
答一:ps:当然还有一些其它的因素要考虑到哈
01:主从复制要同步函数
公司规则了不要使用函数,但是你能控制住人嘛,新来的开发人员呢?
02:二进制日志模式要使用row模式
因为二进制日志文件中会记录具体更改的值
当前环境介绍
mysql> select version(); #mysql版本
+------------+
| version() |
+------------+
| 5.7.22-log |
+------------+
1 row in set (0.00 sec)
mysql> select @@global.autocommit,@@autocommit; #开启了自动提交
+---------------------+--------------+
| @@global.autocommit | @@autocommit |
+---------------------+--------------+
| 1 | 1 |
+---------------------+--------------+
1 row in set (0.00 sec)
mysql> show master status; #当前的binlog日志文件及pos点
+---------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| 21_mysql_bin.000001 | 154 | | | |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
验证参数的状态
mysql> select @@global.binlog_format,@@binlog_format; #全局和当前会话下
+------------------------+-----------------+
| @@global.binlog_format | @@binlog_format |
+------------------------+-----------------+
| ROW | ROW |
+------------------------+-----------------+
1 row in set (0.00 sec)
mysql> select @@global.binlog_row_image,@@binlog_row_image; #全局和当前会话下
+---------------------------+--------------------+
| @@global.binlog_row_image | @@binlog_row_image |
+---------------------------+--------------------+
| FULL | FULL |
+---------------------------+--------------------+
1 row in set (0.00 sec)
mysql> select @@global.binlog_rows_query_log_events,@@binlog_rows_query_log_events; #全局和当前会话下
+---------------------------------------+--------------------------------+
| @@global.binlog_rows_query_log_events | @@binlog_rows_query_log_events |
+---------------------------------------+--------------------------------+
| 1 | 1 |
+---------------------------------------+--------------------------------+
1 row in set (0.00 sec)
测试数据
-- 创建chenliang库
create database if not exists chenliang character set utf8 collate utf8_general_ci;
-- 进入chenliang库
use chenliang;
-- 查看是否成功进入到chenliang库下面
select database();
-- 创建test1表
create table if not exists test1(
id int(10) unsigned not null auto_increment primary key comment"序列号",
name varchar(20) not null comment"姓名",
age int(3) unsigned not null comment"年龄",
jobdate date not null comment"参加工作的时间"
)engine=innodb character set utf8 collate utf8_general_ci comment"测试表1";
-- 插入两行数据
insert into test1(name,age,jobdate) values
("chenliang01",25,curdate()),
("chenliang02",26,curdate());
-- 更改age字段的内容为26,条件是name等于chenliang01,id等于1
update test1 set age=26 where id=1 and name="chenliang01";
-- 删除id等于2,name内容等于chenliang02的记录
delete from test1 where id=2 and name="chenliang02";
-- truncate表
truncate test1;
mysql> show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| 21_mysql_bin.000001 | 2230 | | | |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
用mysqlbinlog命令来查看二进制日志文件(一)
[root@master binlog]# mysqlbinlog 21_mysql_bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#190913 2:38:46 server id 21 end_log_pos 123 CRC32 0x7876ff8f Start: binlog v 4, server v 5.7.22-log created 190913 2:38:46 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
djl7XQ8VAAAAdwAAAHsAAAABAAQANS43LjIyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAB2OXtdEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AY//dng=
'/*!*/;
# at 123
#190913 2:38:46 server id 21 end_log_pos 154 CRC32 0xc794dcdd Previous-GTIDs
# [empty]
# at 154
-- 这是创建chenliang库的sql记录(没有解析,对于DDL语句是可以看到完整的语句的)
#190913 2:39:00 server id 21 end_log_pos 219 CRC32 0x0d65e789 Anonymous_GTID last_committed=0 sequence_number=1rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#190913 2:39:00 server id 21 end_log_pos 385 CRC32 0x27d8846e Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1568356740/*!*/;
SET @@session.pseudo_thread_id=5/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549120/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create database if not exists chenliang character set utf8 collate utf8_general_ci
/*!*/;
# at 385
-- 这是创建test表的语句(没用解析,对于DDL语句是可以直接看到完整的语句的)
#190913 2:39:22 server id 21 end_log_pos 450 CRC32 0x1c0e30ec Anonymous_GTID last_committed=1 sequence_number=2rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 450
#190913 2:39:22 server id 21 end_log_pos 871 CRC32 0xb9db2ee8 Query thread_id=5 exec_time=0 error_code=0
use `chenliang`/*!*/;
SET TIMESTAMP=1568356762/*!*/;
create table if not exists test1(
id int(10) unsigned not null auto_increment primary key comment"序列号",
name varchar(20) not null comment"姓名",
age int(3) unsigned not null comment"年龄",
jobdate date not null comment"参加工作的时间"
)engine=innodb character set utf8 collate utf8_general_ci comment"测试表1"
/*!*/;
# at 871
-- 这是insert时的记录(没有解析,对于DML语句是加密的,看不到完整的sql语句,也看不到具体插入的什么值)
#190913 2:39:33 server id 21 end_log_pos 936 CRC32 0x8a9af7ab Anonymous_GTID last_committed=2 sequence_number=3rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 936
#190913 2:39:33 server id 21 end_log_pos 1021 CRC32 0xa1afe68d Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1568356773/*!*/;
SET @@session.time_zone='SYSTEM'/*!*/;
BEGIN
/*!*/;
# at 1021
# at 1146
#190913 2:39:33 server id 21 end_log_pos 1204 CRC32 0x56e8186c Table_map: `chenliang`.`test1` mapped to number 220
# at 1204
#190913 2:39:33 server id 21 end_log_pos 1287 CRC32 0x5b0be2ac Write_rows: table id 220 flags: STMT_END_F
BINLOG '
pTl7XRMVAAAAOgAAALQEAAAAANwAAAAAAAEACWNoZW5saWFuZwAFdGVzdDEABAMPAwoCPAAAbBjo
Vg==
pTl7XR4VAAAAUwAAAAcFAAAAANwAAAAAAAEAAgAE//ABAAAAC2NoZW5saWFuZzAxGQAAAC3HD/AC
AAAAC2NoZW5saWFuZzAyGgAAAC3HD6ziC1s=
'/*!*/;
# at 1287
#190913 2:39:33 server id 21 end_log_pos 1318 CRC32 0x18fc91ae Xid = 526
COMMIT/*!*/;
# at 1318
-- 这是update时的语句(没有解析,对于DML语句是加密的,看不到完整的sql语句,也看不到更新的前和更新后的数据)
#190913 2:39:48 server id 21 end_log_pos 1383 CRC32 0x7de03699 Anonymous_GTID last_committed=3 sequence_number=4rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1383
#190913 2:39:48 server id 21 end_log_pos 1460 CRC32 0xba7eff82 Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1568356788/*!*/;
BEGIN
/*!*/;
# at 1460
# at 1541
#190913 2:39:48 server id 21 end_log_pos 1599 CRC32 0xbcc4e9d8 Table_map: `chenliang`.`test1` mapped to number 220
# at 1599
#190913 2:39:48 server id 21 end_log_pos 1683 CRC32 0x190ca2e1 Update_rows: table id 220 flags: STMT_END_F
BINLOG '
tDl7XRMVAAAAOgAAAD8GAAAAANwAAAAAAAEACWNoZW5saWFuZwAFdGVzdDEABAMPAwoCPAAA2OnE
vA==
tDl7XR8VAAAAVAAAAJMGAAAAANwAAAAAAAEAAgAE///wAQAAAAtjaGVubGlhbmcwMRkAAAAtxw/w
AQAAAAtjaGVubGlhbmcwMRoAAAAtxw/hogwZ
'/*!*/;
# at 1683
#190913 2:39:48 server id 21 end_log_pos 1714 CRC32 0x0a573be1 Xid = 535
COMMIT/*!*/;
# at 1714
-- 这是delete语句(没有解析,对于DML语句是加密的,看不到完整的sql语句,也看不到删除前的数据)
#190913 2:40:25 server id 21 end_log_pos 1779 CRC32 0xac860f3f Anonymous_GTID last_committed=4 sequence_number=5rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1779
#190913 2:40:25 server id 21 end_log_pos 1856 CRC32 0xf3fd97d0 Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1568356825/*!*/;
BEGIN
/*!*/;
# at 1856
# at 1931
#190913 2:40:25 server id 21 end_log_pos 1989 CRC32 0x87dbbd04 Table_map: `chenliang`.`test1` mapped to number 220
# at 1989
#190913 2:40:25 server id 21 end_log_pos 2048 CRC32 0x1f0b576f Delete_rows: table id 220 flags: STMT_END_F
BINLOG '
2Tl7XRMVAAAAOgAAAMUHAAAAANwAAAAAAAEACWNoZW5saWFuZwAFdGVzdDEABAMPAwoCPAAABL3b
hw==
2Tl7XSAVAAAAOwAAAAAIAAAAANwAAAAAAAEAAgAE//ACAAAAC2NoZW5saWFuZzAyGgAAAC3HD29X
Cx8=
'/*!*/;
# at 2048
#190913 2:40:25 server id 21 end_log_pos 2079 CRC32 0x933ed8e2 Xid = 550
COMMIT/*!*/;
# at 2079
-- 这是truncate表的语句(没有解析,对于DDL语句是可以直接看到完整的语句的)
#190913 2:41:26 server id 21 end_log_pos 2144 CRC32 0xd9a0f580 Anonymous_GTID last_committed=5 sequence_number=6rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 2144
#190913 2:41:26 server id 21 end_log_pos 2230 CRC32 0xaaf9a231 Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1568356886/*!*/;
truncate test1
/*!*/;
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*/;
用mysqlbinlog命令来查看二进制日志文件(二)
[root@master binlog]# mysqlbinlog --base64-output=decode-row -vv 21_mysql_bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#190913 2:38:46 server id 21 end_log_pos 123 CRC32 0x7876ff8f Start: binlog v 4, server v 5.7.22-log created 190913 2:38:46 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
# at 123
#190913 2:38:46 server id 21 end_log_pos 154 CRC32 0xc794dcdd Previous-GTIDs
# [empty]
# at 154
-- 这是第一个事务(创建chenliang库时的sql记录)
#190913 2:39:00 server id 21 end_log_pos 219 CRC32 0x0d65e789 Anonymous_GTID last_committed=0 sequence_number=1rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#190913 2:39:00 server id 21 end_log_pos 385 CRC32 0x27d8846e Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1568356740/*!*/;
SET @@session.pseudo_thread_id=5/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549120/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create database if not exists chenliang character set utf8 collate utf8_general_ci
/*!*/;
# at 385
-- 这是创建test1表的sql记录(DDL语句)
#190913 2:39:22 server id 21 end_log_pos 450 CRC32 0x1c0e30ec Anonymous_GTID last_committed=1 sequence_number=2rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 450
#190913 2:39:22 server id 21 end_log_pos 871 CRC32 0xb9db2ee8 Query thread_id=5 exec_time=0 error_code=0
use `chenliang`/*!*/;
SET TIMESTAMP=1568356762/*!*/;
create table if not exists test1(
id int(10) unsigned not null auto_increment primary key comment"序列号",
name varchar(20) not null comment"姓名",
age int(3) unsigned not null comment"年龄",
jobdate date not null comment"参加工作的时间"
)engine=innodb character set utf8 collate utf8_general_ci comment"测试表1"
/*!*/;
# at 871
#190913 2:39:33 server id 21 end_log_pos 936 CRC32 0x8a9af7ab Anonymous_GTID last_committed=2 sequence_number=3rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 936
-- 这是记录插入数据的语句(解析后,可以看到具体执行的sql语句和具体插入的数据)
#190913 2:39:33 server id 21 end_log_pos 1021 CRC32 0xa1afe68d Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1568356773/*!*/;
SET @@session.time_zone='SYSTEM'/*!*/;
BEGIN
/*!*/;
# at 1021
#190913 2:39:33 server id 21 end_log_pos 1146 CRC32 0x2a92c3ec Rows_query
# insert into test1(name,age,jobdate) values
# ("chenliang01",25,curdate()),
# ("chenliang02",26,curdate())
# at 1146
#190913 2:39:33 server id 21 end_log_pos 1204 CRC32 0x56e8186c Table_map: `chenliang`.`test1` mapped to number 220
# at 1204
#190913 2:39:33 server id 21 end_log_pos 1287 CRC32 0x5b0be2ac Write_rows: table id 220 flags: STMT_END_F
### INSERT INTO `chenliang`.`test1`
### SET -- 第一条记录的具体值
### @1=1 /* INT meta=0 nullable=0 is_null=0 */
### @2='chenliang01' /* VARSTRING(60) meta=60 nullable=0 is_null=0 */
### @3=25 /* INT meta=0 nullable=0 is_null=0 */
### @4='2019:09:13' /* DATE meta=0 nullable=0 is_null=0 */
### INSERT INTO `chenliang`.`test1`
### SET -- 第二条记录的具体值
### @1=2 /* INT meta=0 nullable=0 is_null=0 */
### @2='chenliang02' /* VARSTRING(60) meta=60 nullable=0 is_null=0 */
### @3=26 /* INT meta=0 nullable=0 is_null=0 */
### @4='2019:09:13' /* DATE meta=0 nullable=0 is_null=0 */
# at 1287
#190913 2:39:33 server id 21 end_log_pos 1318 CRC32 0x18fc91ae Xid = 526
COMMIT/*!*/;
# at 1318
-- 这是update时的sql记录(解析后,可以看到具体执行的语句和更新前后的值)
#190913 2:39:48 server id 21 end_log_pos 1383 CRC32 0x7de03699 Anonymous_GTID last_committed=3 sequence_number=4rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1383
#190913 2:39:48 server id 21 end_log_pos 1460 CRC32 0xba7eff82 Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1568356788/*!*/;
BEGIN
/*!*/;
# at 1460
#190913 2:39:48 server id 21 end_log_pos 1541 CRC32 0x65356360 Rows_query
# update test1 set age=26 where id=1 and name="chenliang01"
# at 1541
#190913 2:39:48 server id 21 end_log_pos 1599 CRC32 0xbcc4e9d8 Table_map: `chenliang`.`test1` mapped to number 220
# at 1599
#190913 2:39:48 server id 21 end_log_pos 1683 CRC32 0x190ca2e1 Update_rows: table id 220 flags: STMT_END_F
### UPDATE `chenliang`.`test1`
### WHERE -- 这是更新前记录中每个字段(行/记录)的值
### @1=1 /* INT meta=0 nullable=0 is_null=0 */
### @2='chenliang01' /* VARSTRING(60) meta=60 nullable=0 is_null=0 */
### @3=25 /* INT meta=0 nullable=0 is_null=0 */
### @4='2019:09:13' /* DATE meta=0 nullable=0 is_null=0 */
### SET -- 这是更新后每个字段(行/记录)的值(只更新了一个字段的值)
### @1=1 /* INT meta=0 nullable=0 is_null=0 */
### @2='chenliang01' /* VARSTRING(60) meta=60 nullable=0 is_null=0 */
### @3=26 /* INT meta=0 nullable=0 is_null=0 */
### @4='2019:09:13' /* DATE meta=0 nullable=0 is_null=0 */
# at 1683
#190913 2:39:48 server id 21 end_log_pos 1714 CRC32 0x0a573be1 Xid = 535
COMMIT/*!*/;
# at 1714
-- 这是delete的sql记录(解析后,可以看到具体执行的sql语句和具体删除了哪些数据)
#190913 2:40:25 server id 21 end_log_pos 1779 CRC32 0xac860f3f Anonymous_GTID last_committed=4 sequence_number=5rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1779
#190913 2:40:25 server id 21 end_log_pos 1856 CRC32 0xf3fd97d0 Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1568356825/*!*/;
BEGIN
/*!*/;
# at 1856
#190913 2:40:25 server id 21 end_log_pos 1931 CRC32 0x2e5859f5 Rows_query
# delete from test1 where id=2 and name="chenliang02"
# at 1931
#190913 2:40:25 server id 21 end_log_pos 1989 CRC32 0x87dbbd04 Table_map: `chenliang`.`test1` mapped to number 220
# at 1989
#190913 2:40:25 server id 21 end_log_pos 2048 CRC32 0x1f0b576f Delete_rows: table id 220 flags: STMT_END_F
### DELETE FROM `chenliang`.`test1`
### WHERE 记录的是delete前的该行的数据
### @1=2 /* INT meta=0 nullable=0 is_null=0 */
### @2='chenliang02' /* VARSTRING(60) meta=60 nullable=0 is_null=0 */
### @3=26 /* INT meta=0 nullable=0 is_null=0 */
### @4='2019:09:13' /* DATE meta=0 nullable=0 is_null=0 */
# at 2048
#190913 2:40:25 server id 21 end_log_pos 2079 CRC32 0x933ed8e2 Xid = 550
COMMIT/*!*/;
# at 2079
-- 这是truncate表的sql记录(这是DDL语句哈)
#190913 2:41:26 server id 21 end_log_pos 2144 CRC32 0xd9a0f580 Anonymous_GTID last_committed=5 sequence_number=6rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 2144
#190913 2:41:26 server id 21 end_log_pos 2230 CRC32 0xaaf9a231 Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1568356886/*!*/;
truncate test1
/*!*/;
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*/;
用show binlog events in "二进制文件"