翼度科技»论坛 编程开发 mysql 查看内容

MHA高可用+VIP漂移

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
目录

一、环境搭建

涉及主机
主机名IP地址db01(master)192.168.112.40db02(slave1)192.168.112.50db03(slave2)192.168.112.601、关闭防火墙firewalld,selinux
  1. systemctl stop firewalld
  2. systemctl disable firewalld
  3. setenforce 0
  4. sed -i 's/SELINUX=enforcing /SELINUX=disabled/g' /etc/selinux/config
复制代码
2、每台主机安装MySQL
  1. #二进制安装
  2. wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
  3. tar xzvf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
  4. mkdir /application
  5. mv mysql-5.6.40-linux-glibc2.12-x86_64 /application/mysql-5.6.40
  6. ln -s /application/mysql-5.6.40/ /application/mysql
  7. cd /application/mysql/support-files/
  8. \cp my-default.cnf /etc/my.cnf
  9. cp mysql.server /etc/init.d/mysqld
  10. cd /application/mysql/scripts
  11. useradd mysql -s /sbin/nologin -M
  12. yum -y install autoconf
  13. cd /application/mysql/scripts/
  14. ./mysql_install_db --user=mysql --basedir=/application/mysql --data=/application/mysql/data
  15. echo 'export PATH="/application/mysql/bin:$PATH"' >> /etc/profile.d/mysql.sh
  16. source /etc/profile
  17. sed -i 's#/usr/local#/application#g' /etc/init.d/mysqld /application/mysql/bin/mysqld_safe
  18. #指定MySQL安装根目录以及数据目录
  19. vim /etc/my.cnf
  20. basedir = /application/mysql/
  21. datadir = /application/mysql/data
  22. #设置密码
  23. mysqladmin -uroot password '123'
复制代码
二、基于GTID的主从复制

1、修改/etc/my.cnf文件

主库
  1. vim /etc/my.cnf
  2. [mysqld]        #在mysqld标签下配置
  3. server_id=1
  4. log_bin=mysql-bin        #开启binlog日志
  5. skip-name-resolv        #跳过域名解析
  6. gtid_mode=ON
  7. log_slave_updates        #开启slave binlog同步
  8. enforce_gtid_consistency        #不允许任何违反GTID一致性
  9. [root@db01 ~]# /etc/init.d/mysqld restart        #重启MySQL
复制代码
所有主机
  1. #创建主从复制用户
  2. mysql -uroot -p123        #登录数据库
  3. grant replication slave on *.* to slave@'192.168.112.%' identified by '123';        #创建slave用户
复制代码
从库
  1. [root@db02 ~]# vim /etc/my.cnf
  2. [mysqld]        #在mysqld标签下配置
  3. server_id=2        #主库server-id为1,从库必须大于1
  4. log_bin=mysql-bin        #开启binlog日志
  5. gtid_mode=ON
  6. log_slave_updates        #开启slave binlog同步
  7. enforce_gtid_consistency        #不允许任何违反GTID一致性
  8. [root@db02 ~]# /etc/init.d/mysqld restart        #重启MySQL
  9. [root@db03 ~]# vim /etc/my.cnf
  10. [mysqld]        #在mysqld标签下配置
  11. server_id=3        #主库server-id为1,从库必须大于1
  12. log_bin=mysql-bin        #开启binlog日志
  13. gtid_mode=ON
  14. log_slave_updates        #开启slave binlog同步
  15. enforce_gtid_consistency        #不允许任何违反GTID一致性
  16. [root@db02 ~]# /etc/init.d/mysqld restart        #重启MySQL
复制代码
2、检查GTID状态

主库上查看
  1. show global variables like '%gtid%';
复制代码

3、配置主从复制

从库配置
  1. mysql> change master to
  2.     -> master_host='192.168.112.40',
  3.     -> master_user='slave',
  4.     -> master_password='123',
  5.     -> master_auto_position=1;
  6. mysql> start slave;
  7. mysql> show slave status\G;        #确保从库的IO和SQL线程开启Yes
复制代码

4、从库设置
  1. mysql> set global relay_log_purge = 0;        #禁用自动删除relay log 功能
  2. mysql> set global read_only=1;        #设置从库只读
复制代码
  1. [root@mysql-db02 ~]# vim /etc/my.cnf
  2. #编辑配置文件
  3. [mysqld]
  4. #在mysqld标签下添加
  5. relay_log_purge = 0
  6. #禁用自动删除relay log 永久生效
复制代码
三、部署MHA

1、准备环境(所有节点)
  1. #下载工具包
  2. cd && wget https://download.s21i.faiusr.com/23126342/0/0/ABUIABBPGAAg3OHUiAYolpPt7AQ.zip?f=mysql-master-ha.zip&v=1628778716
复制代码
  1. #安装依赖包
  2. yum install -y perl-DBD-MySQL
  3. yum install -y unzip
  4. mv ABUIABBPGAAg3OHUiAYolpPt7AQ.zip\?f\=mysql-master-ha.zip master-ha.zip
  5. unzip master-ha.zip
  6. cd mysql-master-ha/
  7. #安装node包
  8. rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
  9. #登录数据库
  10. mysql -uroot -p123
  11. #添加MHA管理账号
  12. mysql> grant all privileges on *.* to mha@'192.168.112.%' identified by 'mha';
  13. #查看账号是否添加成功
  14. mysql> select user,host,password from mysql.user;
复制代码
  1. #创建软链接
  2. ln -s /application/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
  3. ln -s /application/mysql/bin/mysql /usr/bin/mysql
复制代码
2、部署管理节点(可以部署在任何机器上)

这里选择db03作为管理节点
  1. [root@db03 ~]# yum install -y epel-release
  2. #安装manager依赖包
  3. [root@db03 ~]# yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
  4. #安装manager包
  5. [root@db03 ~]# cd ~/mysql-master-ha/ && rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
复制代码
编辑manager节点配置文件
  1. [root@db03 ~]# mkdir -p /etc/mha
  2. [root@db03 ~]# mkdir -p /var/log/mha/app1
  3. [root@db03 ~]# vim /etc/mha/app1.cnf
  4. [server default]
  5. manager_log=/var/log/mha/app1/manager.log
  6. manager_workdir=/var/log/mha/app1
  7. master_binlog_dir=/application/mysql/data
  8. user=mha
  9. password=mha
  10. ping_interval=2
  11. repl_password=123
  12. repl_user=slave
  13. ssh_user=root
  14. [server1]
  15. hostname=192.168.112.40
  16. port=3306
  17. [server2]
  18. hostname=192.168.112.50
  19. port=3306
  20. candidate_master=1
  21. check_repl_delay=0
  22. [server3]
  23. hostname=192.168.112.60
  24. port=3306
复制代码
3、配置ssh信任
  1. #创建密钥对
  2. [root@db01 ~]# ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1
  3. #发送密钥,包括自己
  4. [root@db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.112.40
  5. [root@db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.112.50
  6. [root@db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.112.60
复制代码
4、启动测试(manage节点)
  1. #测试ssh
  2. [root@db03 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
复制代码
  1. #测试复制
  2. [root@db03 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
复制代码
如果遇到MySQL Replication Health is NOT OK!
可以尝试在manage节点的/etc/my.cnf配置文件里添加skip-name-resolv跳过解析域名

四、启动MHA
  1. #启动
  2. [root@db03 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
  3. [root@db03 ~]# jobs
  4. [1]+  运行中               nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
  5. #检测状态
  6. [root@db03 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
  7. app1 (pid:19754) is running(0:PING_OK), master:192.168.112.40
复制代码
五、切换master测试

1、检查两从复制情况
  1. #登录db02
  2. [root@db02 ~]# mysql -uroot -p123
  3. #检查复制情况
  4. mysql> show slave status\G;
  5. *************************** 1. row ***************************
  6.                Slave_IO_State: Waiting for master to send event
  7.                   Master_Host: 192.168.112.40
  8.                   Master_User: slave
  9.                   Master_Port: 3306
  10.                 Connect_Retry: 60
  11.               Master_Log_File: mysql-bin.000002
  12.           Read_Master_Log_Pos: 405
  13.                Relay_Log_File: db02-relay-bin.000002
  14.                 Relay_Log_Pos: 615
  15.         Relay_Master_Log_File: mysql-bin.000002
  16.              Slave_IO_Running: Yes
  17.             Slave_SQL_Running: Yes
  18.               Replicate_Do_DB:
  19.           Replicate_Ignore_DB:
  20.            Replicate_Do_Table:
  21.        Replicate_Ignore_Table:
  22.       Replicate_Wild_Do_Table:
  23.   Replicate_Wild_Ignore_Table:
  24.                    Last_Errno: 0
  25.                    Last_Error:
  26.                  Skip_Counter: 0
  27.           Exec_Master_Log_Pos: 405
  28.               Relay_Log_Space: 818
  29.               Until_Condition: None
  30.                Until_Log_File:
  31.                 Until_Log_Pos: 0
  32.            Master_SSL_Allowed: No
  33.            Master_SSL_CA_File:
  34.            Master_SSL_CA_Path:
  35.               Master_SSL_Cert:
  36.             Master_SSL_Cipher:
  37.                Master_SSL_Key:
  38.         Seconds_Behind_Master: 0
  39. Master_SSL_Verify_Server_Cert: No
  40.                 Last_IO_Errno: 0
  41.                 Last_IO_Error:
  42.                Last_SQL_Errno: 0
  43.                Last_SQL_Error:
  44.   Replicate_Ignore_Server_Ids:
  45.              Master_Server_Id: 1
  46.                   Master_UUID: 51fc215b-eab2-11ee-b7f0-000c29c4dc96
  47.              Master_Info_File: /application/mysql/data/master.info
  48.                     SQL_Delay: 0
  49.           SQL_Remaining_Delay: NULL
  50.       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  51.            Master_Retry_Count: 86400
  52.                   Master_Bind:
  53.       Last_IO_Error_Timestamp:
  54.      Last_SQL_Error_Timestamp:
  55.                Master_SSL_Crl:
  56.            Master_SSL_Crlpath:
  57.            Retrieved_Gtid_Set: 51fc215b-eab2-11ee-b7f0-000c29c4dc96:1
  58.             Executed_Gtid_Set: 51fc215b-eab2-11ee-b7f0-000c29c4dc96:1,
  59. e5c86268-ed04-11ee-8716-000c2936523e:1
  60.                 Auto_Position: 1
  61. 1 row in set (0.00 sec)
  62. ERROR:
  63. No query specified
  64. #登录db03
  65. [root@db03 ~]# mysql -uroot -p123
  66. #检查复制情况
  67. mysql> show slave status\G;
  68. *************************** 1. row ***************************
  69.                Slave_IO_State: Waiting for master to send event
  70.                   Master_Host: 192.168.112.40
  71.                   Master_User: slave
  72.                   Master_Port: 3306
  73.                 Connect_Retry: 60
  74.               Master_Log_File: mysql-bin.000002
  75.           Read_Master_Log_Pos: 405
  76.                Relay_Log_File: db03-relay-bin.000004
  77.                 Relay_Log_Pos: 448
  78.         Relay_Master_Log_File: mysql-bin.000002
  79.              Slave_IO_Running: Yes
  80.             Slave_SQL_Running: Yes
  81.               Replicate_Do_DB:
  82.           Replicate_Ignore_DB:
  83.            Replicate_Do_Table:
  84.        Replicate_Ignore_Table:
  85.       Replicate_Wild_Do_Table:
  86.   Replicate_Wild_Ignore_Table:
  87.                    Last_Errno: 0
  88.                    Last_Error:
  89.                  Skip_Counter: 0
  90.           Exec_Master_Log_Pos: 405
  91.               Relay_Log_Space: 1461
  92.               Until_Condition: None
  93.                Until_Log_File:
  94.                 Until_Log_Pos: 0
  95.            Master_SSL_Allowed: No
  96.            Master_SSL_CA_File:
  97.            Master_SSL_CA_Path:
  98.               Master_SSL_Cert:
  99.             Master_SSL_Cipher:
  100.                Master_SSL_Key:
  101.         Seconds_Behind_Master: 0
  102. Master_SSL_Verify_Server_Cert: No
  103.                 Last_IO_Errno: 0
  104.                 Last_IO_Error:
  105.                Last_SQL_Errno: 0
  106.                Last_SQL_Error:
  107.   Replicate_Ignore_Server_Ids:
  108.              Master_Server_Id: 1
  109.                   Master_UUID: 51fc215b-eab2-11ee-b7f0-000c29c4dc96
  110.              Master_Info_File: /application/mysql-5.6.40/data/master.info
  111.                     SQL_Delay: 0
  112.           SQL_Remaining_Delay: NULL
  113.       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  114.            Master_Retry_Count: 86400
  115.                   Master_Bind:
  116.       Last_IO_Error_Timestamp:
  117.      Last_SQL_Error_Timestamp:
  118.                Master_SSL_Crl:
  119.            Master_SSL_Crlpath:
  120.            Retrieved_Gtid_Set: 51fc215b-eab2-11ee-b7f0-000c29c4dc96:1
  121.             Executed_Gtid_Set: 15c89969-ed05-11ee-8717-000c291d4def:1,
  122. 51fc215b-eab2-11ee-b7f0-000c29c4dc96:1
  123.                 Auto_Position: 1
  124. 1 row in set (0.00 sec)
  125. ERROR:
  126. No query specified
复制代码
2、停掉主库
  1. #停掉主库
  2. [root@db01 ~]# /etc/init.d/mysqld stop
  3. Shutting down MySQL...... SUCCESS!
  4. #查看db02的slave状态
  5. mysql> show slave status\G;
  6. Empty set (0.00 sec)
  7. ERROR:
  8. No query specified
  9. #查看db03的slave状态
  10. mysql> show slave status\G;
  11. *************************** 1. row ***************************
  12.                Slave_IO_State: Waiting for master to send event
  13.                   Master_Host: 192.168.112.50
  14.                   Master_User: slave
  15.                   Master_Port: 3306
  16.                 Connect_Retry: 60
  17.               Master_Log_File: mysql-bin.000002
  18.           Read_Master_Log_Pos: 659
  19.                Relay_Log_File: db03-relay-bin.000002
  20.                 Relay_Log_Pos: 662
  21.         Relay_Master_Log_File: mysql-bin.000002
  22.              Slave_IO_Running: Yes
  23.             Slave_SQL_Running: Yes
  24.               Replicate_Do_DB:
  25.           Replicate_Ignore_DB:
  26.            Replicate_Do_Table:
  27.        Replicate_Ignore_Table:
  28.       Replicate_Wild_Do_Table:
  29.   Replicate_Wild_Ignore_Table:
  30.                    Last_Errno: 0
  31.                    Last_Error:
  32.                  Skip_Counter: 0
  33.           Exec_Master_Log_Pos: 659
  34.               Relay_Log_Space: 865
  35.               Until_Condition: None
  36.                Until_Log_File:
  37.                 Until_Log_Pos: 0
  38.            Master_SSL_Allowed: No
  39.            Master_SSL_CA_File:
  40.            Master_SSL_CA_Path:
  41.               Master_SSL_Cert:
  42.             Master_SSL_Cipher:
  43.                Master_SSL_Key:
  44.         Seconds_Behind_Master: 0
  45. Master_SSL_Verify_Server_Cert: No
  46.                 Last_IO_Errno: 0
  47.                 Last_IO_Error:
  48.                Last_SQL_Errno: 0
  49.                Last_SQL_Error:
  50.   Replicate_Ignore_Server_Ids:
  51.              Master_Server_Id: 2
  52.                   Master_UUID: e5c86268-ed04-11ee-8716-000c2936523e
  53.              Master_Info_File: /application/mysql-5.6.40/data/master.info
  54.                     SQL_Delay: 0
  55.           SQL_Remaining_Delay: NULL
  56.       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  57.            Master_Retry_Count: 86400
  58.                   Master_Bind:
  59.       Last_IO_Error_Timestamp:
  60.      Last_SQL_Error_Timestamp:
  61.                Master_SSL_Crl:
  62.            Master_SSL_Crlpath:
  63.            Retrieved_Gtid_Set: e5c86268-ed04-11ee-8716-000c2936523e:1
  64.             Executed_Gtid_Set: 15c89969-ed05-11ee-8717-000c291d4def:1,
  65. 51fc215b-eab2-11ee-b7f0-000c29c4dc96:1,
  66. e5c86268-ed04-11ee-8716-000c2936523e:1
  67.                 Auto_Position: 1
  68. 1 row in set (0.00 sec)
  69. ERROR:
  70. No query specified
复制代码
可以清晰的看到db02已经切换为master
  1. #manage节点的状态检测也停止了
  2. [root@db03 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
  3. app1 is stopped(2:NOT_RUNNING).
  4. #对应的[server1]也被删除了
  5. [root@db03 ~]# cat /etc/mha/app1.cnf
  6. [server default]
  7. manager_log=/var/log/mha/app1/manager.log
  8. manager_workdir=/var/log/mha/app1
  9. master_binlog_dir=/application/mysql/data
  10. password=mha
  11. ping_interval=2
  12. repl_password=123
  13. repl_user=slave
  14. ssh_user=root
  15. user=mha
  16. [server2]
  17. candidate_master=1
  18. check_repl_delay=0
  19. hostname=192.168.112.50
  20. port=3306
  21. [server3]
  22. hostname=192.168.112.60
  23. port=3306
复制代码
3、手动将主库以slave身份加回去

即使重新开启主库,主库也不会自动加回去了,只能以slave的身份手动加回去
  1. mysql> change master to
  2.     -> master_host='192.168.112.50',
  3.     -> master_user='slave',
  4.     -> master_password='123',
  5.     -> master_auto_position=1;
  6. Query OK, 0 rows affected, 2 warnings (0.01 sec)
  7. mysql> start slave;
  8. Query OK, 0 rows affected (0.00 sec)
  9. mysql> show slave status\G;
  10. *************************** 1. row ***************************
  11.                Slave_IO_State: Connecting to master
  12.                   Master_Host: 192.168.112.50
  13.                   Master_User: slave
  14.                   Master_Port: 3306
  15.                 Connect_Retry: 60
  16.               Master_Log_File:
  17.           Read_Master_Log_Pos: 4
  18.                Relay_Log_File: db01-relay-bin.000001
  19.                 Relay_Log_Pos: 4
  20.         Relay_Master_Log_File:
  21.              Slave_IO_Running: Connecting
  22.             Slave_SQL_Running: Yes
  23.               Replicate_Do_DB:
  24.           Replicate_Ignore_DB:
  25.            Replicate_Do_Table:
  26.        Replicate_Ignore_Table:
  27.       Replicate_Wild_Do_Table:
  28.   Replicate_Wild_Ignore_Table:
  29.                    Last_Errno: 0
  30.                    Last_Error:
  31.                  Skip_Counter: 0
  32.           Exec_Master_Log_Pos: 0
  33.               Relay_Log_Space: 151
  34.               Until_Condition: None
  35.                Until_Log_File:
  36.                 Until_Log_Pos: 0
  37.            Master_SSL_Allowed: No
  38.            Master_SSL_CA_File:
  39.            Master_SSL_CA_Path:
  40.               Master_SSL_Cert:
  41.             Master_SSL_Cipher:
  42.                Master_SSL_Key:
  43.         Seconds_Behind_Master: 0
  44. Master_SSL_Verify_Server_Cert: No
  45.                 Last_IO_Errno: 0
  46.                 Last_IO_Error:
  47.                Last_SQL_Errno: 0
  48.                Last_SQL_Error:
  49.   Replicate_Ignore_Server_Ids:
  50.              Master_Server_Id: 0
  51.                   Master_UUID:
  52.              Master_Info_File: /application/mysql/data/master.info
  53.                     SQL_Delay: 0
  54.           SQL_Remaining_Delay: NULL
  55.       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  56.            Master_Retry_Count: 86400
  57.                   Master_Bind:
  58.       Last_IO_Error_Timestamp:
  59.      Last_SQL_Error_Timestamp:
  60.                Master_SSL_Crl:
  61.            Master_SSL_Crlpath:
  62.            Retrieved_Gtid_Set:
  63.             Executed_Gtid_Set: 51fc215b-eab2-11ee-b7f0-000c29c4dc96:1
  64.                 Auto_Position: 1
  65. 1 row in set (0.00 sec)
  66. ERROR:
  67. No query specified
复制代码
六、配置VIP漂移

在MHA(MySQL Master High Availability)高可用架构中,虚拟IP(VIP)漂移是用于确保服务不间断的一种策略,当主数据库发生故障时,VIP会从原主节点迁移到新的主节点,这样应用程序和服务始终可以通过VIP访问到当前的主数据库,而无需修改任何指向数据库的实际IP地址


  • VIP漂移的两种方式

    • 通过keepalived的方式,管理虚拟IP的漂移
    • 通过MHA自带脚本方式,管理虚拟IP的漂移

1、编辑配置文件
  1. #manage节点
  2. [root@db03 ~]# vim /etc/mha/app1.cnf
  3. [server default]
  4. master_ip_failover_script=/etc/mha/master_ip_failover        #在[server default]标签下添加
  5. #随便把之前删除的[server1]加回来
  6. [server1]
  7. hostname=192.168.112.40
  8. port=3306       
复制代码
2、编辑MHA自带的脚本
  1. [root@db03 ~]# vim /etc/mha/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.112.66/24';
  11. my $key = '0';
  12. my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
  13. my $ssh_stop_vip = "/sbin/ifconfig ens33:$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.         my $exit_code = 1;
  29.         eval {
  30.             print "Disabling the VIP on old master: $orig_master_host \n";
  31.             &stop_vip();
  32.             $exit_code = 0;
  33.         };
  34.         if ($@) {
  35.             warn "Got Error: $@\n";
  36.             exit $exit_code;
  37.         }
  38.         exit $exit_code;
  39.     }
  40.     elsif ( $command eq "start" ) {
  41.         my $exit_code = 10;
  42.         eval {
  43.             print "Enabling the VIP - $vip on the new master - $new_master_host \n";
  44.             &start_vip();
  45.             $exit_code = 0;
  46.         };
  47.         if ($@) {
  48.             warn $@;
  49.             exit $exit_code;
  50.         }
  51.         exit $exit_code;
  52.     }
  53.     elsif ( $command eq "status" ) {
  54.         print "Checking the Status of the script.. OK \n";
  55.         exit 0;
  56.     }
  57.     else {
  58.         &usage();
  59.         exit 1;
  60.     }
  61. }
  62. sub start_vip() {
  63.     `ssh $ssh_user\@$new_master_host " $ssh_start_vip "`;
  64. }
  65. sub stop_vip() {
  66.      return 0  unless  ($ssh_user);
  67.     `ssh $ssh_user\@$orig_master_host " $ssh_stop_vip "`;
  68. }
  69. sub usage {
  70.     print
  71.     "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";
  72. }
复制代码
这里面需要依据个人修改的是
my $vip = '192.168.112.66/24';
然后添加执行权限
  1. [root@db03 ~]# chmod a+x /etc/mha/master_ip_failover
复制代码
3、手动绑定VIP(master节点)
  1. #所有主机,主要是ifconfig命令
  2. yum install -y net-tools
  3. #绑定vip
  4. [root@db02 ~]# ifconfig ens33:0 192.168.112.66/24
  5. [root@db02 ~]# ip a | grep ens33
  6. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  7.     inet 192.168.112.50/24 brd 192.168.112.255 scope global noprefixroute ens33
  8.     inet 192.168.112.66/24 brd 192.168.112.255 scope global secondary ens33:0
复制代码
4、启动测试(manage节点)
  1. masterha_check_ssh --conf=/etc/mha/app1.cnf
  2. masterha_check_repl --conf=/etc/mha/app1.cnf
复制代码
5、重启MHA(manage节点)
  1. #启动
  2. [root@db03 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
  3. [1] 21063
  4. [root@db03 ~]# jobs
  5. [1]+  运行中               nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
  6. #检测状态,识别出master
  7. [root@db03 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
  8. app1 (pid:21063) is running(0:PING_OK), master:192.168.112.50
复制代码
6、测试IP漂移
  1. #停掉主库
  2. [root@db02 ~]# /etc/init.d/mysqld stop
  3. Shutting down MySQL..... SUCCESS!
  4. #db01查看slave信息,经历了一个连接不上主库到自己提升为主库的过程
  5. mysql> show slave status\G;
  6. *************************** 1. row ***************************
  7.                Slave_IO_State:
  8.                   Master_Host: 192.168.112.50
  9.                   Master_User: slave
  10.                   Master_Port: 3306
  11.                 Connect_Retry: 60
  12.               Master_Log_File: mysql-bin.000003
  13.           Read_Master_Log_Pos: 231
  14.                Relay_Log_File: db01-relay-bin.000003
  15.                 Relay_Log_Pos: 401
  16.         Relay_Master_Log_File: mysql-bin.000003
  17.              Slave_IO_Running: No
  18.             Slave_SQL_Running: No
  19.               Replicate_Do_DB:
  20.           Replicate_Ignore_DB:
  21.            Replicate_Do_Table:
  22.        Replicate_Ignore_Table:
  23.       Replicate_Wild_Do_Table:
  24.   Replicate_Wild_Ignore_Table:
  25.                    Last_Errno: 0
  26.                    Last_Error:
  27.                  Skip_Counter: 0
  28.           Exec_Master_Log_Pos: 231
  29.               Relay_Log_Space: 1115
  30.               Until_Condition: None
  31.                Until_Log_File:
  32.                 Until_Log_Pos: 0
  33.            Master_SSL_Allowed: No
  34.            Master_SSL_CA_File:
  35.            Master_SSL_CA_Path:
  36.               Master_SSL_Cert:
  37.             Master_SSL_Cipher:
  38.                Master_SSL_Key:
  39.         Seconds_Behind_Master: NULL
  40. Master_SSL_Verify_Server_Cert: No
  41.                 Last_IO_Errno: 2003
  42.                 Last_IO_Error: error reconnecting to master 'slave@192.168.112.50:3306' - retry-time: 60  retries: 1
  43.                Last_SQL_Errno: 0
  44.                Last_SQL_Error:
  45.   Replicate_Ignore_Server_Ids:
  46.              Master_Server_Id: 2
  47.                   Master_UUID: e5c86268-ed04-11ee-8716-000c2936523e
  48.              Master_Info_File: /application/mysql/data/master.info
  49.                     SQL_Delay: 0
  50.           SQL_Remaining_Delay: NULL
  51.       Slave_SQL_Running_State:
  52.            Master_Retry_Count: 86400
  53.                   Master_Bind:
  54.       Last_IO_Error_Timestamp: 240329 00:45:33
  55.      Last_SQL_Error_Timestamp:
  56.                Master_SSL_Crl:
  57.            Master_SSL_Crlpath:
  58.            Retrieved_Gtid_Set: e5c86268-ed04-11ee-8716-000c2936523e:1
  59.             Executed_Gtid_Set: 51fc215b-eab2-11ee-b7f0-000c29c4dc96:1,
  60. e5c86268-ed04-11ee-8716-000c2936523e:1
  61.                 Auto_Position: 1
  62. 1 row in set (0.00 sec)
  63. ERROR:
  64. No query specified
  65. mysql> show slave status\G;
  66. Empty set (0.00 sec)
  67. ERROR:
  68. No query specified
  69. #db03查看slave信息,发现主库已经成功切换到db01
  70. mysql> show slave status\G;
  71. *************************** 1. row ***************************
  72.                Slave_IO_State: Waiting for master to send event
  73.                   Master_Host: 192.168.112.40
  74.                   Master_User: slave
  75.                   Master_Port: 3306
  76.                 Connect_Retry: 60
  77.               Master_Log_File: mysql-bin.000003
  78.           Read_Master_Log_Pos: 445
  79.                Relay_Log_File: db03-relay-bin.000002
  80.                 Relay_Log_Pos: 408
  81.         Relay_Master_Log_File: mysql-bin.000003
  82.              Slave_IO_Running: Yes
  83.             Slave_SQL_Running: Yes
  84.               Replicate_Do_DB:
  85.           Replicate_Ignore_DB:
  86.            Replicate_Do_Table:
  87.        Replicate_Ignore_Table:
  88.       Replicate_Wild_Do_Table:
  89.   Replicate_Wild_Ignore_Table:
  90.                    Last_Errno: 0
  91.                    Last_Error:
  92.                  Skip_Counter: 0
  93.           Exec_Master_Log_Pos: 445
  94.               Relay_Log_Space: 611
  95.               Until_Condition: None
  96.                Until_Log_File:
  97.                 Until_Log_Pos: 0
  98.            Master_SSL_Allowed: No
  99.            Master_SSL_CA_File:
  100.            Master_SSL_CA_Path:
  101.               Master_SSL_Cert:
  102.             Master_SSL_Cipher:
  103.                Master_SSL_Key:
  104.         Seconds_Behind_Master: 0
  105. Master_SSL_Verify_Server_Cert: No
  106.                 Last_IO_Errno: 0
  107.                 Last_IO_Error:
  108.                Last_SQL_Errno: 0
  109.                Last_SQL_Error:
  110.   Replicate_Ignore_Server_Ids:
  111.              Master_Server_Id: 1
  112.                   Master_UUID: 51fc215b-eab2-11ee-b7f0-000c29c4dc96
  113.              Master_Info_File: /application/mysql-5.6.40/data/master.info
  114.                     SQL_Delay: 0
  115.           SQL_Remaining_Delay: NULL
  116.       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  117.            Master_Retry_Count: 86400
  118.                   Master_Bind:
  119.       Last_IO_Error_Timestamp:
  120.      Last_SQL_Error_Timestamp:
  121.                Master_SSL_Crl:
  122.            Master_SSL_Crlpath:
  123.            Retrieved_Gtid_Set:
  124.             Executed_Gtid_Set: 15c89969-ed05-11ee-8717-000c291d4def:1,
  125. 51fc215b-eab2-11ee-b7f0-000c29c4dc96:1,
  126. e5c86268-ed04-11ee-8716-000c2936523e:1
  127.                 Auto_Position: 1
  128. 1 row in set (0.00 sec)
  129. ERROR:
  130. No query specified
复制代码
  1. #在db01上查看vip信息
  2. [root@db01 ~]# ip a |grep ens33
  3. #在db02上查看vip信息
  4. [root@db02 ~]# ip a |grep ens33
  5. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  6.     inet 192.168.112.40/24 brd 192.168.112.255 scope global noprefixroute ens33
  7.     inet 192.168.112.66/24 brd 192.168.112.255 scope global secondary ens33:0
复制代码
来源:https://www.cnblogs.com/misakivv/p/18102949
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具