mysql高可用集群
MySQL之高可用集群部署及故障切换实现一、mha
1、概念
2、mha 的组成
3、mha 的特点
二、搭建mysql+mha
思路和准备工作
1、mha架构
数据库安装
一主两从
mha搭建
2、故障模拟
模拟主库失效
备选主库成为主库
原故障主库恢复重新加入到mha成为从库
3、准备4台安装mysql虚拟机
mha高可用集群相关软件包
mhamanager ip:192.168.221.30
mysql1 ip:192.168.221.20
mysql2 ip:192.168.221.100
mysql3 ip: 192.168.221.110
1、关闭防火墙,检测端口是否开启
|
systemctl stop firewalld systemctl disable firewalld setenforce 0 netstat -natp | grep 3306 |
2、修改mysql节点的主机名
mysql1(192.168.221.20)
|
hostnamectl set -hostname mysql1 su - hostnamectl set -hostname mysql2 su - hostnamectl set -hostname mysql3 su - |
3、修改三台mysql服务器的主配置文件/etc/my.cnf,并创建命令软链接
|
mysql1 vim /etc/my.cnf [mysqld] server-id = 1 log_bin = master-bin log-slave-updates = true systemctl restart mysqld ln -s /usr/ local /mysql/bin/mysql /usr/sbin/ ln -s /usr/ local /mysql/bin/mysqlbinlog /usr/sbin/ mysql2 vim /etc/my.cnf server-id = 2 #server-id = 3 mysql3则为3,三台服务器 server-id 不能一样 log_bin = master-bin relay-log = relay-log-bin relay-log- index = slave-relay-bin. index systemctl restart mysqld ln -s /usr/ local /mysql/bin/mysql /usr/sbin/ ln -s /usr/ local /mysql/bin/mysqlbinlog /usr/sbin/ |
4、配置mysql一主两从
|
(1)所有mysql服务器进行mysql授权 mysql1(192.168.221.20) mysql2(192.168.221.100) mysql3(192.168.221.110) 3台都要配置 mysql -uroot -p123 grant replication slave on *.* to 'myslave' @ '192.168.221.%' identified by '123' ; grant all privileges on *.* to 'mha' @ '192.168.221.%' identified by 'manager' ; grant all privileges on *.* to 'mha' @ 'mysql1' identified by 'manager' ; grant all privileges on *.* to 'mha' @ 'mysql2' identified by 'manager' ; grant all privileges on *.* to 'mha' @ 'mysql3' identified by 'manager' ; flush privileges ; show master status; |
|
(2)在 slave1、slave2 节点执行同步操作 change master to master_host= '192.168.221.20' ,master_user= 'myslave' ,master_password= '123' ,master_log_file= 'master-bin.000005' ,master_log_pos=1991; start slave; show slave status\g slave_io_running: yes slave_sql_running: yes #一般 slave_io_running: no 的可能性: #网络不通 #my.cnf配置有问题 #密码、file文件名、pos偏移量不对 #防火墙没有关闭 |
|
(3)slave1、slave2 节点设置为只读模式 mysql2(192.168.221.100) mysql3(192.168.221.110) set global read_only=1; #改回读写状态 set global read_only=0; |
|
(4)主从复制验证 mysql1(192.168.221.20) 创建库 create database srs; use test; create table test(id int ); insert into test values (1); mysql2(192.168.221.100) mysql3(192.168.221.110) 查询库验证 show databases; |
5、安装 mha 软件
|
(1)所有服务器上都安装 mha 依赖的环境 mhamanager(192.168.221.30) mysql1(192.168.221.20) mysql2(192.168.221.100) mysql3(192.168.221.110) 首先安装 epel 源,需要在线源安装 再在所有服务器上安装 node 组件 #安装在线源 mv /etc/yum.repos.d/repos.bak/centos-* /etc/yum.repos.d/ yum list yum install epel-release --nogpgcheck -y yum install -y perl-dbd-mysql \ perl-config-tiny \ perl-log-dispatch \ perl-parallel-forkmanager \ perl-extutils-cbuilder \ perl-extutils-makemaker \ perl-cpan |
|
(2)所有服务器安装mha node软件包 mhamanager(192.168.221.30) mysql1(192.168.221.20) mysql2(192.168.221.100) mysql3(192.168.221.110) 对于每个操作系统版本不一样,这里 centos7.4 必须选择 0.57 版本。 在所有服务器上必须先安装 node 组件,最后在 mha-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件。 #将软件包mha4mysql-node-0.57.tar.gz放入/opt目录下 cd /opt tar zxvf mha4mysql-node-0.57.tar.gz cd mha4mysql-node-0.57 perl makefile.pl make && make install 在 mha manager 节点上安装 manager 组件 将软件包mha4mysql-manager-0.57.tar.gz放入/opt目录下 cd /opt tar zxvf mha4mysql-manager-0.57.tar.gz cd mha4mysql-manager-0.57 perl makefile.pl make && make install #node组件安装后也会在/usr/ local /bin 下面会生成几个脚本(这些工具通常由 mhamanager 的脚本触发,无需人为操作) #manager 组件安装后在/usr/ local /bin 下面会生成几个工具 |
6、在所有服务器上配置无密码认证
|
(1)在 manager 节点上配置到所有数据库节点的无密码认证 mhamanager(192.168.221.30) ssh-keygen -t rsa #一路按回车键 ssh-copy-id 192.168.221.20 ssh-copy-id 192.168.221.100 ssh-copy-id 192.168.221.110 (2)在 mysql1 上配置到数据库节点 mysql2 和 mysql3 的无密码认证 ssh-keygen -t rsa mysql1(192.168.221.20) ssh-keygen -t rsa ssh-copy-id 192.168.221.100 ssh-copy-id 192.168.221.110 (3)在 mysql2 上配置到数据库节点 mysql1 和 mysql3 的无密码认证 mysql2(192.168.221.100) ssh-keygen -t rsa ssh-copy-id 192.168.221.20 ssh-copy-id 192.168.221.110 (4)在 mysql3 上配置到数据库节点 mysql1 和 mysql2 的无密码认证 mysql3(192.168.221.110) ssh-keygen -t rsa ssh-copy-id 192.168.221.20 ssh-copy-id 192.168.221.100 篇幅太长不予演示 |
7、在 manager 节点上配置 mha
|
mhamanager(192.168.221.30) (1)在 manager 节点上复制相关脚本到/usr/ local /bin 目录 cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/ local /bin #复制后会有四个执行文件 ll /usr/ local /bin/scripts/ (2)复制上述的自动切换时 vip 管理的脚本到 /usr/ local /bin 目录,这里使用master_ip_failover脚本来管理 vip 和故障切换 cp /usr/ local /bin/scripts/master_ip_failover /usr/ local /bin #先清空原有内容 echo '' > /usr/ local /bin/master_ip_failover #直接复制并修改vip相关参数 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.221.200' ; #指定vip的地址 my $brdc = '192.168.221.255' ; #指定vip的广播地址 my $ifdev = 'ens33' ; #指定vip绑定的网卡 my $ key = '1' ; #指定vip绑定的虚拟网卡序列号 my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip" ; #代表此变量值为ifconfig ens33:1 192.168.221.200 my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down" ; #代表此变量值为ifconfig ens33:1 down my $exit_code = 0; #指定退出状态码为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"; } |
标签: