原理简介

  1. 、工作流程:
    mysql keepalived双主热备 - 图1

mysql安装

  1. 首先在两台服务器上安装好mysql数据库,具体操作可参考如下文档。
    centos下安装mysql数据库

双主配置

环境简介

服务器 系统环境 角色
10.8.11.111 CentOS release 6.9 (Final) master1
10.8.11.112 CentOS release 6.9 (Final) master2
10.8.11.119 虚拟IP,无系统 VIP(虚拟,供外部连接访问数据库)
  1. 供参考,mysql主主互备

配置master-master

  1. 修改master1和master2的数据库配置文件
    vi /usr/local/mysql/my.cnf

    1. 11.111上面

      1. server-id = 1
      2. log-bin = mysql-bin
      3. sync_binlog = 1
      4. binlog_checksum = none
      5. binlog_format = mixed
      6. auto-increment-increment = 2
      7. auto-increment-offset = 1
      8. slave-skip-errors = all
    2. 11.112上面

      1. server-id = 2 #注意这个是不同的
      2. log-bin = mysql-bin
      3. sync_binlog = 1
      4. binlog_checksum = none
      5. binlog_format = mixed
      6. auto-increment-increment = 2
      7. auto-increment-offset = 1
      8. slave-skip-errors = all
    3. service mysql restart ,后登录

  2. 创建同步用户

    1. 授予用户连接此数据库

    2. grant replication slave,replication client on *.* to root@'10.8.11.%' identified by "xc123456"; 或者下面的命令,(这个有待商榷)
      grant ALL PRIVILEGES on *.* to root@'10.8.11.%' identified by "xc123456";

    3. flush privileges; 刷新系统权限表

  3. 这是11.111服务器上的mysql keepalived双主热备 - 图2

  4. 这是11.112服务器上的mysql keepalived双主热备 - 图3

同步数据,

  1. 如下是11.111上面操作的
    change master to master_host='10.8.11.112',master_user='root',master_password='xc123456',master_log_file='mysql-bin.000001',master_log_pos=986;
    mysql keepalived双主热备 - 图4

  2. 如下是11.112上面操作的
    change master to master_host='10.8.11.111',master_user='root',master_password='xc123456',master_log_file='mysql-bin.000001',master_log_pos=986;
    mysql keepalived双主热备 - 图5
    P.S.或许会报错,当报错 got fatal error 1236的解决办法

  3. start slave; show slave status \G 查看同步状态

  4. 同步成功后的效果图,

    1. 这是11.112上面的
      mysql keepalived双主热备 - 图6

    2. 这是11.111上面的
      mysql keepalived双主热备 - 图7

测试

  1. 经测试,达到了数据同步
    mysql keepalived双主热备 - 图8

配置keepalived

  1. 两台机器都需要安装

  2. 下载依赖环境
    yum install -y pcre-devel openssl-devel popt-devel
    wget http://www.keepalived.org/software/keepalived-1.2.23.tar.gz
    tar -zxvf keepalived-1.2.23.tar.gz

  3. 进入到安装目录,进行编译和安装
    cd keepalived-1.2.23/
    ./configure --prefix=/usr/local/keepalived
    make && make install

  4. 将keepalived配置成系统服务

    1. cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

    2. cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

    3. mkdir -p /etc/keepalived

    4. cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

    5. cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

配置master1上的keepalived

  1. vi /etc/keepalived/keepalived.conf 清空或备份

    1. ! Configuration File for keepalived
    2. global_defs
    3. {
    4. notification_email
    5. {
    6. sysadmin@firewall.loc
    7. }
    8. notification_email_from Alexandre.Cassen@firewall.loc
    9. smtp_server 127.0.0.1
    10. smtp_connect_timeout 30
    11. router_id mysql #master1和master2上一样
    12. }
    13. vrrp_instance VI_1
    14. {
    15. state BACKUP #master1和master2一样都为BACKUP
    16. interface eth0 #注意自己服务器的网口名
    17. virtual_router_id 59 #路由器标识,master1和master2必须是一致的
    18. priority 100 #优先级,master1比master2高,0-255间的数字都可
    19. advert_int 1
    20. nopreempt #不主动抢占资源,只在master1这台优先级高的设置,master2不设置
    21. authentication
    22. {
    23. auth_type PASS
    24. auth_pass 1111
    25. }
    26. virtual_ipaddress
    27. {
    28. 10.8.11.119 #VIP
    29. }
    30. }
    31. virtual_server 10.8.11.119 3306 #VIP
    32. {
    33. delay_loop 2
    34. lb_algo rr
    35. lb_kind NAT
    36. persistence_timeout 50
    37. protocol TCP
    38. real_server 10.8.11.111 3306
    39. {
    40. weight 3
    41. notify_down /usr/local/keepalived/mysql.sh #当mssql服务down时,执行此脚本,
    42. #杀死keepalived,实现切换
    43. TCP_CHECK
    44. {
    45. connect_timeout 10 #连接超时
    46. nb_get_retry 3 #重试次数
    47. delay_before_retry 3 #重试间隔时间
    48. connect_port 3306
    49. }
    50. }
    51. }
  2. 编写脚本,并授予权限

    1. vi /usr/local/keepalived/mysql.sh ,脚本内容如下

      1. #!/bin/bash
      2. pkill keepalived
    2. 授权,chmod +x /usr/local/keepalived/mysql.sh

    3. 重启,service keepalived start

    4. 添加到开机自启,echo "/etc/init.d/keepalived start " >> /etc/rc.d/rc.sysinit

  3. 不对??

配置master2上的keepalived

  1. vi /etc/keepalived/keepalived.conf 清空或备份

    1. ! Configuration File for keepalived
    2. global_defs
    3. {
    4. notification_email
    5. {
    6. sysadmin@firewall.loc
    7. }
    8. notification_email_from Alexandre.Cassen@firewall.loc
    9. smtp_server 127.0.0.1
    10. smtp_connect_timeout 30
    11. router_id mysql
    12. }
    13. vrrp_instance VI_1
    14. {
    15. state BACKUP
    16. interface eth0
    17. virtual_router_id 59
    18. priority 90
    19. advert_int 1
    20. authentication
    21. {
    22. auth_type PASS
    23. auth_pass 1111
    24. }
    25. virtual_ipaddress
    26. {
    27. 10.8.11.119
    28. }
    29. }
    30. virtual_server 10.8.11.119 3306
    31. {
    32. delay_loop 2
    33. lb_algo rr
    34. lb_kind NAT
    35. persistence_timeout 50
    36. protocol TCP
    37. real_server 10.8.11.112 3306
    38. {
    39. weight 3
    40. notify_down /usr/local/keepalived/mysql.sh
    41. TCP_CHECK
    42. {
    43. connect_timeout 10
    44. nb_get_retry 3
    45. delay_before_retry 3
    46. connect_port 3306
    47. }
    48. }
    49. }
  2. mysql.sh脚本及授权,同上master1。

测试

  1. 用navicat在内网范围,链接VIP(10.8.11.119),并进行读写测试,和高可用测试。

  2. 注意,路由器标识这里,因为在10.8.11.37和11.38上面做过mysql+keepalived的双主热备了,换成
    virtual_router_id 52 ,再次重启keepalived就可以实现了。

  3. keepalived-conf.zip

参考链接:双主热备读写分离mysql主主互备

  1. keepalived的配置详解keepalived工作原理和配置说明