MHA binlog server

GTID模式下切换的时候,默认是不会去Master上获取binlog,如果配置了Binlog Server,MHA机会去binlog-server上获取,下面创建一个binlog server,并验证binlog server能够在主库binlog被清理的情况下提供日志恢复.

搭建binlog_server

创建目录:创建binlog_server用于存放master上的binlog

bash 复制代码
[root@mha2 binlog_server]# mkdir binlog_server
[root@mha2 binlog_server]# pwd
/export/binlog_server

开启远程备份binlog

bash 复制代码
nohup mysqlbinlog --no-defaults --read-from-remote-server --raw --result-file=/export/binlog_server/ --host=192.168.10.129 --port=3358 --user=mha --password=abcd1234 --stop-never mysql-bin.000079 &

--raw: 以原始格式输出,而不是解析为SQL语句

--result-file: 本地路径,用于保存binlog

--user=mha: mysql账户,用于连接mysql并备份binlog,需要有replication cilent权限

--stop-never: 不停

mysql-bin.000060: 在master上第一个binlog,从这个binlog开始向后备份

更新配置:配置中添加了[binlog1] 部分,且[server2]添加了no_master=1

binlog server和master不要放在同一个机器,否则主库挂了同时binlog server也挂了;

bash 复制代码
[server default]
manager_workdir=/export/mha/app1
manager_log=/export/mha/app1/manager.log
master_binlog_dir=/export/data/mysql
log_level=debug
user=mha
password=abcd1234
ping_interval=3
ping_type=INSERT
remote_workdir=/export/mha/app1
ssh_user=root
repl_user=repl
repl_password=abcd1234
 
[server1]
hostname=192.168.10.129
candidate_master=1
port=3358
master_binlog_dir=/export/data/mysql
 
[server2]
no_master=1
hostname=192.168.10.163
port=3358
master_binlog_dir=/export/data/mysql
 
[server3]
hostname=192.168.10.162
port=3358
master_binlog_dir=/export/data/mysql

[binlog1]
hostname=192.168.10.163
master_binlog_dir=/export/binlog_server

查看同步

查看master中的binlog和binlog_server中对比,是否获取了最新的binlog,切master flush的时候从库也会同步更新.

bash 复制代码
# master
mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000079 |      3699 |
| mysql-bin.000080 |       257 |
| mysql-bin.000081 |       234 |
+------------------+-----------+
3 rows in set (0.00 sec)

# slave
[root@mha2 binlog_server]# ls
mysql-bin.000079  mysql-bin.000080  mysql-bin.000081 

模拟故障

1,slave: stop slave io_thread

2, 开启MHA

3, master: 执行一些事务

sql 复制代码
mysql> insert into hero3(name,age) values(name,age);
Query OK, 1 row affected (0.03 sec)

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

mysql> insert into hero3(name,age) values(name,age);
Query OK, 1 row affected (0.00 sec)

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

mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000079 |      3699 |
| mysql-bin.000080 |       257 |
| mysql-bin.000081 |    123737 |
| mysql-bin.000082 |     89801 |
| mysql-bin.000083 |       281 |
| mysql-bin.000084 |      2667 |
| mysql-bin.000085 |       546 |
| mysql-bin.000086 |       234 |
+------------------+-----------+
8 rows in set (0.00 sec)

4, 查看主从数据差异

sql 复制代码
# master
mysql> select count(1) from hero3;
+----------+
| count(1) |
+----------+
|     7170 |
+----------+
1 row in set (0.01 sec)

# slave:
mysql> select count(1) from hero3;
+----------+
| count(1) |
+----------+
|     7168 |
+----------+
1 row in set (0.02 sec)

5, 清理主库所有binlog

sql 复制代码
mysql> purge binary logs to 'mysql-bin.000086';
Query OK, 0 rows affected (0.00 sec)

6, 将主库关闭实例

此时触发故障切换

7, 查看新主从上的数据,同步了刚刚丢失的数据

bash 复制代码
mysql> select count(1) from hero3;
+----------+
| count(1) |
+----------+
|     7170 |
+----------+
1 row in set (0.00 sec)

8,查看切换日志

bash 复制代码
From:
192.168.10.129(192.168.10.129:3358) (current master)
 +--192.168.10.163(192.168.10.163:3358)
 +--192.168.10.162(192.168.10.162:3358)

To:
192.168.10.162(192.168.10.162:3358) (new master)
 +--192.168.10.163(192.168.10.163:3358)
on Dec 23 23:58:36 2024 - [info] Fetching binary logs from binlog server 192.168.10.163..
Mon Dec 23 23:58:36 2024 - [info] Executing binlog save command: save_binary_logs --command=save --start_file=mysql-bin.000082  --start_pos=89047 --output_file=/export/mha/app1/saved_binlog_binlog1_20241223235834.binlog --handle_raw_binlog=0 --skip_filter=1 --disable_log_bin=0 --manager_version=0.58 --oldest_version=5.7.19-log  --debug  --binlog_dir=/export/binlog_server 
  Creating /export/mha/app1 if not exists..    ok.
 Concat binary/relay logs from mysql-bin.000082 pos 89047 to mysql-bin.000086 EOF into /export/mha/app1/saved_binlog_binlog1_20241223235834.binlog ..
Executing command: mysqlbinlog --start-position=89047  /export/binlog_server/mysql-bin.000082 >> /export/mha/app1/saved_binlog_binlog1_20241223235834.binlog
Executing command: mysqlbinlog --start-position=4  /export/binlog_server/mysql-bin.000083 >> /export/mha/app1/saved_binlog_binlog1_20241223235834.binlog
Executing command: mysqlbinlog --start-position=4  /export/binlog_server/mysql-bin.000084 >> /export/mha/app1/saved_binlog_binlog1_20241223235834.binlog
Executing command: mysqlbinlog --start-position=4  /export/binlog_server/mysql-bin.000085 >> /export/mha/app1/saved_binlog_binlog1_20241223235834.binlog
Executing command: mysqlbinlog --start-position=4  /export/binlog_server/mysql-bin.000086 >> /export/mha/app1/saved_binlog_binlog1_20241223235834.binlog
 Concat succeeded.
Mon Dec 23 23:58:37 2024 - [info] scp from root@192.168.10.163:/export/mha/app1/saved_binlog_binlog1_20241223235834.binlog to local:/export/mha/app1/saved_binlog_192.168.10.163_binlog1_20241223235834.binlog succeeded.
Mon Dec 23 23:58:37 2024 - [info] End of log messages from 192.168.10.163.
Mon Dec 23 23:58:37 2024 - [info] Saved mysqlbinlog size from 192.168.10.163 is 134787 bytes.
Mon Dec 23 23:58:37 2024 - [info] Checking if super_read_only is defined and turned on..
Mon Dec 23 23:58:37 2024 - [info]  not present or turned off, ignoring.
Mon Dec 23 23:58:37 2024 - [info] Applying differential binlog /export/mha/app1/saved_binlog_192.168.10.163_binlog1_20241223235834.binlog ..
Mon Dec 23 23:58:38 2024 - [info] Differential log apply from binlog server succeeded.
Mon Dec 23 23:58:38 2024 - [info] Getting new master's binlog name and position..

日志中可以看到去访问binlog,获取binlog,并应用binlog

相关推荐
zzb15809 小时前
RAG from Scratch-优化-query
java·数据库·人工智能·后端·spring·mybatis
一只鹿鹿鹿10 小时前
信息安全等级保护安全建设防护解决方案(总体资料)
运维·开发语言·数据库·面试·职场和发展
堕27410 小时前
MySQL数据库《基础篇--数据库索引(2)》
数据库·mysql
wei_shuo10 小时前
数据库优化器进化论:金仓如何用智能下推把查询时间从秒级打到毫秒级
数据库·kingbase·金仓
71-310 小时前
MySQL的安装和卸载组件
笔记·学习·mysql
雷工笔记10 小时前
Navicat Premium 17 软件安装记录
数据库
wenlonglanying11 小时前
Ubuntu 系统下安装 Nginx
数据库·nginx·ubuntu
数据库小组11 小时前
10 分钟搞定!Docker 一键部署 NineData 社区版
数据库·docker·容器·database·数据库管理工具·ninedata·迁移工具
爬山算法11 小时前
MongoDB(38)如何使用聚合进行投影?
数据库·mongodb
l1t11 小时前
Deep Seek总结的APSW 和 SQLite 的关系
数据库·sqlite