赞
踩
目录
MHA是 开源的MYSQL故障切换方案;全称:Master High Availability,故障切换时间10-30s
MHA由node和manager组成
node:
就是所有数据库机器都需要部署的, master是一个node,slave也是一个node; 相当于监控客户端;
manager:
Manager相当server,它会对node进行管理,配置检查,binlog和relay log的获取,执行切换任务等; 可以单独部署在server上(推荐),单独部署可以管理多套主从架构。也可以部署在某一个slave上; 如果部署在slave上,那么该slave就无法 被升级为主库;
在MySQL MHA检测到MySQL架构中MySQL主库发生故障后,MHA试图从宕机的MySQL主库上保存二进制日志,最大程度的保证数据不会丢失。并选择一个Slave从库,将其提升为新的MySQL主从架构中的主库,并设置其他的Slave从库指向新的MySQL主库。具体过程如下:
1、MySQL MHA试图从宕机崩溃的MySQL主库中保存二进制日志。
2、MySQL MHA试图识别数据最新的Slave。
3、将该Slave数据库的中继日志同步到其他的从库,保证新的MySQL主从同步架构中数据的一致性。
4、应用从Master中保存的二进制日志。
5、将一个Slave提升为新的master。
6、控制其他Slave指向新的master。
本实验共需要四台主机(Centos7.9版本),各节点都要开启bin-log及relay_log,各从节点要关闭relay-log-purge(自动清除中继日志功能)
主机名 | ip | 角色 | 备注 |
manager.opelab.com | 192.168.88.141 | 控制器 | 用于监控管理 |
master.openlab.com | 192.168.88.138 | 主服务器 | 开启bin-log及relay_log |
slave1.openlab.com | 192.168.88.139 | 从服务器 | 开启bin-log及relay_log关闭relay-log-purge |
slave2.openlab.com | 192.168.88.140 | 从服务器 | 开启bin-log及relay_log关闭relay-log-purge |
hostnamectl set-hostname 主机名 修改主机名
为了后期方便在/etc/hosts文件配置(四台都做)
192.168.88.141 manager.openlab.com manager192.168.88.138 master.openlab.com master192.168.88.139 slave1.openlab.com slave1192.168.88.140 slave2.openlab.com slave2
- [root@localhost data]#systemctl stop mysql
- [root@localhost data]#rm -rf /usr/local/mysql/data/*
- [root@localhost data]# mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --explicit_defaults_for_timestamp
- 2023-03-31T08:37:35.118827Z 0 [Warning] InnoDB: New log files created, LSN=45790
- 2023-03-31T08:37:35.188031Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
- 2023-03-31T08:37:35.270253Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 492bacb2-cf9f-11ed-8457-000c29321a06.
- 2023-03-31T08:37:35.271643Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
- 2023-03-31T08:37:40.644324Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
- 2023-03-31T08:37:40.644364Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
- 2023-03-31T08:37:40.648120Z 0 [Warning] CA certificate ca.pem is self signed.
- 2023-03-31T08:37:41.540179Z 1 [Note] A temporary password is generated for root@localhost: tyh4CGtekl!<
注意初始化数据库会产生临时密码(四台主机不一样)
- vim /etc/my.conf
-
- [mysqld]
- datadir=/usr/local/mysql/data
- socket=/tmp/mysql.sock
-
- symbolic-links=0
-
- relay_log=ON
- relay-log=relay-bin
-
- log-bin=mysql-bin
- server_id=138
- character-set-server=utf8
-
- gtid-mode=on
- enforce-gtid-consistency=true
- vim /etc.my.cnf
-
- [mysqld]
- datadir=/usr/local/mysql/data
- socket=/tmp/mysql.sock
- symbolic-links=0
-
- relay_log_purge = 0
- relay_log=ON
- relay-log=relay-log
-
- log-bin=mysql-bin
- server_id=140
- character-set-server=utf8
- gtid-mode=on
- enforce-gtid-consistency=true
从主机处理server_id不同其余配置都一样
- [root@localhost data]# systemctl start mysql
- [root@localhost data]# mysqladmin -p'&)NXfrm#9K7c' password '123456'
- mysqladmin: [Warning] Using a password on the command line interface can be insecure.
- Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
修改不成功可能是有mysql进程在运行要用kill -9 杀死
主138
grant replication slave,replication client on *.* to 'slave'@192.168.88.%' identified by '123456';
从139 140
- mysql [(none)]>change master to
- -> master_user='slave',
- -> master_password='123456',
- -> master_host='192.168.88.138',
- -> master_auto_position=1;
- Query OK, 0 rows affected, 2 warnings (0.01 sec)
-
- mysql [(none)]>start slave;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql [(none)]>show slave status\G
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.88.138
- Master_User: slave
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql-bin.000002
- Read_Master_Log_Pos: 718
- Relay_Log_File: relay-log.000002
- Relay_Log_Pos: 931
- Relay_Master_Log_File: mysql-bin.000002
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
在所有的mysql结点授权有管理权限的用户可在本地网络中有其他节点上远程访问
grant all on *.* to 'mhaadmin'@'192.168.88.%' identified by '123456';
每台主机和 Manager 之间都需要ssh实现无密码互连,只有这样,在 Master 出故障时, Manager 才能顺利的连接进去,实现主从切换功能。
- #四台一起做
- ssh-keygen -f ~/.ssh/id_rsa -P '' -q
- ssh-copy-id manager
再将公钥文件发给受控主机
- scp ~/.ssh/authorized_keys master:~/.ssh/
- scp ~/.ssh/authorized_keys slave1:~/.ssh/
- scp ~/.ssh/authorized_keys slave2:~/.ssh/
-
- [root@manager ~]# for i in master slave1 slave2;do ssh $i hostname;done
- master.openlab.com
- slave1.openlab.com
- slave2.openlab.com
- wget -c https://github.com/yoshinorim/mha4mysqlmanager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
-
- wget -c https://github.com/yoshinorim/mha4mysqlnode/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
Manager节点需要另外多安装一个包。具体需要安装的内容如下:MHA Manager服务器需要安装manager和nodeMHA的Node依赖于perl-DBD-MySQL,所以配置epel源。
- #manager
- wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
- yum install mha4mysql-*.rpm
- 受控机
- yum install mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
- 创建配置文件目录
- mkdir /etc/mha
- 创建日志目录
- mkdir -p /var/log/mha/app1
- vim /etc/mha/app1.cnf
配置文件如下:
[server default] //适用于server1,2,3个server的配置 user=mhaadmin //mha管理用户 password=123456 //mha管理密码 manager_workdir=/var/log/mha/app1 //mha_master自己的工作路径 manager_log=/var/log/mha/app1/manager.log // mha_master自己的日志文件 ssh_user=root // 基于ssh的密钥认证 repl_user=slave //数据库用户名 repl_password=123456 //数据库密码 ping_interval=1 //ping间隔时长 [server1] //节点2 hostname=192.168.88.138 //节点2主机地址 ssh_port=22 //节点2的ssh端口 candidate_master=1 //将来可不可以成为master候选节点/主节点 [server2] hostname=192.168.88.139 ssh_port=22 candidate_master=1 [server3] hostname=192.168.88.140 ssh_port=22 candidate_master=1
在manager上检ssh互相通信配置是否ok
- [root@manager app1]# masterha_check_ssh --conf=/etc/mha/app1.cnf
- .........
- Fri Mar 31 11:18:54 2023 - [info] All SSH connection tests passed successfully.
检查管理的mysql复制集群的连接配置参数是否ok
- masterha_check_repl --conf=/etc/mha/app1.cnf
- ........
-
- MySQL Replication Health is OK.
- [root@manager app1]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null >/var/log/mha/app1/manager.log 2>&1 &
-
- [1] 2001
查看状态(也可以看日志)
- [root@manager app1]# masterha_check_status --conf=/etc/mha/app1.cnf
- app1 (pid:2001) is running(0:PING_OK), master:192.168.88.138
-
- #选举了138为主
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。