个体户做网站有用吗,wordpress可视化采集,wordpress模版c2c商城,网站系统名称怎么填MHA架构介绍
MHA是Master High Availability的缩写#xff0c;它是目前MySQL高可用方面的一个相对成熟的解决方案#xff0c;其核心是使用perl语言编写的一组脚本#xff0c;是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中它是目前MySQL高可用方面的一个相对成熟的解决方案其核心是使用perl语言编写的一组脚本是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中MHA能做到在0~ 30秒之内自动完数据库的故障切换操作并且能在最大程度上保证数据的一致性以达到真正意义上的高可用。
基于MHA的架构不像MMM那样需要搭建主主复制只需要搭建基本的主从复制架构即可。因为MHA在主库挂掉时是在多个从库中选取出一个从库作为新的主库。MHA集群中的各节点彼此之间均需要基于ssh 互信通信以实现远程控制及数据管理功能。
MHA提供功能
可以监控Master节点是否可用当Master不可用时能在多个Slave中选举出新的Master提供了主从切换和故障转移功能MHA会尝试在宕机的Master上保存binlog在最大程度上保证事务不丢失。但如果是Master所在服务器已经无法访问或硬件层面出现了问题则无法成功保存binlogMHA可以与半同步复制结合避免从库之间出现数据不一致的情况支持MySQL基于GTID和基于日志点的两种复制方式
MHA故障转移过程
尝试使用 ssh登录到宕机崩溃的Master节点上保存二进制日志事件binlog events;从多个Slave中识别含有最新更新的Slave将其作为备选的Master然后基于该Slave同步差异的中继日志relaylog到其他的Slave上接着同步从原Master上保存的二进制日志事件binlog events将备选的Master提升为新的Master使其他的Slave连接新的Master进行复制在新的Master启动vip地址保证前端请求可以发送到新的Master。
MHA架构图 本次实验所用机器说明
操作系统 CentOS Stream 9
数据库版本MySQL8.0.36
MHA版本0.58
名称角色IPmanger集群管理节点192.168.0.110master主库192.168.0.104slave1从库192.168.0.103slave2从库192.168.0.102
本次实验的前提是建立在已经搭建好主从复制集群的前提下MySQL的主从复制可以参考这篇文章MySQL5.7.43 GTID主从复制配置
但是在使用MySQL8.0.36配置主从复制的时候和MySQL5.7.43有些地方不同需要注意
等主从复制集群搭建好后就可以开始按下面的步骤开始本次实验了
配置ssh免密登陆
生成密钥
ssh-keygen
生成密钥和公钥后就可以在/root/.ssh/下面看到当然这是默认的路径 将密钥拷贝到其他服务器上
ssh-copy-id -i /root/.ssh/id_rsa root192.168.0.104
ssh-copy-id -i /root/.ssh/id_rsa root192.168.0.103
ssh-copy-id -i /root/.ssh/id_rsa root192.168.0.102 然后到集群中其他节点上进行同样的操作由于是重复的操作这里就不演示了最后测试能否免密登陆即可 安装MHA软件包
MHA下载地址 mha4mysql-manager-0.58.tar.gz wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58.tar.gz mha4mysql-node-0.58.tar.gz wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58.tar.gz 在manager上安装mha4mysql-manager软件包 #下载依赖包
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN#进入安装目录
cd mha4mysql-manager-0.58#编译安装
perl Makefile.PL
make make install在所有节点上安装mha4mysql-node软件包
#下载依赖包
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN#进入安装目录
cd mha4mysql-node-0.58#编译安装
perl Makefile.PL
make make install 关于执行perl Makefile.PL报错解决方法参考文章 Cant locate IPC/Cmd.pm in INC (you may need to install the IPC::Cmd module https://blog.51cto.com/u_16175492/6651879 这里模块的安装可能很慢请naixi 配置manger节点
创建MHA的配置文件存放目录和工作目录
[rootmanager ~]# mkdir /etc/mha
[rootmanager ~]# mkdir /home/mysql_mha
创建MHA的配置文件并添加如下内容
这个组件secondary_check_script在安装的时候路径可能在/usr/bin/下面需要移动到/usr/local/bin/下面 或者在下面的配置文件中更改路径
# mha用于访问数据库的账户和密码
usermha
password密码
# 指定mha的工作目录
manager_workdir/home/mysql_mha# mha日志文件的存放路径
manager_log/home/mysql_mha/manager.log# 指定mha在远程节点上的工作目录
remote_workdir/home/mysql_mha# 可以使用ssh登录的用户
ssh_userroot# 用于主从复制的MySQL用户和密码
repl_userrepl
repl_password密码
# 指定间隔多少秒检测一次
ping_interval1# 指定master节点存放binlog日志文件的目录
master_binlog_dir/var/lib/mysql# 指定一个脚本该脚本实现了在主从切换之后将虚拟IP漂移到新的Master上
master_ip_failover_script/usr/local/bin/master_ip_failover# 指定用于二次检查节点状态的脚本
secondary_check_script/usr/local/bin/masterha_secondary_check -s 192.168.0.104 -s 192.168.0.103 -s 192.168.0.102# 配置集群中的节点信息
[server1]
hostname192.168.0.104
port3306
# 指定该节点可以参与Master选举
candidate_master1[server2]
hostname192.168.0.103
port3306
candidate_master1[server3]
hostname192.168.0.102
port3306
# 指定该节点不能参与Master选举
no_master1编写配置文件中所配置的master_ip_failover脚本该脚本是根据MHA的官方示例修改的MHA默认并没有提供。需要注意脚本中的几处地方需要根据实际情况进行修改已用注释标明
#!/usr/bin/env perluse strict;
use warnings FATAL all;use Getopt::Long;my ($command, $orig_master_host, $orig_master_ip,$ssh_user,$orig_master_port, $new_master_host, $new_master_ip,$new_master_port,$orig_master_ssh_port,$new_master_ssh_port,$new_master_user,$new_master_password
);# 这里定义的虚拟IP可以根据实际情况进行修改
my $vip 192.168.0.111/24;
my $brdc 192.168.0.255;
my $ifdev eth0;
my $key 1;
# 这里的网卡名称 “eth0” 需要根据你机器的网卡名称进行修改
my $ssh_start_vip sudo /sbin/ifconfig eth0:$key $vip;
my $ssh_stop_vip sudo /sbin/ifconfig eth0:$key down;
my $ssh_Bcast_arp sudo /sbin/arping -I bond0 -c 3 -A $vip;GetOptions(commands \$command,ssh_users \$ssh_user,orig_master_hosts \$orig_master_host,orig_master_ips \$orig_master_ip,orig_master_porti \$orig_master_port,orig_master_ssh_porti \$orig_master_ssh_port,new_master_hosts \$new_master_host,new_master_ips \$new_master_ip,new_master_porti \$new_master_port,new_master_ssh_port \$new_master_ssh_port,new_master_user \$new_master_user,new_master_password \$new_master_password);exit main();sub main {$ssh_user defined $ssh_user ? $ssh_user : root;print \n\nIN SCRIPT TEST$ssh_user|$ssh_stop_vip$ssh_user|$ssh_start_vip\n\n;if ( $command eq stop || $command eq stopssh ) {my $exit_code 1;eval {print Disabling the VIP on old master: $orig_master_host \n;stop_vip();$exit_code 0;};if ($) {warn Got Error: $\n;exit $exit_code;}exit $exit_code;}elsif ( $command eq start ) {my $exit_code 10;eval {print Enabling the VIP - $vip on the new master - $new_master_host \n;start_vip();start_arp();$exit_code 0;};if ($) {warn $;exit $exit_code;}exit $exit_code;}elsif ( $command eq status ) {print Checking the Status of the script.. OK \n;exit 0;}else {usage();exit 1;}
}sub start_vip() {ssh $ssh_user\$new_master_host \ $ssh_start_vip \;
}
sub stop_vip() {ssh $ssh_user\$orig_master_host \ $ssh_stop_vip \;
}sub start_arp() {ssh $ssh_user\$new_master_host \ $ssh_Bcast_arp \;
}
sub usage {printUsage: master_ip_failover --commandstart|stop|stopssh|status --ssh_useruser --orig_master_hosthost --orig_master_ipip --orig_master_portport --new_master_hosthost --new_master_ipip --new_master_portport\n;
}还需要给该脚本添加可执行权限否则MHA是无法调用的
[rootmanager ~]# chmod ax /usr/local/bin/master_ip_failover
根据配置文件中remote_workdir的配置需在其他节点上创建MHA的远程工作目录
[rootmaster ~]# mkdir /home/mysql_mha
[rootslave-01 ~]# mkdir /home/mysql_mha
[rootslave-02 ~]# mkdir /home/mysql_mha
在配置文件中指定了让manager使用mha这个用户来访问数据库节点所以需要在master节点上创建mha用户
create user mha% identified with mysql_native_password by 密码;
grant all privileges on *.* to mha%;
flush privileges;
完成以上所有步骤后在manager节点上使用masterha_check_ssh和masterha_check_repl 对配置进行检查其中masterha_check_ssh用于检查ssh登录是否正常而masterha_check_repl则用于检查主从节点的复制链路是否正常
[rootmanager ~]# masterha_check_ssh --conf/etc/mha/mysql_mha.cnf
[rootmanager ~]# masterha_check_repl --conf/etc/mha/mysql_mha.cnf
[rootfa0b3e01d810 bin]# masterha_check_ssh --conf/etc/mha/mysql_mha.cnf
Mon Apr 8 16:54:15 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Apr 8 16:54:15 2024 - [info] Reading application default configuration from /etc/mha/mysql_mha.cnf..
Mon Apr 8 16:54:15 2024 - [info] Reading server configuration from /etc/mha/mysql_mha.cnf..
Mon Apr 8 16:54:15 2024 - [info] Starting SSH connection tests..
Mon Apr 8 16:54:18 2024 - [debug]
Mon Apr 8 16:54:16 2024 - [debug] Connecting via SSH from root192.168.0.103(192.168.0.103:22) to root192.168.0.104(192.168.0.104:22)..
Mon Apr 8 16:54:17 2024 - [debug] ok.
Mon Apr 8 16:54:17 2024 - [debug] Connecting via SSH from root192.168.0.103(192.168.0.103:22) to root192.168.0.102(192.168.0.102:22)..
Mon Apr 8 16:54:17 2024 - [debug] ok.
Mon Apr 8 16:54:18 2024 - [debug]
Mon Apr 8 16:54:16 2024 - [debug] Connecting via SSH from root192.168.0.102(192.168.0.102:22) to root192.168.0.104(192.168.0.104:22)..
Mon Apr 8 16:54:17 2024 - [debug] ok.
Mon Apr 8 16:54:17 2024 - [debug] Connecting via SSH from root192.168.0.102(192.168.0.102:22) to root192.168.0.103(192.168.0.103:22)..
Mon Apr 8 16:54:18 2024 - [debug] ok.
Mon Apr 8 16:54:18 2024 - [debug]
Mon Apr 8 16:54:15 2024 - [debug] Connecting via SSH from root192.168.0.104(192.168.0.104:22) to root192.168.0.103(192.168.0.103:22)..
Mon Apr 8 16:54:17 2024 - [debug] ok.
Mon Apr 8 16:54:17 2024 - [debug] Connecting via SSH from root192.168.0.104(192.168.0.104:22) to root192.168.0.102(192.168.0.102:22)..
Mon Apr 8 16:54:17 2024 - [debug] ok.
Mon Apr 8 16:54:18 2024 - [info] All SSH connection tests passed successfully.
Use of uninitialized value in exit at /usr/local/bin/masterha_check_ssh line 44.[rootfa0b3e01d810 bin]# masterha_check_repl --conf/etc/mha/mysql_mha.cnf
Mon Apr 8 16:54:38 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Apr 8 16:54:38 2024 - [info] Reading application default configuration from /etc/mha/mysql_mha.cnf..
Mon Apr 8 16:54:38 2024 - [info] Reading server configuration from /etc/mha/mysql_mha.cnf..
Mon Apr 8 16:54:38 2024 - [info] MHA::MasterMonitor version 0.58.
Mon Apr 8 16:54:40 2024 - [info] Multi-master configuration is detected. Current primary(writable) master is 192.168.0.104(192.168.0.104:3306)
Mon Apr 8 16:54:40 2024 - [info] Master configurations are as below:
Master 192.168.0.103(192.168.0.103:3306), replicating from 192.168.0.104(192.168.0.104:3306), read-only
Master 192.168.0.104(192.168.0.104:3306), replicating from 192.168.0.103(192.168.0.103:3306)Mon Apr 8 16:54:40 2024 - [info] GTID failover mode 1
Mon Apr 8 16:54:40 2024 - [info] Dead Servers:
Mon Apr 8 16:54:40 2024 - [info] Alive Servers:
Mon Apr 8 16:54:40 2024 - [info] 192.168.0.104(192.168.0.104:3306)
Mon Apr 8 16:54:40 2024 - [info] 192.168.0.103(192.168.0.103:3306)
Mon Apr 8 16:54:40 2024 - [info] 192.168.0.102(192.168.0.102:3306)
Mon Apr 8 16:54:40 2024 - [info] Alive Slaves:
Mon Apr 8 16:54:40 2024 - [info] 192.168.0.103(192.168.0.103:3306) Version8.0.36 (oldest major version between slaves) log-bin:enabled
Mon Apr 8 16:54:40 2024 - [info] GTID ON
Mon Apr 8 16:54:40 2024 - [info] Replicating from 192.168.0.104(192.168.0.104:3306)
Mon Apr 8 16:54:40 2024 - [info] Primary candidate for the new Master (candidate_master is set)
Mon Apr 8 16:54:40 2024 - [info] 192.168.0.102(192.168.0.102:3306) Version8.0.36 (oldest major version between slaves) log-bin:enabled
Mon Apr 8 16:54:40 2024 - [info] GTID ON
Mon Apr 8 16:54:40 2024 - [info] Replicating from 192.168.0.104(192.168.0.104:3306)
Mon Apr 8 16:54:40 2024 - [info] Not candidate for the new Master (no_master is set)
Mon Apr 8 16:54:40 2024 - [info] Current Alive Master: 192.168.0.104(192.168.0.104:3306)
Mon Apr 8 16:54:40 2024 - [info] Checking slave configurations..
Mon Apr 8 16:54:40 2024 - [info] Checking replication filtering settings..
Mon Apr 8 16:54:40 2024 - [info] binlog_do_db , binlog_ignore_db
Mon Apr 8 16:54:40 2024 - [info] Replication filtering check ok.
Mon Apr 8 16:54:40 2024 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Mon Apr 8 16:54:40 2024 - [info] Checking SSH publickey authentication settings on the current master..
Mon Apr 8 16:54:40 2024 - [info] HealthCheck: SSH to 192.168.0.104 is reachable.
Mon Apr 8 16:54:40 2024 - [info]
192.168.0.104(192.168.0.104:3306) (current master)--192.168.0.103(192.168.0.103:3306)--192.168.0.102(192.168.0.102:3306)Mon Apr 8 16:54:40 2024 - [info] Checking replication health on 192.168.0.103..
Mon Apr 8 16:54:40 2024 - [info] ok.
Mon Apr 8 16:54:40 2024 - [info] Checking replication health on 192.168.0.102..
Mon Apr 8 16:54:40 2024 - [info] ok.
Mon Apr 8 16:54:40 2024 - [info] Checking master_ip_failover_script status:
Mon Apr 8 16:54:40 2024 - [info] /usr/local/bin/master_ip_failover --commandstatus --ssh_userroot --orig_master_host192.168.0.104 --orig_master_ip192.168.0.104 --orig_master_port3306IN SCRIPT TESTroot|sudo /sbin/ifconfig eth0:1 downroot|sudo /sbin/ifconfig eth0:1 192.168.0.111/24Checking the Status of the script.. OK
Mon Apr 8 16:54:40 2024 - [info] OK.
Mon Apr 8 16:54:40 2024 - [warning] shutdown_script is not defined.
Mon Apr 8 16:54:40 2024 - [info] Got exit code 0 (Not master dead).MySQL Replication Health is OK.以上检测都通过后就可以启动MHA服务了。启动命令如下
[rootmanager ~]# nohup masterha_manager --conf/etc/mha/mysql_mha.cnf
启动完成后可以使用ps命令查看masterha_manager进程是否存在如下存在则代表启动成功
[rootmanager ~]# ps aux |grep masterha_manager
root 2842 0.3 1.1 299648 22032 pts/0 S 18:30 0:00 perl /usr/bin/masterha_manager --conf/etc/mha/mysql_mha.cnf
root 2901 0.0 0.0 112728 976 pts/0 R 18:31 0:00 grep --colorauto masterha_manager
[rootmanager ~]# 最后我们需要到master节点上手动去配置虚拟IP。因为MHA只会在主从切换时漂移虚拟IP到新的Master节点而不会在第一次启动时主动去设置Master的虚拟IP所以我们需要手动设置。设置虚拟IP的命令如下
[rootmaster ~]# ifconfig eth0:1 192.168.0.111/24 到此为止我们就已经完成了MHA高可用架构的搭建接下来我们对其进行一些简单的测试。例如测试下是否能正常ping 通虚拟IP毕竟应用端访问数据库时连接的是虚拟IP所以首先得确保虚拟IP是能够被访问的。如下 MHA架构优缺点
优点
使用Perl脚本语言开发并且完全开源开发者可以根据自己的需求进行二次开发能够支持基于GTID和基于日志点的复制模式MHA在进行故障转移时更不易产生数据丢失在一个监控节点上可以监控多个Replication集群
缺点
MHA默认不提供虚拟IP功能需要自行编写脚本或利用第三方工具来实现虚拟IP的配置MHA启动后只会对Master进行监控不会对Slave进行监控也无法监控复制链路的情况集群环境需要能够通过 ssh免密登录存在一定的安全隐患MHA没有提供对Slave的读负载均衡功能需要通过第三方工具来实现