MHA+keepalived 环境搭建



一.准备一套搭建好的MHA系统
步骤,参考下面地址:
http://blog.itpub.net/15412087/viewspace-2153879/

二.下载keepalived
下载地址:http://www.keepalived.org/download.html
上传至mysql1(master) 和 mysql3(candidate)

三.安装keepalived
1.安装依赖包
yum install -y gcc* openssl-devel popt-devel
yum install ipvsadm

2.编译安装
./configure --prefix=/usr/local/keepalived
make && make install

3.拷贝文件并且加入系统服务
cp /tmp/keepalived-2.0.4/keepalived/etc/init.d/keepalived /etc/init.d/
chmod +x /etc/init.d/keepalived
mkdir -p /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf    /etc/keepalived/
cp /tmp/keepalived-2.0.4/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived  /usr/sbin/

四.配置文件
vim /etc/keepalived/keepalived.conf
1.主节点

点击(此处)折叠或打开

  1. global_defs {
  2. notification_email {
  3. 277106621@qq.com
  4. }
  5. notification_email_from 305854332@qq.com
  6. smtp_server smtp.qq.com
  7. smtp_connect_timeout 30
  8. router_id MYSQL_MHA
  9. vrrp_skip_check_adv_addr
  10. vrrp_strict
  11. vrrp_garp_interval 0
  12. vrrp_gna_interval 0
  13. }
  14. vrrp_instance VI_1 {
  15. state BACKUP
  16. interface eth0
  17. virtual_router_id 51
  18. priority 100
  19. advert_int 1
  20. nopreempt
  21. authentication {
  22. auth_type PASS
  23. auth_pass 1111
  24. }
  25. virtual_ipaddress {
  26. 192.168.1.207/24
  27. }
  28. }

2.从节点

点击(此处)折叠或打开

  1. ! Configuration File for keepalived

  2. global_defs {
  3.    notification_email {
  4.     277106621@qq.com
  5.    }
  6.    notification_email_from 305854332@qq.com
  7.    smtp_server smtp.qq.com
  8.    smtp_connect_timeout 30
  9.    router_id MYSQL_MHA
  10.    vrrp_skip_check_adv_addr
  11.    vrrp_strict
  12.    vrrp_garp_interval 0
  13.    vrrp_gna_interval 0
  14. }

  15. vrrp_instance VI_1 {
  16.     state BACKUP
  17.     interface eth0
  18.     virtual_router_id 51
  19.     priority 90
  20.     advert_int 1
  21.     nopreempt
  22.     authentication {
  23.         auth_type PASS
  24.         auth_pass 1111
  25.     }
  26.     virtual_ipaddress {
  27.         192.168.1.207/24
  28.     }
  29. }

这里master服务器的state不配置成MASTER,且配置nopreempt,是期望在master宕机后再恢复时,不主动将MASTER状态抢过来,避免MySQL服务的波动。
这里没有配置vrrp_script,在后面会让mha实现vip的自动漂移。

参数文件说明:
https://blog.csdn.net/weiyuefei/article/details/78106202

五.启动keepalived服务
先启动主节点,后启动从节点keepalived服务
# service keepalived start
Starting keepalived:                                       [  OK  ]
# service keepalived status
keepalived (pid  30030) is running...

完成后,在主节点发现vip
测试:停止master服务器keepalived,检查VIP是否切换到备选master服务器(用ip addr命令验证即可)

六.修改MHA ip 迁移脚本

点击(此处)折叠或打开

  1. # cat /etc/mha/mhamanager/app1/scripts/master_ip_failover
  2. #!/usr/bin/env perl
  3. use strict;
  4. use warnings FATAL => 'all';
  5. use Getopt::Long;
  6. my (
  7. $command, $ssh_user, $orig_master_host, $orig_master_ip,
  8. $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
  9. );
  10. my $vip = '192.168.1.207/24';
  11. my $ssh_start_vip = "/etc/init.d/keepalived start";
  12. my $ssh_stop_vip = "/etc/init.d/keepalived stop";
  13. GetOptions(
  14. 'command=s' => \$command,
  15. 'ssh_user=s' => \$ssh_user,
  16. 'orig_master_host=s' => \$orig_master_host,
  17. 'orig_master_ip=s' => \$orig_master_ip,
  18. 'orig_master_port=i' => \$orig_master_port,
  19. 'new_master_host=s' => \$new_master_host,
  20. 'new_master_ip=s' => \$new_master_ip,
  21. 'new_master_port=i' => \$new_master_port,
  22. );
  23. exit &main();
  24. sub main {
  25. print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
  26. if ( $command eq "stop" || $command eq "stopssh" ) {
  27. my $exit_code = 1;
  28. eval {
  29. print "Disabling the VIP on old master: $orig_master_host \n";
  30. &stop_vip();
  31. $exit_code = 0;
  32. };
  33. if ($@) {
  34. warn "Got Error: $@\n";
  35. exit $exit_code;
  36. }
  37. exit $exit_code;
  38. }
  39. elsif ( $command eq "start" ) {
  40. my $exit_code = 10;
  41. eval {
  42. print "Enabling the VIP - $vip on the new master - $new_master_host \n";
  43. &start_vip();
  44. $exit_code = 0;
  45. };
  46. if ($@) {
  47. warn $@;
  48. exit $exit_code;
  49. }
  50. exit $exit_code;
  51. }
  52. elsif ( $command eq "status" ) {
  53. print "Checking the Status of the script.. OK \n";
  54. exit 0;
  55. }
  56. else {
  57. &usage();
  58. exit 1;
  59. }
  60. }
  61. sub start_vip() {
  62. `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  63. }
  64. sub stop_vip() {
  65. return 0 unless ($ssh_user);
  66. `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  67. }
  68. sub usage {
  69. print
  70. "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip
  71. --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
  72. }

点击(此处)折叠或打开

  1. # cat /etc/mha/mhamanager/app1/scripts/master_ip_online_change
  2. #!/usr/bin/env perl
  3. use strict;
  4. use warnings FATAL =>'all';
  5. use Getopt::Long;
  6. my $vip = '192.168.1.207/24'; # Virtual IP
  7. my $ssh_start_vip = "/etc/init.d/keepalived start";
  8. my $ssh_stop_vip = "/etc/init.d/keepalived stop";
  9. my $exit_code = 0;
  10. my (
  11. $command, $orig_master_is_new_slave, $orig_master_host,
  12. $orig_master_ip, $orig_master_port, $orig_master_user,
  13. $orig_master_password, $orig_master_ssh_user, $new_master_host,
  14. $new_master_ip, $new_master_port, $new_master_user,
  15. $new_master_password, $new_master_ssh_user,
  16. );
  17. GetOptions(
  18. 'command=s' => \$command,
  19. 'orig_master_is_new_slave' => \$orig_master_is_new_slave,
  20. 'orig_master_host=s' => \$orig_master_host,
  21. 'orig_master_ip=s' => \$orig_master_ip,
  22. 'orig_master_port=i' => \$orig_master_port,
  23. 'orig_master_user=s' => \$orig_master_user,
  24. 'orig_master_password=s' => \$orig_master_password,
  25. 'orig_master_ssh_user=s' => \$orig_master_ssh_user,
  26. 'new_master_host=s' => \$new_master_host,
  27. 'new_master_ip=s' => \$new_master_ip,
  28. 'new_master_port=i' => \$new_master_port,
  29. 'new_master_user=s' => \$new_master_user,
  30. 'new_master_password=s' => \$new_master_password,
  31. 'new_master_ssh_user=s' => \$new_master_ssh_user,
  32. );
  33. exit &main();
  34. sub main {
  35. #print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
  36. if ( $command eq "stop" || $command eq "stopssh" ) {
  37. # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
  38. # If you manage master ip address at global catalog database,
  39. # invalidate orig_master_ip here.
  40. my $exit_code = 1;
  41. eval {
  42. print "\n\n\n***************************************************************\n";
  43. print "Disabling the VIP - $vip on old master: $orig_master_host\n";
  44. print "***************************************************************\n\n\n\n";
  45. &stop_vip();
  46. $exit_code = 0;
  47. };
  48. if ($@) {
  49. warn "Got Error: $@\n";
  50. exit $exit_code;
  51. }
  52. exit $exit_code;
  53. }
  54. elsif ( $command eq "start" ) {
  55. # all arguments are passed.
  56. # If you manage master ip address at global catalog database,
  57. # activate new_master_ip here.
  58. # You can also grant write access (create user, set read_only=0, etc) here.
  59. my $exit_code = 10;
  60. eval {
  61. print "\n\n\n***************************************************************\n";
  62. print "Enabling the VIP - $vip on new master: $new_master_host \n";
  63. print "***************************************************************\n\n\n\n";
  64. &start_vip();
  65. $exit_code = 0;
  66. };
  67. if ($@) {
  68. warn $@;
  69. exit $exit_code;
  70. }
  71. exit $exit_code;
  72. }
  73. elsif ( $command eq "status" ) {
  74. print "Checking the Status of the script.. OK \n";
  75. `ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
  76. exit 0;
  77. }
  78. else {
  79. &usage();
  80. exit 1;
  81. }
  82. }
  83. # A simple system call that enable the VIP on the new master
  84. sub start_vip() {
  85. `ssh $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  86. }
  87. # A simple system call that disable the VIP on the old_master
  88. sub stop_vip() {
  89. `ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  90. }
  91. sub usage {
  92. print
  93. "Usage: master_ip_failover -command=start|stop|stopssh|status -orig_master_host=host -orig_master_ip=ip -
  94. orig_master_port=po
  95. rt -new_master_host=host -new_master_ip=ip -new_master_port=port\n";
  96. }

七.配置report脚本

点击(此处)折叠或打开

  1. # cat /etc/mha/mhamanager/app1/scripts/send_report
  2. #!/usr/bin/perl
  3. # Copyright (C) 2011 DeNA Co.,Ltd.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # itunder the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc.,
  18. # 51Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. ## Note: This is a sample script and is notcomplete. Modify the script based on your environment.
  20. use strict;
  21. use warnings FATAL => 'all';
  22. use Mail::Sender;
  23. use Getopt::Long;
  24. #new_master_host and new_slave_hosts areset only when recovering master succeeded
  25. my ( $dead_master_host, $new_master_host, $new_slave_hosts,$subject, $body );
  26. my $smtp='smtp.163.com';
  27. my $mail_from='www26454285@163.com';
  28. my $mail_user='www26454285@163.com';
  29. my $mail_pass='26454285wwj';
  30. my $mail_to=['277106621@qq.com'];
  31. GetOptions(
  32. 'orig_master_host=s' => \$dead_master_host,
  33. 'new_master_host=s' =>\$new_master_host,
  34. 'new_slave_hosts=s' =>\$new_slave_hosts,
  35. 'subject=s' =>\$subject,
  36. 'body=s' => \$body,
  37. );
  38. mailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);
  39. sub mailToContacts {
  40. my ( $smtp, $mail_from, $user, $passwd, $mail_to, $subject, $msg ) = @_;
  41. open my $DEBUG, "> /tmp/monitormail.log"
  42. or die "Can't open the debug file:$!\n";
  43. my $sender = new Mail::Sender {
  44. ctype => 'text/plain;charset=utf-8',
  45. encoding => 'utf-8',
  46. smtp => $smtp,
  47. from => $mail_from,
  48. auth => 'LOGIN',
  49. TLS_allowed => '0',
  50. authid => $user,
  51. authpwd => $passwd,
  52. to => $mail_to,
  53. subject => $subject,
  54. debug => $DEBUG
  55. };
  56. $sender->MailMsg(
  57. { msg => $msg,
  58. debug => $DEBUG
  59. }
  60. )or print $Mail::Sender::Error;
  61. return 1;
  62. }
  63. # Do whatever you want here
  64. exit 0;

八.启动mha
nohup masterha_manager --conf= /etc/mha/mhamanager/app1/conf/app1.cnf  --ignore_last_failover  --remove_dead_master_conf   > /tmp/mha_manager.log < /dev/null 2>&1 &

检查MHA状态
#  masterha_check_status --conf=/etc/mha/mhamanager/app1/conf/app1.cnf

测试ssh
masterha_check_ssh --conf=/etc/mha/mhamanager/app1/conf/app1.cnf

测试主从复制
masterha_check_repl --conf=/etc/mha/mhamanager/app1/conf/app1.cnf


九.测试failover
1.关闭master节点
# pkill mysql

2.现象
mha服务停止
master节点keepalived服务停止 
vip 迁移至backup节点
/etc/mha/mhamanager/app1/conf/app1.cnf [server1] 内容被删除
master 迁移至109.115.1.205

3.将原master恢复
重新启动原主库
mysqld_safe --defaults-file=/etc/my5.7_3306.cnf --user=mysql &

- 设置同步信息
CHANGE MASTER TO MASTER_HOST='192.168.1.205', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000009', MASTER_LOG_POS=234,MASTER_USER='REPL_USER', MASTER_PASSWORD='repl1234';
start slave;

- 开启原master keepalived
service keepalived status

- 重新添加mha配置文件原master信息
[server1]
hostname=192.168.1.203
port=3306
ssh_port=22
candidate_master=1
master_binlog_dir=/mysql/mysql3306/mysql3306

- 启动mha
nohup masterha_manager --conf=/etc/mha/mhamanager/app1/conf/app1.cnf --ignore_last_failover --remove_dead_master_conf  > /tmp/mha_manager.log < /dev/null 2>&1 &

# masterha_check_status --conf=/etc/mha/mhamanager/app1/conf/app1.cnf
app1 (pid:32584) is running(0:PING_OK), master:192.168.1.205

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15412087/viewspace-2157231/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15412087/viewspace-2157231/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值