首页 > 数据库 >mysql数据库,外键关联删除主表数据,binlog只记主表binlog

mysql数据库,外键关联删除主表数据,binlog只记主表binlog

时间:2024-09-09 10:52:06浏览次数:8  
标签:binlog affected 只记 sec mysql Query 主表 id select

环境:8.0.32

场景一、开启外键约束关联删除时,删除主表记录,binlog中内容解析

1、写入测试数据,并删除主键 id=1,子表id=1一并删除

mysql> create table t1(id int primary key ,name varchar(200));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 select 1,'zs';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t1 select 2,'ls';
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t1 select 3,'ww';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> create table t2(id int,mx varchar(200) ,constraint wj foreign key(id) references t1(id) ON UPDATE NO ACTION ON DELETE CASCADE);
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
|  1 | zs   |
|  2 | ls   |
|  3 | ww   |
+----+------+
3 rows in set (0.00 sec)

mysql> insert into t2 select 4,4;
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`ceshi`.`t2`, CONSTRAINT `wj` FOREIGN KEY (`id`) REFERENCES `t1` (`id`) ON DELETE CASCADE)
mysql> insert into t2 select 1,1;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t2 select 1,2;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t2 select 1,3;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)

mysql> delete from t1 where id=1;
Query OK, 1 row affected (0.01 sec)

mysql> select * from t2;
Empty set (0.00 sec)

 

 

2、分析binlog

BEGIN
/*!*/;
# at 354
#240909 10:41:26 server id 3306  end_log_pos 411 CRC32 0x4d8ebc49     Table_map: `ceshi`.`t1` mapped to number 131
# has_generated_invisible_primary_key=0
# at 411
#240909 10:41:26 server id 3306  end_log_pos 455 CRC32 0xaa89f9cc     Delete_rows: table id 131 flags: STMT_END_F

BINLOG '
VmDeZhPqDAAAOQAAAJsBAAAAAIMAAAAAAAMABWNlc2hpAAJ0MQACAw8CIAMCAQEAAgP8/wBJvI5N
VmDeZiDqDAAALAAAAMcBAAAAAIMAAAAAAAEAAgAC/wABAAAAAgB6c8z5iao=
'/*!*/;
### DELETE FROM `ceshi`.`t1`
### WHERE
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2='zs' /* VARSTRING(800) meta=800 nullable=1 is_null=0 */
# at 455
#240909 10:41:26 server id 3306  end_log_pos 486 CRC32 0x9ce4b572     Xid = 77
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*/;

 

 

场景二、关闭外键约束,关联删除时,删除主表记录,binlog中内容解析

1、测试数据如下,关闭外键约束后,删除主表记录,子表数据并没有跟着删除

mysql> select * from t1;
+----+------+
| id | name |
+----+------+
|  2 | ls   |
|  3 | ww   |
+----+------+
2 rows in set (0.00 sec)

mysql> select * from t2;
Empty set (0.00 sec)

mysql> insert into t2 select 2,2;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t2 select 2,3;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t2 select 2,4;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> SET FOREIGN_KEY_CHECKS=0;
Query OK, 0 rows affected (0.00 sec)

mysql> delete from t1 where id=2;
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+----+------+
| id | name |
+----+------+
|  3 | ww   |
+----+------+
1 row in set (0.00 sec)

mysql> select * from t2;
+------+------+
| id   | mx   |
+------+------+
|    2 | 2    |
|    2 | 3    |
|    2 | 4    |
+------+------+
3 rows in set (0.00 sec)

 

2、分析 binlog

#240909 10:53:51 server id 3306  end_log_pos 2661 CRC32 0xe5698ce8     Query    thread_id=12    exec_time=0    error_code=0
SET TIMESTAMP=1725850431/*!*/;
BEGIN
/*!*/;
# at 2661
#240909 10:53:51 server id 3306  end_log_pos 2718 CRC32 0xcbe15e87     Table_map: `ceshi`.`t1` mapped to number 131
# has_generated_invisible_primary_key=0
# at 2718
#240909 10:53:51 server id 3306  end_log_pos 2762 CRC32 0xab449127     Delete_rows: table id 131 flags: STMT_END_F

BINLOG '
P2PeZhPqDAAAOQAAAJ4KAAAAAIMAAAAAAAMABWNlc2hpAAJ0MQACAw8CIAMCAQEAAgP8/wCHXuHL
P2PeZiDqDAAALAAAAMoKAAAAAIMAAAAAAAMAAgAC/wACAAAAAgBscyeRRKs=
'/*!*/;
### DELETE FROM `ceshi`.`t1`
### WHERE
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='ls' /* VARSTRING(800) meta=800 nullable=1 is_null=0 */
# at 2762
#240909 10:53:51 server id 3306  end_log_pos 2793 CRC32 0x11115280     Xid = 93
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;

 

 

 

标签:binlog,affected,只记,sec,mysql,Query,主表,id,select
From: https://www.cnblogs.com/nanxiang/p/18404147

相关文章

  • [Kafka]binlog kafka并行消费提升小窍门
      线上库存Process实例配置详情:  背景:1.业务是通过监听上游mysqlbinlog完成的2.binlog是通过DDHkafka下发的3.consumer消费已经做到了7ms的单条消息消费性能优化4.怎样还能再提醒消费方的消费速率呢?5.当先consumer实例蓝绿组共12个实例2C2G(CPU2......
  • 【MySQL】binlog常见问题
    https://mp.weixin.qq.com/s/n9vWkee2N-gpXpOHIYtRJg关于binlog,这里常见的问题如下:binlog是什么binlog的配置和查看binlog的类型binlog如何数据恢复binlog是逻辑日志还是物理日志binlog的作用binlog是什么binlo(binarylog),是MySql的二进制日志文件,这个文件记录了我们所......
  • Mysql 通过binlog日志恢复数据
    Binlog日志,即binarylog,是二进制日志文件,有两个作用,一个是增量备份,另一个是主从复制,即主节点维护一个binlog日志文件,从节点从binlog中同步数据,也可以通过binlog日志来恢复数据1,登录mysql查看binlog日志的状态,输入showvariableslike‘%log_bin%’;查看binlog为off关闭状态2,开......
  • mysql查看binlog日志
      业内目前推荐使用的是row模式,准确性高,虽然说文件大,但是现在有SSD和万兆光纤网络,这些磁盘IO和网络IO都是可以接受的。 那么,大家一定想问,为什么不推荐使用mixed模式,理由如下假设master有两条记录,而slave只有一条记录。master的数据为+----+------------------------------......
  • linux下试验中间件canal的example示例-binlog日志的实时获取显示以及阿里巴巴中间件ca
    一、linux下试验中间件canal的example示例-binlog日志的实时获取显示    今天重装mysql后,进行了canal的再次试验,原来用的mysql5.7,今天重装直接换了5.6算了。反正测试服务器的mysql也不常用。canal启动后日志显示examplepreparetofindstartpositionjustshowmaste......
  • mysql8.x通过备份文件及binlog日志恢复数据
    问题简述记一次mysql数据库被误删(是整个库被删了)后的还原前提条件数据库版本为mysql8.x以上具有库被删除前的完整备份数据库开启binlog最近备份时间不能超过日志删除时间#查看数据库是否开启binlogshowvariableslike'log_bin';#默认binlog存储位置/var/lib/mysql......
  • 删库了不用跑路!binlog恢复数据实操
    各位道友大家好呀!想必道友们或多或少都听说过MySQL的binlog的作用,它记录了数据库整个的生命周期,可用于恢复数据或者从库同步数据。那么如果发生了数据库误删,具体该怎样恢复数据呢?下面就以一个例子来给道友们演示一下,让我们开始吧!doit!数据备份首先,数据库要定时进行备份,因为如......
  • MySQL 备库可以设置 sync_binlog 非 1 吗?【转】
    众所周知,防止断电丢失Binlog、故障恢复过程丢失数据,MySQL主库必须设置sync_binlog=1。那么作为备库可以例外吗?我们的第一反应当然是不行,既然主库会丢数据,备库自然一样。但其实不然,备库丢了数据是可以重新从主库上复制的,只要这个复制的位置和备库本身数据的位置一致就OK了,它......
  • ABAP 宿主表达式(Host Expressions)
    ABAP宿主表达式是一种在ABAP7.40及更高版本中引入的特性,‌它允许在SQL表达式的操作数位置或编写SQL语句的工作区中使用任何ABAP表达式。‌ 这种表达式通过在表达式前加上@符号来标识,‌形式为@(abap_expression)。‌宿主表达式的引入,‌使得ABAP开发者能够更灵活地在SQL查询中使用......
  • 使用Canal监听Binlog将数据发送到RocketMQ
    文章目录一、部署RocketMQ二、部署MySQL1、开启mysql的binlog写入功能2、创建一个有相关权限的mysqlslave账号三、部署Canal1、修改conf/canal.properties配置文件2、修改conf/example/instance.properties配置文件四、实际操作一、部署RocketMQwin11部署Rocke......