这篇文章主要讲解了“docker怎么搭建读写分离环境”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“docker怎么搭建读写分离环境”吧!
# 读写分离
## 本地目录结构
/root
- /dev
- /mysql-master
- /conf.d
- docker.cnf
- mysql.cnf
- /data
- /mysql-slave0
- /conf.d
- docker.cnf
- mysql.cnf
- /data
## master 配置文件
docker.cnf
[mysqld]
skip-host-cache
skip-name-resolve
server-id = 1
log-bin = master-bin
default-storage-engine=INNODB
symbolic-links=0
binlog-ignore-db=mysql
## master
docker run -p 3308:3306 \
-v /root/dev/mysql-master/conf.d/:/etc/mysql/conf.d/ \
-v /root/dev/mysql-master/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--privileged=true \
--name mysql-master \
-d mysql \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci
## slave 配置文件
[mysqld]
skip-host-cache
skip-name-resolve
server-id = 2
read-only = 1
relay-log = slave-relay-bin
log-slave-updates = 1
skip-log-bin
symbolic-links = 0
default-storage-engine=INNODB
## slave
docker run -p 3309:3306 \
-v /root/dev/mysql-slave0/conf.d/:/etc/mysql/conf.d/ \
-v /root/dev/mysql-slave0/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--privileged=true \
--name mysql-slave0 \
--link mysql-master:mysql-master-host \
-d mysql \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci
# master 上
create user 'rep1'@'%' identified by 'rep1'
GRANT REPLICATION SLAVE ON *.* TO rep1@'%' with grant option;
flush privileges;
## 查看 file 和 position
show master status;
# slave 上
stop slave;
change master to master_host='mysql-master-host'
, master_user='rep1'
, master_password='rep1'
, master_log_file='master-bin.000003'
, master_log_pos=156;
start slave;
show slave status\G
发现
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
同步正确
# 设置写库具有读写权限,读库去写权限只有读权限
## 注: root 账号是超级账号,设置只读无效。因此需要开新账号,设置只读步骤如下:
## 创建账号
CREATE USER 'base_sa'@'%' identified by 'base_sa';
## 授权,此时去查看读库,发现账号已经同步成功
GRANT ALL PRIVILEGES ON testdatabase.* TO 'base_sa'@'%' WITH GRANT OPTION;
## 如何设置读库只读两个办法
方法1: docker.cnf 配置文件中设置 read-only = 1
方法2: 直接运行 set global read_only = 1;
感谢各位的阅读,以上就是“docker怎么搭建读写分离环境”的内容了,经过本文的学习后,相信大家对docker怎么搭建读写分离环境这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是天达云,小编将为大家推送更多相关知识点的文章,欢迎关注!