Directorio artículo
MySQL presentación maestro-esclavo
- En un entorno de producción, una base de datos de un solo nodo es muy peligroso, en caso de fallo de nuestros datos no se pueden recuperar. En este artículo se describe la copia de seguridad de datos a través del anfitrión MySQL 5.7 basada en acoplable acumulación
preparativos
- En primer lugar, crear una red de superposición cruzada anfitrión, pero no el socio menor Haga clic aquí para ver la red
docker 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
La parte inferior sg
es lo que nos basamos swarm
creación de overlay
la red
- Ver
node
nodo
[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
Podemos ver que además del gpu03
nodo, así como sangang
un nodo, entonces se crea un nodo en gpu03 mysql-master
servicio, la creación de nodos Sangang mysql-slave
servicio
Crear un servicio de mysql-master
- Crear varios directorios bajo el directorio ventana acoplable
[root@gpu03 docker]# ls
conf data docker-compose.yml init-d
- Crear un archivo de variable de entorno .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
- Crear un fichero my.cnf
[root@gpu03 docker]# vim conf/my.cnf
[mysqld]
server-id=1
log-bin=mysql-bin
- Crear un archivo 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
- Crear una ventana acoplable-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
- comienzo
docker-compose up -d
- Ver registro
docker logs -f mysql-master
Crear un servicio de mysql-esclavo
- Crear varios directorios bajo el directorio ventana acoplable -
[root@sangang docker]# ls
conf data docker-compose.yml init-d
- Crear un archivo de variable de entorno .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
- Crear un fichero my.cnf
[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
- Crear un archivo 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
- Crear una ventana acoplable-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
- comienzo
docker-compose up -d
- Ver registro
docker logs -f mysql-slave
- Ingrese para ver el estado mysql-esclavo
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
Podemos ver las dos propiedades son Yes
, lo que indica un éxito
- Slave_IO_Running:
Yes
- Slave_SQL_Running:
Yes
verificación
- Iniciar sesión mysql-master y crear una base de datos
[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>
- Inicia sesión para ver la base de datos mysql-esclavo
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>
- Podemos ver existen bibliotecas mysql-esclavo
结束