docker-compose实现mysql主从复制

1 配置docker-compose.yml

version: '3'
services:
  mysql-master:
    image: mysql:5.7
    container_name: mysql-master
    environment:
      - MYSQL_ROOT_PASSWORD=123456
      - MYSQL_LOG_CONSOLE=true
    volumes:
      - /home/doc/master/log:/var/log/mysql #
      - /home/doc/master/data:/var/lib/mysql #
      - /home/doc/master/conf:/etc/mysql #
    networks: 
      - mysql-net
    ports:
      - "8001:3306"
  mysql-slave:
    image: mysql:5.7
    container_name: mysql-slave
    environment:
      - MYSQL_ROOT_PASSWORD=123456
      - MYSQL_LOG_CONSOLE=true
    volumes:
      - /home/doc/slave/log:/var/log/mysql
      - /home/doc/slave/data:/var/lib/mysql
      - /home/doc/slave/conf:/etc/mysql
    networks:
      - mysql-net
    ports:
      - "8002:3306"
networks:
  mysql-net:

2 数据库配置文件

    2.1 master 数据库 配置my.cnf

    vim /home/doc/master/conf/my.cnf  (在此目录下并没有my.cnf,需要自己创建)

[client]
default-charset-set=utf8

[mysql]
default-charset-set=utf8

[mysqld]
init_connect='Set collation_connection=utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
#需要要配置,要不然mysql连接会很慢
skip-name-resolve

server-id=1
log-bin=mysql-bin
read-only=0
#需要进行复制的数据库,
binlog-do-db=shopmall

replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=preformance_schema

    2.2 slave 数据库配置文件配置

     vim  /home/doc/slave/conf/my.cnf

[client]
default-charset-set=utf8

[mysql]
default-charset-set=utf8

[mysqld]
init_connect='Set collation_connection=utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve



server-id=20
log-bin=mysql-bin
read-only=1
binlog-do-db=shopmall
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=preformance_schema

3 重启mysql数据库

   docker-compose restart   (该命令需要在docker-compose.yml同目录下使用)

 

4 为master 授权用户 同步数据

  4.1 进入mysql-master

        docker exec -it mysql-master bash

  4.2  进入mysql内部

         4.2.1 授权root 可以远程访问(主从无关)

                  grant all  privileges on *.* to 'root‘@%' identified by 'root' with grant option;

                  flush privileges;

         4.2.2 添加用来同步的用户(授权一个用户可以访问主节点,进行日志复制)

                  GRANT REPLICATION SLAVE ON *.* to 'sync'@'%'' identified by '123456'

  4.3 查看master状态

        show master status

        docker-compose实现mysql主从复制_第1张图片

 5 配置slave 同步master数据

     1 进入 slave

        docker exec -it mysql-slave bash

     2 进入mysql内部

        2.1 授权root可以远程访问

              grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;

              flush privileges;

        2.2 连接master的配置

              change master to master_host='master的ip',master_user='sync',master_password='123456',

              master_log_file='mysql-bin.000001',master_log_pos=429,master_port=8001
    3  启动同步库

        start  slave;

    4  查看从库状态

        show slave status;

 

 

 

 

你可能感兴趣的:(docker)