标签: mysql

  • MySQL + keepalived (CentOS7)高可用方案

    本文主要介绍了利用 keepalived 实现 MySQL 数据库的高可用。

    由于要做高可用,那么自然就需要 MySQL 双主模式,又因为数据库的特殊性,我们需要特别注意主备间的切换(不是想切就切),这个时候就需要 keepalived 非抢占模式。所以我们需要做:

    • 安装 MySQL 双主(被动)模式,实现数据的冗余备份
    • 安装 keepalived nopreempt 模式,实现 MySQL 数据库的故障切换

    1 环境准备

    host1host2 部署高可用,host3 上部署 MySQL 客户端用于测试,详细信息如下:

    hostname ip OS install
    host1 192.168.1.51 CentOS7.0 MySQL-server-5.5.45 / keepalived
    host2 192.168.1.52 CentOS7.0 MySQL-server-5.5.45 / keepalived
    host3 192.168.1.53 CentOS7.0 MySQL-client-5.5.45

    2 安装 MySQL 双主结构

    MySQL 双主顾名思义就是互为主备,需要特别注意一些配置。

    • 下面是 host1 的配置:
    • log-bin = bin
      relay-log = relay-bin
      server-id = 1
      skip-slave-start = 1
      auto_increment_offset = 1
      auto_increment_increment = 10
      # log_slave_updates = 1
    • 下面是 host2 的配置:
    • log-bin = bin
      relay-log = relay-bin
      server-id = 2
      skip-slave-start = 1
      auto_increment_offset = 2
      auto_increment_increment = 10
      # log_slave_updates = 1
    • 注意:
    log-bin 是一定要开启的,主要用于主备之间的同步
    relay-log 重命名以下也有必要,这个参数可以不配置
    两个实例的 server-id 必须配置不一样
    skip-slave-start 是为了数据一致性
    auto_increment_offset 是自增字段从哪开始
    auto_increment_increment 是自增字段增加的幅度,配置成10是为了容易预估表的行数
    如果你要给这两个实例再加 slave,那么你就需要配置 log_slave_updates 参数
    • 按上面的配置启动 host1host2 上的 MySQL 实例
    • 设置主主模式
    ---- 在 host1 上执行:
    mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replication'@'192.168.1.52' IDENTIFIED BY PASSWORD 'helloworld';
    ---- 在 host2 上执行:
    mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replication'@'192.168.1.51' IDENTIFIED BY PASSWORD 'helloworld';
    ---- 在 host1 上执行:
    mysql> start slave;
    ---- 在 host1 上执行:
    mysql> start slave;
    • note: 如果报如下错误:
    ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number

    只需要按照如下处理即可:

    mysql> select password('helloworld');
    +-------------------------------------------+
    | password('helloworld')                    |
    +-------------------------------------------+
    | *D35DB127DB631E6E27C6B75E8D376B04F64FAF83 |
    +-------------------------------------------+
    1 row in set (0.00 sec)
    mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replication'@'192.168.10.51' IDENTIFIED BY PASSWORD '*D35DB127DB631E6E27C6B75E8D376B04F64FAF83';
    mysql> flush privileges;

    3 部署高可用检测脚本

    # cat /sbin/ha_check.sh
    #!/bin/bash
    
    counter=$(ps -C mysqld --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        service keepalived stop
    fi
    
    # chmod +x /sbin/ha_check.sh

    4 部署 keepalived 非抢占模式

    • 安装 keepalived
    # yum install keepalived
    • 修改配置文件
    • ###### host1 配置文件
      ! Configuration File for keepalived
      global_defs {
              notification_email {
                  cjfeii@126.com
              }
              notification_email_from ha@126.com
              smtp_server smtp.126.com
              smtp_connect_timeout 30
              router_id host1
      }
      vrrp_sync_group hw_vsg {
              group {
                      hw_vi
              }
      }
      vrrp_script check_apps {
          script "/sbin/ha_check.sh"
          interval 3
      }
      vrrp_instance hw_vi {
              state BACKUP
              nopreempt
      
              interface eth0
      
              virtual_router_id 51
              priority 150
              advert_int 3
              authentication {
                      auth_type PASS
                      auth_pass 123456
              }
      
              virtual_ipaddress {
                      192.168.10.100/24 dev em1 scope global
                      192.168.10.200/24 dev em1 scope global
              }
              track_script {
                  check_apps
              }
      }
      ######
      ###### host2 配置文件
      ! Configuration File for keepalived
      global_defs {
              notification_email {
                  cjfeii@126.com
              }
              notification_email_from ha@126.com
              smtp_server smtp.126.com
              smtp_connect_timeout 30
              router_id host2
      }
      vrrp_sync_group hw_vsg {
              group {
                      hw_vi
              }
      }
      vrrp_script check_apps {
          script "/sbin/ha_check.sh"
          interval 3
      }
      vrrp_instance hw_vi {
              state BACKUP
      
              interface eth0
      
              virtual_router_id 51
              priority 100
              advert_int 3
              authentication {
                      auth_type PASS
                      auth_pass 123456
              }
      
              virtual_ipaddress {
                      192.168.10.100/24 dev em1 scope global
                      192.168.10.200/24 dev em1 scope global
              }
              track_script {
                  check_apps
              }
      }
    • 启动 keepalived
    # service keepalived start