016-mha模式配置

来源:互联网 发布:上海孚盟软件怎么样 编辑:程序博客网 时间:2024/05/01 05:50

描述:MHA(Master High Availability),目前mysql中比较常用的解决方案。
工具:
  1. masterha_check_ssh 检查MHA的SSH配置状况
  2. masterha_check_repl 检查MySQL复制状况
  3. masterha_manger 启动MHA
  4. masterha_check_status 检测当前MHA运行状态
  5. masterha_master_monitor 检测master是否宕机
  6. masterha_master_switch 控制故障转移(自动或者手动)
  7. masterha_conf_host 添加或删除配置的server信息
      
环境配置:
IP作用    描述 mysql5.7版本192.168.68.129master  (主)node_master节点192.168.68.130slave(从+备用master    )node_bakup_master备用192.168.68.131slave    (从)节点192.168.68.132slave+manager(从+管理)节点+管理机192.168.68.110共享IP,无需机器

一、一主三从的配置:(忽略过程)
    注意:  
  1. #1、my.cnf 中要禁用gtid模式 ,5.7版本默认禁用gtid模式,如果非5.7版本需要在[mysqld]中增加

  2. gtid_mode=0
  1. #2、4台mysql中的server_id 不能相同

  2. servicer_id=1、2、3、4
  1. #3、 auto.cnf 中的 server-uuid 4台mysql不能相同
  2. [root@slave tmp]# cd /usr/local/mysql/data/[root@slave data]# cat auto.cnf [auto]server-uuid=8c1ea0b7-5be8-11e7-93a7-000c292ef002[root@slave data]#
二、增加mha需要复制时的用户名及密码:
  1.  ## all_db 都需要执行:
  2. grant all privileges on *.* to 'mha_manager'@'%' identified by '123456';
  3. grant replication slave on *.* to 'repl'@'%' identified by '123456';
  4. flush privileges;
  5. ## 如果有一台slave上面运行manager,在此电脑上面执行: 此次是 192.168.68.132 使用 managerdb 代表:
  6. GRANT ALL PRIVILEGES ON *.* TO 'mha_manager'@'manager.100' IDENTIFIED BY '123456'
  7. (manager.100)是本机名称,uname -a 可以查看到
三、ssh-keygen 建立免密信任
    
  1. ## 192.168.68.129
  2. ssh-keygen ##猛砸回车
  3. cd ~/.ssh/
  4. ls | grep id_rsa
  5. 显示如下:
 
  1. ## 192.168.68.129 复制id_rsa.pub 密钥,并后成key+ip的文件
  2. cp id_rsa.pub key129
  3. ## 将生成的key+ip文件分发到各机器上面
  4. scp key129 192.168.68.130:~/.ssh/
  5. scp key129 192.168.68.131:~/.ssh/
  6. scp key129 192.168.68.132:~/.ssh/
  7. ## 130、131、132 重复上述工作
  8. 最后的结果如下:
 
  1. ## 129、130、131、132 重复如下操作
  2. cat key129 key130 key131 key132 > authorized_keys
  3. ## 完后成,在每一台机器上面访问其它三台机器,
  4. ssh 192.168.68.xx
  5. ## 免密登录 算是正确
四、下载 rpm 包:并复制到all_db 中
  1. mha4mysql-node-0.56-0.el6.noarch.rpm
  2. mha4mysql-manager-0.56-0.el6.noarch.rpm
  3. ##如何下载请自行百度
五、安装 mha4mysql-node-0.56-0.el6.noarch.rpm 包在所有的机器上面 all_db


六、manager:即192.168.68.132上面操作内容如下: 安装 mha4mysql-manager-0.56-0.el6.noarch.rpm 包

  1. yum install perl cpan
  2. yum install perl-Config-Tiny
  3. yum install perl-Time-HiRes
  4. yum install -y rrdtool perl-rrdtool rrdtool-devel perl-Params-Validate
  5. ## 无法使用yum 源的请自行百度,非常简单

  1. wget http://downloads.naulinux.ru/pub/NauLinux/6x/i386/sites/School/RPMS/perl-Log-Dispatch-2.27-1.el6.noarch.rpm
  2. wget http://dl.Fedoraproject.org/pub/epel/6/i386/perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
  3. wget http://dl.fedoraproject.org/pub/epel/6/i386/perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
  4. wget http://dl.fedoraproject.org/pub/epel/6/i386/perl-Mail-Sendmail-0.79-12.el6.noarch.rpm

  1. rpm -ivh perl-Mail-Sender-0.8.16-1.el6.rf.noarch.rpm
  2. rpm -ivh perl-Mail-Sendmail-0.79-1.2.el6.rf.noarch.rpm
  3. yum localinstall perl-Log-Dispatch-2.26-1.el6.rf.noarch.rpm
  4. yum localinstall perl-Parallel-ForkManager-0.7.5-2.2.el6.rf.noarch.rpm
  1. rpm -ivh mha4mysql-manager-0.53-0.el6.noarch.rpm


七、建立mha需要的目录及文件 manager_db中操作:
  1. ## manager-db 中操作
  2. shell> mkdir /etc/masterha --建立文件夹
  3. shell> vim /etc/masterha/app1.cnf -- 新建配置文件部分
  1. #### app1.cnf 配置如下 manager-db 中操作
  2. [server default]
  3. manager_log=/var/log/mha/app1/manager.log
  4. manager_workdir=/var/log/mha/app1
  5. master_binlog_dir=/usr/local/mysql/data
  6. remote_workdir=/var/log/mha/app1
  7. user=mha_manager
  8. password=123456
  9. repl_user=repl
  10. repl_password=123456
  11. ssh_user=root
  12. ping_interval=1
  13. secondary_check_script="masterha_secondary_check -s 192.168.68.129 -s 192.168.68.130"
  14. master_ip_failover_script="/tmp/master_ip_failover.sh"
  15. [server1]
  16. hostname=192.168.68.129
  17. #可以升为master
  18. candidate_master=1
  19. [server2]
  20. hostname=192.168.68.130
  21. candidate_master=1
  22. check_repl_delay=0
  23. [server3]
  24. hostname=192.168.68.131
  25. no_master=1
  26. [server4]
  27. hostname=192.168.68.132
  28. no_master=1
  1. ### 所有的DB 上面建立软件 连接 注意all-db 所有的机器都需要操作
  2. ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
  3. ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
  4. ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
  5. ln -s /usr/local/mysql/bin/mysql /usr/bin
  1. ### app1.cnf 中要使用的脚本 /tmp/master_ip_failover.sh 内容如下 只需要在manager中操作如下
  2. ### 脚本要顶格上,以上两行,请自行删除。 其中的IP为共享IP
  3. #!/usr/bin/env perl
  4. use strict;
  5. use warnings FATAL =>'all';
  6. use Getopt::Long;
  7. my (
  8. $command, $ssh_user, $orig_master_host, $orig_master_ip,
  9. $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
  10. );
  11. my $vip = '192.168.68.110/24'; # Virtual IP
  12. my $key = "1";
  13. my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
  14. my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
  15. my $exit_code = 0;
  16. GetOptions(
  17. 'command=s' => \$command,
  18. 'ssh_user=s' => \$ssh_user,
  19. 'orig_master_host=s' => \$orig_master_host,
  20. 'orig_master_ip=s' => \$orig_master_ip,
  21. 'orig_master_port=i' => \$orig_master_port,
  22. 'new_master_host=s' => \$new_master_host,
  23. 'new_master_ip=s' => \$new_master_ip,
  24. 'new_master_port=i' => \$new_master_port,
  25. );
  26. exit &main();
  27. sub main {
  28. #print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
  29. if ( $command eq "stop" || $command eq "stopssh" ) {
  30. # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
  31. # If you manage master ip address at global catalog database,
  32. # invalidate orig_master_ip here.
  33. my $exit_code = 1;
  34. eval {
  35. print "\n\n\n***************************************************************\n";
  36. print "Disabling the VIP - $vip on old master: $orig_master_host\n";
  37. print "***************************************************************\n\n\n\n";
  38. &stop_vip();
  39. $exit_code = 0;
  40. };
  41. if ($@) {
  42. warn "Got Error: $@\n";
  43. exit $exit_code;
  44. }
  45. exit $exit_code;
  46. }
  47. elsif ( $command eq "start" ) {
  48. # all arguments are passed.
  49. # If you manage master ip address at global catalog database,
  50. # activate new_master_ip here.
  51. # You can also grant write access (create user, set read_only=0, etc) here.
  52. my $exit_code = 10;
  53. eval {
  54. print "\n\n\n***************************************************************\n";
  55. print "Enabling the VIP - $vip on new master: $new_master_host \n";
  56. print "***************************************************************\n\n\n\n";
  57. &start_vip();
  58. $exit_code = 0;
  59. };
  60. if ($@) {
  61. warn $@;
  62. exit $exit_code;
  63. }
  64. exit $exit_code;
  65. }
  66. elsif ( $command eq "status" ) {
  67. print "Checking the Status of the script.. OK \n";
  68. `ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
  69. exit 0;
  70. }
  71. else {
  72. &usage();
  73. exit 1;
  74. }
  75. }
  76. # A simple system call that enable the VIP on the new master
  77. sub start_vip() {
  78. `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  79. }
  80. # A simple system call that disable the VIP on the old_master
  81. sub stop_vip() {
  82. `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  83. }
  84. sub usage {
  85. print
  86. "Usage: master_ip_failover –command=start|stop|stopssh|status –orig_master_host=host –orig_master_ip=ip –orig_master_port=po
  87. rt –new_master_host=host –new_master_ip=ip –new_master_port=port\n";
  88. }

  1. ### 需要给/tmp 的格式 和master_ip_failover.sh 执行权限 manager db中操作
  2. cd /tmp
  3. chmod 777 /tmp
  4. chmod 777 master_ip_failover.sh
  5. chown +x master_ip_failover.sh
八、测试是否成功 manager_db中操作
  1. ## manager_db 有如下图显示才算成功 测试ssh 是否Ok
  2. masterha_check_ssh --conf=/etc/masterha/app1.cnf

 

  1. ## manager_db 有如下图显示才算成功 测试复制是否Ok
  2. masterha_check_repl --conf=/etc/masterha/app1.cnf
 
 
 
九、准备开启mha

  1. ## master _db 上面执行 ,请注意是master_db 而非manager_db
  2. master_db:
  3. ifconfig eth0:0 192.168.68.110 netmask 255.255.255.0 up
  1. ## 请注意是manager_db 上面执行
  2. masterha_manager --conf=/etc/masterha/app1.cnf
  3. ### 回车后,会卡住如下图,不要管
 
  1. ### 在manager_db 中,另起一个shell 查看mha状态
  2. masterha_check_status --conf=/etc/masterha/app1.cnf
  3. ### 如果启动成功,如下提示
 
十、测试mha是否自动切换 
  1. ### master_db上面执行 192.168.68.129
  2. ip addr show
  3. ### 观察是否是双IP 192.168.68.129 与192.168.68.110 (110为共享IP)
  4. service mysqld stop
  5. ##再次观察IP , 只有一个 192.168.68.129
  6. ip addr show
  7. ### 再去备用-master 192.168.68.130 观察,是否成双IP
  8. ## 192.168.68.130 与192.168.68.110
  9. ip addr show
  10. mysql -uroot -p123456
  11. show processlist;
  12. ## 我们会看到有两台slave(131,132)及 130升级为 master
  13. ## 如切换成功,mha程序自动停止
  14. ### 如果想再次启动mha ,需要的操作如下:
  15. ### 现在的状态为 129 ,stop ,130 master ,131 slave ,132 slave+manager
  16. # 1、 130_db
  17. mysql -uroot -p123456
  18. show master status;
  19. ### 记录日志文件及日志位置
  20. #2、 129_db
  21. service mysqld start
  22. mysql -uroot -p123456
  23. stop slave ;
  24. change master to master_host='192.168.68.129',master_user='repl', master_password='123456', master_log_file='mysql-bin.00000xxx',master_log_pos=xxx;
  25. start slave;
  26. # 3、 132_db manager 操作
  27. rm -rf /var/log/mha/app1/app1.master_status.health
  28. masterha_check_status --conf=/etc/masterha/app1.cnf
  29. #### 这次启动不用修改 app1.cnf中的任何内容




原创粉丝点击