Table of Contents
1. table-map-id 和 Innodb的table-id是否是同一个概念?
2. table-map-id是否和物理文件有绑定关系
3. table_id 和file handler是否有直接联系?
结论:
Home Database Mysql Tutorial Binlog中table_map_id 的探究_MySQL

Binlog中table_map_id 的探究_MySQL

Jun 01, 2016 pm 01:48 PM
surface

bitsCN.com

背景:

最近,线上Row Based Replication(下称RBR)环境中遇到了一个Bug。这个bug简单的描述就是:RBR对于DML需要通过table-map的event来标注每一个有更新的表。

而当一个DML同时操作多个表,且其中2个表的mapid相同时(通常为0),会导致slave执行这个event时crash,并重启mysqld实例

可见这个bug的毁灭性极大。

那么table-map-id 究竟从何而来?有什么办法知道每个表table-map-id,从而进行一些必要的监控呢?

下文将用几个例子来进行分析说明。

 

1. table-map-id 和 Innodb的table-id是否是同一个概念?

其实这个问题的答案是显而易见的。因为并非Innodb的表才支持RBR,如果这个问题答案为“是”,那么非innodb的表在RBR中的table-map-id从何而来呢?又怎么保证和innodb的map-id不重复呢?

所以,显然table-map-id和Innodb数据字典中的table-id是完全不同的两个概念。

即便如此,下面还是用一个实例进行验证

create table map_id_test (ID int primary key);insert into map_id_test values (1);show binlog events in 'log-prefix.000025';
Copy after login

输出结果:

Log_name             Pos     Event_type   Server_id    End_log_pos    Infolog-prefix.000025    2156    Query        15757        2224           BEGINlog-prefix.000025    2224    Table_map    15757        2274           table_id: 88 (test.map_id_test)log-prefix.000025    2274    Write_rows   15757        2308           table_id: 88 flags: STMT_END_Flog-prefix.000025    2308    Xid          15757        2335           COMMIT /* xid=346 */
Copy after login

查看Innodb的table-id:

select TABLE_ID from INNODB_SYS_TABLESTATS where `SCHEMA`='test' and NAME='map_id_test';
Copy after login

得到TABLE_ID = 170

 

 

2. table-map-id是否和物理文件有绑定关系

虽然table-map-id和Innodb的table-id是完全不同的概念。而我们知道Innodb中的table-id和物理文件有绑定关系,即rename table的操作不会改变dict-table中的table-id。

那么binlog中的table-map-id是不是有可能借鉴了这种实现方式,也有这个特性呢?

下面是具体测试过程

set global binlog_format='row';create table map_id_test1 (ID int primary key);create table map_id_test2 (ID int primary key);insert into map_id_test1 values (1);insert into map_id_test2 values (1);show binlog events in 'log-prefix.000025';
Copy after login

输出结果如下:此时table1 对应table_id:83 , tabl2 对应table_id:84 

Log_name             Pos     Event_type   Server_id End_log_pos    Infolog-prefix.000025    1157    Query        15757     1225           BEGINlog-prefix.000025    1225    Table_map    15757     1276           table_id: 83 (test.map_id_test1)log-prefix.000025    1276    Write_rows   15757     1310           table_id: 83 flags: STMT_END_Flog-prefix.000025    1310    Xid          15757     1337           COMMIT /* xid=327 */log-prefix.000025    1337    Query        15757     1405           BEGINlog-prefix.000025    1405    Table_map    15757     1456           table_id: 84 (test.map_id_test2)log-prefix.000025    1456    Write_rows   15757     1490           table_id: 84 flags: STMT_END_Flog-prefix.000025    1490    Xid          15757     1517           COMMIT /* xid=330 */
Copy after login

执行rename table,交换table1和table2

rename table map_id_test1 to map_id_test1_bak,map_id_test2 to map_id_test1, map_id_test1_bak to map_id_test2;
Copy after login

 查看binlog:此时table1 对应table_id:86 , tabl2 对应table_id:87。

Log_name             Pos     Event_type    Server_id    End_log_pos    Infolog-prefix.000025    1688    Query         15757        1756           BEGINlog-prefix.000025    1756    Table_map     15757        1807           table_id: 86 (test.map_id_test1)log-prefix.000025    1807    Write_rows    15757        1841           table_id: 86 flags: STMT_END_Flog-prefix.000025    1841    Xid           15757        1868           COMMIT /* xid=334 */log-prefix.000025    1868    Query         15757        1936           BEGINlog-prefix.000025    1936    Table_map     15757        1987           table_id: 87 (test.map_id_test2)log-prefix.000025    1987    Write_rows    15757        2021           table_id: 87 flags: STMT_END_Flog-prefix.000025    2021    Xid           15757        2048           COMMIT /* xid=335 */
Copy after login

从实验可以得出结论,RBR中的table_id 不仅和物理文件没有绑定关系,在MySQL实例的运行过程中也不是静态不变的。

因此,大胆猜测,table_id 和file handler有关系。下面的测试将进行验证。

 

3. table_id 和file handler是否有直接联系?

insert into map_id_test1 values (3);flush tables;insert into map_id_test1 values (4);show binlog events in 'log-prefix.000025';
Copy after login

执行结果: 从结果可以看出,flush table导致了,file handler的重新打开。同时也使table-map-id 发生了变化,且线性递增。

Log_name             Pos     Event_type    Server_id    End_log_pos    Infolog-prefix.000025    2424    Query         15757        2492           BEGINlog-prefix.000025    2492    Table_map     15757        2543           table_id: 89 (test.map_id_test1)log-prefix.000025    2543    Write_rows    15757        2577           table_id: 89 flags: STMT_END_Flog-prefix.000025    2577    Xid           15757        2604           COMMIT /* xid=383 */log-prefix.000025    2604    Query         15757        2679           use `test`; flush tableslog-prefix.000025    2679    Query         15757        2747           BEGINlog-prefix.000025    2747    Table_map     15757        2798           table_id: 90 (test.map_id_test1)log-prefix.000025    2798    Write_rows    15757        2832           table_id: 90 flags: STMT_END_Flog-prefix.000025    2832    Xid           15757        2859           COMMIT /* xid=385 */
Copy after login

 

结论:

1. RBR中的Table_ID 和Innodb中的table_id 没有关系,且和物理文件没有对应关系。

2. Flush Table 可以重置RBR中的Table_ID ,如果有表遇到了map_id=0 的情况,可以使用这个方法尝试解决问题。

3. 虽然和File Handler 有关,但是和 /proc/$PID/fd/ 中的fd数值没有直接联系

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles