binlog-do-db参数的影响
本次测试均为binlog_format=row格式,因为binlog_format=statement格式在复制场景下,对函数和存储过程使用不友好,
很容易导致主从数据不一致,生产环境很少有使用statement格式。
使用use指定库
在test库进行ddl操作和dml操作
[root@localhost] 15:17:10 [test]> flush binary logs;
Query OK, 0 rows affected (0.01 sec)
[root@localhost] 15:21:26 [test]> show binary logs
-> ;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 24296 |
| mysql-bin.000002 | 528 |
| mysql-bin.000003 | 217 |
| mysql-bin.000004 | 241 |
| mysql-bin.000005 | 7994 |
| mysql-bin.000006 | 804 |
| mysql-bin.000007 | 194 |
+------------------+-----------+
7 rows in set (0.00 sec)
[root@localhost] 15:21:45 [test]> create table do_1(id int primary key auto_increment,d_date timestamp);
Query OK, 0 rows affected (0.01 sec)
[root@localhost] 15:22:59 [test]> insert into do_1 values(null,sysdate());
Query OK, 1 row affected (0.00 sec)
[root@localhost] 15:23:21 [test]> insert into do_1 values(null,sysdate());
Query OK, 1 row affected (0.00 sec)
[root@localhost] 15:23:22 [test]> insert into do_1 values(null,sysdate());
Query OK, 1 row affected (0.00 sec)
[root@localhost] 15:23:23 [test]> insert into do_1 values(null,sysdate());
Query OK, 1 row affected (0.00 sec)
[root@localhost] 15:23:24 [test]> insert into do_1 values(null,sysdate());
Query OK, 1 row affected (0.00 sec)
[root@localhost] 15:23:25 [test]> insert into do_1 values(null,sysdate());
Query OK, 1 row affected (0.00 sec)
[root@localhost] 15:23:26 [test]> insert into do_1 values(null,sysdate());
Query OK, 1 row affected (0.01 sec)
[root@localhost] 15:23:26 [test]> insert into do_1 values(null,sysdate());
Query OK, 1 row affected (0.01 sec)
[root@localhost] 15:23:27 [test]> insert into do_1 values(null,sysdate());
Query OK, 1 row affected (0.00 sec)
[root@localhost] 15:23:27 [test]> insert into do_1 values(null,sysdate());
Query OK, 1 row affected (0.00 sec)
[root@localhost] 15:23:28 [test]>
查看binlog信息,可以看到ddl和dml语句在binlog里面均有记录
SET @@SESSION.GTID_NEXT= '7e998574-1f1b-11f0-b16c-000c295ef870:101'/*!*/;
# at 259
#250630 15:22:59 server id 22130 end_log_pos 404 CRC32 0x383a81b5 Query thread_id=11 exec_time=0 error_code=0
use `test`/*!*/;
SET TIMESTAMP=1751268179/*!*/;
SET @@session.pseudo_thread_id=11/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1168113664/*!*/;
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=45/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
SET @@session.explicit_defaults_for_timestamp=1/*!*/;
create table do_1(id int primary key auto_increment,d_date timestamp)
/*!*/;
# at 404
#250630 15:23:21 server id 22130 end_log_pos 469 CRC32 0x66908aaf GTID last_committed=1 sequence_number=2 rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= '7e998574-1f1b-11f0-b16c-000c295ef870:102'/*!*/;
# at 469
#250630 15:23:21 server id 22130 end_log_pos 549 CRC32 0x703d7334 Query thread_id=11 exec_time=0 error_code=0
SET TIMESTAMP=1751268201/*!*/;
SET @@session.time_zone='SYSTEM'/*!*/;
BEGIN
/*!*/;
# at 549
#250630 15:23:21 server id 22130 end_log_pos 612 CRC32 0xd55d0567 Rows_query
# insert into do_1 values(null,sysdate())
# at 612
#250630 15:23:21 server id 22130 end_log_pos 661 CRC32 0x8ae7c40c Table_map: `test`.`do_1` mapped to number 121
# at 661
#250630 15:23:21 server id 22130 end_log_pos 705 CRC32 0xca0907f8 Write_rows: table id 121 flags: STMT_END_F
### INSERT INTO `test`.`do_1`
### SET
### @1=1 /* INT meta=0 nullable=0 is_null=0 */
### @2=1751268201 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */
# at 705
#250630 15:23:21 server id 22130 end_log_pos 736 CRC32 0x833ea768 Xid = 92
COMMIT/*!*/;
。。。。。。
不使用use指定库
DATABASE为NULL的情况下
登录数据库,不使用use指定任何数据库
[root@localhost] 15:37:28 [(none)]> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
[root@localhost] 15:39:30 [(none)]> alter table test.do_1 add column info varchar(20);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
[root@localhost] 15:40:25 [(none)]> desc test.do_1;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| d_date | timestamp | YES | | NULL | |
| info | varchar(20) | YES | | NULL | |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
[root@localhost] 15:40:30 [(none)]>
[root@localhost] 15:42:10 [(none)]> create table test.do_2(id int );
Query OK, 0 rows affected (0.01 sec)
查看binlog,由于设置了binlog_rows_query_log_events=on,可以看到ddl能够正常记录到binlog日志,如果binlog_rows_query_log_events=off,在此种情况下,是看不到test库ddl下的相关日志。
# at 3724
#250630 15:40:16 server id 22130 end_log_pos 3789 CRC32 0xce83335f GTID last_committed=11 sequence_number=12 rbr_only=no
SET @@SESSION.GTID_NEXT= '7e998574-1f1b-11f0-b16c-000c295ef870:112'/*!*/;
# at 3789
#250630 15:40:16 server id 22130 end_log_pos 3908 CRC32 0xdd14a033 Query thread_id=12 exec_time=0 error_code=0
SET TIMESTAMP=1751269216/*!*/;
alter table test.do_1 add column info varchar(20)
/*!*/;
# at 3908
#250630 15:43:59 server id 22130 end_log_pos 3973 CRC32 0x3b6ea0a4 GTID last_committed=12 sequence_number=13 rbr_only=no
SET @@SESSION.GTID_NEXT= '7e998574-1f1b-11f0-b16c-000c295ef870:113'/*!*/;
# at 3973
#250630 15:43:59 server id 22130 end_log_pos 4074 CRC32 0x3e78357b Query thread_id=12 exec_time=0 error_code=0
SET TIMESTAMP=1751269439/*!*/;
create table test.do_2(id int )
/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
DATABASE为非操作的DB库
使用use,指定在其他数据库进行ddl操作(可以看到在use test1库后,再次执行ddl操作,binlog日志无do_3表的操作记录)
[root@localhost] 15:43:59 [(none)]> use test1
Database changed
[root@localhost] 15:44:14 [test1]> select database();
+------------+
| database() |
+------------+
| test1 |
+------------+
1 row in set (0.00 sec)
[root@localhost] 15:45:01 [test1]>
[root@localhost] 15:44:10 [test1]> create table test.do_3(id int );
Query OK, 0 rows affected (0.00 sec)
[root@localhost] 15:45:35 [test1]> alter table test.do_3 add column info varchar(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
[root@localhost] 15:45:41 [test1]>
[root@localhost] 15:51:06 [test1]> desc test.do_3;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| info | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
[root@localhost] 15:51:26 [test1]>
测试dml
[root@localhost] 15:56:25 [test1]> insert into test.do_3 values(1,'MySQL'),(2,'Oracle'),(3,'PG');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
[root@localhost] 15:57:08 [test1]>
查看binlog,binlog日志中记录test.do_3的相关操作
# at 4139
#250630 15:57:08 server id 22130 end_log_pos 4212 CRC32 0x592089e9 Query thread_id=12 exec_time=0 error_code=0
SET TIMESTAMP=1751270228/*!*/;
BEGIN
/*!*/;
# at 4212
#250630 15:57:08 server id 22130 end_log_pos 4297 CRC32 0xf1f56897 Rows_query
# insert into test.do_3 values(1,'MySQL'),(2,'Oracle'),(3,'PG')
# at 4297
#250630 15:57:08 server id 22130 end_log_pos 4347 CRC32 0x4857dcf0 Table_map: `test`.`do_3` mapped to number 125
# at 4347
#250630 15:57:08 server id 22130 end_log_pos 4413 CRC32 0x21e25679 Write_rows: table id 125 flags: STMT_END_F
### INSERT INTO `test`.`do_3`
### SET
### @1=1 /* INT meta=0 nullable=1 is_null=0 */
### @2='MySQL' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */
### INSERT INTO `test`.`do_3`
### SET
### @1=2 /* INT meta=0 nullable=1 is_null=0 */
### @2='Oracle' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */
### INSERT INTO `test`.`do_3`
### SET
### @1=3 /* INT meta=0 nullable=1 is_null=0 */
### @2='PG' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */
# at 4413
#250630 15:57:08 server id 22130 end_log_pos 4444 CRC32 0x64fcc895 Xid = 127
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*/;
[mysql@pg13_1 log_bin]$
跨库关联操作DML
使用use指定数据库操作
跨库关联操作,更新do_1表的数据
[root@localhost] 16:12:22 [test]> update do_1 a left join test1.test b on a.id=b.id set a.info=b.info where b.id is not null;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
[root@localhost] 16:13:49 [test]> select * from do_1;
+----+---------------------+--------+
| id | d_date | info |
+----+---------------------+--------+
| 1 | 2025-06-30 15:23:21 | MySQL |
| 2 | 2025-06-30 15:23:22 | Oracle |
| 3 | 2025-06-30 15:23:23 | PG |
| 4 | 2025-06-30 15:23:24 | NULL |
| 5 | 2025-06-30 15:23:25 | NULL |
| 6 | 2025-06-30 15:23:26 | NULL |
| 7 | 2025-06-30 15:23:26 | NULL |
| 8 | 2025-06-30 15:23:27 | NULL |
| 9 | 2025-06-30 15:23:27 | NULL |
| 10 | 2025-06-30 15:23:28 | NULL |
+----+---------------------+--------+
10 rows in set (0.00 sec)
[root@localhost] 16:13:59 [test]>
查看binlog,可以看到对do_1表 的相关操作记录
BEGIN
/*!*/;
# at 497
#250630 16:13:49 server id 22130 end_log_pos 611 CRC32 0x5e2c5e49 Rows_query
# update do_1 a left join test1.test b on a.id=b.id set a.info=b.info where b.id is not null
# at 611
#250630 16:13:49 server id 22130 end_log_pos 663 CRC32 0xffc33a1e Table_map: `test`.`do_1` mapped to number 122
# at 663
#250630 16:13:49 server id 22130 end_log_pos 769 CRC32 0x5343dc3d Update_rows: table id 122 flags: STMT_END_F
### UPDATE `test`.`do_1`
### WHERE
### @1=1 /* INT meta=0 nullable=0 is_null=0 */
### @2=1751268201 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */
### @3=NULL /* VARSTRING(80) meta=80 nullable=1 is_null=1 */
### SET
### @1=1 /* INT meta=0 nullable=0 is_null=0 */
### @2=1751268201 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */
### @3='MySQL' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */
。。。。。。
DATABASE为NULL的情况下
关联更新do_1表
[root@localhost] 16:21:30 [(none)]> select * from test.do_1;
+----+---------------------+------+
| id | d_date | info |
+----+---------------------+------+
| 1 | 2025-06-30 15:23:21 | NULL |
| 2 | 2025-06-30 15:23:22 | NULL |
| 3 | 2025-06-30 15:23:23 | NULL |
| 4 | 2025-06-30 15:23:24 | NULL |
| 5 | 2025-06-30 15:23:25 | NULL |
| 6 | 2025-06-30 15:23:26 | NULL |
| 7 | 2025-06-30 15:23:26 | NULL |
| 8 | 2025-06-30 15:23:27 | NULL |
| 9 | 2025-06-30 15:23:27 | NULL |
| 10 | 2025-06-30 15:23:28 | NULL |
+----+---------------------+------+
10 rows in set (0.00 sec)
[root@localhost] 16:21:41 [(none)]> update test.do_1 a left join test1.test b on a.id=b.id set a.info=b.info where b.id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
[root@localhost] 16:22:16 [(none)]> select *from test.do_1 where id=1;
+----+---------------------+-------+
| id | d_date | info |
+----+---------------------+-------+
| 1 | 2025-06-30 15:23:21 | MySQL |
+----+---------------------+-------+
1 row in set (0.00 sec)
[root@localhost] 16:22:29 [(none)]>
查看binlog,可以看到相关记录
### UPDATE `test`.`do_1`
### WHERE
### @1=1 /* INT meta=0 nullable=0 is_null=0 */
### @2=1751268201 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */
### @3=NULL /* VARSTRING(80) meta=80 nullable=1 is_null=1 */
### SET
### @1=1 /* INT meta=0 nullable=0 is_null=0 */
### @2=1751268201 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */
### @3='MySQL' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */
# at 1538
#250630 16:22:16 server id 22130 end_log_pos 1569 CRC32 0xd2cae91a Xid = 160
COMMIT/*!*/;
DATABASE为非操作的数据库
跨库关联更新do_1
[root@localhost] 16:24:51 [test1]> select * from test.do_1 where id=2;
+----+---------------------+------+
| id | d_date | info |
+----+---------------------+------+
| 2 | 2025-06-30 15:23:22 | NULL |
+----+---------------------+------+
1 row in set (0.00 sec)
[root@localhost] 16:25:01 [test1]> update test.do_1 a left join test b on a.id=b.id set a.info=b.info where b.id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
[root@localhost] 16:25:08 [test1]> select * from test.do_1 where id=2;
+----+---------------------+--------+
| id | d_date | info |
+----+---------------------+--------+
| 2 | 2025-06-30 15:23:22 | Oracle |
+----+---------------------+--------+
1 row in set (0.00 sec)
[root@localhost] 16:25:14 [test1]>
查看binlog,可以看到do_1表的相关操作
### UPDATE `test`.`do_1`
### WHERE
### @1=2 /* INT meta=0 nullable=0 is_null=0 */
### @2=1751268202 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */
### @3=NULL /* VARSTRING(80) meta=80 nullable=1 is_null=1 */
### SET
### @1=2 /* INT meta=0 nullable=0 is_null=0 */
### @2=1751268202 /* TIMESTAMP(0) meta=0 nullable=1 is_null=0 */
### @3='Oracle' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */
# at 1932
#250630 16:25:08 server id 22130 end_log_pos 1963 CRC32 0xd7dae666 Xid = 165
COMMIT/*!*/;
。。。。。。
总结
- 该参数生产环境一般不建议使用,因为在备份恢复的时候,由于该参数,可能会导致部分表的数据丢失。
- 该参数在row格式下,使用use指定库后,若修改的数据都是在该库下,所有的ddl,dml均会记录到binlog日志中。
- 该参数在row格式下,如果未指定任何数据库,dml日志会记录到binlog日志中,由于设置了binlog_rows_query_log_events=on,可以看到ddl能够正常记录到binlog日志,如果binlog_rows_query_log_events=off,在此种情况下,是看不到test库ddl下的相关日志。
- 该参数在row格式下,DATABSE未非操作的DB库时,ddl不会记录到binlog日志,dml会记录到binlog日志。