Docker key to build a cross-host MySQL master-slave replication

MySQL master-slave presentation

  • In a production environment, a single-node database is very dangerous, in case of failure of our data can not be recovered. This article describes Docker build MySQL-5.7-based cross-host data backup

Ready to work

  • First, create a cross-host overlay network, but not the junior partner Click here to view the networkdocker network ls
[root@gpu03 docker]# docker network ls
NETWORK ID          NAME                            DRIVER              SCOPE
d0f2d68382d9        bridge                          bridge              local
b519d6da2092        docker_default                  bridge              local
ec949a93ee57        host                            host                local
kvt9ibn4tua4        ingress                         overlay             swarm
a93c3b19194a        none                            null                local
1fx2olvzn3os        sg                              overlay             swarm

The bottom sgis what we based swarmcreation of overlaynetwork

  • View nodenode
[root@gpu03 docker]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
sflpfedy8d3qrbt4izkmezq2g *   gpu03               Ready               Active              Leader              19.03.2
ae2swiz3fhd2ma3yeure5s72h     sangang             Ready               Active                                  19.03.8

We can see that in addition to the gpu03node as well as sanganga node, then we create a node in gpu03 mysql-masterservice, sangang node creation mysql-slaveservice

Create a mysql-master service

  • Create several directories under the directory docker
[root@gpu03 docker]# ls
conf  data  docker-compose.yml  init-d
  • Create an environment variable file .env
[root@gpu03 docker]# vim .env
MYSQL_ROOT_PASSWORD=root
MYSQL_MASTER_SERVICE_NAME=mysql-master
MYSQL_MASTER_SERVICE_USER=sangang
MYSQL_MASTER_SERVICE_PASSWORD=sangang
MYSQL_MASTER_SERVICE_POST=3306
  • Create a my.cnf file
[root@gpu03 docker]# vim conf/my.cnf
[mysqld]
server-id=1
log-bin=mysql-bin
  • Create a file init-master.sh
[root@gpu03 docker]# vim init-d/init-master.sh
#!/bin/bash
mysql -uroot -p$MYSQL_ROOT_PASSWORD << EOF
CREATE USER '$MYSQL_MASTER_SERVICE_USER'@'%' IDENTIFIED BY '$MYSQL_MASTER_SERVICE_PASSWORD';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '$MYSQL_MASTER_SERVICE_USER'@'%';
EOF
  • Create a docker-compose.yml
[root@gpu03 docker]# vim docker-compose.yml
version: '3'
services:
  mysql-master:
    image: mysql:5.7
    container_name: mysql-master
    restart: always
    env_file:
      - .env
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - TZ=Asia/Shanghai
    ports:
      - "3339:3306"
    networks:
      - sg
    volumes:
      - ./conf/my.cnf:/etc/my.cnf
      - ./data:/var/lib/mysql
      - ./init-d/init-master.sh:/docker-entrypoint-initdb.d/1-init-master.sh

networks:
  sg:
    external: true
  • start updocker-compose up -d
  • View Logdocker logs -f mysql-master

Create a mysql-slave service

  • Create several directories under the docker directory -
[root@sangang docker]# ls
conf  data  docker-compose.yml  init-d
  • Create an environment variable file .env
[root@sangang docker]# vim .env
MYSQL_ROOT_PASSWORD=root
MYSQL_MASTER_SERVICE_NAME=mysql-master
MYSQL_MASTER_SERVICE_USER=sangang
MYSQL_MASTER_SERVICE_PASSWORD=sangang
MYSQL_MASTER_SERVICE_POST=3306
  • Create a my.cnf file
[root@sangang docker]# vim conf/my.cnf
[mysqld]
## 设置server_id,注意要唯一
server-id=101
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
  • Create a file init-salve.sh
[root@sangang docker]# vim init-d/init-slave.sh
#!/bin/bash
mysql -uroot -p$MYSQL_ROOT_PASSWORD << EOF
change master to master_host='$MYSQL_MASTER_SERVICE_NAME',master_user='$MYSQL_MASTER_SERVICE_USER', master_password='$MYSQL_MASTER_SERVICE_PASSWORD',master_port=$MYSQL_MASTER_SERVICE_POST;
start slave;
EOF
  • Create a docker-compose.yml
[root@sangang docker]# vim docker-compose.yml
version: '3'
services:
  mysql-master:
    image: mysql:5.7
    container_name: mysql-slave
    restart: always
    env_file:
      - .env
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - TZ=Asia/Shanghai
    ports:
      - "3306:3306"
    networks:
      - sg
    volumes:
      - ./conf/my.cnf:/etc/my.cnf
      - ./data:/var/lib/mysql
      - ./init-d/init-slave.sh:/docker-entrypoint-initdb.d/1-init-slave.sh

networks:
  sg:
    external: true
  • start updocker-compose up -d
  • View Logdocker logs -f mysql-slave
  • Log in to view the mysql-slave state
sangang@sangang:~/docker$ docker exec -it mysql-slave bash
root@417cbe900756:/# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-master
                  Master_User: sangang
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 316
               Relay_Log_File: edu-mysql-relay-bin.000006
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

We can see the following two properties are Yes, indicating a success

  • Slave_IO_Running: Yes
  • Slave_SQL_Running: Yes

verification

  • Login mysql-master and create a database
[root@gpu03 docker]# docker exec -it mysql-master bash
root@3f61cceebbab:/# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database video;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| video              |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

  • Log in to view the mysql-slave database
sangang@sangang:~/docker$ docker exec -it mysql-slave bash
root@417cbe900756:/# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| video              |
+--------------------+
5 rows in set (0.07 sec)

mysql> 

  • We can see mysql-slave libraries exist
  • 结束
Released two original articles · won praise 2 · Views 166

Guess you like

Origin blog.csdn.net/weixin_43972854/article/details/105224175