主机配置 从docker仓库拉取mysql5.7
docker pull mysql:5.7
docker启动mysql部署
docker run -p 3307:3306 --name mysql-master \
-v /mydata/mysql/master/log:/var/log/mysql \
-v /mydata/mysql/master/data:/var/lib/mysql \
-v /mydata/mysql/master/conf:/etc/mysql \
-v /home/mysql/master/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
查看运行中容器状态
docker ps
docker镜像查看
docker images
配置mysql配置文件
vim /mydata/mysql/master/conf/my.cnf
[mysqld]
init_connect=SET collation_connection = utf8_unicode_ci
init_connect=SET NAMES utf8
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
server_id=1
log-bin=mysql-bin
read-only=0
binlog-do-db=wordpress
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
重启mysql容器服务
docker restart mysql-master
进入docker-mysql容器
docker exec -it mysql-master bash
进入mysql
mysql -u root -p
创建用户并赋予访问权限
CREATE USER backup IDENTIFIED WITH mysql_native_password BY '123456';
给backup用户远程访问权限
GRANT REPLICATION SLAVE ON *.* TO 'backup'@'%';
flush privileges;
显示主机id
SHOW VARIABLES LIKE 'server_id';
显示主节点状态
SHOW MASTER STATUS\G;
"""
File: mysql-bin.000001
Position: 154
Binlog_Do_DB: wordpress
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
这里的 File的值和Position的值很重要 等会要需要
"""
从机操作
从docker仓库拉取mysql5.7
docker pull mysql:5.7
docker启动mysql部署
docker run -p 3307:3306 --name mysql-slave \
-v /mydata/mysql/master/log:/var/log/mysql \
-v /mydata/mysql/master/data:/var/lib/mysql \
-v /mydata/mysql/master/conf:/etc/mysql \
-v /home/mysql/master/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
查看运行中容器状态
docker ps
docker镜像查看
docker images
配置mysql配置文件
vim /mydata/mysql/master/conf/my.cnf
[mysqld]
init_connect=SET collation_connection = utf8_unicode_ci
init_connect=SET NAMES utf8
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
server_id=2
log-bin=mysql-bin
read-only=1
binlog-do-db=wordpress
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
重启从节点mysql容器服务
docker restart mysql-slaver
进入从节点mysql容器内部
docker exec -it mysql-slave bash
mysql -uroot -p
显示主机id
SHOW VARIABLES LIKE 'server_id';
主从同步
CHANGE MASTER TO MASTER_HOST='192.168.10.3',MASTER_USER='backup',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=352,MASTER_PORT=3307;
"""
MASTER_HOST=主机ip
MASTER_USER=主机用户名
MASTER_PASSWORD=主机密码
MASTER_LOG_FILE=主机二进制文件名称
MASTER_LOG_POS=主机pos
MASTER_PORT=主机端口
"""
开始同步
START SLAVE;
显示同步状态
SHOW SLAVE STATUS\G;
主机操作 主从同步验证
CREATE TABLE tb(id int);
从机查看
show tables;
我这里是指定的wordpress库 所以两个库都需要有 也要保住两个库的数据一样不然报错会断开主从同步
|