使用Keepalived实现MySQL双主高可用(2)

mysql> show databases;
+--------------------+
| Database          |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb              |
+--------------------+

mysql> use testdb;

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user          |
+----------------+

mysql> select number,name from user;
+--------+------+
| number | name |
+--------+------+
|      1 | testid  |
+--------+------+
---------------------

可以看到已经成功同步过去,同样在backup插入到user表数据,一样同步过去,双主配置没有问题。

五、配置keepalived实现双机热备

1.master安装keepalived并配置:

# yum install -y keepalived

# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
  notification_email {
    admin@test.com
  }
  notification_email_from admin@test.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id MYSQL_HA
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0            #根据实际网络接口进行更改
    virtual_router_id 51
    priority 100              #优先级,master设置为100
    advert_int 1
    nopreempt                  #不主动抢占资源,只在master上设置
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.30
    }
}

virtual_server 192.168.1.30 3306 {
    delay_loop 2
    #lb_algo rr
    #lb_kind NAT
    persistence_timeout 50
    protocol TCP

real_server 192.168.1.10 3306 {              #检测本地mysql
        weight 3
        notify_down /tmp/mysql.sh              #当mysql服务down时,执行此脚本,杀死keepalived实现切换
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

backup安装keepalived并配置:

# yum install -y keepalived

# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
  notification_email {
    admin@test.com
  }
  notification_email_from admin@test.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id MYSQL_HA
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/597d8abc26500a04cd70dece67ce56c1.html