mysql-master:
image: mysql:8.0
container_name: mysql-master
environment:
# 时区上海
TZ: Asia/Shanghai
# root 密码
MYSQL_ROOT_PASSWORD: cad2024
ports:
- "3316:3306"
volumes:
# 数据挂载
- /docker/mysql_master/data/:/var/lib/mysql/
# 配置挂载
- /docker/mysql_master/conf/:/etc/mysql/conf.d/
command:
# 将mysql8.0默认密码策略 修改为 原先 策略 (mysql8.0对其默认策略做了更改 会导致密码无法匹配)
--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
privileged: true
restart: always
mysql-slave:
image: mysql:8.0
container_name: mysql-slave
environment:
# 时区上海
TZ: Asia/Shanghai
# root 密码
MYSQL_ROOT_PASSWORD: cad2024
ports:
- "3326:3306"
volumes:
# 数据挂载
- /docker/mysql-slave/data/:/var/lib/mysql/
# 配置挂载
- /docker/mysql-slave/conf/:/etc/mysql/conf.d/
command:
# 将mysql8.0默认密码策略 修改为 原先 策略 (mysql8.0对其默认策略做了更改 会导致密码无法匹配)
--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
privileged: true
restart: always
2、修改 master 基本配置
[client]default-character-set=utf8[mysql]default-character-set=utf8[mysqld]init_connect='SET collation_connection = utf8_unicode_ci'init_connect='SET NAMES utf8'character-set-server=utf8collation-server=utf8_unicode_ciskip-character-set-client-handshakeskip-name-resolve#注意:skip-name-resolve 一定要加,不然连接 mysql 会超级慢#唯一idserver_id=1#复制是通过二进制日志操作增删改,并记录执行到那个位置log-bin=mysql-bin#不止读read-only=0#需要备份的数据库模式binlog-do-db=gulimall_umsbinlog-do-db=gulimall_pmsbinlog-do-db=gulimall_omsbinlog-do-db=gulimall_smsbinlog-do-db=gulimall_wmsbinlog-do-db=gulimall_admin#不需要备份的数据库replicate-ignore-db=mysqlreplicate-ignore-db=sysreplicate-ignore-db=information_schemareplicate-ignore-db=performance_schema
3、修改 slave 基本配置
[client]default-character-set=utf8[mysql]default-character-set=utf8[mysqld]init_connect='SET collation_connection = utf8_unicode_ci'init_connect='SET NAMES utf8'character-set-server=utf8collation-server=utf8_unicode_ciskip-character-set-client-handshakeskip-name-resolveserver_id=2log-bin=mysql-bin#只读read-only=1binlog-do-db=gulimall_umsbinlog-do-db=gulimall_pmsbinlog-do-db=gulimall_omsbinlog-do-db=gulimall_smsbinlog-do-db=gulimall_wmsbinlog-do-db=gulimall_adminreplicate-ignore-db=mysqlreplicate-ignore-db=sysreplicate-ignore-db=information_schemareplicate-ignore-db=performance_schema
4、为 master 授权用户来他的同步数据
#给backup用户可以访问mysql并进行日志复制
CREATE USER 'backup'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'backup'@'%';#查看 master 状态show master status;
5、配置 slaver 同步 master 数据
1)、告诉从mysql需要同步那个主节点change master tomaster_host='117.50.198.127 ',master_user='backup',master_password='123456 ',mas ter_log_file='mysql-bin.000001',master_log_pos=0,master_port=3316;2)、启动从库同步start slave;3)、查看从库状态show slave status;