MySQL 5.5 keepalived + haproxy + MHA 自动容灾

vip 192.168.161.80

mysql master 192.168.161.81

mysql backup 192.168.161.82(haproxy master)

mysql slave     192.168.161.83 (haproxy backup)

keepalived安装:

[plain]  view plain copy
  1. yum -y install ipvsadm openssl-devel libnl* popt-static  
  2. uname -r  
  3. ln -s /usr/src/kernels/2.6.32-279.el6.x86_64 /usr/src/linux  
  4. wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz  
  5. tar zxf keepalived-1.2.6.tar.gz   
  6. cd keepalived-1.2.6  
  7. ./configure  
  8. make   
  9. make install   
  10. #在82上配置  
  11. vim /usr/local/etc/keepalived/keepalived.conf  
  12. global_defs {  
  13.    notification_email {  
  14.      DBA@xx.com  
  15.    }  
  16.    notification_email_from 192.168.161.82@xx.com  
  17.    smtp_server 127.0.0.1  
  18.    smtp_connect_timeout 30  
  19.    router_id  NodeA  
  20. }  
  21.   
  22. vrrp_instance VI_1 {  
  23.     state MASTER  
  24.     interface eth1  
  25.     virtual_router_id 82  
  26.     nopreempt  
  27.     priority 100  
  28.     advert_int 1  
  29.     authentication {  
  30.         auth_type PASS  
  31.         auth_pass 1111  
  32.     }  
  33.     virtual_ipaddress {  
  34.         192.168.161.80/24  
  35.     }  
  36. }  
  37.   
  38. 在83上配置  
  39. <pre name="code" class="plain">vim /usr/local/etc/keepalived/keepalived.conf  
  40. global_defs {  
  41.    notification_email {  
  42.      DBA@xx.com  
  43.    }  
  44.    notification_email_from 192.168.161.83@xx.com  
  45.    smtp_server 127.0.0.1  
  46.    smtp_connect_timeout 30  
  47.    router_id  NodeA  
  48. }  
  49.   
  50. vrrp_instance VI_1 {  
  51.     state BACKUP  
  52.     interface eth1  
  53.     virtual_router_id 83  
  54.     nopreempt  
  55.     priority 100  
  56.     advert_int 1  
  57.     authentication {  
  58.         auth_type PASS  
  59.         auth_pass 1111  
  60.     }  
  61.     virtual_ipaddress {  
  62.         192.168.161.80/24  
  63.     }  
  64. }  
  65.   
  66.   
  67. /usr/local/sbin/keepalived -f /usr/local/etc/keepalived/keepalived.conf  

haproxy安装:

[plain]  view plain copy
  1. wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz  
  2. tar zxvf haproxy-1.4.22.tar.gz  
  3. cd haproxy-1.4.22  
  4. uname -a //查看linux内核版本  
  5. make TARGET=linux26 PREFIX=/usr/local/haproxy-1.4.22  
  6. make install PREFIX=/usr/local/haproxy-1.4.22  
  7. #在82和83上创建conf文件  
  8. vim /usr/local/haproxy-1.4.22/conf/haproxy.conf  
  9. global  
  10.         maxconn 10240  
  11.         chroot /usr/local/haproxy-1.4.22  
  12.         uid 99  
  13.         gid 99  
  14.         daemon  
  15.         quiet  
  16.         nbproc 3  
  17.         pidfile /tmp/haproxy.pid  
  18.   
  19. defaults  
  20.         mode http  
  21.         retries 2  
  22.         option redispatch  
  23.         option abortonclose  
  24.         maxconn 4096  
  25.         timeout check 2000  
  26.         log 127.0.0.1 local0 err #[err warning info debug]  
  27.   
  28. listen test1  
  29.         bind 0.0.0.0:5306  
  30.         mode tcp  
  31.         maxconn 4086  
  32.         #log 127.0.0.1 local0 debug  
  33.         server s1 192.168.161.81:3306  
  34.   
  35. #创建替换脚本 这里没有使用平滑重启  
  36. vim /opt/shell/haproxy_reload.sh  
  37. #!/bin/sh  
  38. sed 's/'$1'/'$2'/g' -i /usr/local/haproxy-1.4.22/conf/haproxy.conf  
  39. ps aux | grep 'haproxy -f /usr/local/haproxy-1.4.22/conf/haproxy.conf' | grep -v grep | awk '{print $2}' | xargs kill  
  40. /usr/local/haproxy-1.4.22/sbin/haproxy -f /usr/local/haproxy-1.4.22/conf/haproxy.conf  
  41.   
  42. #启动haproxy  
  43. /usr/local/haproxy-1.4.22/sbin/haproxy  -f /usr/local/haproxy-1.4.22/conf/haproxy.conf   

在81和83上安装mha node,在82上安装mha manager

[plain]  view plain copy
  1. #mysql bin  
  2. cp /usr/local/mysql/bin/* /usr/local/bin/  
  3.   
  4. #mysql node  
  5. yum install perl-CPAN -y  
  6. tar zxf mha4mysql-node-0.53.tar.gz  
  7. cd mha4mysql-node-0.53  
  8. perl Makefile.PL   
  9. make && make install  
  10.   
  11. #mha manager  
  12. yum install perl-Config-Tiny perl-Params-Validate perl-Log-Dispatch perl-Parallel-ForkManager -y  
  13. tar zxf mha4mysql-manager-0.53.tar.gz  
  14. cd mha4mysql-manager-0.53  
  15. perl Makefile.PL   
  16. make && make install  
  17.   
  18. #在三台服务器上分别执行public ssh  
  19. ssh-keygen -t rsa  
  20. <pre name="code" class="plain">ssh-copy-id -i /root/.ssh/id_rsa.pub root@182.168.161.80  
 ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.161.81ssh-copy-id -i /root/.ssh/id_rsa.pub root@182.168.161.82
[plain]  view plain copy
  1. ssh-copy-id -i /root/.ssh/id_rsa.pub root@182.168.161.83  
  2.   
  3. #replication user  
  4. GRANT ALL PRIVILEGES ON *.* TO 'rep_user'@'192.168.%' IDENTIFIED BY '123456';  
  5.   
  6. #slave set  
  7. set global read_only=1;  
  8. set global relay_log_purge=0;  
  9. 01 00 * * * /usr/local/bin/purge_relay_logs -user=rep_user -password=123456 -disable_relay_log_purge >> /tmp/purge_relay_logs.log 2>&1  
  10.   
  11. #check  
  12. masterha_check_ssh --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf --remove_dead_master_conf   
  13. masterha_check_repl --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf --remove_dead_master_conf  
  14.   
  15. #start  
  16. nohup masterha_manager --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /tmp/mha_manager.log 2>&1 &  
  17.   
  18. #status  
  19. masterha_check_status --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf  
  20.   
  21. #log  
  22. tail -1000 /var/log/masterha/app1/manager.log  
  23.   
  24. #error  
  25. #[error][/usr/lib/perl5/site_perl/5.8.8/MHA/ManagerUtil.pm, ln178] Got ERROR: Use of uninitialized value in scalar chomp at /usr/lib/perl5/site_perl/5.8.8/MHA/ManagerConst.pm line 90.  
  26. vi /usr/local/share/perl5/MHA/ManagerConst.pm  
  27. 89   my $msg  = $args{message};  
  28. 90   $msg = "" unless($msg);  <=插入一行  
  29. 91   chomp $msg;  
 
 

mha 配置文件

[plain]  view plain copy
  1. <pre name="code" class="plain">vim/usr/local/mha4mysql-manager-0.53/conf/app1.cnf  
  2.   
  3. [server default]  
  4. manager_workdir=/usr/local/mha4mysql-manager-0.53  
  5. manager_log=/usr/local/mha4mysql-manager-0.53/manager.log  
  6. master_ip_failover_script="/usr/local/mha4mysql-manager-0.53/scripts/master_ip_failover"  
  7. master_ip_online_change_script="/usr/local/mha4mysql-manager-0.53/scripts/master_ip_online_change"  
  8. ping_interval=1  
  9. user=xm_dba  
  10. password=123456  
  11. repl_user=rep_user  
  12. repl_password=123456  
  13. ssh_user=root  
  14.   
  15. [server1]  
  16. hostname=192.168.161.81  
  17. port=3306  
  18. candidate_master=1  
  19. master_binlog_dir="/home/mysql/data3306"  
  20.   
  21. [server2]  
  22. hostname=192.168.161.82  
  23. port=3306  
  24. candidate_master=1  
  25. master_binlog_dir="/home/mysql/data3306"  
  26.   
  27. [server3]  
  28. hostname=192.168.161.83  
  29. port=3306  
  30. no_master=1  
  31. master_binlog_dir="/home/mysql/data3306"  
 
 

[plain]  view plain copy
  1. <pre name="code" class="plain">vim /usr/local/mha4mysql-manager-0.53/scripts/master_ip_failover  
  2.   
  3. #!/usr/bin/env perl  
  4. use strict;  
  5. use warnings FATAL => 'all';  
  6.   
  7. use Getopt::Long;  
  8.   
  9. my (  
  10.     $command,          $ssh_user,        $orig_master_host, $orig_master_ip,  
  11.     $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port  
  12. );  
  13.   
  14. my $vip = '192.168.161.80';  # Virtual IP   
  15. my $key = "80";   
  16. my $ssh_start_vip = "ifup eth0:$key ";  
  17. my $ssh_stop_vip = "ifdown eth0:$key ";  
  18. my $check_vip = "ip add";  
  19. my $m_haproxy_ip = "192.168.161.82";  
  20. my $s_haproxy_ip = "192.168.161.83";  
  21. my $haproxy_change_master = "";  
  22. GetOptions(  
  23.     'command=s'          => \$command,  
  24.     'ssh_user=s'         => \$ssh_user,  
  25.     'orig_master_host=s' => \$orig_master_host,  
  26.     'orig_master_ip=s'   => \$orig_master_ip,  
  27.     'orig_master_port=i' => \$orig_master_port,  
  28.     'new_master_host=s'  => \$new_master_host,  
  29.     'new_master_ip=s'    => \$new_master_ip,  
  30.     'new_master_port=i'  => \$new_master_port,  
  31. );  
  32. #my $haproxy_change_master = "/opt/shell/haproxy_reload.sh $orig_master_ip $new_master_ip ";  
  33.   
  34. exit &main();  
  35.   
  36. sub main {  
  37.     print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";   
  38.   
  39.     if ( $command eq "stop" || $command eq "stopssh" ) {  
  40.   
  41.         # $orig_master_host, $orig_master_ip, $orig_master_port are passed.  
  42.         # If you manage master ip address at global catalog database,  
  43.         # invalidate orig_master_ip here.  
  44.         my $exit_code = 1;  
  45.         eval {  
  46.             print "Disabling the VIP on old master: $orig_master_host \n";  
  47.             &stop_vip();  
  48.             $exit_code = 0;  
  49.         };  
  50.         if ($@) {  
  51.             warn "Got Error: $@\n";  
  52.             exit $exit_code;  
  53.         }  
  54.         exit $exit_code;  
  55.     }  
  56.     elsif ( $command eq "start" ) {  
  57.   
  58.         # all arguments are passed.  
  59.         # If you manage master ip address at global catalog database,  
  60.         # activate new_master_ip here.  
  61.         # You can also grant write access (create user, set read_only=0, etc) here.  
  62.         my $exit_code = 10;  
  63.         eval {  
  64.             print "Enabling the VIP - $vip on the new master - $new_master_host \n";  
  65.             &start_vip();  
  66.             $exit_code = 0;  
  67.         };  
  68.         if ($@) {  
  69.             warn $@;  
  70.             exit $exit_code;  
  71.         }  
  72.         exit $exit_code;  
  73.     }  
  74.     elsif ( $command eq "status" ) {  
  75.         print "Checking the Status of the script.. OK \n";   
  76.         `ssh $ssh_user\@$vip \" $check_vip \"`;  
  77.         exit 0;  
  78.     }  
  79.     else {  
  80.         &usage();  
  81.         exit 1;  
  82.     }  
  83. }  
  84.   
  85. # A simple system call that enable the VIP on the new master   
  86. sub start_vip() {  
  87.     $haproxy_change_master = "/opt/shell/haproxy_reload.sh $orig_master_ip $new_master_ip ";  
  88.     `ssh $ssh_user\@$m_haproxy_ip \" $haproxy_change_master \"`;  
  89.     `ssh $ssh_user\@$s_haproxy_ip \" $haproxy_change_master \"`;  
  90.     #`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;  
  91. }  
  92. # A simple system call that disable the VIP on the old_master  
  93. sub stop_vip() {  
  94.     #`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;  
  95. }  
  96.   
  97. sub usage {  
  98.     print  
  99.     "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";  
  100. }  

这个脚本是从切换vip的脚本改过来的,以后在进一步优化吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值