docker-compose实现mysql主从复制

利用docker-compose实现mysql主从复制

1.首先创建挂载的目录以及配置信息

1.1 主

mkdir -p /opt/mysql/master/data
mkdir -p /opt/mysql/master/config
# 编写配置文件
vim /opt/mysql/master/config/my.cnf

my.cnf配置信息

[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
secure_file_priv=/var/lib/mysql
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

2.2 从

mkdir -p /opt/mysql/slave/data
mkdir -p /opt/mysql/slave/config
# 编写配置文件
vim /opt/mysql/slave/config/my.cnf

my.cnf配置信息

[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
secure_file_priv=/var/lib/mysql
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

2.编写docker-compose.yaml文件

version: '3'
services:
  mysql:
    image: mysql:8.0.20
    restart: always
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      TZ: Asia/Shanghai
    ports:
      - 3306:3306
    # 挂载信息 
    volumes:
      - /opt/mysql/master/data:/var/lib/mysql
      - /opt/mysql/master/config/my.cnf:/etc/mysql/my.cnf
    command:
      --max_connections=1000
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --default-authentication-plugin=mysql_native_password
      --server-id=1 
      --log-bin=master-binlog 
      --innodb-flush-log-at-trx-commit=1 
      --sync-binlog=1 
      --innodb-file-per-table=1 
      --innodb-buffer-pool-size=2G
    networks:
      - lph

  mysql-slave:
    image: mysql:8.0.20
    restart: always
    container_name: mysql-slave
    environment:
      # 设置密码 
      MYSQL_ROOT_PASSWORD: 123456
      TZ: Asia/Shanghai
    ports:
      - 3305:3306
    volumes:
      # 挂载信息 
      - /opt/mysql/slave/data:/var/lib/mysql
      - /opt/mysql/slave/config/my.cnf:/etc/mysql/my.cnf
    command: 
      --max_connections=1000
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --default-authentication-plugin=mysql_native_password
      --server-id=2 
      --log-bin=slave-binlog 
      --skip-log-bin 
      --skip-slave-start 
      --innodb-flush-log-at-trx-commit=1 
      --sync-binlog=1 
      --innodb-file-per-table=1 
      --innodb-buffer-pool-size=2G
    networks:
      - lph
networks:
  lph:
    driver: bridge

3.启动服务

docker-compose -f docker-compose.yaml up -d

4.设置主从

4.1 主

# 进入容器
docker exec -it mysql bash
# 登录mysql
mysql -u root -p 
# 输入密码.... 在docker-compose.yaml里面已经配置了 123456

# 创建用户
CREATE USER 'lph'@'%' IDENTIFIED BY '123456'; 
GRANT REPLICATION SLAVE ON *.* TO 'lph'@'%';
FLUSH PRIVILEGES;
# 查看主状态
SHOW MASTER STATUS;
reset master;
# 记录下 master-binlog.000001 以及位置 2659
mysql> SHOW MASTER STATUS;
+----------------------+----------+--------------+------------------+-------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| master-binlog.000001 |     2659 |              |                  |                   |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4.2 从

# 进入容器
docker exec -it mysql-slave bash# 登录mysql
mysql -u root -p 
# 输入密码.... 在docker-compose.yaml里面已经配置了 123456
# 连接主数据库
CHANGE MASTER TO MASTER_HOST='mysql', MASTER_PORT=3306, MASTER_USER='lph', MASTER_PASSWORD='123456', MASTER_LOG_FILE='master-binlog.000001', MASTER_LOG_POS=1009;

# 配置生效
start slave;
# 查看从服务器状态
SHOW slave STATUS\G

结束!

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