• mysql---MHA(高可用)


    MHA概述

    magterhight availabulity :基于主库的高可用环境下,主故障切换
    基础要求:主从架构 (一主两从)
    解决mysql的单点故障问题,一旦数据库崩溃,MHA会在0-30s内这东东完成故障切换。
    复制方式:半同步复制

    MHA原理
    数据库不需要抢占,只需要主从同步即可,只要有一台数据库服务器写入,就会自动提交客户端。
    master崩溃,slave 就会从主的二进制日志文件保存文件
    slave识别最新的更新日志
    差异部分同步到slave

    其他的slave继续和新的master同步

    MHA工作原理总结如下:

    1、从宕机崩溃的master保存二进制日志事件(binlog  events);
    2、识别含有最新的更新 slave 日志
    3、应用差异的中继日志(relay log)到其他的slave
    4、应用从master保存的二进制日志事件
    5、提升一个 salve 为新的master
    6、使其他的slave连接行的master 进行复制。

    MHA :为了解决的了是故障切换、数据尽可能的保存,以及所有节点日志的一致性

    实验思路

    1.MHA架构
    1)数据库安装
    2)一主两从
    3)MHA搭建

    2.故障模拟
    1)主库失效
    2)备选主库成为主库
    3)原故障主库恢复重新加入到MHA成为从库

    组件

    node组件---》都需要部署在所有的服务器上。manager组件依赖于node组件,node组件监控mysql的状态。
    node组件靠ssh来进行通信

    实验操作

    一、主从复制

    1. 组件
    2. MHA manager 节点服务器:CentOS7.6(64 位) manager/192.168.233.10 ,安装MHA node 和 manager 组件
    3. Master 节点服务器:CentOS7.6(64 位) mysql1/192.168.233.21 ,安装mysql5.7、MHA node 组件
    4. Slave1 节点服务器:CentOS7.6(64 位) mysql2/192.168.233.22 ,安装mysql5.7、MHA node 组件
    5. Slave2 节点服务器:CentOS7.6(64 位) mysql3/192.168.233.23 ,安装mysql5.7、MHA node 组件
    6. systemctl stop firewalld
    7. systemctl disable firewalld
    8. setenforce 0
    9. 1.Master、Slave1、Slave2 节点上安装 mysql5.7
    10. 2.修改 Master、Slave1、Slave2 节点的主机名
    11. hostnamectl set-hostname Master
    12. hostnamectl set-hostname Slave1
    13. hostnamectl set-hostname Slave2
    14. 3.修改 Master、Slave1、Slave2 节点的 Mysql主配置文件/etc/my.cnf
    15. ##Master 节点##
    16. vim /etc/my.cnf
    17. [mysqld]
    18. server-id = 1
    19. log_bin = master-bin
    20. log-slave-updates = true
    21. log_bin = master-bin:
    22. 用于记录主服务器上的更改操作的日志文件。
    23. 这个配置用于主服务器,将生成的二进制日志文件保存为"master-bin"(可以是其他自定义的名称)。
    24. log-slave-updates = true
    25. 从服务器是否要记录它自己执行的更改操作到自己的二进制日志文件中。
    26. 设置为"true"表示从服务器会记录自己执行的更改操作,将其写入从服务器的二进制日志文件中。
    27. systemctl restart mysqld
    28. ##Slave1 节点##
    29. vim /etc/my.cnf
    30. server-id = 2 #三台服务器的 server-id 不能一样
    31. log_bin = master-bin
    32. relay-log = relay-log-bin
    33. relay-log-index = slave-relay-bin.index
    34. log_bin = master-bin:
    35. 指定主服务器(master)的二进制日志文件名称,用于记录主服务器上的更改操作的日志文件。
    36. relay-log = relay-log-bin:
    37. 指定从服务器的中继日志文件名称,即用于记录主服务器的二进制日志在从服务器上执行的中继日志。
    38. 从服务器会读取主服务器的二进制日志并将其记录到中继日志中。这个配置用于从服务器。
    39. relay-log-index = slave-relay-bin.index
    40. 指定从服务器的中继日志索引文件的名称,该索引文件用于跟踪中继日志文件的位置和顺序。
    41. 通过这个索引文件,从服务器知道哪个中继日志文件是下一个要读取和执行的。这个配置用于从服务器。
    42. systemctl restart mysqld
    43. ###Slave2 节点##
    44. vim /etc/my.cnf #三台服务器的 server-id 不能一样
    45. server-id = 3
    46. relay-log = relay-log-bin
    47. relay-log-index = slave-relay-bin.index
    48. #slave2不用设置master,指定主的备服务器为slave1即可。
    49. systemctl restart mysqld
    50. 4.在 Master、Slave1、Slave2 节点上都创建两个软链接
    51. ln -s /usr/local/mysql/bin/mysql /usr/sbin/
    52. ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
    53. 5.配置 mysql 一主两从
    54. 1)所有数据库节点进行 mysql 授权
    55. mysql -u root -p123456
    56. #从数据库同步使用
    57. grant replication slave on *.* to 'myslave'@'192.168.10.%' identified by '123456';
    58. #manager 使用
    59. grant all privileges on *.* to 'mha'@'192.168.10.%' identified by 'manager';
    60. #防止从库通过主机名连接不上主库
    61. grant all privileges on *.* to 'mha'@'master' identified by 'manager';
    62. grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
    63. grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
    64. flush privileges;
    65. 2)在 Master 节点查看二进制文件和同步点
    66. show master status;
    67. +-------------------+----------+--------------+------------------+-------------------+
    68. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    69. +-------------------+----------+--------------+------------------+-------------------+
    70. | master-bin.000001 | 1747 | | | |
    71. +-------------------+----------+--------------+------------------+-------------------+
    72. 3)在 Slave1、Slave2 节点执行同步操作
    73. change master to master_host='192.168.10.100',master_user='myslave',master_password='123456',master_log_file='master-bin.000004',master_log_pos=154;
    74. start slave;
    75. 4)在 Slave1、Slave2 节点查看数据同步结果
    76. show slave status\G;
    77. //确保 IO 和 SQL 线程都是 Yes,代表同步正常。
    78. Slave_IO_Running: Yes
    79. Slave_SQL_Running: Yes
    80. 5)两个从库必须设置为只读模式:
    81. set global read_only=1;
    82. 6)插入数据测试数据库同步
    83. ##在 Master 主库插入条数据,测试是否同步##
    84. create database test_db;

    二、安装MHA所有组件

    1. 6.安装 MHA 软件
    2. 1)所有服务器上都安装 MHA 依赖的环境,首先安装 epel 源
    3. yum install epel-release --nogpgcheck -y
    4. yum install -y perl-DBD-MySQL \
    5. perl-Config-Tiny \
    6. perl-Log-Dispatch \
    7. perl-Parallel-ForkManager \
    8. perl-ExtUtils-CBuilder \
    9. perl-ExtUtils-MakeMaker \
    10. perl-CPAN
    11. 2)安装 MHA 软件包,先在所有服务器上必须先安装 node 组件
    12. 对于每个操作系统版本不一样,这里 CentOS7.6选择 0.57 版本。
    13. 在所有服务器上必须先安装 node 组件,最后在 MHA-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件。
    14. cd /opt
    15. tar zxvf mha4mysql-node-0.57.tar.gz
    16. cd mha4mysql-node-0.57
    17. perl Makefile.PL
    18. make && make install
    19. 3)在 MHA manager 节点上安装 manager 组件
    20. cd /opt
    21. tar zxvf mha4mysql-manager-0.57.tar.gz
    22. cd mha4mysql-manager-0.57
    23. perl Makefile.PL
    24. make && make install
    25. ----------------------------------------------------------------------------------------------------------
    26. #manager 组件安装后在/usr/local/bin 下面会生成几个工具,主要包括以下几个:
    27. masterha_check_ssh 检查 MHA 的 SSH 配置状况
    28. masterha_check_repl 检查 MySQL 复制状况
    29. masterha_manger 启动 manager的脚本
    30. masterha_check_status 检测当前 MHA 运行状态
    31. masterha_master_monitor 检测 master 是否宕机
    32. masterha_master_switch 控制故障转移(自动或者 手动)
    33. masterha_conf_host 添加或删除配置的 server 信息
    34. masterha_stop 关闭manager
    35. #node 组件安装后也会在/usr/local/bin 下面会生成几个脚本
    36. (这些工具通常由 MHAManager 的脚本触发,无需人为操作)主要如下:
    37. save_binary_logs 保存和复制 master 的二进制日志
    38. apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的 slave
    39. filter_mysqlbinlog 去除不必要的 ROLLBACK 事件(MHA 已不再使用这个工具)
    40. purge_relay_logs 清除中继日志(不会阻塞 SQL 线程)
    41. ----------------------------------------------------------------------------------------------------------
    42. 7.在所有服务器上配置无密码认证
    43. 1)在 manager 节点上配置到所有数据库节点的无密码认证
    44. ssh-keygen -t rsa #一路按回车键
    45. ssh-copy-id 192.168.10.102
    46. ssh-copy-id 192.168.10.101
    47. ssh-copy-id 192.168.10.100
    48. 2)在 master 上配置到数据库节点 slave1 和 slave2 的无密码认证
    49. ssh-keygen -t rsa
    50. ssh-copy-id 192.168.233.22
    51. ssh-copy-id 192.168.233.23
    52. 3)在 slave1 上配置到数据库节点 master 和 slave2 的无密码认证
    53. ssh-keygen -t rsa
    54. ssh-copy-id 192.168.233.21
    55. ssh-copy-id 192.168.233.23
    56. 4)在 slave2 上配置到数据库节点 master 和 slave1 的无密码认证
    57. ssh-keygen -t rsa
    58. ssh-copy-id 192.168.233.21
    59. ssh-copy-id 192.168.233.22
    60. 8.在 manager 节点上配置 MHA
    61. 1)在 manager 节点上复制相关脚本到/usr/local/bin 目录
    62. cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
    63. //拷贝后会有四个执行文件
    64. ll /usr/local/bin/scripts/
    65. ----------------------------------------------------------------------------------------------------------
    66. master_ip_failover #自动切换时 VIP 管理的脚本
    67. master_ip_online_change #在线切换时 vip 的管理
    68. power_manager #故障发生后关闭主机的脚本
    69. send_report #因故障切换后发送报警的脚本
    70. ----------------------------------------------------------------------------------------------------------
    71. 2)复制上述的自动切换时 VIP 管理的脚本到 /usr/local/bin 目录,
    72. 这里使用master_ip_failover脚本来管理 VIP 和故障切换
    73. cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
    74. 3)修改内容如下:(删除原有内容,直接复制并修改vip相关参数)
    75. vim /usr/local/bin/master_ip_failover
    76. #!/usr/bin/env perl
    77. use strict;
    78. use warnings FATAL => 'all';
    79. use Getopt::Long;
    80. my (
    81. $command, $ssh_user, $orig_master_host, $orig_master_ip,
    82. $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
    83. );
    84. #############################添加内容部分#########################################
    85. my $vip = '192.168.233.100'; #指定vip的地址
    86. my $brdc = '192.168.233.255'; #指定vip的广播地址
    87. my $ifdev = 'ens33'; #指定vip绑定的网卡
    88. my $key = '1'; #指定vip绑定的虚拟网卡序列号
    89. my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; #代表此变量值为ifconfig ens33:1 192.168.233.100
    90. my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; #代表此变量值为ifconfig ens33:1 192.168.233.100 down
    91. my $exit_code = 0; #指定退出状态码为0
    92. #my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
    93. #my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
    94. ##################################################################################
    95. GetOptions(
    96. 'command=s' => \$command,
    97. 'ssh_user=s' => \$ssh_user,
    98. 'orig_master_host=s' => \$orig_master_host,
    99. 'orig_master_ip=s' => \$orig_master_ip,
    100. 'orig_master_port=i' => \$orig_master_port,
    101. 'new_master_host=s' => \$new_master_host,
    102. 'new_master_ip=s' => \$new_master_ip,
    103. 'new_master_port=i' => \$new_master_port,
    104. );
    105. exit &main();
    106. sub main {
    107. print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
    108. if ( $command eq "stop" || $command eq "stopssh" ) {
    109. my $exit_code = 1;
    110. eval {
    111. print "Disabling the VIP on old master: $orig_master_host \n";
    112. &stop_vip();
    113. $exit_code = 0;
    114. };
    115. if ($@) {
    116. warn "Got Error: $@\n";
    117. exit $exit_code;
    118. }
    119. exit $exit_code;
    120. }
    121. elsif ( $command eq "start" ) {
    122. my $exit_code = 10;
    123. eval {
    124. print "Enabling the VIP - $vip on the new master - $new_master_host \n";
    125. &start_vip();
    126. $exit_code = 0;
    127. };
    128. if ($@) {
    129. warn $@;
    130. exit $exit_code;
    131. }
    132. exit $exit_code;
    133. }
    134. elsif ( $command eq "status" ) {
    135. print "Checking the Status of the script.. OK \n";
    136. exit 0;
    137. }
    138. else {
    139. &usage();
    140. exit 1;
    141. }
    142. }
    143. sub start_vip() {
    144. `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
    145. }
    146. ## A simple system call that disable the VIP on the old_master
    147. sub stop_vip() {
    148. `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
    149. }
    150. sub usage {
    151. print
    152. "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
    153. }
    154. ----------------------------------------------------------------------------------------------------------------
    155. #!/usr/bin/env perl
    156. use strict;
    157. use warnings FATAL => 'all';
    158. use Getopt::Long;
    159. my (
    160. $command, $ssh_user, $orig_master_host, $orig_master_ip,
    161. $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
    162. );
    163. my $vip = '192.168.10.99';
    164. my $brdc = '192.168.10.255';
    165. my $ifdev = 'ens33';
    166. my $key = '1';
    167. my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
    168. my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
    169. my $exit_code = 0;
    170. GetOptions(
    171. 'command=s' => \$command,
    172. 'ssh_user=s' => \$ssh_user,
    173. 'orig_master_host=s' => \$orig_master_host,
    174. 'orig_master_ip=s' => \$orig_master_ip,
    175. 'orig_master_port=i' => \$orig_master_port,
    176. 'new_master_host=s' => \$new_master_host,
    177. 'new_master_ip=s' => \$new_master_ip,
    178. 'new_master_port=i' => \$new_master_port,
    179. );
    180. exit &main();
    181. sub main {
    182. print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
    183. if ( $command eq "stop" || $command eq "stopssh" ) {
    184. my $exit_code = 1;
    185. eval {
    186. print "Disabling the VIP on old master: $orig_master_host \n";
    187. &stop_vip();
    188. $exit_code = 0;
    189. };
    190. if ($@) {
    191. warn "Got Error: $@\n";
    192. exit $exit_code;
    193. }
    194. exit $exit_code;
    195. }
    196. elsif ( $command eq "start" ) {
    197. my $exit_code = 10;
    198. eval {
    199. print "Enabling the VIP - $vip on the new master - $new_master_host \n";
    200. &start_vip();
    201. $exit_code = 0;
    202. };
    203. if ($@) {
    204. warn $@;
    205. exit $exit_code;
    206. }
    207. exit $exit_code;
    208. }
    209. elsif ( $command eq "status" ) {
    210. print "Checking the Status of the script.. OK \n";
    211. exit 0;
    212. }
    213. else {
    214. &usage();
    215. exit 1;
    216. }
    217. }
    218. sub start_vip() {
    219. `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
    220. }
    221. ### A simple system call that disable the VIP on the old_master
    222. sub stop_vip() {
    223. `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
    224. }
    225. sub usage {
    226. print
    227. "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
    228. }
    229. 管理 MySQL 主从复制设置中的虚拟 IP 故障切换而设计的,可能用于主服务器需要切换到另一台服务器的情景。
    230. 它使用 SSH 在远程服务器上执行命令来控制虚拟 IP 地址
    231. ----------------------------------------------------------------------------------------------------------------
    232. 4)创建 MHA 软件目录并拷贝配置文件,这里使用app1.cnf配置文件来管理 mysql 节点服务器
    233. mkdir /etc/masterha
    234. cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
    235. vim /etc/masterha/app1.cnf #删除原有内容,直接复制并修改节点服务器的IP地址
    236. [server default]
    237. manager_log=/var/log/masterha/app1/manager.log
    238. manager_workdir=/var/log/masterha/app1
    239. master_binlog_dir=/usr/local/mysql/data
    240. master_ip_failover_script=/usr/local/bin/master_ip_failover
    241. master_ip_online_change_script=/usr/local/bin/master_ip_online_change
    242. password=manager
    243. ping_interval=1
    244. remote_workdir=/tmp
    245. repl_password=123456
    246. repl_user=myslave
    247. secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.10.101 -s 192.168.10.100
    248. #从对主监听
    249. shutdown_script=""
    250. ssh_user=root
    251. user=mha
    252. [server1]
    253. hostname=192.168.10.102
    254. #主服务器
    255. port=3306
    256. [server2]
    257. candidate_master=1
    258. check_repl_delay=0
    259. hostname=192.168.10.101
    260. #备用主服务器
    261. port=3306
    262. [server3]
    263. hostname=192.168.10.100
    264. #从服务器2
    265. port=3306
    266. ----------------------------------------------------------------------------------------------------------
    267. [server default]
    268. manager_log=/var/log/masterha/app1/manager.log      #manager日志
    269. manager_workdir=/var/log/masterha/app1         #manager工作目录
    270. master_binlog_dir=/usr/local/mysql/data/         #master保存binlog的位置,这里的路径要与master里配置的binlog的路径一致,以便MHA能找到
    271. master_ip_failover_script=/usr/local/bin/master_ip_failover  #设置自动failover时候的切换脚本,也就是上面的那个脚本
    272. master_ip_online_change_script=/usr/local/bin/master_ip_online_change  #设置手动切换时候的切换脚本
    273. password=manager #设置mysql中root用户的密码,这个密码是前文中创建监控用户的那个密码
    274. ping_interval=1 #设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行failover
    275. remote_workdir=/tmp #设置远端mysql在发生切换时binlog的保存位置
    276. repl_password=123 #设置复制用户的密码
    277. repl_user=myslave #设置复制用户的用户
    278. report_script=/usr/local/send_report     #设置发生切换后发送的报警的脚本
    279. secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.233.22 -s 192.168.233.23 #指定检查的从服务器IP地址
    280. shutdown_script="" #设置故障发生后关闭故障主机脚本(该脚本的主要作用是关闭主机防止发生脑裂,这里没有使用)
    281. ssh_user=root #设置ssh的登录用户名
    282. user=mha #设置监控用户root
    283. [server1]
    284. hostname=192.168.233.21
    285. port=3306
    286. [server2]
    287. hostname=192.168.233.22
    288. port=3306
    289. candidate_master=1
    290. #设置为候选master,设置该参数以后,发生主从切换以后将会将此从库提升为主库,
    291. 即使这个从库不是集群中最新的slave
    292. check_repl_delay=0
    293. #默认情况下如果一个slave落后master 超过100M的relay logs的话,MHA将不会选择该slave作为一个新的master,
    294. 因为对于这个slave的恢复需要花费很长时间;通过设置check_repl_delay=0
    295. MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,
    296. 因为这个候选主在切换的过程中一定是新的master
    297. [server3]
    298. hostname=192.168.233.23
    299. port=3306

    三、测试是否成功

    1. 9.第一次配置需要在 Master 节点上手动开启虚拟IP
    2. /sbin/ifconfig ens33:1 192.168.10.110/24
    3. 10.在 manager 节点上测试 ssh 无密码认证,如果正常最后会输出 successfully,如下所示。
    4. masterha_check_ssh -conf=/etc/masterha/app1.cnf
    5. Tue Nov 26 23:09:45 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
    6. Tue Nov 26 23:09:45 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
    7. Tue Nov 26 23:09:45 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
    8. Tue Nov 26 23:09:45 2020 - [info] Starting SSH connection tests..
    9. Tue Nov 26 23:09:46 2020 - [debug]
    10. Tue Nov 26 23:09:45 2020 - [debug] Connecting via SSH from root@192.168.80.11(192.168.80.11:22) to root@192.168.80.12(192.168.80.12:22)..
    11. Tue Nov 26 23:09:46 2020 - [debug] ok.
    12. Tue Nov 26 23:09:47 2020 - [debug]
    13. Tue Nov 26 23:09:46 2020 - [debug] Connecting via SSH from root@192.168.80.12(192.168.80.12:22) to root@192.168.80.11(192.168.80.11:22)..
    14. Tue Nov 26 23:09:47 2020 - [debug] ok.
    15. Tue Nov 26 23:09:47 2020 - [info] All SSH connection tests passed successfully.
    16. 11.在 manager 节点上测试 mysql 主从连接情况,最后出现 MySQL Replication Health is OK 字样说明正常。如下所示。
    17. masterha_check_repl -conf=/etc/masterha/app1.cnf
    18. Tue Nov 26 23:10:29 2020 - [info] Slaves settings check done.
    19. Tue Nov 26 23:10:29 2020 - [info]
    20. 192.168.80.11(192.168.80.11:3306) (current master)
    21. +--192.168.80.12(192.168.80.12:3306)
    22. Tue Nov 26 23:10:29 2020 - [info] Checking replication health on 192.168.80.12..
    23. Tue Nov 26 23:10:29 2020 - [info] ok.
    24. Tue Nov 26 23:10:29 2020 - [info] Checking master_ip_failover_script status:
    25. Tue Nov 26 23:10:29 2020 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.80.11 --orig_master_ip=192.168.80.11 --orig_master_port=3306
    26. IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 192.168.80.200===
    27. Checking the Status of the script.. OK
    28. Tue Nov 26 23:10:29 2020 - [info] OK.
    29. Tue Nov 26 23:10:29 2020 - [warning] shutdown_script is not defined.
    30. Tue Nov 26 23:10:29 2020 - [info] Got exit code 0 (Not master dead).
    31. MySQL Replication Health is OK.
    32. 12.在 manager 节点上启动 MHA
    33. nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &

    四、故障模拟

    1. 1、systemctl stop mysqld
    2. 此时vip转移到新主
    3. 2、停止新主的mysql服务
    4. 修改后的:
    5. systemctl stop mysqld
    6. vim /etc/my.cnf
    7. ----------------------
    8. server-id = 2
    9. log_bin = master-bin
    10. log-slave-updates = true
    11. ----------------------
    12. systemctl start mysqld.service
    13. 原主
    14. vim /etc/my.cnf
    15. ----------------------
    16. server-id = 1
    17. log_bin = master-bin
    18. relay-log = relay-log-bin
    19. relay-log-index = slave-relay-bin.index
    20. ----------------------
    21. systemctl start mysqld.service
    22. 3、在 manager 节点上修改配置文件app1.cnf
    23. (再把这个记录添加进去,因为它检测掉失效时候会自动消失)
    24. vim /etc/masterha/app1.cnf
    25. 修改
    26. 旧主的server
    27. secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.10.101 -s 192.168.10.102 监控主的设备
    28. [server1]
    29. hostname=192.168.10.100
    30. port=3306
    31. [server2]
    32. hostname=192.168.10.102
    33. port=3306
    34. candidate_master=1
    35. check_repl_delay=0
    36. [server3]
    37. hostname=192.168.10.101
    38. port=3306
    39. nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
    40. 新主和就主进入数据库
    41. stop slave;
    42. 在旧主--------------------------------------------
    43. change master to master_host='192.168.10.100',master_user='myslave',master_password='123456',master_log_file='master-bin.000004',master_log_pos=154;
    44. reset slave;
    45. start slave;
    46. show slave status\G; 查看二进制文件和同步点
    47. //确保 IO 和 SQL 线程都是 Yes,代表同步正常
    48. Slave_IO_Running: Yes
    49. Slave_SQL_Running: Yes
    50. ---------------------------------------------------
    51. 验证
    52. 在新主终端创建新数据库,查看是否同步到旧主的库

  • 相关阅读:
    C51之智能感应垃圾桶
    Day39、40、41 尚硅谷JUC——ThreadPool线程池
    java计算机毕业设计智慧校园实习岗位需求对接网络平台源程序+mysql+系统+lw文档+远程调试
    七月集训(第21天) —— 堆(优先队列)
    Dotnet工具箱:开源、免费的纯前端工具网站,带你探索10大工具分类和73个实时在线小工具
    CSS3技巧37:JS+CSS3 制作旋转图片墙
    sql:SQL优化知识点记录(十二)
    第4章 漏洞扫描
    短时间内防止多次点击
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java高校学生宿舍管理信息系统3x4rz
  • 原文地址:https://blog.csdn.net/qq_61843057/article/details/134406624