准备两台mysql服务器
这里用docker 演示
1.Docker部署mysql
docker run -p 3307:3306 --name mysql-master -v /Users/ga666666/Desktop/master/data:/var/lib/mysql -v /Users/ga666666/Desktop/master/conf:/etc/mysql
-e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
docker run -p 3308:3306 --name mysql-salve -v /Users/ga666666/Desktop/salve/data:/var/lib/mysql -v /Users/ga666666/Desktop/salve/conf:/etc/mysql
-e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
2.修改Master配置文件my.cnf
[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
## 设置server_id,同一局域网中需要唯一
server_id=101
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能
log-bin=mall-mysql-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
3.修改Master & Slave配置文件auto.cnf
默认目录在 /var/lib/mysql
# Master
[auto]
server-uuid=49932dc6-6ebf-11ed-aa9a-0242ac110003
#Salve
[auto]
server-uuid=159d0c6b-2d39-11ed-ad53-000c29c9f5d4
#保证master和salve的uuid不相同
4.Master运行SQL
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
FLUSH PRIVILEGES;
SHOW MASTER STATUS;
这两个参数下面会用到
5.Salve运行SQL
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
CHANGE MASTER TO MASTER_HOST='172.17.0.3',
MASTER_USER='slave',MASTER_PASSWORD='123456', MASTER_PORT=3306,
MASTER_LOG_FILE='mall-mysql-bin.000001',MASTER_LOG_POS=559;
start slave;
show slave status;
MASTER_HOST改成master所在的ip
如果没有启动,下面有报错原因,大多为UUID冲突
{!{data:image/webp;base64,UklGRjoWAABXRUJQVlA4WAoAAAAwAAAADwQAKwEASUNDUBgCAAAAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAFgAAAAcAHMAUgBHAEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAABvogAAOPUAAAOQWFlaIAAAAAAAAGKZAAC3hQAAGNpYWVogAAAAAAAAJKAAAA+EAAC2z3BhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABYWVogAAAAAAAA9tYAAQAAAADTLW1sdWMAAAAAAAAAAQAAAAxlblVTAAAAIAAAABwARwBvAG8AZwBsAGUAIABJAG4AYwAuACAAMgAwADEANkFMUEiYEQAAAbBA/58hO9Y8xba5erFt2zaXSY5t21jbiG2sort3bcc27832mfx/XV31/9e9eafuvtMRAVGSpLrNSCnnITsHKjDc7foDIqH/R+g/of+E/hP6T+g/of+E/hP6T+g/of+E/hP6T+g/of+E/hP6T+g/of/8/wCh/5SoVevvBTQ55l2vnNDoPbhHUiZ7PM87mLB+8/pRz/N+faFPkiZehYRDy289//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