mysql5.7MHA配置
准备工作,三台服务器,安装mysql5.7-glibc版本,做主从配置,用gtid模式。
安装mysql
1、准备安装包
mysql
链接:https://pan.baidu.com/s/14w-oNT-oeTUujhFAnqjkSA
提取码:g8tk
[root@hk2 install]# ll mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz -rw-r--r-- 1 root root 661214270 Jan 20 19:59 mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
2、解压缩,环境变量,加path
tar zxvf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.32-linux-glibc2.12-x86_64 /data/server/mysql 环境变量 echo 'export PATH=$PATH:/data/server/mysql/bin' >> /etc/profile source /etc/profile 建用户,授权 useradd -s /sbin/nologin -M mysql mkdir -p /linux0224/ mkdir -p /linux0224/mysql_3306/
授权
chown -R mysql.mysql /linux0224/
chown -R mysql.mysql /linux0224/mysql_3306/
# 因为解压目录不一样,所以做一个软链接
ln -s /data/server/mysql /opt/mysql
chown -R mysql.mysql /opt/mysql*
3、初始化
mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/linux0224/mysql_3306/
4、修改配置文件
cat >/etc/my.cnf <<'EOF' [mysqld] port=3306 user=mysql basedir=/opt/mysql datadir=/linux0224/mysql_3306/ socket=/tmp/mysql.sock [mysql] socket=/tmp/mysql.sock EOF
5、修改启动脚本
cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld systemctl daemon-reload systemctl status mysqld systemctl start mysqld
6、登录验证
[root@hk2 install]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.32 MySQL Community Server (GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> # 完成
三台服务器都安装好mysql,这是基础安装,没有做主从和优化,先保证mysql能够启动正常。
主从配置:
如果主库有数据,先做备份,从库导入。
mysqldump -A > all.sql 拷贝到从库 for i in 52 53 ;do scp /data/full.sql 172.16.5.$i:/opt/;done mysql < /opt/all.sql
主从配置,主库:
[root@hk33 install]# cat /etc/my.cnf [mysqld] binlog_format=row gtid-mode=on enforce-gtid-consistency=true log-slave-updates=1 port=3306 user=mysql basedir=/opt/mysql datadir=/linux0224/mysql_3306/ socket=/tmp/mysql.sock log_error=/var/log/mysql/mysql.err server_id=1 log_bin=/binlog/mysql-bin [mysql] socket=/tmp/mysql.sock [client] socket=/tmp/mysql.sock
从库两台和主库配置一样,id不一样。
创建binlog目录,授权 mkdir /binlog/ chown mysql.mysql /binlog/
日志目录
mkdir /var/log/mysql/
chown mysql.mysql /var/log/mysql/
建立主从关系,因为要做MHA,三台服务器都执行如下语句。
grant replication slave on *.* to 'slave'@'%' identified by 'slave_pwd';
然后在主库上执行如下命令,查看binlog
mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------------------------------------------------+ | mysql-bin.000003 | 652 | | | 03a2b688-ddc5-11ee-90df-000c293d0361:1-2, 1b604040-ddc4-11ee-929d-000c291478ee:1-3 | +------------------+----------+--------------+------------------+------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
在两台从库上执行建立同步
从库分别执行 停止slave stop slave; change master to master_host='172.16.5.51',master_user='slave',master_password='slave_pwd',MASTER_AUTO_POSITION=1; 开启slave start slave;
建立一个只读用户,在主库执行就可以,会自动同步操作 grant select on *.* to dev0224@'172.16.5.%' identified by 'dev0224'; # 用root设置全局锁 set global read_only=1; 区别 1. grant select 是我们主动给用户账户,限制只查询的权限,只是针对这个账号而已 2. 设置--read-only只读会话,是针对当前数据库实例而言的,所有用户而言
MHA工具会检测mysql命令,这里还需要加软连接
ln -s /opt/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog ln -s /opt/mysql/bin/mysql /usr/bin/mysql
yum install sshpass -y ssh-keygen sshpass -p '123123' ssh-copy-id 172.16.5.51 -o StrictHostKeyChecking=no sshpass -p '123123' ssh-copy-id 172.16.5.52 -o StrictHostKeyChecking=no sshpass -p '123123' ssh-copy-id 172.16.5.53 -o StrictHostKeyChecking=no
链接:https://pan.baidu.com/s/1aYC-EnDKc6N016DnbQawrQ
提取码:06g2
mha-node软件,--mha-manage通信 # mha perl语言开发的 # 1.先安装依赖环境 yum install -y perl-DBD-MySQL -y # 2.安装软件 yum localinstall mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
因为Manager管理节点,通过ssh检测mysql集群,如果master
节点服务器宕机,或者网络故障,MHA也无法完成故障切换了。
yum install epel-release -y yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN perl-Time-HiRes yum localinstall -y mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
mha命令,脚本,通过这个用户,检测 所有机器的 主从复制状态 # db-51执行即可 mysql> mysql> grant all privileges on *.* to mha@'%' identified by 'mha0224'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mkdir -p /etc/mha #<==在/etc下创建mha目录。 mkdir -p /var/log/mha/app1 #<==在/etc下创建mha目录。
使用该配置即可
所有节点要执行下边这一句,mysql命令行
grant replication slave on . to repl_0224@'%' identified by 'repl_pwd';
以下在53上执行
# author:www.yuchaoit.cn cat > /etc/mha/app1.cnf << 'EOF' [server default] manager_log=/var/log/mha/app1/manager.log manager_workdir=/var/log/mha/app1.log master_binlog_dir=/mysql_binlog/ # 该脚本暂时先注释 master_ip_failover_script=/usr/local/bin/master_ip_failover user=mha password=mha0224 ping_interval=2 # 填入你当前主从赋值,账号密码 repl_user=repl_0224 repl_password=repl_pwd # mha 用这个账户,免密登录3个节点机器 # 听懂111 ssh_user=root [server1] hostname=172.16.5.51 port=3306 [server2] hostname=172.16.5.52 port=3306 [server3] hostname=172.16.5.53 port=3306 EOF
创建基于VIP的MHA高可用性数据库
只需要修改这个脚本,填入你的网卡环境即可
创建如下脚本,mha配置文件,会自动调用它,perl语言脚本 /usr/local/bin/master_ip_failover # 填入如下内容 #!/usr/bin/env perl use strict; use warnings FATAL => 'all'; use Getopt::Long; my ( $command, $ssh_user, $orig_master_host, $orig_master_ip, $orig_master_port, $new_master_host, $new_master_ip, $new_master_port ); my $vip = '172.16.5.55/24'; # 给网卡添加的别名 my $key = '1'; my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; my $ssh_Bcast_arp="/sbin/arping -I ens33 -c 3 -A 172.16.5.55"; GetOptions( 'command=s' => \$command, 'ssh_user=s' => \$ssh_user, 'orig_master_host=s' => \$orig_master_host, 'orig_master_ip=s' => \$orig_master_ip, 'orig_master_port=i' => \$orig_master_port, 'new_master_host=s' => \$new_master_host, 'new_master_ip=s' => \$new_master_ip, 'new_master_port=i' => \$new_master_port, ); exit &main(); sub main { print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$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(); $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; } } # author: www.yuchaoit.cn sub start_vip() { `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`; } sub stop_vip() { return 0 unless ($ssh_user); `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`; } sub usage { print "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"; }
# 创建 ifconfig ens33:1 172.16.5.55/24 下边两行不用执行 # 删除 ifconfig ens33:1 del 172.16.5.55 # 停止 ifconfig ens33:1 down
准备启动MHA,先进行一下状态检查
检测如下MHA运行条件 # mha提供了很方便的脚本,检测你的mha环境搭建情况 - SSH免密登录 - MySQL主从复制 [root@db-53 ~]#masterha_check_ssh --conf=/etc/mha/app1.cnf Tue Aug 2 00:36:08 2022 - [info] All SSH connection tests passed successfully. # 主从检测脚本,会用到mha账号,在三个机器上进行检测登录 # 再检查主从复制状态 masterha_check_repl --conf=/etc/mha/app1.cnf
结果要全部成功才行
1. 启动mha的管理节点,后台运行即可---用的是 db-53机器 [root@db-53 ~]# [root@db-53 ~]#nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover /var/log/mha/app1/manager.log 2>&1 & [1] 3647 [root@db-53 ~]#nohup: ignoring input and appending output to ‘nohup.out’ [root@db-53 ~]# [root@db-53 ~]# [root@db-53 ~]#jobs [1]+ Running nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover /var/log/mha/app1/manager.log 2>&1 & [root@db-53 ~]# [root@db-53 ~]# [root@db-53 ~]#ps -ef|grep mha root 3647 1634 1 20:37 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover /var/log/mha/app1/manager.log root 3664 1634 0 20:37 pts/0 00:00:00 grep --color=auto mha 2 .检查MHA是否运行中 [root@db-53 ~]#masterha_check_status --conf=/etc/mha/app1.cnf app1 (pid:3647) is running(0:PING_OK), master:172.16.5.51 # mha此时确认主库正常,还是 51机器
故障演练
期望的结果是 1. 当前环境 db-51 master db-52 slave db-53 slave 2. 干掉主库 pkill mysql 2.1 查看VIP的状态,MHA脚本切换VIP到新的主库上,51机器的VIP没了 已确认 51机器的VIP没了 3. 查看主从复制关系 [root@db-53 ~]#mysql -uroot -plinux0224 mysql> show slave status\G 得到结论,MHA自动选举 db-52为新的主库 还得做数据的验证,看看旧的主库51机器的binlog,和52新主库对比 [root@db-51 /mysql_binlog]#mysqlbinlog -vv mysql-bin.000002 |grep -i 'gtid' 最后一次提交是 事务id是 8 自动下一次的事务,9开始 ---看52机器 ----52机器 4. MHA再发生切换后,该程序会挂掉 也确认了
测试新的主从关系
52机器写入数据 已确认 事务提交更新,写入到新机器 52的 GTID版本号里了 6a952706-1a30-11ed-882e-000c294c7d18:2 去新的从库查看数据,以及比对GTID号码 GTID号码 以配置文件里 server_id SHOW GLOBAL VARIABLES LIKE 'server_uuid';
到现在,52已经提升为主了。
恢复MHA,先将51加入进mysql集群,做为52的从。
1. 51机器,直接启动,和新的主库建立复制就行,预测,51机器,是否能看到最新的 52机器的数据 # 1.启动 systemctl start mysqld # 2. 加入主从 mysql -uroot -plinux0224 change master to master_host='172.16.5.52', master_user='slave', master_password='slave_pwd' , MASTER_AUTO_POSITION=1; start slave; show slave status;
然后将51加入到MHA集群中
[root@db-53 ~]#cat /etc/mha/app1.cnf [server default] manager_log=/var/log/mha/app1/manager.log manager_workdir=/var/log/mha/app1.log master_binlog_dir=/mysql_binlog/ master_ip_failover_script=/usr/local/bin/master_ip_failover password=mha0224 ping_interval=2 repl_password=repl0224 repl_user=repl ssh_user=root user=mha [server2] hostname=10.0.0.52 port=3306 [server3] hostname=10.0.0.53 port=3306 1.添加db-51机器的 配置信息 masterha_conf_host --command=add --conf=/etc/mha/app1.cnf --hostname=172.16.5.51 --block=server1 --params="port=3306" [root@db-53 ~]#cat /etc/mha/app1.cnf [server default] manager_log=/var/log/mha/app1/manager.log manager_workdir=/var/log/mha/app1.log master_binlog_dir=/mysql_binlog/ master_ip_failover_script=/usr/local/bin/master_ip_failover password=mha0224 ping_interval=2 repl_password=repl0224 repl_user=repl ssh_user=root user=mha [server1] hostname=10.0.0.51 port=3306 [server2] hostname=10.0.0.52 port=3306 [server3] hostname=10.0.0.53 port=3306 [root@db-53 ~]# 2.重新检测2 ssh,repl 先检查复制情况 [root@db-53 ~]#masterha_check_repl --conf=/etc/mha/app1.cnf masterha_check_ssh --conf=/etc/mha/app1.cnf 3. 启动即可 nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover /var/log/mha/app1/manager.log 2>&1 &
这时是将51加入到集群中了,但现在主还在52上,需要手动将52再停掉,51会自动提升为master。然后再执行上边类似的命令,将52再加入回来。整个演练就结束了。