简介 我这里模拟了一台服务器,开启了两
个mysql8的容器
,一个作为主
,一个作为从
。
Master
/root/.docker/voldata/mysql8/master
编写主容器docker-compose.yml
脚本version: '3.9'
services:
mysql8-master-ins:
image: mysql:8.0.33
container_name: mysql8-master
logging:
driver: "json-file"
options:
max-size: "100m"
deploy:
resources:
limits:
cpus: "1"
memory: 2g
restart_policy:
condition: on-failure
privileged: true
environment:
MYSQL_ROOT_HOST: '%'
MYSQL_ROOT_PASSWORD: Cn123456 #记得冒号与密码之间有个空格哈,这是yml的书写格式
TZ: "Asia/Shanghai"
MYSQL_USER: master
MYSQL_PASSWORD: Cn123456
command:
--bind-address=172.18.0.100
--default-authentication-plugin=mysql_native_password
--max_connections=400
--innodb_lock_wait_timeout=500
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--default-time-zone='+8:00'
--explicit_defaults_for_timestamp=true
--max_allowed_packet=128M
--lower_case_table_names=1
--expire-logs-days=7
ports:
- 3308:3306
volumes:
- /etc/localtime:/etc/localtime:ro
- /root/.docker/voldata/mysql8/master/data:/var/lib/mysql
- /root/.docker/voldata/mysql8/master/my.cnf:/etc/mysql/my.cnf
- /root/.docker/voldata/mysql8/master/conf:/etc/mysql/conf.d
- /root/.docker/voldata/mysql8/master/mysql-files:/var/lib/mysql-files
- /root/.docker/voldata/mysql8/master/logs:/var/log/mysql
networks:
traefik:
ipv4_address: 172.18.0.100
networks:
traefik:
external: true
编写主容器的my.cnf
[ client]
default- character- set = utf8mb4
[ mysql]
default- character- set=utf8mb4
[ mysqld]
user=mysql
pid- file= /var/run/mysqld/mysqld.pid
socket= /var/run/mysqld/mysqld.sock
datadir= /var/lib/mysql
server_id = 1
gtid_mode = on
enforce_gtid_consistency = on
default- storage- engine = INNODB
lower_case_table_names = 1
max_connections = 400
max_connect_errors = 1000
explicit_defaults_for_timestamp = true
max_allowed_packet=128M
default- time_zone='+8: 00'
interactive_timeout = 1800
wait_timeout = 1800
tmp_table_size = 134217728
max_heap_table_size = 134217728
character_set_server = utf8mb4
collation_server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
symbolic- links=0
skip_name_resolve = 1
default_authentication_plugin=mysql_native_password
secure_file_priv=/var/lib/mysql
sql_mode=STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_ENGINE_SUBSTITUTION
max_connections=1000
innodb_lock_wait_timeout = 500
log- bin = /var/lib/mysql/mysql- bin
log_slave_updates = on
binlog_format = row
expire_logs_days = 7
skip_slave_start = 1
general_log = 1
general_log_file = /var/log/mysql/access.log
log- error = /var/log/mysql/error.log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 5
log_slow_slave_statements = 1
long_query_time = 8
min_examined_row_limit = 100
启动容器cd master
docker-compose up -d
Slave
/root/.docker/voldata/mysql8/slave
编写从容器docker-compose.yml
脚本version: '3.9'
services:
mysql8-slave-ins:
image: mysql:8.0.33
container_name: mysql8-slave
logging:
driver: "json-file"
options:
max-size: "100m"
deploy:
resources:
limits:
cpus: "1"
memory: 2g
restart_policy:
condition: on-failure
privileged: true
environment:
MYSQL_ROOT_HOST: '%'
MYSQL_ROOT_PASSWORD: Cn123456 #记得冒号与密码之间有个空格哈,这是yml的书写格式
TZ: "Asia/Shanghai"
MYSQL_USER: slave
MYSQL_PASSWORD: Cn123456
command:
--bind-address=172.18.0.101
--default-authentication-plugin=mysql_native_password
--max_connections=400
--innodb_lock_wait_timeout=500
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--default-time-zone='+8:00'
--explicit_defaults_for_timestamp=true
--max_allowed_packet=128M
--lower_case_table_names=1
--expire-logs-days=7
ports:
- 3309:3306
volumes:
- /etc/localtime:/etc/localtime:ro
- /root/.docker/voldata/mysql8/slave/data:/var/lib/mysql
- /root/.docker/voldata/mysql8/slave/my.cnf:/etc/mysql/my.cnf
- /root/.docker/voldata/mysql8/slave/conf:/etc/mysql/conf.d
- /root/.docker/voldata/mysql8/slave/mysql-files:/var/lib/mysql-files
- /root/.docker/voldata/mysql8/slave/logs:/var/log/mysql
networks:
traefik:
ipv4_address: 172.18.0.101
networks:
traefik:
external: true
编写从容器的my.cnf
[ client]
default- character- set = utf8mb4
[ mysql]
default- character- set=utf8mb4
[ mysqld]
user=mysql
pid- file= /var/run/mysqld/mysqld.pid
socket= /var/run/mysqld/mysqld.sock
datadir= /var/lib/mysql
server_id = 2
gtid_mode = on
enforce_gtid_consistency = on
default- storage- engine = INNODB
lower_case_table_names = 1
max_connections = 400
max_connect_errors = 1000
explicit_defaults_for_timestamp = true
max_allowed_packet=128M
default- time_zone='+8: 00'
interactive_timeout = 1800
wait_timeout = 1800
tmp_table_size = 134217728
max_heap_table_size = 134217728
character_set_server = utf8mb4
collation_server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
symbolic- links=0
skip_name_resolve = 1
default_authentication_plugin=mysql_native_password
secure_file_priv=/var/lib/mysql
sql_mode=STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_ENGINE_SUBSTITUTION
max_connections=1000
innodb_lock_wait_timeout = 500
log- bin = /var/lib/mysql/mysql- bin
log_slave_updates = on
binlog_format = row
expire_logs_days = 7
skip_slave_start = 1
read_only = on
general_log = 1
general_log_file = /var/log/mysql/access.log
log- error = /var/log/mysql/error.log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 5
log_slow_slave_statements = 1
long_query_time = 8
min_examined_row_limit = 100
启动容器cd slave
docker-compose up -d
新建的用户赋权限 给mysql的master
账户和slave
账户权限,其他权限各自按需添加
先进入主容器 docker exec -it mysql8-master bash;
mysql -uroot -pCn123456;
GRANT ALL ON *.* TO master;
flush privileges;
从容器添加权限 docker exec -it mysql8-slave bash;
mysql -uroot -pCn123456;
GRANT REPLICATION CLIENT ON *.* TO slave;
GRANT REPLICATION SLAVE ON *.* TO slave;
GRANT SUPER ON *.* TO slave;
GRANT RELOAD ON *.* TO slave;
GRANT SELECT ON *.* TO slave;
flush privileges;
开启备份 进入从
节点的mysql,使用slave
账户连接主节点,开启备份:
docker exec -it mysql8-slave bash;
mysql -uslave -pCn123456;
CHANGE MASTER TO master_host='172.18.0.100', master_port=3306, master_user='master', master_password='Cn123456', master_auto_position=1;
START SLAVE;
查看状态 SHOW SLAVE STATUS\G