MySQL主库binlog(master-log)与从库relay-log关系代码详解

 更新时间:2017年10月12日 11:50:51   作者:遥望青春  
这篇文章主要介绍了MySQL主库binlog与从库relay-log关系的相关内容,涉及部分代码,需要的朋友可以参考。

主库binlog:

# at 2420
#170809 17:16:20 server id 1882073306 end_log_pos 2451 CRC32 0x58f2db87  Xid = 32880
COMMIT/*!*/;
# at 2451
#170814 11:07:18 server id 1882073306 end_log_pos 2528 CRC32 0x40774a4b  Query thread_id=92 exec_time=0 error_code=0
SET TIMESTAMP=1502680038/*!*/;
BEGIN
/*!*/;
# at 2528
# at 2560
#170814 11:07:18 server id 1882073306 end_log_pos 2560 CRC32 0x7bdf274b  Intvar
SET INSERT_ID=107/*!*/;
#170814 11:07:18 server id 1882073306 end_log_pos 2669 CRC32 0x68e441c8  Query thread_id=92 exec_time=0 error_code=0
SET TIMESTAMP=1502680038/*!*/;
insert into t2 (name) values ('a100')
/*!*/;
# at 2669
# at 2701
#170814 11:07:27 server id 1882073306 end_log_pos 2701 CRC32 0xcf89b910  Intvar
SET INSERT_ID=108/*!*/;
#170814 11:07:27 server id 1882073306 end_log_pos 2810 CRC32 0x78466d7b  Query thread_id=92 exec_time=0 error_code=0
SET TIMESTAMP=1502680047/*!*/;
insert into t2 (name) values ('a200')
/*!*/;
# at 2810
# at 2842
#170814 11:07:30 server id 1882073306 end_log_pos 2842 CRC32 0x1e5a0847  Intvar
SET INSERT_ID=109/*!*/;
#170814 11:07:30 server id 1882073306 end_log_pos 2951 CRC32 0xebeb947c  Query thread_id=92 exec_time=0 error_code=0
SET TIMESTAMP=1502680050/*!*/;
insert into t2 (name) values ('a300')
/*!*/;
# at 2951
#170814 11:07:34 server id 1882073306 end_log_pos 2982 CRC32 0x6436ad60  Xid = 32934
COMMIT/*!*/;

从库relay-log:

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#170809 17:17:24 server id 1882083306 end_log_pos 120 CRC32 0x5df4221c  Start: binlog v 4, server v 5.6.23-72.1-log created 170809 17:17:24
# at 120
#700101 8:00:00 server id 1882073306 end_log_pos 0 CRC32 0x0b8a412f Rotate to test-mysql-bin.000116 pos: 2451
# at 172
#170809 16:28:12 server id 1882073306 end_log_pos 0 CRC32 0xd0d3bf30 Start: binlog v 4, server v 5.6.23-72.1-log created 170809 16:28:12
# at 288
#170814 11:07:18 server id 1882073306 end_log_pos 2528 CRC32 0x40774a4b  Query thread_id=92 exec_time=0 error_code=0
SET TIMESTAMP=1502680038/*!*/;
SET @@session.pseudo_thread_id=92/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1073741824/*!*/;
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/*!*/;
BEGIN
/*!*/;
# at 365
# at 397
#170814 11:07:18 server id 1882073306 end_log_pos 2560 CRC32 0x7bdf274b  Intvar
SET INSERT_ID=107/*!*/;
#170814 11:07:18 server id 1882073306 end_log_pos 2669 CRC32 0x68e441c8  Query thread_id=92 exec_time=0 error_code=0
use `db1`/*!*/;
SET TIMESTAMP=1502680038/*!*/;
insert into t2 (name) values ('a100')
/*!*/;
# at 506
# at 538
#170814 11:07:27 server id 1882073306 end_log_pos 2701 CRC32 0xcf89b910  Intvar
SET INSERT_ID=108/*!*/;
#170814 11:07:27 server id 1882073306 end_log_pos 2810 CRC32 0x78466d7b  Query thread_id=92 exec_time=0 error_code=0
SET TIMESTAMP=1502680047/*!*/;
insert into t2 (name) values ('a200')
/*!*/;
# at 647
# at 679
#170814 11:07:30 server id 1882073306 end_log_pos 2842 CRC32 0x1e5a0847  Intvar
SET INSERT_ID=109/*!*/;
#170814 11:07:30 server id 1882073306 end_log_pos 2951 CRC32 0xebeb947c  Query thread_id=92 exec_time=0 error_code=0
SET TIMESTAMP=1502680050/*!*/;
insert into t2 (name) values ('a300')
/*!*/;
# at 788
#170814 11:07:34 server id 1882073306 end_log_pos 2982 CRC32 0x6436ad60  Xid = 32934
COMMIT/*!*/;

注意relay log的这一行:

#700101  8:00:00 server id 1882073306  end_log_pos 0 CRC32 0x0b8a412f  Rotate to test-mysql-bin.000116  pos: 2451

说明此relay log保存的是主库 test-mysql-bin.000116 的信息,从position 2451 开始。

看一个具体的对应关系:

主库的binlog如下:

# at 2560
#170814 11:07:18 server id 1882073306 end_log_pos 2560 CRC32 0x7bdf274b Intvar
SET INSERT_ID=107/*!*/;
#170814 11:07:18 server id 1882073306 end_log_pos 2669 CRC32 0x68e441c8 Query thread_id=92 exec_time=0 error_code=0
SET TIMESTAMP=1502680038/*!*/;
insert into t2 (name) values ('a100')
/*!*/;
# at 2669

对应从库relay-log如下几行:

# at 397
#170814 11:07:18 server id 1882073306 end_log_pos 2560 CRC32 0x7bdf274b  Intvar
SET INSERT_ID=107/*!*/;
#170814 11:07:18 server id 1882073306 end_log_pos 2669 CRC32 0x68e441c8  Query thread_id=92 exec_time=0 error_code=0
use `db1`/*!*/;
SET TIMESTAMP=1502680038/*!*/;
insert into t2 (name) values ('a100')
/*!*/;
# at 506

另外注意show slave status\G的以下几行的关系:

Master_Log_File: test-mysql-bin.000117
Read_Master_Log_Pos: 774

上面二行代表IO线程,相对于主库

Relay_Log_File: relay-log.000038
Relay_Log_Pos: 723

上面二行代表了sql线程,相对于从库

Relay_Master_Log_File: test-mysql-bin.000117
Exec_Master_Log_Pos: 555

上面二行代表了sql线程,相对主库

其中Relay_Log_Pos: 723 和 Exec_Master_Log_Pos: 555 对应的sql语句一致。

总结

以上就是本文关于MySQL主库binlog与从库relay-log关系代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅:mysql中binlog_format模式与配置详细分析几个比较重要的MySQL变量MySQL prepare原理详解等,有什么问题可以随时留言,欢迎大家交流讨论。

相关文章

  • Mysql存储过程学习笔记--建立简单的存储过程

    Mysql存储过程学习笔记--建立简单的存储过程

    我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给定参数(如果该存储过程带有参数)来调用执行它。
    2014-08-08
  • mysql 5.1版本修改密码及远程登录mysql数据库的方法

    mysql 5.1版本修改密码及远程登录mysql数据库的方法

    这篇文章主要介绍了mysql 5.1版本修改密码及远程登录mysql数据库的方法,需要的朋友可以参考下
    2017-04-04
  • MySQL5.7.18主从复制搭建(一主一从)教程详解

    MySQL5.7.18主从复制搭建(一主一从)教程详解

    MySQL使用3个线程来执行复制功能(其中1个在主服务器上,另两个在从服务器上。这篇文章主要介绍了MySQL5.7.18主从复制搭建(一主一从)教程详解,需要的朋友可以参考下
    2017-08-08
  • MySQL运行报错:“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法

    MySQL运行报错:“Expression #1 of SELECT list is not in GR

    这篇文章主要给大家介绍了关于MySQL运行报错:“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”的解决方法,文中将解决方法介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • Windows10下MySQL5.7.31解压版安装与卸载方法

    Windows10下MySQL5.7.31解压版安装与卸载方法

    这篇文章主要介绍了Windows10下MySQL5.7.31解压版安装与卸载,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-12-12
  • 深入分析MySQL数据类型 DECIMAL

    深入分析MySQL数据类型 DECIMAL

    这篇文章主要介绍了MySQL数据类型 DECIMAL的相关资料,帮助大家更好的理解和使用MySQL数据库,感兴趣的朋友可以了解下
    2020-09-09
  • Mysql如何查询某条记录在分页的第几页详析

    Mysql如何查询某条记录在分页的第几页详析

    查询是我们日常工作中经常会遇到的一个功能,下面这篇文章主要给大家介绍了关于Mysql如何查询某条记录在分页的第几页的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-11-11
  • MySQL 数据类型详情

    MySQL 数据类型详情

    这篇文章主要介绍了MySQL 数据类型,数值类型分类又分严格数值类型和近似数值数据类型,下面文章围绕MySQL 数据类型展开内容,需要的朋友可以参考一下
    2021-11-11
  • MySQL中的空格处理方法

    MySQL中的空格处理方法

    在MySQL中,空格是一个特殊的字符,本文主要介绍了MySQL中的空格处理方法,具有一定的参考价值,感兴趣的可以了解一下
    2023-11-11
  • MySQL数据库对敏感数据加密及解密的实现方式

    MySQL数据库对敏感数据加密及解密的实现方式

    这篇文章主要介绍了MySQL数据库对敏感数据加密及解密的实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2020-03-03

最新评论