基于docker搭建mysql主从架构
1、创建主从数据库mysql文件夹,用于docker启动文件挂载
mkdir -p /usr/local/mysql/master1/conf
mkdir -p /usr/local/mysql/master1/data
mkdir -p /usr/local/mysql/slave1/conf
mkdir -p /usr/local/mysql/slave1/data
2、初始数据库文件配置
cd /usr/local/mysql/master1/conf
vim my.cnf
[mysqld]
datadir = /usr/local/mysql/master1/data
character-set-server = utf8
lower-case-table-names = 1
server-id = 1
log-bin=mysql-bin
binlog_format = STATEMENT
cd /usr/local/mysql/slave1/conf
vi my.cnf
[mysqld]
datadir = /usr/local/mysql/slave1/data
character-set-server = utf8
lower-case-table-names = 1
server-id = 2
relay-log = mysql-relay
chmod -R 777 /usr/local/mysql
3、docker部署mysql主从数据库
docker pull mysql:8.0
docker images
docker run --name=mysql-master-1 \
--privileged=true \
-p 8808:3306 \
-v /usr/local/mysql/master1/data/:/var/lib/mysql \
-v /usr/local/mysql/master1/conf/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/mysql/master1/mysql-files/:/var/lib/mysql-files/ \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:8.0 --lower_case_table_names=1
docker ps
docker exec -it mysql-master-1 /bin/bash
mysql -uroot -p
quit
exit
docker run --name=mysql-slave-1 \
--privileged=true \
-p 8809:3306 \
-v /usr/local/mysql/slave1/data/:/var/lib/mysql \
-v /usr/local/mysql/slave1/conf/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/mysql/slave1/mysql-files/:/var/lib/mysql-files/ \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:8.0 --lower_case_table_names=1
4、主从节点数据库配置
-- 主数据库创建用户 slave 并授权
create user 'qiyu-slave'@'%' identified with mysql_native_password by 'qiyu-81710181';
grant replication slave on *.* to 'qiyu-slave'@'%';
flush privileges;
show variables like 'server_id';
set global server_id = 1;
show master status;
reset master;
编写从数据库的复制配置文件
show variables like 'server_id';
set global server_id = 2;
stop slave;
reset slave;
change master to master_host='主机的ip',master_port=8808,master_user='qiyu-slave',master_password='qiyu-pwd',master_log_file='mysql.bin.000001',master_log_pos=156;
start slave;
stop slave;
reset slave;
start slave;
show slave status;
5、验证主从架构是否搭建成功
show slave status
- 主节点可以尝试创建数据库或者表格,查看从节点数据库是否也主动把主节点创建的内容同步,同步成功则说明主从架构搭建成功。