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

相关推荐
异世界贤狼转生码农1 小时前
MongoDB Windows 系统实战手册:从配置到数据处理入门
数据库·mongodb
QuZhengRong1 小时前
【数据库】Navicat 导入 Excel 数据乱码问题的解决方法
android·数据库·excel
码农阿豪1 小时前
Windows从零到一安装KingbaseES数据库及使用ksql工具连接全指南
数据库·windows
冷崖6 小时前
MySQL异步连接池的学习(五)
学习·mysql
时序数据说7 小时前
时序数据库市场前景分析
大数据·数据库·物联网·开源·时序数据库
听雪楼主.11 小时前
Oracle Undo Tablespace 使用率暴涨案例分析
数据库·oracle·架构
我科绝伦(Huanhuan Zhou)11 小时前
KINGBASE集群日常维护管理命令总结
数据库·database
妖灵翎幺11 小时前
Java应届生求职八股(2)---Mysql篇
数据库·mysql
HMBBLOVEPDX11 小时前
MySQL的事务日志:
数据库·mysql
weixin_4196583113 小时前
MySQL数据库备份与恢复
数据库·mysql