本示例所有配置都在docker环境下进行配置,使用docker-compose启动;
docker环境及docker-compose安装请看👉Docker install

服务器规划

服务器

IP地址

用途

centos7.5.1804(Core)

192.168.8.155

mysql-master1/keepalive1

centos7.5.1804(Core)

192.168.8.156

mysql-master2/keepalive2

centos7.5.1804(Core)

192.168.8.157

mysql-slave

-

192.168.8.158

VIP

MYSQL集群配置(docker环境)

一、MYSQL双主一丛集群架构图

mysql Keepalived双主架构_mysql

二、mysql配置文件(my.cnf)

[mysqld]

#日志设置
log-error = /var/lib/mysql/db-err.log
slow-query-log-file = /var/lib/mysql/db-slow.log
relay-log=/var/lib/mysql/relay-log
long-query-time = 20

#开启 binlog 同步
server_id = 001
log-bin = /var/lib/mysql/db-bin.log
max-binlog-cache_size = 64M
max-binlog-size = 1G
expire_logs_days = 15
binlog-format = mixed
innodb_flush_log_at_trx_commit = 1
sync-binlog = 1

## 主主同步配置
auto-increment-increment=2
auto-increment-offset=1
log-slave-updates=1

#性能调优配置
innodb_buffer_pool_size = 24576M
max_connections = 5000
max_connect_errors = 6000
external-locking = FALSE
max_allowed_packet = 64M
join_buffer_size = 64M
sort_buffer_size = 2M
read_rnd_buffer_size = 16M
[mysqld]

#日志设置
log-error = /var/lib/mysql/db-err.log
slow-query-log-file = /var/lib/mysql/db-slow.log
relay-log=/var/lib/mysql/relay-log
long-query-time = 20

#开启 binlog 同步
server_id = 002
log-bin = /var/lib/mysql/db-bin.log
max-binlog-cache_size = 64M
max-binlog-size = 1G
expire_logs_days = 15
binlog-format = mixed
innodb_flush_log_at_trx_commit = 1
sync-binlog = 1

## 主主同步配置
auto-increment-increment=2
auto-increment-offset=2
log-slave-updates=1

#性能调优配置
innodb_buffer_pool_size = 24576M
max_connections = 5000
max_connect_errors = 6000
external-locking = FALSE
max_allowed_packet = 64M
join_buffer_size = 64M
sort_buffer_size = 2M
read_rnd_buffer_size = 16M
[mysqld]

#日志设置
log-error = /var/lib/mysql/db-err.log

#主从设置
server_id = 003
slave_parallel_workers=2
relay_log_info_repository = TABLE
master_info_repository    = TABLE
relay_log_recovery=1



#性能调优设置
innodb_buffer_pool_size = 24576M
max_connections = 5000
max_connect_errors = 6000
external-locking = FALSE
max_allowed_packet = 64M
join_buffer_size = 64M
sort_buffer_size = 2M
read_rnd_buffer_size = 16M

三、mysql docker-compose启动文件(docker-compose.yml)

version: '3'
services:
  mysql:
    image: mysql:5.7.35
    restart: always
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: 1qazxsw2
      TZ: Asia/Shanghai
    ports:
      - 3306:3306
    volumes:
      - ./data:/var/lib/mysql
      - ./config/my.cnf:/etc/mysql/my.cnf
    command:
    #  --max_connections=1000
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
version: '3'
services:
  mysql:
    image: mysql:5.7.35
    restart: always
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: 1qazxsw2
      TZ: Asia/Shanghai
    ports:
      - 3306:3306
    volumes:
      - ./data:/var/lib/mysql
      - ./config/my.cnf:/etc/mysql/my.cnf
    command:
    #  --max_connections=1000
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
version: '3'
services:
  mysql:
    image: mysql:5.7.35
    restart: always
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: 1qazxsw2
      TZ: Asia/Shanghai
    ports:
      - 3306:3306
    volumes:
      - ./data:/var/lib/mysql
      - ./config/my.cnf:/etc/mysql/my.cnf
    command:
    #  --max_connections=1000
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci

四、本示例mysql测试目录数据结构:

mysql Keepalived双主架构_mysql_02

五、检查目录:(dir可自行配置)

#!/bin/bash -v
dir=/docker/mysql
if [ ! -d "/docker/mysql" ];then
mkdir -p $dir
else
echo "该文件夹已存在!"
fi

六、启动docker容器(三台节点都执行)

启动:docker-compose -f /docker/mysql/docker-compose.yml up -d 查看日志:docker-compose logs -f (未指定配置文件需要/docker/mysql目录下执行)

mysql docker容器启动成功:

mysql Keepalived双主架构_mysql_03

七、进入Mysql容器配置双主互从和主从模式

创建同步用户

进入容器:docker exec -it mysql /bin/bash 进入mysql:mysql -u root -p

创建同步账号:(黄底部分用户自行修改)
CREATE USER 'user'@'%';ALTER USER 'user'@'%' IDENTIFIED WITH mysql_native_password BY '123456';GRANT REPLICATION SLAVE ON *.* TO 'user'@'%';flush privileges; 以上内容三台服务器都需执行!


155服务器执行同步156服务器

停止已经启动的绑定:stop slave; 重置绑定:reset master;

查看主库状态:show master status;

mysql Keepalived双主架构_mysql_04

执行同步命令:(master_log_file,master_log_pos 填写156服务器的status状态值)

change master to master_host='192.168.8.156',master_port=3306,master_user='user',master_password='123456',master_log_file='db-bin.000003',master_log_pos=3988;

启动同步:start slave; 查看同步结果:show slave status\G(Slave_IO-Running 和 Slave_SQL_Running 都为 Yes 即为成功)

mysql Keepalived双主架构_docker_05

156服务器执行同步155服务器

停止已经启动的绑定:stop slave; 重置绑定:reset master; 查看主库状态:show master status; 执行同步命令:(master_log_file,master_log_pos 填写155服务器的status状态值)
change master to master_host='192.168.8.155',master_port=3306,master_user='user',master_password='123456',master_log_file='db-bin.000003',master_log_pos=3988;
启动同步:start slave; 查看同步结果:show slave status\G(Slave_IO-Running 和 Slave_SQL_Running 都为 Yes 即为成功)

157服务器执行同步155服务器

停止已经启动的绑定:stop slave; 重置绑定:reset master; 查看主库状态:show master status; 执行同步命令:(master_log_file,master_log_pos 填写155服务器的status状态值)
change master to master_host='192.168.8.155',master_port=3306,master_user='user',master_password='123456',master_log_file='db-bin.000003',master_log_pos=3988;
启动同步:start slave; 查看同步结果:show slave status\G(Slave_IO-Running 和 Slave_SQL_Running 都为 Yes 即为成功)


👍至此我们双主一丛的架构就部署好了(155、156服务器可读可写、157服务器作为备份服务器,只可读)


八、Myql高可用(VIP+Keepalive)``

155、156服务器部署keepalive

yum install -y keepalive

155服务器配置(keepalived.conf)和mysql脚本文件(mysql_down.sh)
global_defs {
  router_id LVS_DEVEL
}

vrrp_sync_group VG_1 {
  group {
    VI_1
  }
}

vrrp_instance VI_1 {
  state BACKUP  #两个机器都是这个值,不要设置为 MASTER,使用下面的 priority 来控制
  interface ens192  #通过 ifconfig 查看
  virtual_router_id 155
  priority 100 #优先级,另一台机器设置为 90
  advert_int 1
  nopreempt  # 另一台优先级低的机器不设置此参数!
  authentication {
    auth_type root
    auth_pass root
  }
  virtual_ipaddress {
   192.168.8.158   # VIP
  }
}

virtual_server 192.168.8.158 3306 {  # VIP
  delay_loop 2
  lb_algo rr
  lb_kind DR
  persistence_timeout 50
  protocol TCP

  real_server 192.168.8.155 3306 {   # 机器的IP地址
    weight 3
    notify_down /docker/mysql/bin/mysql.sh
    TCP_CHECK {
      connect_timeout 5
      nb_get_retry 3
      delay_before_retry 3
      connect_port 3306
    }
  }
}
#!/bin/sh
pkill keepalived
156服务器配置(keepalived.conf)和mysql脚本文件(mysql_down.sh)
global_defs {
  router_id LVS_DEVEL
}

vrrp_sync_group VG_1 {
  group {
    VI_1
  }
}

vrrp_instance VI_1 {
  state BACKUP  #两个机器都是这个值,不要设置为 MASTER,使用下面的 priority 来控制
  interface ens192  #通过 ifconfig 查看
  virtual_router_id 156
  priority 90 #优先级,另一台机器设置为 90
  advert_int 1
  authentication {
    auth_type root
    auth_pass root
  }
  virtual_ipaddress {
   192.168.8.158  # VIP
  }
}

virtual_server 192.168.8.158 3306 {  # VIP
  delay_loop 2
  lb_algo rr
  lb_kind DR
  persistence_timeout 50
  protocol TCP

  real_server 192.168.8.156 3306 {   # 机器的IP地址
    weight 3
    notify_down /docker/mysql/bin/mysql.sh
    TCP_CHECK {
      connect_timeout 5
      nb_get_retry 3
      delay_before_retry 3
      connect_port 3306
    }
  }
}
#!/bin/sh
pkill keepalived
Keepalive启动及配置文件目录

脚本目录:mysql项目目录的bin目录下(如果本机的mysql宕机,那么就关闭本机的keepalive)
配置文件目录:/etc/keepalived/keepalived.conf 启动命令:systemctl start keepalived.service 查看状态:systemctl status keepalived.service 重启:systemctl restart keepalived.service


依次启动155、156服务器下的keepalive;

验证vip是否挂在网卡

ip addr show dev ens192

mysql Keepalived双主架构_数据库_06


👍至此我们Mysql+Keepalive高可用架构就部署完成。