centos7使用docker-compose一键搭建mysql高可用主从集群

docker部署

环境准备

卸载旧版本

yum remove -y docker \
  docker-client \
  docker-client-latest \
  docker-common \
  docker-latest \
  docker-latest-logrotate \
  docker-logrotate \
  docker-selinux \
  docker-engine-selinux \
  docker-engine

安装依赖

yum install -y yum-utils \ device-mapper-persistent-data \ lvm2

下载阿里云仓库

wget http://mirrors.aliyun.com/repo/Centos-7.repo
 
wget  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
 
yum install -y epel-release
 
mv *.repo /etc/yum.repo.d/

安装docker

yum install -y docker-ce

开启docker

systemctl start docker.service 
systemctl enable docker.service 
systemctl status  docker.service 
docker version

安装docker-compose

linux安装docker和docker-compose_linux安装docker-compose_yuanzelin8的博客-CSDN博客

服务器IP:192.168.100.8 

文件夹和其他文件如图:

centos7使用docker-compose一键搭建mysql高可用主从集群_第1张图片

编写docker-compose.yml文件

version: '3'
services:
  mysql-slave-lb:
    image: nginx:latest
    container_name: mysql-slave-lb
    ports:
    - 3307:3307
    volumes:
    - /home/docker/mariadb10.7/nginx/nginx.conf:/etc/nginx/nginx.conf
    networks:
    - mysql
    depends_on:
    - mysql-master
    - mysql-slave1
    - mysql-slave2
    - mysql-slave3
  mysql-master:
    image: mariadb:10.7.3
    container_name: mysql-master
    environment:
      MYSQL_ROOT_PASSWORD: "123456"
      MASTER_SYNC_USER: "sync_admin" #设置脚本中定义的用于同步的账号
      MASTER_SYNC_PASSWORD: "123456" #设置脚本中定义的用于同步的密码
      ADMIN_USER: "root" #当前容器用于拥有创建账号功能的数据库账号
      ADMIN_PASSWORD: "123456"
      ALLOW_HOST: "10.10.%.%" #允许同步账号的host地址
      TZ: "Asia/Shanghai" #解决时区问题
    ports:
    - 3306:3306
    networks:
      mysql:
        ipv4_address: "10.10.10.10" #固定ip,因为从库在连接master的时候,需要设置host
    volumes:
    - /home/docker/mariadb10.7/master:/docker-entrypoint-initdb.d #挂载master脚本
    - /home/docker/mariadb10.7/master/data:/var/lib/mysql
    command:
    -  "--server-id=1"
    -  "--character-set-server=utf8mb4"
    -  "--collation-server=utf8mb4_unicode_ci"
    -  "--log-bin=mysql-bin"
    -  "--sync_binlog=1"
  mysql-slave1:
    image: mariadb:10.7.3
    container_name: mysql-slave1
    environment:
      MYSQL_ROOT_PASSWORD: "123456"
      SLAVE_SYNC_USER: "sync_admin" #用于同步的账号,由master创建
      SLAVE_SYNC_PASSWORD: "123456"
      ADMIN_USER: "root"
      ADMIN_PASSWORD: "123456"
      MASTER_HOST: "10.10.10.10" #master地址,开启主从同步需要连接master
      TZ: "Asia/Shanghai" #设置时区
    networks:
     mysql:
       ipv4_address: "10.10.10.20" #固定ip
    volumes:
    - /home/docker/mariadb10.7/slave1:/docker-entrypoint-initdb.d #挂载slave脚本
    - /home/docker/mariadb10.7/slave1/data:/var/lib/mysql
    command:
    -  "--server-id=2"
    -  "--character-set-server=utf8mb4"
    -  "--collation-server=utf8mb4_unicode_ci"
  mysql-slave2:
    image: mariadb:10.7.3
    container_name: mysql-slave2
    environment:
      MYSQL_ROOT_PASSWORD: "123456"
      SLAVE_SYNC_USER: "sync_admin"
      SLAVE_SYNC_PASSWORD: "123456"
      ADMIN_USER: "root"
      ADMIN_PASSWORD: "123456"
      MASTER_HOST: "10.10.10.10"
      TZ: "Asia/Shanghai"
    networks:
      mysql:
        ipv4_address: "10.10.10.30" #固定ip
    volumes:
    - /home/docker/mariadb10.7/slave2:/docker-entrypoint-initdb.d #挂载slave脚本
    - /home/docker/mariadb10.7/slave2/data:/var/lib/mysql
    command: #这里需要修改server-id,保证每个mysql容器的server-id都不一样
    -  "--server-id=3"
    -  "--character-set-server=utf8mb4"
    -  "--collation-server=utf8mb4_unicode_ci"
  mysql-slave3:
    image: mariadb:10.7.3
    container_name: mysql-slave3
    environment:
      MYSQL_ROOT_PASSWORD: "123456"
      SLAVE_SYNC_USER: "sync_admin" #用于同步的账号,由master创建
      SLAVE_SYNC_PASSWORD: "123456"
      ADMIN_USER: "root"
      ADMIN_PASSWORD: "123456"
      MASTER_HOST: "10.10.10.10" #master地址,开启主从同步需要连接master
      TZ: "Asia/Shanghai" #设置时区
    networks:
     mysql:
       ipv4_address: "10.10.10.40" #固定ip
    volumes:
    - /home/docker/mariadb10.7/slave3:/docker-entrypoint-initdb.d #挂载slave脚本
    - /home/docker/mariadb10.7/slave3/data:/var/lib/mysql
    command:
    -  "--server-id=4"
    -  "--character-set-server=utf8mb4"
    -  "--collation-server=utf8mb4_unicode_ci"
networks:
  mysql:
    driver: bridge
    ipam:
      driver: default
      config:
      - subnet: "10.10.0.0/16"


你可能感兴趣的:(linux,docker)