利用MHA实现mysql的高可用

1.配置基于gtid的主从复制

[root@server1 MYSQL]# vim /etc/my.cnf

在这里插入图片描述

[root@server1 MYSQL]# systemctl start mysqld
[root@server1 MYSQL]# cat /var/log/mysqld.log |grep password
2020-03-08T06:45:33.282665Z 1 [Note] A temporary password is generated for root@localhost: 4(OoP3Y&3!yE
[root@server1 MYSQL]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 

在这里插入图片描述在这里插入图片描述

[root@server2 MYSQL]# vim /etc/my.cnf

在这里插入图片描述

[root@server2 MYSQL]# systemctl start mysqld
[root@server2 MYSQL]# cat /var/log/mysqld.log |grep password
2020-03-08T06:53:32.565415Z 1 [Note] A temporary password is generated for root@localhost: (ugscGoJ78a1
[root@server2 MYSQL]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 

在这里插入图片描述在这里插入图片描述

[root@server3 MYSQL]# vim /etc/my.cnf

在这里插入图片描述

[root@server3 MYSQL]# systemctl start mysqld
[root@server3 MYSQL]# cat /var/log/mysqld.log |grep password
2020-03-08T07:01:04.059684Z 1 [Note] A temporary password is generated for root@localhost: l8!Q95srK8,/
[root@server3 MYSQL]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

在这里插入图片描述在这里插入图片描述

2. 安装MHA管理软件

[root@server4 ~]# ls
MHA-7
[root@server4 ~]# cd MHA-7/
[root@server4 MHA-7]# ls
master_ip_failover
master_ip_online_change
mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
mha4mysql-node-0.58-0.el7.centos.noarch.rpm
perl-Config-Tiny-2.14-7.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm
perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm
perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
perl-Mail-Sendmail-0.79-21.el7.noarch.rpm
perl-MIME-Lite-3.030-1.el7.noarch.rpm
perl-MIME-Types-1.38-2.el7.noarch.rpm
perl-Parallel-ForkManager-1.18-2.el7.noarch(1).rpm
perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
[root@server4 MHA-7]# 
[root@server4 MHA-7]# yum install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm perl-* mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y

生成密钥,并发给server1 2 3 ,使server4可以免密登陆

[root@server4 MHA-7]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 

ssh-copy-id root@172.25.254.1
ssh-copy-id root@172.25.254.2
ssh-copy-id root@172.25.254.3
[root@server4 MHA-7]# ssh server1
[root@server1 ~]# logout
Connection to server1 closed.
[root@server4 MHA-7]# ssh server2
Last login: Sun Mar  8 14:22:58 2020 from foundation8.ilt.example.com
[root@server2 ~]# logout
Connection to server2 closed.
[root@server4 MHA-7]# ssh server3
Last login: Sun Mar  8 14:23:41 2020 from foundation8.ilt.example.com
[root@server3 ~]# logout
Connection to server3 closed.
[root@server4 MHA-7]#

在这里插入图片描述
将MHA软件同发送给server1 2 3

[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server1:/root/
The authenticity of host 'server1 (172.25.254.1)' can't be established.
ECDSA key fingerprint is 00:d6:7c:cd:b0:27:4d:d3:1c:cb:1e:12:43:1a:5a:e0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server1,172.25.254.1' (ECDSA) to the list of known hosts.
root@server1's password: 
mha4mysql-node-0.58-0.el7.centos.noarch.rpm            100%   35KB  35.5KB/s   00:00    
[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server2:/root/
The authenticity of host 'server2 (172.25.254.2)' can't be established.
ECDSA key fingerprint is d4:42:84:ef:fe:b7:ad:e2:06:f7:37:9a:f5:b0:c9:8f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server2,172.25.254.2' (ECDSA) to the list of known hosts.
root@server2's password: 
mha4mysql-node-0.58-0.el7.centos.noarch.rpm            100%   35KB  35.5KB/s   00:00    
[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server3:/root/
The authenticity of host 'server3 (172.25.254.3)' can't be established.
ECDSA key fingerprint is a7:44:fc:4a:52:58:80:45:e5:9e:f5:87:81:fe:c7:fb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server3,172.25.254.3' (ECDSA) to the list of known hosts.
root@server3's password: 
mha4mysql-node-0.58-0.el7.centos.noarch.rpm            100%   35KB  35.5KB/s   00:00    
[root@server4 MHA-7]# 

将server4发送的软件在server1、server2和server3中安装

[root@server1 ~]# yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@server2 ~]# yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@server3 ~]# yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm

server4配置MHA:

<1> 创建mha工作目录及编写配置文件

[root@server4 MHA-7]# mkdir /etc/masterha
[root@server4 MHA-7]# cd /etc/masterha/
[root@server4 masterha]# l
-bash: l: command not found
[root@server4 masterha]# ls
[root@server4 masterha]# vim master.cnf
[root@server4 masterha]# vim master.cnf
[root@server4 masterha]# 

在这里插入图片描述在这里插入图片描述
<2> 检测ssh连接
通过masterha_check_ssh检查ssh连接,由于之前我们设置的是server4与server1,2,3建立免密连接,但server1,2,3之间却不是免密连接的,因此我们需要使各结点均可免密登录即可。

[root@server4 masterha]# scp -r ~/.ssh server1:
id_rsa                                             100% 1675     1.3MB/s   00:00    
id_rsa.pub                                         100%  394   368.6KB/s   00:00    
known_hosts                                        100%  543   475.0KB/s   00:00    
[root@server4 masterha]# scp -r ~/.ssh server2:
id_rsa                                             100% 1675     1.1MB/s   00:00    
id_rsa.pub                                         100%  394   342.7KB/s   00:00    
known_hosts                                        100%  543   511.8KB/s   00:00    
[root@server4 masterha]# scp -r ~/.ssh server3:
id_rsa                                             100% 1675     1.1MB/s   00:00    
id_rsa.pub                                         100%  394   365.1KB/s   00:00    
known_hosts               

masterha_check_ssh --conf=/etc/masterha/master.cnf     ##可以看到ssh连接成功

在这里插入图片描述在这里插入图片描述
<3> 检查复制功能
由于之前我们对数据库进行了安全初始化,数据库不允许root用户远程登录,因此我们需要在主库中给root用户授权,然后才能通过复制功能检测(在server1中授权即可)

在这里插入图片描述在这里插入图片描述在这里插入图片描述
<4> 测试手动切换master
(当前server1是主,server2和server3为备)
进行死切
当server1节点服务停止后,server2能否接替成为主节点

在server1中关闭mysql服务

##在server1中关闭服务
[root@server1 ~]# systemctl stop mysqld

##在server4中手动切换
[root@server4 ~]# masterha_master_switch --master_state=dead --conf=/etc/masterha/master.cnf --dead_master_host=172.25.254.1 --dead_master_ip=172.25.254.1 --dead_master_port=3306 --new_master_host=172.25.254.2 --new_master_port=3306

在这里插入图片描述在这里插入图片描述
在其他结点测试,查看主数据库是否切换,server3中,查看从结点的状态,可以看到master的主机地址已经切换,在server2中从节点状态为空
在这里插入图片描述在这里插入图片描述

恢复server1数据库,将server1作为slave加到高可用集群中

systemctl start mysqld
mysql -uroot -pWestos+001
change master to master_host='172.25.254.2',
master_user='repl',
master_password='Westos+001',
master_auto_position=1;
start slave;
show slave status\G

在这里插入图片描述在这里插入图片描述
MHA只负责节点之间的切换,底层的主从复制需要自己提前保证正确

(5)手动在线切换master( 活切 )

<1> 在/etc/masterha目录下生成一个master.failover.complete文件,是来记录failover情况的,再进行failover时必须先把这个文件删除,不然不会failover

cd /etc/masterha
rm -fr master.failover.complete 

在这里插入图片描述
<2> 手动在线切换

masterha_master_switch --master_state=alive --conf=/etc/masterha/master.cnf --new_master_host=172.25.254.1 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000

在这里插入图片描述在这里插入图片描述
<3> server1中查看之前从结点的设置,变为空,则证明现在server1是master,server2和server3中查看从结点状态,会发现master的ip变为server1的 ip
在这里插入图片描述在这里插入图片描述
(6)半自动切换节点
MHA自带的manager守护进程后台自动检测

[root@server4 masterha]# nohup masterha_manager --conf=/etc/masterha/master.cnf &>/dev/null &
[1] 12868
[root@server4 masterha]# 

在这里插入图片描述server1上
在这里插入图片描述现在手动关闭server1上的mysqld服务

[root@server1 ~]# systemctl stop mysqld
[root@server1 ~]#

在这里插入图片描述在这里插入图片描述现在让server1重新上线,成为slave
在这里插入图片描述关闭MHA的manager的守护进程

[root@server4 masterha]# masterha_stop --conf=/etc/masterha/master.cnf
MHA Manager is not running on master(2:NOT_RUNNING).
[root@server4 masterha]#

(7) 测试全自动切换master(谁是主---->谁得到VIP)
实际上就是编写了两个脚本,把它们加入到配置文件中,在出问题时,通过使用这两个脚本,实现了master的一个专门的ip的漂移,以此来实现master的切换

首先清理master.failover.complete
然后修改两个脚本
[root@server4 bin]# vim master_ip_failover
[root@server4 bin]# vim master_ip_online_change

在这里插入图片描述在这里插入图片描述将脚本加到配置文件中

vim /etc/masterha/master.cnf

在这里插入图片描述
添加脚本的执行权限

[root@server4 masterha]# cd /usr/local/bin/
[root@server4 bin]# ls
master_ip_failover  master_ip_online_change
[root@server4 bin]# chmod +x *
[root@server4 bin]# ls
master_ip_failover  master_ip_online_change
[root@server4 bin]# 
目前server2是主节点,为server2配置VIP
[root@server2 ~]# ip addr add 172.25.254.100/24 dev eth0
[root@server2 ~]# 

测试VIP的漂移(手动)
[root@server4 bin]# masterha_stop --conf=/etc/masterha/master.cnf
MHA Manager is not running on master(2:NOT_RUNNING).
[root@server4 bin]# 

将server2往server1上面进行迁移
(在server2在线的情况下将server1提升为新的master)

在这里插入图片描述在这里插入图片描述在这里插入图片描述 发现此时VIP已经漂移到了server1上面
在这里插入图片描述
测试VIP的全自动漂移

在这里插入图片描述将主节点挂掉
[root@server1 ~]# systemctl stop mysqld
在这里插入图片描述在这里插入图片描述在这里插入图片描述
至此,就实现了自动切换主节点以及VIP的自动漂移。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MHA(Master High Availability)是一个用于MySQL主从复制环境下自动故障切换和主从切换的高可用性解决方案。它可以自动监控MySQL主从复制环境的运行状态,并在主库发生故障时自动将从库切换为新的主库,从而实现MySQL高可用性。MHA的安装和配置相对简单,可以通过以下步骤实现: 1.安装MHA软件包和依赖项 ```shell yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Config-IniFiles perl-Data-Dumper -y rpm -ivh mha4mysql-node-0.58-0.el6.noarch.rpm ``` 2.创建MHA配置文件 ```shell vi /etc/mha/app1.cnf ``` 在配置文件中添加以下内容: ```shell [server default] user=root password=123456 ssh_user=root repl_user=repl repl_password=123456 ping_interval=3 ping_type=SELECT master_binlog_dir=/data/mysql/mysql3306/data remote_workdir=/usr/local/mha ssh_port=22 master_ip_failover_script=/usr/local/mha/failover master_ip_online_change_script=/usr/local/mha/online_change shutdown_script=/usr/local/mha/shutdown_script report_script=/usr/local/mha/send_report [server1] hostname=192.168.1.101 port=3306 [server2] hostname=192.168.1.102 port=3306 ``` 3.测试MHA配置文件 ```shell masterha_check_ssh --conf=/etc/mha/app1.cnf masterha_check_repl --conf=/etc/mha/app1.cnf ``` 4.启动MHA管理节点 ```shell nohup masterha_manager --conf=/etc/mha/app1.cnf & ``` 5.测试MHA自动故障切换 在MHA管理节点上执行以下命令: ```shell masterha_master_switch --master_state=alive --conf=/etc/mha/app1.cnf --orig_master_is_new_slave --new_master=192.168.1.102 ``` 该命令将会将原来的主库(192.168.1.101)切换为从库,将原来的从库(192.168.1.102)切换为新的主库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值