千家信息网

【Mysql】MHA配置虚拟ip

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,虚拟ip飘逸说到Failover,通常有两种方式:一种是虚拟IP地址,一种是全局配置文件。MHA并没有限定使用哪一种方式,而是让用户自己选择,虚拟IP地址的方式会牵扯到其它的软件,这里就不赘述了上篇文
千家信息网最后更新 2025年02月02日【Mysql】MHA配置虚拟ip

虚拟ip飘逸

  1. 说到Failover,通常有两种方式:一种是虚拟IP地址,一种是全局配置文件。
  2. MHA并没有限定使用哪一种方式,而是让用户自己选择,虚拟IP地址的方式会牵扯到其它的软件,这里就不赘述了
  3. 上篇文章搭建mha+keepalive就是通过keepalive来控制虚拟ip,本片文章在介绍一种虚拟ip的方式

配置虚拟ip

  1. 采用ifconfig的方式
  2. /sbin/ifconfig eth0:1 192.168.6.66/24
删除VIP:
  1. /sbin/ifconfig eth0:1 down
网上找了一个master_ip_failover脚本就是用此方法更改VIP:
  1. [root@localhost app1]# more /usr/local/bin/master_ip_online_change_script
  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.6.66/24'; # Virtual IP
  11. my $key = "1";
  12. my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
  13. my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";

  14. GetOptions(
  15. 'command=s' => \$command,
  16. 'ssh_user=s' => \$ssh_user,
  17. 'orig_master_host=s' => \$orig_master_host,
  18. 'orig_master_ip=s' => \$orig_master_ip,
  19. 'orig_master_port=i' => \$orig_master_port,
  20. 'new_master_host=s' => \$new_master_host,
  21. 'new_master_ip=s' => \$new_master_ip,
  22. 'new_master_port=i' => \$new_master_port,
  23. );

  24. exit &main();

  25. sub main {

  26. print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

  27. if ( $command eq "stop" || $command eq "stopssh" ) {

  28. # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
  29. # If you manage master ip address at global catalog database,
  30. # invalidate orig_master_ip here.
  31. my $exit_code = 1;
  32. eval {
  33. print "Disabling the VIP on old master: $orig_master_host \n";
  34. &stop_vip();
  35. $exit_code = 0;
  36. };
  37. if ($@) {
  38. warn "Got Error: $@\n";
  39. exit $exit_code;
  40. }
  41. exit $exit_code;
  42. }
  43. elsif ( $command eq "start" ) {

  44. # all arguments are passed.
  45. # If you manage master ip address at global catalog database,
  46. # activate new_master_ip here.
  47. # You can also grant write access (create user, set read_only=0, etc) here.
  48. my $exit_code = 10;
  49. eval {
  50. print "Enabling the VIP - $vip on the new master - $new_master_host \n";
  51. &start_vip();
  52. $exit_code = 0;
  53. };
  54. if ($@) {
  55. warn $@;
  56. exit $exit_code;
  57. }
  58. exit $exit_code;
  59. }
  60. elsif ( $command eq "status" ) {
  61. print "Checking the Status of the script.. OK \n";
  62. `ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
  63. exit 0;
  64. }
  65. else {
  66. &usage();
  67. exit 1;
  68. }
  69. }

  70. # A simple system call that enable the VIP on the new master
  71. sub start_vip() {
  72. `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  73. }
  74. # A simple system call that disable the VIP on the old_master
  75. sub stop_vip() {
  76. `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  77. }

  78. sub usage {
  79. print
  80. "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=po
  81. rt\n";
  82. }

将此脚本复制两次到/usr/local/bin, 分别命名为master_ip_failover 和master_ip_online_change_script

然后将/etc/app1.cnf 中下面两行注释去掉:

master_ip_failover_script=/usr/local/bin/master_ip_failovermaster_ip_online_change_script=/usr/local/bin/master_ip_online_change_script


实验
  1. 1.主库(115)添加一个vip
  1. [root@node2 .ssh]# /sbin/ifconfig eth0:1 192.168.6.66/24
    [root@node2 .ssh]# ip a
    1: lo: mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    valid_lft forever preferred_lft forever
    2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0
    inet 192.168.6.66/24 brd 192.168.6.255 scope global secondary eth0:1
    inet6 fe80::a00:27ff:fe03:1ba1/64 scope link
    valid_lft forever preferred_lft forever

  1. 2.关闭主库mysql
  1. [root@node2 .ssh]# /etc/init.d/mysqld stop
    Stopping mysqld: [ OK ]
    [root@node2 .ssh]# ip a
    1: lo: mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    valid_lft forever preferred_lft forever
    2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0 ---vip飘走了
    inet6 fe80::a00:27ff:fe03:1ba1/64 scope link
    valid_lft forever preferred_lft forever

  1. 3 查看newmaster(114)的ip ----vip已飘到new master上了
  1. [root@node1 ~]# ip a
    1: lo: mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    valid_lft forever preferred_lft forever
    2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 08:00:27:7a:c6:9c brd ff:ff:ff:ff:ff:ff
    inet 192.168.6.114/24 brd 192.168.6.255 scope global eth0
    inet 192.168.6.66/24 brd 192.168.6.255 scope global secondary eth0:1
    inet6 fe80::a00:27ff:fe7a:c69c/64 scope link
    valid_lft forever preferred_lft forever

  2. 4 重构mysql.重启mha监控
  3. 5 关闭114mysql,vip飘回115上去了




0