[ mysql部署主从数据库 ]


Mysql部署主从数据

docker-compose.yml

version: "3.0"
services:
  mysql-master:
    hostname: mysql-master
    container_name: mysql-master 
    image: mysql:latest
    ports:
      - 3307:3306
    environment:
      - MYSQL_ROOT_PASSWORD=chenhaifeng
    volumes:
      - ./master/my.cnf:/etc/mysql/conf.d/my.cnf
      - ./master/init.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      - mynet

  mysql-slave:
    hostname: mysql-slave
    container_name: mysql-slave
    image: mysql:latest
    ports:
      - 3308:3306
    environment:
      - MYSQL_ROOT_PASSWORD=chenhaifeng
    volumes:
      - ./slave/my.cnf:/etc/mysql/conf.d/my.cnf
    networks:
      - mynet

networks:
  mynet:
    driver: bridge

master

init.sql

-- update user
alter user 'root'@'%' identified with mysql_native_password by 'chenhaifeng';
-- update user
alter user 'root'@'localhost' identified with mysql_native_password by 'chenhaifeng';
-- flush
FLUSH PRIVILEGES;

my.cnf

[mysqld]
server_id=100
log-bin=mysql-bin
read-only=0
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

查看master开始位置

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      157 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

Slave

my.cnf

[mysqld]
server_id=200
log-bin=mysql-bin
read-only=1
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

开启连接

change master to master_host='master', master_user='root', master_password='chenhaifeng', master_port=3306, master_log_file='mysql-bin.000003', master_log_pos=911, master_connect_retry=30;

start slave;  启动

show slave status \G;  查看状态