【全栈之路】docker安装mysql5.6主从复制,shardingsphere配置读写分离准备工作
准备好docker的环境,下载镜像,
docker pull shuogesha/mysql5.6
建立好文件夹挂挂载到容器上如图
编辑master.cnf
[mysqld]
server-id=1 #为服务器设置一个独一无二的id便于区分
log-bin=mysql-bin #将mysql二进制日志取名为mysql-bin
binlog-ignore-db=mysql #不需要同步的数据库
expire_logs_days=2 #二进制日志保留2天
编辑slave.cnf
[mysqld]
server-id=2 #为服务器设置一个独一无二的id便于区分
log-bin=mysql-bin #将mysql二进制日志取名为mysql-bin
binlog-ignore-db=mysql #不需要同步的数据库i
log-slave-updates=1
expire_logs_days=2 #二进制日志保留2天
启动虚拟网络
docker network create --driver bridge --subnedocker run -d -p 13307:3306 -e MYSQL_ROOT_PASSWORD=yi -v /Users/zhaohaiyuan/Downloads/docker/mysqls/slave/data:/var/lib/mysql -v /Users/zhaohaiyuan/Downloads/docker/mysqls/slave/conf:/etc/mysql/conf.d --net mysql_net --ip 172.25.0.3 --name mysql13307 shuogesha/mysql5.6t 172.25.0.0/16 mysql_net
开始启动master和节点
docker run -d -p 13306:3306 -e MYSQL_ROOT_PASSWORD=yi -v /Users/zhaohaiyuan/Downloads/docker/mysqls/master/data:/var/lib/mysql -v /Users/zhaohaiyuan/Downloads/docker/mysqls/master/conf:/etc/mysql/conf.d --net mysql_net --ip 172.25.0.2 --name mysql13306 shuogesha/mysql5.6
docker run -d -p 13307:3306 -e MYSQL_ROOT_PASSWORD=yi -v /Users/zhaohaiyuan/Downloads/docker/mysqls/slave/data:/var/lib/mysql -v /Users/zhaohaiyuan/Downloads/docker/mysqls/slave/conf:/etc/mysql/conf.d --net mysql_net --ip 172.25.0.3 --name mysql13307 shuogesha/mysql5.6
启动以后查看docker network inspect mysql_net
[
{
"Name": "mysql_net",
"Id": "42759aa354a6b9fca822c223c4a4b077029a101011cdcc211a3504a76011385f",
"Created": "2020-03-04T00:55:51.8625357Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.25.0.0/16"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"6628b0b9465387af6cb6fb6b8838fbcb00aeb72d7c663c5c112a3db88b271872": {
"Name": "mysql13307",
"EndpointID": "20fa1e2616d0d56b0180b4755a61a62b37026c83c9a330811d63cd567b45f3cf",
"MacAddress": "02:42:ac:19:00:03",
"IPv4Address": "172.25.0.3/16",
"IPv6Address": ""
},
"9f58fafe0a745ec3899946a888e680c924f995b4bf020b37922bba4b461e0951": {
"Name": "mysql13306",
"EndpointID": "803a09dffad20154147d48f2daa96322cc0baa420554eb529f5314514a57e2a7",
"MacAddress": "02:42:ac:19:00:02",
"IPv4Address": "172.25.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
连接master或者bash进入新加同步账号
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
flush privileges; --刷新权限
查看master的状态,重要
SHOW MASTER STATUS;
连接slave,进入配置master
change master to master_host='172.25.0.2',
master_user='slave',
master_password='123456',
master_log_file='mysql-bin.000001',
master_port=3306,
master_log_pos=120;
start slave;
show slave status;
成功啦,然后找到字段查看是否有这个内容
Slave has read all relay log; waiting for the slave I/O thread to update it
然后测试在master新建一个库测试 salve会自动同步过去
当然java下面可以使用shardingsphere读写分离