Create content for the master node

Before writing, we need to create a my.cnf configuration file to configure the master

[mysqld]
log_bin = mysql-bin
server_id = 10
Copy the code

Then we create the Dockerfile for the master

FROM mysql:5.7
ADD ./master/my.cnf /etc/mysql/my.cnf
Copy the code

Create the contents of the slave node

Before writing, we need to create a configuration file for the slave node my.cnf

[mysqld] log_bin = mysql-bin server_id = 11 relay_log = /var/lib/mysql/mysql-relay-bin log_slave_updates = 1 read_only =  1Copy the code

Then we create the Dockerfile for the master

FROM mysql:5.7
ADD ./slave/my.cnf /etc/mysql/my.cnf
Copy the code

Write the docker-comemess. yml file

We create such a directory structure

version: "3"
services:
  db-master:
    build: 
      context: . /
      dockerfile: master/Dockerfile
    restart: always
    environment:
      MYSQL_DATABASE: 'db'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'user'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'password'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '3306:3306'
    # Where our data will be persisted
    volumes:
      - my-db-master:/var/lib/mysql
    networks:
      - net-mysql
  
  db-slave:
    build: 
      context: . /
      dockerfile: slave/Dockerfile
    restart: always
    environment:
      MYSQL_DATABASE: 'db'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'user'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'password'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '3307:3306'
    # Where our data will be persisted
    volumes:
      - my-db-slave:/var/lib/mysql
    networks:
      - net-mysql
  
# Names our volume
volumes:
  my-db-master:
  my-db-slave: 

networks: 
  net-mysql:
    driver: bridge
Copy the code

Now we can start these two nodes with docker-compose

docker-compose up -d
Copy the code

Now we can see some of the mirror build information. After both nodes are started, we can see the status of both nodes

docker-compose ps -a
Copy the code

Let’s check the network information of these two nodes again for future use

docker network ls
Copy the code

docker inspect 62fa5033ce48
Copy the code

A master-slave configuration

After startup, enter the slave node

docker-compose exec db-slave bash
Copy the code

Enter the slave node mysql

mysql -u root -p
Copy the code

Configure the master node information on the slave node, and then set the current node to the slave node.

Mysql > CHANGE MASTER TO MASTER_HOST='192.168.64.3', MASTER_USER='root', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=0; Query OK, 0 rows affected, 2 warnings (0.11 SEC) mysql> start slave; Query OK, 0 rows affected (0.00 SEC)Copy the code

Normally, you would create a user and assign permissions to it instead of assigning root to it.The user authorization.

Now look at the state of the slave node

mysql> show slave status\G;
Copy the code

If YES is displayed, then our master/slave configuration is successful and we now create a test_DB3 database on the master node