[关闭]
@llplmlyd 2021-10-20T20:00:01.000000Z 字数 4941 阅读 574

MySQL --MHA 高可用架构构建

数据库


服务器 ip 信息

  1. server 1 172.19.120.141 master
  2. server 2 172.19.120.142 slave
  3. server 3 172.19.120.143 backup
  4. 互信端口 默认22
  5. 绑定方法:/sbin/ifconfig 网卡名称:网卡序号 ip/mask
  6. vip 172.19.120.239/22
  7. e.g. /sbin/ifconfig eth0:1 172.19.120.239/22
  8. 系统版本
  9. CentOS 7
  10. #自动切换时vip管理的脚本,如果使用keepalived的,可以编写脚本完成对vip的管理
  11. master_ip_failover
  12. #在线切换时vip的管理
  13. master_ip_online_change
  14. #故障发生后关闭主机的脚本
  15. power_manager
  16. #因故障切换后发送报警的脚本
  17. send_report

refer

https://www.cnblogs.com/John-2011/p/7808554.html
https://devopstack.cn/%E6%95%B0%E6%8D%AE%E5%BA%93/363.html#421_node
https://www.cnblogs.com/shengdimaya/p/6949882.html

mha集群中所有机器相互配置互信 (root)

  1. ssh-keygen -t rsa
  1. ## 有密码
  2. ssh-copy-id -i /root/.ssh/id_rsa.pub -p 22 -o StrictHostKeyChecking=no root@server1/2/3
  3. ## 无密码
  4. touch authorized_keys
  5. chmod 600 /root/.ssh/authorized_keys
  1. #mha
  2. sshd:172.19.120.141/255.255.255.255 172.19.120.142/255.255.255.255 172.19.120.143/255.255.255.255
  1. [root@server2 .ssh]# ssh -p22 172.19.120.141
  2. Last failed login: Sat Nov 7 13:23:40 CST 2020 from 172.19.120.143 on ssh:notty
  3. There were 4 failed login attempts since the last successful login.
  4. Last login: Fri Nov 6 18:35:02 2020
  5. [root@server1 ~]# exit

安装MHA

  1. yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y
  2. wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
  3. rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
  1. ## mysql中创建用户
  2. GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, PROCESS, FILE, SUPER, LOCK TABLES, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'mhamonitor'@server1/2/2 identified by 'xxxx';
  3. mysql> select user.host from mysql.user where user='mhamonitor';
  4. +----------------+
  5. | host |
  6. +----------------+
  7. | 172.19.120.141 |
  8. | 172.19.120.142 |
  9. | 172.19.120.143 |
  10. +----------------+
  11. ## 禁止删除中继日志,详细脚本内容见文档末尾
  12. mkdir /var/job
  13. touch purge_relay_logs.sh
  14. -----
  15. ## 将禁止删除的任务加入定时任务中
  16. echo "0 4 * * * /bin/bash /var/job/purge_relay_log.sh" >> /var/spool/cron/root

部署管理监控mha节点 以 143 server3 作为管理节点

  1. # yum install perl-Parallel-ForkManager perl-Log-Dispatch perl-Config-Tiny perl-DBD-MySQL -y
  2. Package perl-Parallel-ForkManager-1.18-2.el7.noarch already installed and latest version
  3. Package perl-Log-Dispatch-2.41-1.el7.1.noarch already installed and latest version
  4. Package perl-Config-Tiny-2.14-7.el7.noarch already installed and latest version
  5. Package perl-DBD-MySQL-4.023-6.el7.x86_64 already installed and latest version
  6. # 安装管理节点rpm包
  7. wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
  8. rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
  1. mkdir -p /etc/masterha/
  2. touch /etc/masterha/app_33066.cnf
  3. -----------------------------------------
  4. [server default]
  5. manager_workdir=/var/log/masterha/app_33066
  6. manager_log=/var/log/masterha/app_33066/manager.log
  7. master_binlog_dir=/var/mysql33066/log
  8. master_ip_failover_script=/usr/local/bin/master_ip_failover_33066
  9. master_ip_online_change_script=/usr/local/bin/master_ip_online_change_33066
  10. user=mhamonitor
  11. password=xxxxxx
  12. ping_interval=1
  13. remote_workdir=/tmp
  14. repl_password=xxxxx
  15. repl_user=mhamonitor
  16. #secondary_check_script=/usr/local/bin/masterha_secondary_check_33066
  17. #init_conf_load_script=/usr/local/bin/init_conf_load_script_33066
  18. #report_script=/usr/local/bin/send_report_33066
  19. ssh_user=root
  20. ssh_port=22
  21. [server1]
  22. hostname=172.19.120.141
  23. port=33066
  24. master_binlog_dir=/var/mysql33066/log
  25. candidate_master=1
  26. [server2]
  27. hostname=172.19.120.142
  28. port=33066
  29. master_binlog_dir=/var/mysql33066/log
  30. candidate_master=1
  31. [server3]
  32. hostname=172.19.120.143
  33. port=33066
  34. master_binlog_dir=/var/mysql33066/log
  35. no_master=1
  1. master_ip_failover
  2. master_ip_online_change
  3. purge_relay_log
  4. monitor_manager_33066.sh
  5. /var/log/masterha/app_33066.cnf

定时任务

  1. */1 * * * * /bin/bash /var/job/monitor_manager_33066

部署完后查看日志 成功

  1. Checking the Status of the script.. OK
  2. Sat Nov 7 16:24:03 2020 - [info] OK.
  3. Sat Nov 7 16:24:03 2020 - [warning] shutdown_script is not defined.
  4. Sat Nov 7 16:24:03 2020 - [info] Set master ping interval 1 seconds.
  5. Sat Nov 7 16:24:03 2020 - [info] Set secondary check script: /usr/local/bin/masterha_secondary_check_33066
  6. Sat Nov 7 16:24:03 2020 - [info] Starting ping health check on 172.19.120.141(172.19.120.141:33066)..
  7. Sat Nov 7 16:24:03 2020 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..

monitor_manager

  1. #! /bin/bash
  2. if [ `ps -ef | grep "masterha_manager -conf=/etc/masterha/app_33066.cnf" | grep -v grep |wc -l` -eq 0 ]
  3. then
  4. nohup /usr/bin/masterha_manager -conf=/etc/masterha/app_33066.cnf --ignore_fail_on_start 2>&1 /var/log/masterha/app1/manager.log &
  5. fi

purge_relay_log

  1. WORKDIR=$(cd $(dirname $0);pwd)
  2. host=172.19.120.141
  3. user='mhamonitor'
  4. passwd='xxxx'
  5. port=33066
  6. log_dir="/var/log/purge_relay_logs_${port}"
  7. work_dir='/var/job'
  8. purge='/usr/bin/purge_relay_logs'
  9. if [ ! -d $work_dir ]
  10. then
  11. mkdir $work_dir -p
  12. fi
  13. if [ ! -d $log_dir ]
  14. then
  15. mkdir $log_dir -p
  16. fi
  17. $purge --host=$host --user=$user --password=$passwd --disable_relay_log_purge --port=$port --workdir=$work_dir >> $log_dir/purge_relay_logs.log 2>&1

master_ip_online_change

略[ github]

master_ip_failover

略[github]

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注