赞
踩
传统mysql主从架构存在单点故障的问题
传统架构中,只有一个mysql主服务器,所以当出现单点故障的时候,整个服务器群集就会瘫痪掉
为了解决这种情况,我们需要在主服务器宕机的时候,重新建立一台主服务器,负责监控等工作
VMware软件
一台centos7作为MHA
三台centos7作为mysql服务器
搭建好mysql主从复制环境
没有搭建的可以看我之前的博客
https://blog.csdn.net/CN_TangZheng/article/details/103897251
[root@slave2 ~]# mount.cifs //192.168.11.11/ccc /mnt
Password for root@//192.168.11.11/ccc:
[root@slave2 ~]# cd /mnt/shuju数据库/mha
[root@slave2 mha]#
[root@slave2 mha]# tar zxvf cmake-2.8.6.tar.gz -C /opt
[root@slave2 mha]# cd /opt/cmake-2.8.6/
[root@slave2 cmake-2.8.6]# ./configure
[root@slave1 cmake-2.8.6]# gmake
[root@slave1 cmake-2.8.6]# gmake install
https://blog.csdn.net/CN_TangZheng/article/details/103897251
记得关闭防火墙
主从复制环境中的mysql同步的用户名为:myslave 密码:123456
额外调整
所有数据库授权mha用户对数据库的操作权限,每一个mysql数据库都要设置
'//下面三条授权按理论是不用添加的,但是做案例实验环境时候诵过MHA检查MySQL主从有报错,报两个从库通过主机名连接不上主库,所以所有数据库加上下面的授权;'
mysql> grant all privileges on *.* to 'mha'@'master' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
'//三台mysql服务器的授权增加一个给mha管理'
mysql> grant all privileges on *.* to 'mha'@'192.168.79.%' identified by 'manager';
'//主备mysql服务器添加用户和权限'
'//192.168.79.0段的从服务器拥有复制权限,可以使用myslave身份123456密码复制所有的库和表'
mysql> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.79.%' IDENTIFIED BY '123456';
slave1和slave2两个从数据库设置为只读
mysql> set global read_only=1;
所有slave节点配置二进制日志,注释掉client段的默认字符集
[root@slave1 ~]# vim /etc/my.cnf
[mysqld]段落添加
log-bin=slave1-bin '//添加二进制日志'
所以slave节点都要配置,我就不一一展示了
'//从服务器上都注释掉client段的默认字符集'
[root@slave1 ~]# vim /etc/my.cnf
[client]
#default-character-set=utf8 '//注释掉此'
创建软连接
所有节点创建软连接
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/
创建mysql软连接
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/
所有服务器上都要安装mha依赖的环境,此处仅展示mha_manager的安装
[root@mha_manager ~]# yum install epel-release --nogpgcheck -y
[root@mha_manager ~]# yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
所有服务器都安装安装node组件,此处仅展示master服务器的安装
[root@master ~]# tar zxvf /mnt/shuju数据库/mha/mha4mysql-node-0.57.tar.gz
[root@master ~]# cd mha4mysql-node-0.57/
[root@master mha4mysql-node-0.57]# yum install perl-Module-Install -y
[root@master mha4mysql-node-0.57]# perl Makefile.PL
==> Auto-install the 1 mandatory module(s) from CPAN? [y] y '//输入y'
[root@master mha4mysql-node-0.57]# make
[root@master mha4mysql-node-0.57]# make install
仅mha_manager服务器安装manager组件
[root@mha_manager ~]# tar zxvf /mnt/shuju数据库/mha/mha4mysql-manager-0.57.tar.gz
[root@mha_manager ~]# cd mha4mysql-manager-0.57/
[root@mha_manager mha4mysql-manager-0.57]# perl Makefile.PL
[root@mha_manager mha4mysql-manager-0.57]# make
[root@mha_manager mha4mysql-manager-0.57]# make install
mha_manager主机配置到所有数据库节点的密钥对验证
[root@mha_manager ~]# ssh-keygen -t rsa
'//创建非对称密钥对'
'//因为想要免密登陆,所以三个都直接回车就行,不需要设置密码'
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
'//上传秘钥到节点服务器'
[root@mha_manager ~]# ssh-copy-id 192.168.79.134
Are you sure you want to continue connecting (yes/no)? yes
root@192.168.79.134's password: //输入密码'
'//相同方法建立其他服务器'
[root@mha_manager ~]# ssh-copy-id 192.168.79.135
[root@mha_manager ~]# ssh-copy-id 192.168.79.136
在master上配置到数据库节点slave1和slave2的免密验证
[root@master mha4mysql-node-0.57]# ssh-keygen -t rsa '//创建非对称密钥对'
'//用上述方法建立其他服务器'
[root@master mha4mysql-node-0.57]# ssh-copy-id 192.168.79.135
[root@master mha4mysql-node-0.57]# ssh-copy-id 192.168.79.136
在slave1上配置到数据库节点master和slave2的免密验证
[root@slave1 ~]# ssh-keygen -t rsa
[root@slave1 ~]# ssh-copy-id 192.168.79.134
[root@slave1 ~]# ssh-copy-id 192.168.79.136
在slave2上配置到数据库节点master和slave1的免密验证
[root@slave2 ~]# ssh-keygen -t rsa
[root@slave2 ~]# ssh-copy-id 192.168.79.134
[root@slave2 ~]# ssh-copy-id 192.168.79.135
复制相关脚本到/usr/local/bin目录
[root@mha_manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
[root@mha_manager samples]# ls -l /usr/local/bin/scripts/
-rwxr-xr-x. 1 1001 1001 3648 5月 31 2015 master_ip_failover 自动切换时 VIP管理的脚本
-rwxr-xr-x. 1 1001 1001 9870 5月 31 2015 master_ip_online_change 在线切换VIP的管理
-rwxr-xr-x. 1 1001 1001 11867 5月 31 2015 power_manager 故障发生后关闭主机的脚本
-rwxr-xr-x. 1 1001 1001 1360 5月 31 2015 send_report 因故障切换后发送报警的脚本
[root@mha_manager ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/ '//自动切换时 VIP管理的脚本'
修改master_ip_failover 脚本
[root@mha_manager ~]# vim /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 = '192.168.79.100'; my $brdc = '192.168.79.255'; my $ifdev = 'ens33'; my $key = '1'; my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; my $exit_code = 0; #my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;"; #my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key"; 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; } } sub start_vip() { `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`; } # A simple system call that disable the VIP on the old_master sub stop_vip() { `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"; }
创建MHA软件目录并拷贝配置文件
[root@mha_manager ~]# mkdir /etc/masterha [root@mha_manager ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/ [root@mha_manager ~]# vim /etc/masterha/app1.cnf '//删除内容,重新编写脚本' [server default] manager_log=/var/log/masterha/app1/manager.log manager_workdir=/var/log/masterha/app1 master_binlog_dir=/usr/local/mysql/data master_ip_failover_script=/usr/local/bin/master_ip_failover master_ip_online_change_script=/usr/local/bin/master_ip_online_change password=manager ping_interval=1 remote_workdir=/tmp repl_password=123456 repl_user=myslave secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.79.135 -s 192.168.79.136 shutdown_script="" ssh_user=root user=mha [server1] hostname=192.168.79.134 port=3306 [server2] candidate_master=1 hostname=192.168.79.135 check_repl_delay=0 port=3306 [server3] hostname=192.168.79.136 port=3306
验证密钥对
[root@mha_manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Fri Jan 10 01:14:10 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Jan 10 01:14:10 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Jan 10 01:14:10 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Jan 10 01:14:10 2020 - [info] Starting SSH connection tests..
Fri Jan 10 01:14:11 2020 - [debug]
Fri Jan 10 01:14:10 2020 - [debug] Connecting via SSH from root@192.168.79.135(192.168.79.135:22) to root@192.168.79.136(192.168.79.136:22)..
Fri Jan 10 01:14:11 2020 - [debug] ok.
Fri Jan 10 01:14:12 2020 - [debug]
Fri Jan 10 01:14:10 2020 - [debug] Connecting via SSH from root@192.168.79.136(192.168.79.136:22) to root@192.168.79.135(192.168.79.135:22)..
Fri Jan 10 01:14:11 2020 - [debug] ok.
Fri Jan 10 01:14:12 2020 - [info] All SSH connection tests passed successfully.
测试mysql主从连接
[root@mha_manager ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf
相关报错,请查阅我的博客
https://blog.csdn.net/CN_TangZheng/article/details/103925515
添加虚拟IP
[root@master ~]# /sbin/ifconfig ens33:1 192.168.79.100/24
启动MHA
[root@mha_manager ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null >
[root@mha_manager ~]# /var/log/masterha/app1/manager.log 2>&1 &
'//查看到当前的master节点是谁'
[root@mha_manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
'//查看当前日志信息'
[root@mha_manager ~]# cat /var/log/masterha/app1/manager.log
启动监控观察日志记录
[root@mha_manager ~]# tailf /var/log/masterha/app1/manager.log
关掉master服务器
[root@master ~]# pkill -9 mysqld
此时发现主服务器已经变更
slave1查看
[root@slave1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.79.135 netmask 255.255.255.0 broadcast 192.168.79.255
inet6 fe80::159a:a8d1:5769:74d0 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:34:57:c1 txqueuelen 1000 (Ethernet)
RX packets 347068 bytes 28229347 (26.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 528201 bytes 67755670 (64.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.79.100 netmask 255.255.255.0 broadcast 192.168.79.255
ether 00:0c:29:34:57:c1 txqueuelen 1000 (Ethernet)
mha_manager查看
'//会动态显示后续信息'
Generating relay diff files from the latest slave succeeded.
192.168.79.136(192.168.79.136:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.79.135(192.168.79.135:3306)
192.168.79.135(192.168.79.135:3306): Resetting slave info succeeded.
Master failover to 192.168.79.135(192.168.79.135:3306) completed successfully.
直接在mha_manager上安装mysql
[root@mha_manager ~]# yum install mysql -y
在vip的数据库服务器上提取
[root@slave1 ~]# mysql -uroot -p
Enter password:
mysql> grant all on *.* to 'root'@'%' identified by 'abc123';
在客户机(mha_manager)上用虚拟IP进行登录
[root@mha_manager ~]# mysql -h 192.168.79.100 -uroot -pabc123
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| yiku |
| mysql |
| performance_schema |
| erku |
| sanku |
| sys |
+--------------------+
8 rows in set (0.00 sec)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。