赞
踩
MHA Manager(管理节点)
MHA Node(数据节点)
实验准备环境
1、主从复制的基础上搭建MHA
主服务器
主机名:server4
从服务器1
主机名:server5
从服务器2
主机名:server3
MHA服务器
主机名:server6
所有服务器安装MHA依赖环境
yum install epel-release --nogpgcheck
yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN
在所有服务器上都安装node组件
1、下载mha4mysql-node-0.57.tar.gz包
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57/
perl Makefile.PL
make && make install
在mha服务器上安装manager组件
1、下载mha4mysql-manager-0.57.tar.gz包
[root@server6 opt]# tar zxvf mha4mysql-manager-0.57.tar.gz
[root@server6 opt]# cd mha4mysql-manager-0.57/
[root@server6 mha4mysql-manager-0.57]# perl Makefile.PL
[root@server6 mha4mysql-manager-0.57]# make && make install
主数据库:
[root@server4 ~]# ssh-keygen -t rsa
[root@server4 ~]# ssh-copy-id 20.0.0.50
[root@server4 ~]# ssh-copy-id 20.0.0.30
从数据库1:
[root@server5 opt]# ssh-keygen -t rsa
[root@server5 opt]# ssh-copy-id 20.0.0.40
[root@server5 opt]# ssh-copy-id 20.0.0.30
从数据库2:
[root@server5 opt]# ssh-keygen -t rsa
[root@server5 opt]# ssh-copy-id 20.0.0.40
[root@server5 opt]# ssh-copy-id 20.0.0.50
MHA服务器:
[root@server6 ~]# ssh-keygen -t rsa
[root@server6 ~]# ssh-copy-id 20.0.0.40
[root@server6 ~]# ssh-copy-id 20.0.0.50
[root@server6 ~]# ssh-copy-id 20.0.0.30
配置MHA服务器
[root@server6 ~]# cp -ra /opt/mha4mysql-manager-0.57/samples/scripts/ /usr/local/bin [root@server6 ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/ [root@server6 ~]# 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 = '20.0.0.200'; my $brdc = '20.0.0.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 \"`; } 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"; }
[root@server6 ~]# mkdir /etc/masterha [root@server6 ~]# cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/ [root@server6 ~]# vim /etc/masterha/app1.cnf 设置为一下内容 [server default] manager_workdir=/var/log/masterha/app1 manager_log=/var/log/masterha/app1/manager.log 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 user=mha ping_interval=1 remote_workdir=/tmp repl_password=123 repl_user=myslave secondary_check_script= /usr/local/bin/masterha_secondary_check -s 20.0.0.40 -s 20.0.0.30 shutdown_script="" ssh_user=root [server1] hostname=20.0.0.40 port=3306 [server2] hostname=20.0.0.50 port=3306 candidate_master=1 check_repl_delay=0 [server3] hostname=20.0.0.30 port=3306 [root@server6 ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
查看 MHA 状态,可以看到当前的 master 是主服务器节点
masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:24405) is running(0:PING_OK), master:20.0.0.40
创建虚拟地址
ifconfig ens33:1 20.0.0.200/24
查看从1服务器的虚拟IP地址有没有转移过来,从2上查看主服务器是不是从1
关闭主服务器的mysql服务
pkill mysqld
重新开启manager服务器
主服务器开启mysql服务
systemctl restart mysqld
查看从1服务器查看二进制文件和同步点
mysql> show master status;
在主服务器上执行同步,查看 IO 和 SQL 线程都是 yes 代表同步是否正常
mysql> change master to
master_host=‘20.0.0.20’,master_user=‘myslave’,master_password=‘123’,master_log_file=‘master-bin.000003’,master_log_pos=120;
mysql> start slave;
mysql> show slave status \G
在从1上创建,主服务器和从2上查看
从1服务器
mysql> create database aa;
mysql> use aa;
mysql> create table test(id int(3));
mysql> insert into test values(1);
mysql> select * from test;
±-----+
| id |
±-----+
| 1 |
±-----+
主服务器
mysql> use aa;
mysql> select * from test;
±-----+
| id |
±-----+
| 1 |
±-----+
从2服务器
mysql> use aa;
mysql> select * from test;
±-----+
| id |
±-----+
| 1 |
±-----+
在manager服务器上修改配置文件(再把这个记录添加进去,因为它检测掉失效时候会自动消失)
vi /etc/masterha/app1.cnf
…
[server1]
hostname=192.168.8.134
port=3306
…
在manager服务器上启动manager
masterha_check_ssh -conf=/etc/masterha/app1.cnf
masterha_check_repl -conf=/etc/masterha/app1.cnf
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
masterha_check_status --conf=/etc/masterha/app1.cnf
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。