上一片博客中,讲述了怎么去配置MHA架构!这片博客不再细说,只说明其中MySQL主从搭建,这里使用的是gtid加上半同步复制!

步骤与上一片博客一样,不同之处在于MySQL主从的搭建!详细的gtid搭建过程https://www.cnblogs.com/wxzhe/p/10055154.html

上一片博客中,把MySQL主从的搭建由filename和pos的过程改变为如下的基于gtid的过程就可以,因此不再详细说明,只展示gtid的搭建!

四台服务器分配如下:

MHA管理节点: 10.0.102.214

MySQL主节点:   10.0.102.204

MySQL从节点1:  10.0.102.179 【这个节点可以作为备用的主】

MySQL从节点2: 10.0.102.221

搭建基于gtid的数据库复制!

第一步:保证三个服务器的数据是一致的【处于一致的状态】

第二步:在主上和备用的主上创建复制账户,用户名和密码要保持一致!

第三步:开启gtid,加载半同步复制的插件!

三台服务器中配置文件加入以下参照:

  1. plugin_dir=/usr/local/mysql/lib/plugin/ #因为这里MySQL5.7是源码安装的位置,若是使用rpm包安装,则注意更改位置
  2. plugin_load=semisync_master.so #这两个插件尽量在每个服务器都安装吧
  3. plugin_load=semisync_slave.so
  4. gtid-mode=on #开启gtid
  5. enforce-gtid-consistency #确保gtid全局的一致性
  6. log-bin=
  7. character_set_server=utf8 #设置字符集
  8. log_slave_updates #gtid复制时,一定要开启

设置完配置文件之后,重启服务器,然后再从上执行以下命令!

  1. mysql> change master to master_host="10.0.102.204", master_user="repl",master_password="123456",master_auto_position = 1;
  2. Query OK, 0 rows affected, 2 warnings (0.09 sec)
  3.  
  4. mysql> start slave;
  5. Query OK, 0 rows affected (0.01 sec)

若上面没有报错,则使用show slave status查看复制的状态!

检查MHA的状态

ssh状态检查:

  1. [root@test3 ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
  2. Sun Dec 9 11:42:50 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  3. Sun Dec 9 11:42:50 2018 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
  4. Sun Dec 9 11:42:50 2018 - [info] Reading server configuration from /etc/masterha/app1.cnf..
  5. Sun Dec 9 11:42:50 2018 - [info] Starting SSH connection tests..
  6. Sun Dec 9 11:42:51 2018 - [debug]
  7. Sun Dec 9 11:42:50 2018 - [debug] Connecting via SSH from root@10.0.102.204(10.0.102.204:22) to root@10.0.102.179(10.0.102.179:22)..
  8. Sun Dec 9 11:42:51 2018 - [debug] ok.
  9. Sun Dec 9 11:42:51 2018 - [debug] Connecting via SSH from root@10.0.102.204(10.0.102.204:22) to root@10.0.102.221(10.0.102.221:22)..
  10. Sun Dec 9 11:42:51 2018 - [debug] ok.
  11. Sun Dec 9 11:42:51 2018 - [debug]
  12. Sun Dec 9 11:42:51 2018 - [debug] Connecting via SSH from root@10.0.102.179(10.0.102.179:22) to root@10.0.102.204(10.0.102.204:22)..
  13. Sun Dec 9 11:42:51 2018 - [debug] ok.
  14. Sun Dec 9 11:42:51 2018 - [debug] Connecting via SSH from root@10.0.102.179(10.0.102.179:22) to root@10.0.102.221(10.0.102.221:22)..
  15. Sun Dec 9 11:42:51 2018 - [debug] ok.
  16. Sun Dec 9 11:42:52 2018 - [debug]
  17. Sun Dec 9 11:42:51 2018 - [debug] Connecting via SSH from root@10.0.102.221(10.0.102.221:22) to root@10.0.102.204(10.0.102.204:22)..
  18. Sun Dec 9 11:42:52 2018 - [debug] ok.
  19. Sun Dec 9 11:42:52 2018 - [debug] Connecting via SSH from root@10.0.102.221(10.0.102.221:22) to root@10.0.102.179(10.0.102.179:22)..
  20. Sun Dec 9 11:42:52 2018 - [debug] ok.
  21. Sun Dec 9 11:42:52 2018 - [info] All SSH connection tests passed successfully.

masterha_check_ssh --conf=/etc/masterha/app1.cnf

  1. [root@test3 ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
  2. Sun Dec 9 11:42:50 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  3. Sun Dec 9 11:42:50 2018 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
  4. Sun Dec 9 11:42:50 2018 - [info] Reading server configuration from /etc/masterha/app1.cnf..
  5. Sun Dec 9 11:42:50 2018 - [info] Starting SSH connection tests..
  6. Sun Dec 9 11:42:51 2018 - [debug]
  7. Sun Dec 9 11:42:50 2018 - [debug] Connecting via SSH from root@10.0.102.204(10.0.102.204:22) to root@10.0.102.179(10.0.102.179:22)..
  8. Sun Dec 9 11:42:51 2018 - [debug] ok.
  9. Sun Dec 9 11:42:51 2018 - [debug] Connecting via SSH from root@10.0.102.204(10.0.102.204:22) to root@10.0.102.221(10.0.102.221:22)..
  10. Sun Dec 9 11:42:51 2018 - [debug] ok.
  11. Sun Dec 9 11:42:51 2018 - [debug]
  12. Sun Dec 9 11:42:51 2018 - [debug] Connecting via SSH from root@10.0.102.179(10.0.102.179:22) to root@10.0.102.204(10.0.102.204:22)..
  13. Sun Dec 9 11:42:51 2018 - [debug] ok.
  14. Sun Dec 9 11:42:51 2018 - [debug] Connecting via SSH from root@10.0.102.179(10.0.102.179:22) to root@10.0.102.221(10.0.102.221:22)..
  15. Sun Dec 9 11:42:51 2018 - [debug] ok.
  16. Sun Dec 9 11:42:52 2018 - [debug]
  17. Sun Dec 9 11:42:51 2018 - [debug] Connecting via SSH from root@10.0.102.221(10.0.102.221:22) to root@10.0.102.204(10.0.102.204:22)..
  18. Sun Dec 9 11:42:52 2018 - [debug] ok.
  19. Sun Dec 9 11:42:52 2018 - [debug] Connecting via SSH from root@10.0.102.221(10.0.102.221:22) to root@10.0.102.179(10.0.102.179:22)..
  20. Sun Dec 9 11:42:52 2018 - [debug] ok.
  21. Sun Dec 9 11:42:52 2018 - [info] All SSH connection tests passed successfully.
  22. [root@test3 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
  23. Sun Dec 9 11:43:39 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  24. Sun Dec 9 11:43:39 2018 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
  25. Sun Dec 9 11:43:39 2018 - [info] Reading server configuration from /etc/masterha/app1.cnf..
  26. Sun Dec 9 11:43:39 2018 - [info] MHA::MasterMonitor version 0.56.
  27. Sun Dec 9 11:43:39 2018 - [info] GTID failover mode = 1
  28. Sun Dec 9 11:43:39 2018 - [info] Dead Servers:
  29. Sun Dec 9 11:43:39 2018 - [info] Alive Servers:
  30. Sun Dec 9 11:43:39 2018 - [info] 10.0.102.204(10.0.102.204:3306)
  31. Sun Dec 9 11:43:39 2018 - [info] 10.0.102.179(10.0.102.179:3306)
  32. Sun Dec 9 11:43:39 2018 - [info] 10.0.102.221(10.0.102.221:3306)
  33. Sun Dec 9 11:43:39 2018 - [info] Alive Slaves:
  34. Sun Dec 9 11:43:39 2018 - [info] 10.0.102.179(10.0.102.179:3306) Version=5.7.22-log (oldest major version between slaves) log-bin:enabled
  35. Sun Dec 9 11:43:39 2018 - [info] GTID ON
  36. Sun Dec 9 11:43:39 2018 - [info] Replicating from 10.0.102.204(10.0.102.204:3306)
  37. Sun Dec 9 11:43:39 2018 - [info] Primary candidate for the new Master (candidate_master is set)
  38. Sun Dec 9 11:43:39 2018 - [info] 10.0.102.221(10.0.102.221:3306) Version=5.7.22-log (oldest major version between slaves) log-bin:enabled
  39. Sun Dec 9 11:43:39 2018 - [info] GTID ON
  40. Sun Dec 9 11:43:39 2018 - [info] Replicating from 10.0.102.204(10.0.102.204:3306)
  41. Sun Dec 9 11:43:39 2018 - [info] Not candidate for the new Master (no_master is set)
  42. Sun Dec 9 11:43:39 2018 - [info] Current Alive Master: 10.0.102.204(10.0.102.204:3306)
  43. Sun Dec 9 11:43:39 2018 - [info] Checking slave configurations..
  44. Sun Dec 9 11:43:39 2018 - [info] read_only=1 is not set on slave 10.0.102.179(10.0.102.179:3306).
  45. Sun Dec 9 11:43:39 2018 - [info] read_only=1 is not set on slave 10.0.102.221(10.0.102.221:3306).
  46. Sun Dec 9 11:43:39 2018 - [info] Checking replication filtering settings..
  47. Sun Dec 9 11:43:39 2018 - [info] binlog_do_db= , binlog_ignore_db=
  48. Sun Dec 9 11:43:39 2018 - [info] Replication filtering check ok.
  49. Sun Dec 9 11:43:39 2018 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
  50. Sun Dec 9 11:43:39 2018 - [info] Checking SSH publickey authentication settings on the current master..
  51. Sun Dec 9 11:43:39 2018 - [info] HealthCheck: SSH to 10.0.102.204 is reachable.
  52. Sun Dec 9 11:43:39 2018 - [info]
  53. 10.0.102.204(10.0.102.204:3306) (current master)
  54. +--10.0.102.179(10.0.102.179:3306)
  55. +--10.0.102.221(10.0.102.221:3306)
  56.  
  57. Sun Dec 9 11:43:39 2018 - [info] Checking replication health on 10.0.102.179..
  58. Sun Dec 9 11:43:39 2018 - [info] ok.
  59. Sun Dec 9 11:43:39 2018 - [info] Checking replication health on 10.0.102.221..
  60. Sun Dec 9 11:43:39 2018 - [info] ok.
  61. Sun Dec 9 11:43:39 2018 - [info] Checking master_ip_failover_script status:
  62. Sun Dec 9 11:43:39 2018 - [info] /usr/local/bin/master_ip_failover --ssh_user=root --command=status --ssh_user=root --orig_master_host=10.0.102.204 --orig_master_ip=10.0.102.204 --orig_master_port=3306
  63.  
  64. IN SCRIPT TEST====service keepalived stop==service keepalived start===
  65.  
  66. Checking the Status of the script.. OK
  67. Sun Dec 9 11:43:39 2018 - [info] OK.
  68. Sun Dec 9 11:43:39 2018 - [warning] shutdown_script is not defined.
  69. Sun Dec 9 11:43:39 2018 - [info] Got exit code 0 (Not master dead).
  70.  
  71. MySQL Replication Health is OK.
  72. [root@test3

复制状态检查:

  1. Sun Dec 9 12:14:28 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  2. Sun Dec 9 12:14:28 2018 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
  3. Sun Dec 9 12:14:28 2018 - [info] Reading server configuration from /etc/masterha/app1.cnf..
  4. Sun Dec 9 12:14:28 2018 - [info] MHA::MasterMonitor version 0.56.
  5. Sun Dec 9 12:14:28 2018 - [info] GTID failover mode = 1
  6. Sun Dec 9 12:14:28 2018 - [info] Dead Servers:
  7. Sun Dec 9 12:14:28 2018 - [info] Alive Servers:
  8. Sun Dec 9 12:14:28 2018 - [info] 10.0.102.204(10.0.102.204:3306)
  9. Sun Dec 9 12:14:28 2018 - [info] 10.0.102.179(10.0.102.179:3306)
  10. Sun Dec 9 12:14:28 2018 - [info] 10.0.102.221(10.0.102.221:3306)
  11. Sun Dec 9 12:14:28 2018 - [info] Alive Slaves:
  12. Sun Dec 9 12:14:28 2018 - [info] 10.0.102.179(10.0.102.179:3306) Version=5.7.22-log (oldest major version between slaves) log-bin:enabled
  13. Sun Dec 9 12:14:28 2018 - [info] GTID ON
  14. Sun Dec 9 12:14:28 2018 - [info] Replicating from 10.0.102.204(10.0.102.204:3306)
  15. Sun Dec 9 12:14:28 2018 - [info] Primary candidate for the new Master (candidate_master is set)
  16. Sun Dec 9 12:14:28 2018 - [info] 10.0.102.221(10.0.102.221:3306) Version=5.7.22-log (oldest major version between slaves) log-bin:enabled
  17. Sun Dec 9 12:14:28 2018 - [info] GTID ON
  18. Sun Dec 9 12:14:28 2018 - [info] Replicating from 10.0.102.204(10.0.102.204:3306)
  19. Sun Dec 9 12:14:28 2018 - [info] Not candidate for the new Master (no_master is set)
  20. Sun Dec 9 12:14:28 2018 - [info] Current Alive Master: 10.0.102.204(10.0.102.204:3306)
  21. Sun Dec 9 12:14:28 2018 - [info] Checking slave configurations..
  22. Sun Dec 9 12:14:28 2018 - [info] read_only=1 is not set on slave 10.0.102.179(10.0.102.179:3306).
  23. Sun Dec 9 12:14:28 2018 - [info] read_only=1 is not set on slave 10.0.102.221(10.0.102.221:3306).
  24. Sun Dec 9 12:14:28 2018 - [info] Checking replication filtering settings..
  25. Sun Dec 9 12:14:28 2018 - [info] binlog_do_db= , binlog_ignore_db=
  26. Sun Dec 9 12:14:28 2018 - [info] Replication filtering check ok.
  27. Sun Dec 9 12:14:28 2018 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
  28. Sun Dec 9 12:14:28 2018 - [info] Checking SSH publickey authentication settings on the current master..
  29. Sun Dec 9 12:14:28 2018 - [info] HealthCheck: SSH to 10.0.102.204 is reachable.
  30. Sun Dec 9 12:14:28 2018 - [info]
  31. 10.0.102.204(10.0.102.204:3306) (current master)
  32. +--10.0.102.179(10.0.102.179:3306)
  33. +--10.0.102.221(10.0.102.221:3306)
  34.  
  35. Sun Dec 9 12:14:28 2018 - [info] Checking replication health on 10.0.102.179..
  36. Sun Dec 9 12:14:28 2018 - [info] ok.
  37. Sun Dec 9 12:14:28 2018 - [info] Checking replication health on 10.0.102.221..
  38. Sun Dec 9 12:14:28 2018 - [info] ok.
  39. Sun Dec 9 12:14:28 2018 - [info] Checking master_ip_failover_script status:
  40. Sun Dec 9 12:14:28 2018 - [info] /usr/local/bin/master_ip_failover --ssh_user=root --command=status --ssh_user=root --orig_master_host=10.0.102.204 --orig_master_ip=10.0.102.204 --orig_master_port=3306
  41.  
  42. IN SCRIPT TEST====service keepalived stop==service keepalived start===
  43.  
  44. Checking the Status of the script.. OK
  45. Sun Dec 9 12:14:28 2018 - [info] OK.
  46. Sun Dec 9 12:14:28 2018 - [warning] shutdown_script is not defined.
  47. Sun Dec 9 12:14:28 2018 - [info] Got exit code 0 (Not master dead).
  48.  
  49. MySQL Replication Health is OK.

masterha_check_repl --conf=/etc/masterha/app1.cnf

复制状态信息中会显示gtid的信息提示!

若上面两步检查都没有错误,可以开启MHA的监控!

  1. nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover &

查看MHA的运行状态:

  1. [root@test3 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
  2. app1 (pid:22124) is running(0:PING_OK), master:10.0.102.204
  3.  
  4. #显示app1正在运行,集群中主服务器为10.0.102.204

这时候MHA集群已经搭建完毕,只要停掉主服务器,就会自动选择备用的从作为主服务器的!

配置VIP漂移

目前集群master是204这台服务器,想一个场景,前端应用正在连接这台数据库,然后master服务器因某种原因宕掉了,这时候在MHA看来,我们可以把备用的主也就是179这台服务器作为新的master,维持集群的正常运行;但是对于前端应用来说,不可能集群没宕一次就修改一次源码中的数据库的IP地址。我们需要一个VIP来和前端相连,这个VIP总是指向正常运行数据库服务器!

MHA引入VIP有两种方式,一种是使用keepalive,另一种是使用MHA自带的脚本!

使用keepalive配置VIP漂移

按照keepalive软件,在当前的主和要作为备份的主上,按照keepalive!

  1. #直接yum按照即可
  2. yum install -y keepalived

然后编辑配置文件,yum安装默认配置在/etc/下面

  1. #当前主服务器的配置文件
    cat /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3.  
  4. global_defs {
  5. smtp_server 192.168.200.1
  6. smtp_connect_timeout
  7. router_id MYSQL_HA
  8. }
  9.  
  10. vrrp_instance VI_1 {
  11. state BACKUP
  12. interface eth0
  13. virtual_router_id
  14. priority
  15. advert_int
  16. authentication {
  17. auth_type PASS
  18. auth_pass
  19. }
  20. virtual_ipaddress {
  21. 10.0.102.110
  22. }
  23. }

  24. #当前作为备份主的服务器的配置文件
  25. cat /etc/keepalived/keepalived.conf
  26. ! Configuration File for keepalived
  27.  
  28. global_defs {
  29. smtp_server 192.168.200.1
  30. smtp_connect_timeout
  31. router_id MYSQL_HA
  32. }
  33.  
  34. vrrp_instance VI_1 {
  35. state BACKUP
  36. interface eth0
  37. virtual_router_id
  38. priority
  39. advert_int
  40. authentication {
  41. auth_type PASS
  42. auth_pass
  43. }
  44. virtual_ipaddress {
  45. 10.0.102.110
  46. }
  47. }

注意:上面两台服务器的keepalived都设置为了BACKUP模式,在keepalived中2种模式,分别是master->backup模式和backup->backup模式。这两种模式有很大区别。在master->backup模式下,一旦主库宕机,虚拟ip会自动漂移到从库,当主库修复后,keepalived启动后,还会把虚拟ip抢占过来,即使设置了非抢占模式(nopreempt)抢占ip的动作也会发生。在backup->backup模式下,当主库宕机后虚拟ip会自动漂移到从库上,当原主库恢复和keepalived服务启动后,并不会抢占新主的虚拟ip,即使是优先级高于从库的优先级别,也不会发生抢占。为了减少ip漂移次数,通常是把修复好的主库当做新的备库。

keepalive配置完成之后,启动keepalive,可以先测试keepalive是否会完成VIP的漂移!

keepalive配置完成之后,可以设置failover脚本!

需要注意的是,这里我们要测试VIP漂移,因此需要指定failover的脚本位置,在配置文件中指定!

  1. [root@test3 masterha]# cat masterha_default.cnf
  2. [server default]
  3. user=root
  4. password=123456
  5. ssh_user=root
  6. ssh_port=22
  7. ping_interval=3
  8. repl_user=repl
  9. repl_password=123456
  10.  
  11. master_binlog_dir= /data/mysql/
  12.  
  13. remote_workdir=/data/log/masterha
  14.  
  15. secondary_check_script= masterha_secondary_check -s test1 -s mgt01 --user=root --port=22 --master_host=test2 --master_port=3306
  16.  
  17. #failover参数位置
    master_ip_failover_script= /usr/local/bin/master_ip_failover --ssh_user=root
  18. # shutdown_script= /script/masterha/power_manager
  19. #report_script= /usr/local/bin/send_report
    #在线修改脚步位置,
  20. master_ip_online_change_script= /script/masterha/master_ip_online_change --ssh_user=root

failover脚步需要自己编辑,在网上找了http://www.ywnds.com/?p=8116  这片博客中给出的failover脚步,测试的时候有点问题,MHA得不到failover的状态!我自己改了一下【删了两个变量的引用】,能用,如下!

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

cat master_ip_failover

配置了脚本之后,再次检查复制状态:

  1. masterha_check_repl --conf=/etc/masterha/app1.cnf

若复制状态检查中,显示failover错误的信息,可以单独执行下面的命令,查看脚本的状态及报错提示!

  1. /usr/local/bin/master_ip_failover --ssh_user=root --command=status --ssh_user=root --orig_master_host=10.0.102.179 --orig_master_ip=10.0.102.179 --orig_master_port=3306

开启MHA的监控状态,若正常启动没有报错,则说明配置完成!

当前的状态是:

204的服务器是当前的master,并且有VIP。

179服务器上是备用的主。

221服务器仅是从!

停掉204的数据库服务,查看vip是否会转移到179上,集群的主服务器是否也会转到179上?

停掉204的数据库

  1. [root@test2 ~]# ip addr
  2. : lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
  3. link/loopback ::::: brd :::::
  4. inet 127.0.0.1/ scope host lo
  5. inet6 ::/ scope host
  6. valid_lft forever preferred_lft forever
  7. : eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
  8. link/ether fa:1d:ae::: brd ff:ff:ff:ff:ff:ff
  9. inet 10.0.102.204/ brd 10.0.103.255 scope global eth0
  10. inet 10.0.102.110/32 scope global eth0
  11. inet6 fe80::f81d:aeff:fe12:/ scope link
  12. valid_lft forever preferred_lft forever
  13. [root@test2 ~]# service mysqld stop #停掉数据库
  14. Shutting down MySQL............ SUCCESS!
    [root@test2 ~]# ip addr #可以看到VIP已经不在204上了
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
        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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether fa:1d:ae:12:52:00 brd ff:ff:ff:ff:ff:ff
        inet 10.0.102.204/22 brd 10.0.103.255 scope global eth0
        inet6 fe80::f81d:aeff:fe12:5200/64 scope link
           valid_lft forever preferred_lft forever
    [root@test2 ~]#

查看179服务器的VIP:

  1. #可以看到VIP已经漂移,并且179服务器已经变为master服务器!
    [root@test1 ~]# ip addr
  2. : lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
  3. link/loopback ::::: brd :::::
  4. inet 127.0.0.1/ scope host lo
  5. inet6 ::/ scope host
  6. valid_lft forever preferred_lft forever
  7. : eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
  8. link/ether fa:bc::8d:2e: brd ff:ff:ff:ff:ff:ff
  9. inet 10.0.102.179/ brd 10.0.103.255 scope global eth0
  10. inet 10.0.102.110/ scope global eth0
  11. inet6 fe80::f8bc:66ff:fe8d:2e00/ scope link
  12. valid_lft forever preferred_lft forever
  13. [root@test1 ~]# mysql -e "show processlist;"
  14. +----+------+------------+------+------------------+------+---------------------------------------------------------------+------------------+
  15. | Id | User | Host | db | Command | Time | State | Info |
  16. +----+------+------------+------+------------------+------+---------------------------------------------------------------+------------------+
  17. | | repl | mgt01: | NULL | Binlog Dump GTID | | Master has sent all binlog to slave; waiting for more updates | NULL |
  18. | | root | localhost | NULL | Query | | starting | show processlist |
  19. +----+------+------------+------+------------------+------+---------------------------------------------------------------+------------------+

还可以MHA的日志:

  1. cat /data/log/app1/manager.log

上面使用keepalive来做高可用,完成了VIP的漂移的测试,下面使用MHA自带的脚本来测试一下!

使用自带脚本配置VIP漂移

需要修改master_ip_failover脚本。

注意修改脚本中的VIP地址,以及ifconfig命令的绝对路径!

  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings FATAL => 'all';
  4. use Getopt::Long;
  5. my (
  6. $command, $ssh_user, $orig_master_host, $orig_master_ip,
  7. $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
  8. );
  9. my $vip = '10.0.102.110/22';
  10. my $key = '';
  11. my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
  12. my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
  13.  
  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.  
  25. exit &main();
  26.  
  27. sub main {
  28.  
  29. print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
  30.  
  31. if ( $command eq "stop" || $command eq "stopssh" ) {
  32.  
  33. my $exit_code = ;
  34. eval {
  35. print "Disabling the VIP on old master: $orig_master_host \n";
  36. &stop_vip();
  37. $exit_code = ;
  38. };
  39. if ($@) {
  40. warn "Got Error: $@\n";
  41. exit $exit_code;
  42. }
  43. exit $exit_code;
  44. }
  45. elsif ( $command eq "start" ) {
  46.  
  47. my $exit_code = ;
  48. eval {
  49. print "Enabling the VIP - $vip on the new master - $new_master_host \n";
  50. &start_vip();
  51. $exit_code = ;
  52. };
  53. if ($@) {
  54. warn $@;
  55. exit $exit_code;
  56. }
  57. exit $exit_code;
  58. }
  59. elsif ( $command eq "status" ) {
  60. print "Checking the Status of the script.. OK \n";
  61. exit ;
  62. }
  63. else {
  64. &usage();
  65. exit ;
  66. }
  67. }
  68.  
  69. sub start_vip() {
  70. `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  71. }
  72. sub stop_vip() {
  73. return unless ($ssh_user);
  74. `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  75. }
  76.  
  77. sub usage {
  78. print
  79. "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";
  80. }

cat /usr/local/bin/master_ip_failover

注意要给脚本加上可执行权限

  1. chmod +x master_ip_failover

然后修改配置文件:

  1. [server default]
  2. manager_log=/data/log/app1/manager.log
  3. manager_workdir=/data/log/app1
  4. master_binlog_dir=/data/mysql
  5. master_ip_failover_script=/usr/local/bin/master_ip_failover
  6. master_ip_online_change_script=/usr/local/bin/master_ip_failover
  7. password=
  8. ping_interval=
  9. remote_workdir=/data/log/masterha
  10. repl_password=
  11. repl_user=repl
  12. report_script=/usr/local/bin/send_report
  13. secondary_check_script=masterha_secondary_check -s test1 -s mgt01 --user=root --port= --master_host=test2 --master_port=
  14. ssh_port=
  15. ssh_user=root
  16. user=root
  17.  
  18. [server1]
  19. candidate_master=
  20. hostname=10.0.102.204
  21. port=3306
  1. [server2]
  2. candidate_master=1
  3. hostname=10.0.102.179
  4. port=
  1.  
  2. [server3]
  3. hostname=10.0.102.221
  4. no_master=
  5. port=

然后进行repl测试,若测试通过,在启动MHA监控!

使用自带的脚本做failover需要手动添加虚拟IP,在当前的master上添加VIP!

  1. [root@test1 ~]# ifconfig eth0: 10.0.102.110/22 #添加虚拟IP
  2. [root@test1 ~]# ifconfig
  3. eth0 Link encap:Ethernet HWaddr FA:BC::8D:2E:
  4. inet addr:10.0.102.179 Bcast:10.0.103.255 Mask:255.255.252.0
  5. inet6 addr: fe80::f8bc:66ff:fe8d:2e00/ Scope:Link
  6. UP BROADCAST RUNNING MULTICAST MTU: Metric:
  7. RX packets: errors: dropped: overruns: frame:
  8. TX packets: errors: dropped: overruns: carrier:
  9. collisions: txqueuelen:
  10. RX bytes: (7.2 GiB) TX bytes: (2.5 GiB)
  11.  
  12. eth0: Link encap:Ethernet HWaddr FA:BC::8D:2E:
  13. inet addr:10.0.102.110 Bcast:10.0.103.255 Mask:255.255.252.0
  14. UP BROADCAST RUNNING MULTICAST MTU: Metric:
  15.  
  16. lo Link encap:Local Loopback
  17. inet addr:127.0.0.1 Mask:255.0.0.0
  18. inet6 addr: ::/ Scope:Host
  19. UP LOOPBACK RUNNING MTU: Metric:
  20. RX packets: errors: dropped: overruns: frame:
  21. TX packets: errors: dropped: overruns: carrier:
  22. collisions: txqueuelen:
  23. RX bytes: (361.8 KiB) TX bytes: (361.8 KiB)

这是179的服务器上即有虚拟IP,也是MySQL集群的主服务器!然后停掉MySQL服务,查看VIP的漂移和主从的切换!

  1. [root@test1 ~]# service mysqld stop #停掉主服务器,虚拟IP已经漂移
  2. Shutting down MySQL............ SUCCESS!
  3. [root@test1 ~]# ifconfig
  4. eth0 Link encap:Ethernet HWaddr FA:BC::8D:2E:
  5. inet addr:10.0.102.179 Bcast:10.0.103.255 Mask:255.255.252.0
  6. inet6 addr: fe80::f8bc:66ff:fe8d:2e00/ Scope:Link
  7. UP BROADCAST RUNNING MULTICAST MTU: Metric:
  8. RX packets: errors: dropped: overruns: frame:
  9. TX packets: errors: dropped: overruns: carrier:
  10. collisions: txqueuelen:
  11. RX bytes: (7.2 GiB) TX bytes: (2.5 GiB)
  12.  
  13. lo Link encap:Local Loopback
  14. inet addr:127.0.0.1 Mask:255.0.0.0
  15. inet6 addr: ::/ Scope:Host
  16. UP LOOPBACK RUNNING MTU: Metric:
  17. RX packets: errors: dropped: overruns: frame:
  18. TX packets: errors: dropped: overruns: carrier:
  19. collisions: txqueuelen:
  20. RX bytes: (361.9 KiB) TX bytes: (361.9 KiB)

在备用主服务器204上查看:

  1. [root@test2 ~]# ip addr
  2. : lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
  3. link/loopback ::::: brd :::::
  4. inet 127.0.0.1/ scope host lo
  5. inet6 ::/ scope host
  6. valid_lft forever preferred_lft forever
  7. : eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
  8. link/ether fa:1d:ae::: brd ff:ff:ff:ff:ff:ff
  9. inet 10.0.102.204/ brd 10.0.103.255 scope global eth0
  10. inet 10.0.102.110/ brd 10.0.103.255 scope global secondary eth0:
  11. inet6 fe80::f81d:aeff:fe12:/ scope link
  12. valid_lft forever preferred_lft forever
  13. [root@test2 ~]# mysql -e "show processlist"
  14. +----+------+--------------------+------+------------------+------+---------------------------------------------------------------+------------------+
  15. | Id | User | Host | db | Command | Time | State | Info |
  16. +----+------+--------------------+------+------------------+------+---------------------------------------------------------------+------------------+
  17. | | repl | 10.0.102.221: | NULL | Binlog Dump GTID | | Master has sent all binlog to slave; waiting for more updates | NULL |
  18. | | root | localhost | NULL | Query | | starting | show processlist |
  19. +----+------+--------------------+------+------------------+------+---------------------------------------------------------------+------------------+
  20. [root@test2 ~]#

可以看到虚拟IP已经漂移,并且主从已经切换!

MHA集群(gtid复制)和vip漂移的更多相关文章

  1. MySQL高可用篇之MHA集群

    1 准备工作 1.1 修改主机名 vim /etc/hosts # 添加对应主机 192.168.28.128 mha1 192.168.28.131 mha2 192.168.28.132 mha3 ...

  2. 基于Docker Compose构建的MySQL MHA集群

    Docker MySQL MHA 基于Docker 1.13.1之上构建的MySQL MHA Docker Compose Project 可快速启动GTID模式下的MasterHA集群, 主用于My ...

  3. 三十六.MHA集群概述 、 部署MHA集群 测试配置

    1.准备MHA集群环境 准备6台虚拟机,并按照本节规划配置好IP参数 在这些虚拟机之间实现SSH免密登录 在相应节点上安装好MHA相关的软件包   使用6台RHEL 7虚拟机,如图-1所示.准备集群环 ...

  4. MHA集群搭建

    (1).简介 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开 ...

  5. MYSQL之MHA集群

    环境 manager 192.168.137.141 master1 192.168.137.144 master2 192.168.137.145 slave 192.168.137.141 vip ...

  6. MongoDB之分片集群与复制集

    分片集群 1.1.概念 分片集群是将数据存储在多台机器上的操作,主要由查询路由mongos.分片.配置服务器组成. ●查询路由根据配置服务器上的元数据将请求分发到相应的分片上,本身不存储集群的元数据, ...

  7. solr集群搭建(复制)

    Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南) 1   什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引 ...

  8. Tomcat集群session复制与Oracle的坑。。

    问题描述 公司某个系统使用了tomcat自带的集群session复制功能,然后后报了一个oracle驱动包里面的连接不能被序列化的异常. 01-Nov-2017 16:45:26.653 SEVERE ...

  9. 使用GTID给Galera集群做数据库异步复制

    一.为什么要做Galera集群异步复制 Galera集群解决了数据库高可用的问题,但是存在局限性,例如耗时的事务处理可能会导致集群性能急剧下降,甚至出现阻塞现象.而不幸的是,类似报表等业务需求就需要做 ...

随机推荐

  1. java基础解析系列(一)---String、StringBuffer、StringBuilder

    java基础解析系列(一)---String.StringBuffer.StringBuilder 前言:本系列的主题是平时容易疏忽的知识点,只有基础扎实,在编码的时候才能更注重规范和性能,在出现bu ...

  2. 洛谷P3166 数三角形 [CQOI2014] 数论

    正解:数论 解题报告: 传送门! 很久以前做的题了呢,,,回想方法还想了半天QAQ 然后写这题题解主要是因为看到了好像有很新颖的法子,就想着,学习一下趴,那学都学了不写博客多可惜 首先港下最常规的方法 ...

  3. 【爬坑】python3+pyqt5+pyinstaller 打包成exe的各种问题

    windows系统+python3+pyqt5+pyinstaller打包,经常会出现各种打包异常情况.如果代码没有特别异常,那么综合原因,大抵都是这四个元素之间的匹配问题,引起的.作者:一心狮链接: ...

  4. express 默认模板引擎

    使用express -t ejs microblog创建出来的居然不是ejs项目,而是jade项目.现在的版本已经没有-t这个命令了,改为express -e microblog.运行完之后,根据提示 ...

  5. 开机自启:bat实现一次性打开win7中的常用软件和文件夹

    需求说明: 我们电脑(windows)办公,经常上班前一开机,就要手动打开很多文件夹和程序. 想节省时间,一键打开常用的文件夹和程序,研究了一下bat命令,于是mystart.bat产生了. myst ...

  6. 前端 HTML标签介绍

    那什么是HTML标签呢? 1. 在HTML中规定标签使用英文的的尖括号即"<"和">"包起来,如`<html>`.`<p>` ...

  7. unity3d之 C# WaitFOrSeconds()

    学习unity3d不久.在使用WaitFOrSeconds()时,遇到了不少麻烦,故记录,以警示后人. 首先介绍C#和javascript 在使用它有非常大的差别. javascript能够直接使用 ...

  8. mysql 开启profiling

    mysql 开启profiling

  9. MySql操作语句集锦

    Windows服务 -- 启动MySQL    net start mysql-- 创建Windows服务    sc create mysql binPath= mysqld_bin_path(注意 ...

  10. a文件.o文件和.so文件有什么区别?

    .o类似于windows的.obj .a是多个.o合在一起,用于静态连接. .so文件(shared object)类似于.dll文件.,用于动态连接.