部署容器

wget https://vip.123pan.cn/1815238395/dockerimage/mysql.5.7.tar && docker load -i mysql.5.7.tar

docker-compose文件内容

version: '3'

services:
  mysql-master:
    image: mysql:5.7
    container_name: mysql-master
    networks:
      yewu-network:
        ipv4_address: 192.168.177.51
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: HVSfrZlryFjcpIvW
    entrypoint:
      - sh
      - -euc
      - |
        cat <<'EOF'> /etc/my.cnf
        [mysqld]
        skip-host-cache
        skip-name-resolve
        datadir=/var/lib/mysql
        socket=/var/run/mysqld/mysqld.sock
        secure-file-priv=/var/lib/mysql-files
        user=mysql
        symbolic-links=0
        pid-file=/var/run/mysqld/mysqld.pid
        server-id=1
        log_bin=mysql-bin
        binlog_format=ROW
        [client]
        socket=/var/run/mysqld/mysqld.sock
        !includedir /etc/mysql/conf.d/
        !includedir /etc/mysql/mysql.conf.d/
        EOF
        docker-entrypoint.sh mysqld --innodb-buffer-pool-size=2G --innodb-log-file-size=256M
    ports:
      - 3306:3306
    volumes:
      - ./mysql-master-data:/var/lib/mysql

  mysql-slave1:
    image: mysql:5.7
    container_name: mysql-slave1
    networks:
      yewu-network:
        ipv4_address: 192.168.177.52
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: HVSfrZlryFjcpIvW
    #      MYSQL_DATABASE: yewu
    #      MYSQL_USER: yewu
    #      MYSQL_PASSWORD: HVSfrZlryFjcpIvW
    #      MYSQL_MASTER_HOST: mysql-master
    #      MYSQL_MASTER_PORT: 3306
    entrypoint:
      - sh
      - -euc
      - |
        cat <<'EOF'> /etc/my.cnf
        [mysqld]
        skip-host-cache
        skip-name-resolve
        datadir=/var/lib/mysql
        socket=/var/run/mysqld/mysqld.sock
        secure-file-priv=/var/lib/mysql-files
        user=mysql
        symbolic-links=0
        pid-file=/var/run/mysqld/mysqld.pid
        server-id=2
        relay-log=mysql-relay-bin
        log_bin=mysql-bin
        binlog_format=ROW
        read_only=1
        [client]
        socket=/var/run/mysqld/mysqld.sock
        !includedir /etc/mysql/conf.d/
        !includedir /etc/mysql/mysql.conf.d/
        EOF
        docker-entrypoint.sh mysqld --innodb-buffer-pool-size=2G --innodb-log-file-size=256M
    volumes:
      - ./mysql-slave1-data:/var/lib/mysql

  mysql-slave2:
    image: mysql:5.7
    container_name: mysql-slave2
    networks:
      yewu-network:
        ipv4_address: 192.168.177.53
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: HVSfrZlryFjcpIvW
    #      MYSQL_DATABASE: yewu
    #      MYSQL_USER: yewu
    #      MYSQL_PASSWORD: HVSfrZlryFjcpIvW
    #      MYSQL_MASTER_HOST: mysql-master
    #      MYSQL_MASTER_PORT: 3306
    entrypoint:
      - sh
      - -euc
      - |
        cat <<'EOF'> /etc/my.cnf
        [mysqld]
        skip-host-cache
        skip-name-resolve
        datadir=/var/lib/mysql
        socket=/var/run/mysqld/mysqld.sock
        secure-file-priv=/var/lib/mysql-files
        user=mysql
        symbolic-links=0
        pid-file=/var/run/mysqld/mysqld.pid
        server-id=3
        relay-log=mysql-relay-bin
        log_bin=mysql-bin
        binlog_format=ROW
        read_only=1
        [client]
        socket=/var/run/mysqld/mysqld.sock
        !includedir /etc/mysql/conf.d/
        !includedir /etc/mysql/mysql.conf.d/
        EOF
        docker-entrypoint.sh mysqld --innodb-buffer-pool-size=2G --innodb-log-file-size=256M
    volumes:
      - ./mysql-slave2-data:/var/lib/mysql

networks:
  yewu-network:
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.177.0/24
    name: yewu-network

集群信息

(1)mysql集群信息
192.168.177.51 主
192.168.177.52 从
192.168.177.53 从

(2)连接命令

mysql -uroot -pHVSfrZlryFjcpIvW

(3)主从配置

# 主库配置
create user 'repl'@'%' identified by 'HVSfrZlryFjcpIvW';
grant replication slave,replication client on *.* to 'repl'@'%';
flush privileges;
show master status;

# 从库配置
show global variables like '%log%';
show global variables like '%server%';
show master logs;
show master status;
change master to master_host='mysql-master',master_user='repl',master_password='HVSfrZlryFjcpIvW',master_log_file='mysql-bin.000004',master_log_pos=767;

start slave;
show slave status;

(4)创建测试数据库,并查看从库是否同步。

CREATE DATABASE `test` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';

2023-11-03T02:23:50.png

最后修改:2023 年 11 月 03 日
如果觉得我的文章对你有用,请随意赞赏