mha+mysql自动切换

来源:互联网 发布:linux hook技术 编辑:程序博客网 时间:2024/04/27 04:19
manager:192.168.2.254master:192.168.2.252slave:192.168.2.253VIP:192.168.2.144操作系统:centos6.5 64位1.配置ssh互信(root用户 每台都执行)  # ssh-keygen   # ssh-copy-id 192.168.2.252  # ssh-copy-id 192.168.2.253  # ssh-copy-id 192.168.2.2542.在manager节点上安装mha  # yum install epel-release  # yum install perl-DBD-MySQL  # yum install perl-Config-Tiny  # yum install perl-Log-Dispatch  # yum install perl-Parallel-ForkManager  # rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm  # rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm  3.在node节点上装Mha(也就是master和slave上)  # yum install epel-release  # yum install perl-DBD-MySQL  4.安装配置mysql半同步插件(master和slave都操作)  mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';        mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';    [mysqld]  rpl_semi_sync_master_enabled=1  rpl_semi_sync_master_timeout=1000  rpl_semi_sync_slave_enabled=1  relay_log_purge=0  skip-name-resolve  server-id = 1    //只是这个参数不一样  log-bin=mysql-bin  read_only=1  slave-skip-errors=1396    在master和slave节点做软连接,否则可能会报错  ln -s /usr/local/mysql/bin/* /usr/bin/  masterha_check_repl --conf=/root/masterha/app1.cnf  masterha_check_ssh --conf=/root/masterha/app1.cnf  5.在master和slave里搭建主从mysql环境   此处略1万行   6.配置管理的MHA    [server default]    manager_workdir=/root/masterhamanager_log=/root/masterha/manager.loguser=rootpassword=redhatssh_user=rootrepl_user=webslave  repl_password=redhat ping_interval=1shutdown_script=""master_ip_online_change_script=""    master_ip_failover_script="/root/masterha/master_ip_failover"report_script="" [server1]hostname=192.168.2.252master_binlog_dir=/usr/local/mysql/datacandidate_master=1 [server2]hostname=192.168.2.253master_binlog_dir=/usr/local/mysql/datacandidate_master=1[server3]hostname=192.168.2.248master_binlog_dir=/usr/local/mysql/datacandidate_master=1master_ip_failover  脚本内容如下:#!/usr/bin/env perluse 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,  $new_master_user,  $new_master_password);my $vip = '192.168.2.144/24';  # Virtual IPmy $key = '1';my $gateway = '192.168.2.1';my $ssh_start_vip = "sudo /sbin/ifconfig eth0:$key $vip;sudo /sbin/arping -I eth0 -c 3 -s $vip $gateway >/dev/null 2>&1";my $ssh_stop_vip = "sudo /sbin/ifconfig eth0:$key down";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" ) {# $orig_master_host, $orig_master_ip, $orig_master_port are passed.# If you manage master ip address at global catalog database,# invalidate orig_master_ip here.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" ) {# all arguments are passed.# If you manage master ip address at global catalog database,# activate new_master_ip here.# You can also grant write access (create user, set read_only=0, etc) here.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;}}# A simple system call that enable the VIP on the new mastersub start_vip() {`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;}# A simple system call that disable the VIP on the old_mastersub stop_vip() {return 0 unless ($ssh_user);`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";}    7.MHA启动及基本维护   nohup masterha_manager --conf=/root/masterha/app1.cnf > /tmp/mha_manager.log  </dev/null 2>&1 &    --这是启动   masterha_check_ssh --conf=/root/masterha/app1.cnf  --查看ssh互信是否正常   masterha_check_repl --conf=/root/masterha/app1.cnf  --查看主从复制是否正常   masterha_stop  --conf=/root/masterha/app1.cnf   --停止  8.测试  1.主库断电    2.主库断网  3.主库重启  4.主库关机  以上情况都测试过都能自动切换~  请看日志分析切换过程9.主库模拟故障后的恢复  在mha的日志里查看  Sat Jun 13 19:31:17 2015 - [info]  All other slaves should start replication from here. Statement should be:   CHANGE MASTER TO MASTER_HOST='192.168.2.253', MASTER_PORT=3306, MASTER_LOG_FILE='binlog.000003', MASTER_LOG_POS=120, MASTER_USER='webslave', MASTER_PASSWORD='xxx';      然后再就得主库里执行下,再start slave ,然后就得主库就变为了新的主库的从库了。  记得看下show slave status\G;                              

0 0