docker-compose下部署mycat+mysql读写分离

1,服务器环境: centos7
2,安装docker-compos

a1> sudo curl-Lhttps://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname-s)-$(uname-m)-o/usr/local/bin/docker-compose
a2>sudo chmod +x /usr/local/bin/docker-compose
3,建立docker镜像源(这里我们使用ustc)
a1> vi /etc/docker/daemon.json
a2> 加入此信息到文件
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
修改完成后重启docker服务
a3> service docker restart
4,拉取mysql和mycat镜像(此步骤可跳过 , 在启动项目时会自动拉取) mysql使用5.6 , mycat使用1.6.5-release
a1> docker pull mysql:5.6
a2> docker pull qiyue/mycat:1.6.5
5,编写yaml文件(此项我们配置一个读一个写一个mycat)

docker-compose下部署mycat+mysql读写分离
文章图片
version: '3'
services:
mysql_write:
image: mysql:5.6
ports:
- 3306:3306
networks:
db:
ipv4_address: 172.18.0.10
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- ./mysql/write/:/etc/mysql/conf.d
mysql_read:
image: mysql:5.6
ports:
- 3307:3306
networks:
db:
ipv4_address: 172.18.0.20
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- ./mysql/read/:/etc/mysql/conf.d
mycat:
image: qiyue/mycat:1.6.5
networks:
db:
ipv4_address: 172.18.0.30
ports:
- 8066:8066
- 9066:9066
depends_on:
- mysql_read
- mysql_write
volumes:
- ./mycat/conf/:/usr/local/mycat/conf/
networks:
db:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.18.0.0/24


mysql read的挂载配置文件 :
[mysqld]
server-id=2
log-bin=mysql-bin
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
sync_master_info=1
sync_relay_log=1
sync_relay_log_info=1
skip-host-cache
skip-name-resolve
[mysqldump]
quick
quote-names
max_allowed_packet=16M
#其他详细设置按自己需求设置
msyql write的配置文件:
[mysqld]
server-id=1
log-bin=mysql-bin
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=information_schema
skip-host-cache
skip-name-resolve
[mysqldump]
quick
quote-names
max_allowed_packet=16M
#其他详细设置按自己需求设置
注意 :mysql需要分配静态ip , 因为容器重启后 , 分配给容器的ip不是固定的 , 在配置主从的时候 , 需要有ip来指到主和从 ,
用docker的容器名字来映射无法成功 .
6,配置主从 在上以步骤启动完成后 :
进入mysql_write命令界面输入:
a1> grant replication slave on *.* to 'qiyue'@'172.18.0.20' identified by '123456';
a2> flush privileges;
a3> show master status;


docker-compose下部署mycat+mysql读写分离
文章图片
a4> 记住 File 和 position
a5> 不需要重启此mysql容器
进入mysql_read命令界面输入:
a1> change master to master_host='172.18.0.10',
master_port=3306,
master_user='qiyue',
master_password='123456',
master_log_file='mysql-bin.000006',
master_log_pos=120;
a2> start slave;
a3>show slave status;
a4> 查看Slave_IO_Running和Slave_SQL_Running都为yes则成功
a5> 不需要重启此mysql容器
7,配置mycat 先在server.xml里面配置一个mycat用户:

dbs
123456

配置schema.xml:




autoIncrement="true"/>


balance="3"
writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100">
select user();








【docker-compose下部署mycat+mysql读写分离】配置完成后重启mycat容器便可以看效果了 , read表的结构建为MyISAM查询更快

    推荐阅读