5ep9lzv 发表于 2024-10-5 03:44:45

原创 MySQL主从复制没法过滤binlog event的缺陷,ProxySQL给补齐了


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">作者介绍</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">贺春旸,</strong>dbaplus社群金牌专家,凡普金科和爱钱进DBA团队负责人,《MySQL管理之道:性能调优、高可用与监控》<span style="color: black;">第1</span>&amp;二版、《MySQL运维进阶指南》作者,曾任职于中国移动飞信、安卓机锋网。五次荣获dbaplus年度MVP,致力于MariaDB、MongoDB等开源技术的<span style="color: black;">科研</span>,<span style="color: black;">重点</span>负责数据库性能调优、监控和架构设计。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MySQL主从复制,在最新的9.0版本里,仍旧<span style="color: black;">没法</span>实现过滤binlog event事件,例如主库上执行drop和truncate操作,从库同步复制后,直接过滤掉drop和truncate操作。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DBA、运维<span style="color: black;">或</span><span style="color: black;">开发</span>一旦手滑误操作,直接P0,将是毁灭性的<span style="color: black;">损伤</span>。<span style="color: black;">那样</span>,<span style="color: black;">咱们</span>就需要借助ProxySQL来<span style="color: black;">处理</span>MySQL主从复制这一缺陷。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1、</span>思路</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>主库执行drop和truncate,临时关闭binlog,在命令前面<span style="color: black;">增多</span>set sql_log_bin=0,<span style="color: black;">这般</span>主库执行完以后,binlog不会记录,从库<span style="color: black;">亦</span>就不会执行。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2、</span>实施</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">1、安装ProxySQL</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># cat &lt;&lt;EOF | tee /etc/yum.repos.d/proxysql.repo</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">name=ProxySQL repository</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">baseurl=https://repo.proxysql.com/ProxySQL/proxysql-2.6.x/centos/\$releasever</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">gpgcheck=1</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">gpgkey=https://repo.proxysql.com/ProxySQL/proxysql-2.6.x/repo_pub_key</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EOF</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># yum install proxysql -y</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># systemctl start proxysql</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2、配置ProxySQL规则</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">## 连接到ProxySQL管理界面</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt=Admin&gt; </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-- 配置MySQL服务器 - 写主节点VIP<span style="color: black;">位置</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,127.0.0.1,6666);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-- 配置用户</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">INSERT INTO mysql_users(username,password,default_hostgroup) VALUES (rd,123456,1);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-- 设置<span style="color: black;">查找</span>规则来重写DROP和TRUNCATE语句</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">INSERT INTO mysql_query_rules (rule_id, active, match_pattern, replace_pattern, destination_hostgroup, apply)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">VALUES (1, 1, ^DROP (.*)$, SET sql_log_bin=0; DROP \1; SET sql_log_bin=1;, 1, 1);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">INSERT INTO mysql_query_rules (rule_id, active, match_pattern, replace_pattern, destination_hostgroup, apply)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">VALUES (2, 1, ^TRUNCATE (.*)$, SET sql_log_bin=0; TRUNCATE \1; SET sql_log_bin=1;, 1, 1);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-- 加载配置到运行时环境</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LOAD MYSQL SERVERS TO RUNTIME;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LOAD MYSQL USERS TO RUNTIME;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">LOAD MYSQL QUERY RULES TO RUNTIME;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-- <span style="color: black;">保留</span>配置</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SAVE MYSQL SERVERS TO DISK;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SAVE MYSQL USERS TO DISK;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SAVE MYSQL QUERY RULES TO DISK;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3、这个配置的工作原理如下</strong></p><span style="color: black;">咱们</span>定义了一个主服务器组:主库(hostgroup 1)<span style="color: black;">咱们</span>创建了一个DBA、运维、Java<span style="color: black;">开发</span><span style="color: black;">运用</span>的用户名 rd,默认连接到主库。<span style="color: black;">咱们</span>设置了两个<span style="color: black;">查找</span>规则:规则1和2:匹配DROP和TRUNCATE语句,在它们前面添加SET sql_log_bin=0,这<span style="color: black;">能够</span><span style="color: black;">保证</span><span style="color: black;">仅有</span>特定的DROP或TRUNCATE操作不被记录到binlog,且在执行完后再开启binlog。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如,<span style="color: black;">倘若</span>原始<span style="color: black;">查找</span>是DROP TABLE t6; 重写后的<span style="color: black;">查找</span>将变为:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SET sql_log_bin=0; DROP TABLE t6; SET sql_log_bin=1;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4、<span style="color: black;">运用</span>这种<span style="color: black;">办法</span>的<span style="color: black;">优良</span></strong></p>它只影响DROP和TRUNCATE操作,其他操作不受影响。它自动在这些操作前禁用binlog记录,操作后重新启用,无需手动<span style="color: black;">干涉</span>。这些更改对应用程序是透明的,不需要修改应用代码。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3、</span><span style="color: black;">运用</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DBA、运维<span style="color: black;">或</span>Java<span style="color: black;">开发</span>直接<span style="color: black;">拜访</span>ProxySQL的6033端口:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># mysql -u rd -p123456 -h 127.0.0.1 -P6033</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当执行drop和truncate操作时,只会在主库上执行,从库上不会执行,从而实现了过滤binlog event事件。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">注:rd用户需要在后端MySQL主库上创建。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">举荐</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为了和<span style="color: black;">大众</span><span style="color: black;">一块</span>探索AI<span style="color: black;">关联</span>技术在大数据、数据资产管理、数据库、运维等<span style="color: black;">行业</span>的最佳落<span style="color: black;">地区</span>式,挖掘由此激发的软件发展和技术进步,<strong style="color: blue;">第九届DAMS中国数据智能管理峰会将于2024年11月29日在上海举办</strong>,携手一众产学研界技术领跑单位,带来新思路、重实践、可落地的全日干货盛宴。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">活动详情:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://www.bagevent.com/event/8805002?bag_track=WAZ</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">返回<span style="color: black;">外链论坛: http://www.fok120.com</span>,查看<span style="color: black;">更加多</span></span></a></p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">责任编辑:网友投稿</span></p>




wrjc1hod 发表于 2024-11-5 04:34:33

感谢楼主的分享!我学到了很多。

qzmjef 发表于 前天 08:24

感谢您的精彩评论,为我带来了新的思考角度。
页: [1]
查看完整版本: 原创 MySQL主从复制没法过滤binlog event的缺陷,ProxySQL给补齐了