mysql主从架构原理
MySQL通用架构方案
搭建步骤
1. docker创建俩台mysql 端口:23306 和 33306
docker run -p 23306:3306 --privileged=true --name mysql-23306
-v /D/Docker/mysql-23306/mysql:/etc/mysql
-v /D/Docker/mysql-23306/logs:/logs
-v /D/Docker/mysql-23306/data:/var/lib/mysql
-v /D/Docker/mysql-23306/mysql-files:/var/lib/mysql-files
-e MYSQL_ROOT_PASSWORD=admin -d mysql:5.7
2. 创建my.cnf,并将my.cnf拷贝到docker容器中,并重启生效
docker cp D:/Docker/mysql-23306/mysql/my.cnf mysql-23306:/etc/mysql/
配置文件内容如下:
[mysqld]
server_id = 2
log_bin=mysql-bin
replicate-wild-ignore-table=mysql.*
replicate-wild-ignore-table=sys.*
链接mysql,使用sql查看刚刚配置的server_id
show VARIABLES like '%server_id%'
3. 配置完成,开始执行sql,设置主从
- docker查看俩台mysql的ip。
λ docker inspect mysql-23306 --format='{{.NetworkSettings.IPAddress}}
'172.17.0.2
λ docker inspect mysql-33306 --format='{{.NetworkSettings.IPAddress}}
'172.17.0.3
- 注意:这里是为了后面同步做准备。要写容器内的ip地址,而不能是127.0.0.1。否则,使用
show slave status 命令查看状态的时候,Slave_IO_Runing 会一直显示Colleting。
- 在23306创建账号,刷新权限
grant replication slave on *.* to 'rep'@'%' identified by '123456';
FLUSH PRIVILEGES;
show master status;
- 在33306执行change命令,并开启同步
change命令语法:CHANGE MASTER TO MASTER_HOST='master_host_name', MASTER_USER='replication_user_name', MASTER_PASSWORD='replication_password', MASTER_LOG_FILE='recorded_log_file_name',MASTER_LOG_POS='POS';
CHANGE MASTER TO MASTER_HOST='172.17.0.2', MASTER_USER='rep', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=482;
start slave;
stop slave;
show slave status
至此,主从同步完成。
主主同步
接着我们把33306当作主库,23306当作从库,再次重复操作。
- 在33306创建账号,刷新权限
grant replication slave on *.* to 'rep'@'%' identified by '123456';
FLUSH PRIVILEGES;
show master status;
- 在23306执行change命令,并开启同步
CHANGE MASTER TO MASTER_HOST='172.17.0.3', MASTER_USER='rep', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=154;
start slave;
stop slave;
show slave status
|