docker-compose installs mysql master-slave (multiple servers)

Please click to install docker and docker-compose

 

1. Prepare two servers 192.168.0.11 (master) 192.168.0.12 (slave) 

Configure mysql host

Directory Structure

 

 

192.168.0.11 (main) install mysql to create docker-compose.yml file of mysql

version: '3'
services:
  mysql:
    image: mysql
    restart: always
    container_name: mysql_master
    environment:
      MYSQL_ROOT_PASSWORD: 123456
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M;
    ports:
      - 13306:3306
    volumes:
      - /root/docker-compose-service/mysql/data:/var/lib/mysql
      - /root/docker-compose-service/mysql/conf.d:/etc/mysql/conf.d
      - /etc/localtime:/etc/localtime:ro

Create the mysql.cnf file under the conf.d folder

[mysqld]
log-bin = mysql-bin
server-id = 999

 

Start the mysql container docker-compose up -d

View container docker ps 

 

Explain that the container is started, enter the container to view the status of the master node

# 进入容器
docker exec -it mysql_master /bin/bash
# 链接mysql
mysql -uroot -p
#输入密码
# 指定ip
mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.0.11' IDENTIFIED BY '123456';
# 所有ip
mysql>GRANT REPLICATION SLAVE ON *.* to 'root'@'%' identified by '123456';

 Then view the status of the main container database

mysql> show master status;

Record the File and Position here, you need to use it when configuring the slave later

 

Configure the mysql slave The directory structure is the same as that of the host 

The docker-compose.yml from mysql is not the same as mysql.cnf

docker-compose.yml

version: '3'
services:
  mysql:
    image: mysql
    restart: always
    container_name: mysql_slave
    environment:
      MYSQL_ROOT_PASSWORD: 123456
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M;
    ports:
      - 13306:3306
    volumes:
      - /root/docker-compose-service/mysql/data:/var/lib/mysql
      - /root/docker-compose-service/mysql/conf.d:/etc/mysql/conf.d
      - /etc/localtime:/etc/localtime:ro

mysql.cnf

[mysqld]
log-bin = mysql-bin
server-id = 1

Start the mysql container docker-compose up -d

View container docker ps

Into the slave container  

docker exec -it mysql_slave /bin/bash

# 链接mysql
mysql -uroot -p
#输入密码
# 配置链接的主机参数
mysql> change master to
    -> master_host='192.168.0.11',
    -> master_user='root',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=156,
    -> master_port=3306,
    -> master_password='123456';
# master_host:主机的ip
# master_user:主机用户名
# master_log_file:记录的File地址
# master_log_pos:记录的postion地址
# master_port:主机的端口
# master_password:主机的登录密码
# 启动slave
mysql> start slave;

# 查看主从链接状态
mysql> show slave status;

Start mysql docker-compose up -d from the library

Navicat connects the main library and the slave library

When running sql from the library, if two yes appear, it is ok.  

Add a database to the main library, insert a record to see if there is any data in the library, if yes, everything is ok

Guess you like

Origin blog.csdn.net/qq_37557563/article/details/115346144