Docker容器启动Mysql,Docker实现Mysql主从,读写分离
相关内容原文地址:
博客园: 当我遇上你csy:Docker实战之MySQL主从复制
CSDN:liumiaocn:MySQL: 容器化方式启动
一、Docker文件编排
编排docker-compose-mysql-cluster.yml,安装master和slave节点:
version: '3'
services:
mysql-master:
image: mysql:5.7
container_name: mysql-master
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- "3307:3306"
volumes:
- "./mysql/master/my.cnf:/etc/my.cnf"
- "./mysql/master/data:/var/lib/mysql"
links:
- mysql-slave
mysql-slave:
image: mysql:5.7
container_name: mysql-slave
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- "3308:3306"
volumes:
- "./mysql/slave/my.cnf:/etc/my.cnf"
- "./mysql/slave/data:/var/lib/mysql"
- 配置master配置文件my.cnf
[mysqld]
# [必须]启用二进制日志
log-bin=mysql-bin
# [必须]服务器唯一ID,默认是1,一般取IP最后一段
server-id=1
## 复制过滤:也就是指定哪个数据库不用同步(mysql库一般不同步)
binlog-ignore-db=mysql
- 配置slave配置文件my.cnf
[mysqld]
# [必须]服务器唯一ID,默认是1,一般取IP最后一段
server-id=2
- 启动docker-compose,创建docker镜像文件
docker-compose -f docker-compose-mysql-cluster.yml up -d
通过docker ps可查看已启动的实例容器。
二、配置主从复制
2.1 配置master
2.2 配置slave
三、验证主从复制
3.1 master创建db
查看slave是否同步创建:
四、读写分离
MySQL主从复制是其自己的功能,实现读写分离就得依靠其他组件了,比如sharding-jdbc。但是sharding-jdbc只是实现读写分离,本身的权限控制还是需要MySQL这边来配置的。
- 配置master账户及权限
创建帐号并授予读写权限:
CREATE USER 'master'@'%' IDENTIFIED BY 'Password123';
GRANT select,insert,update,delete ON *.* TO 'master'@'%';
flush privileges;
- 配置slave账户及权限
创建帐号并授予只读权限:
use mysql;
CREATE USER 'slave'@'%' IDENTIFIED BY 'Password123';
GRANT select ON *.* TO 'slave'@'%';
FLUSH PRIVILEGES;
五、Docker启动Mysql
docker run --name mysql -d
-e MYSQL_ROOT_PASSWORD=liumiao123
-e TZ=Asia/Shanghai
-v `pwd`/mysql/data/:/var/lib/mysql
-v `pwd`/mysql/conf.d/:/etc/mysql/conf.d
-p 32011:3306 liumiaocn/mysql:5.7.16
进入mysql容器内部:
docker exec -it mysql sh
六、Docker-Compose方式
liumiaocn:mysql liumiao$ cat docker-compose.yaml
version: '2'
services:
# database service: mysql
mysql:
image: liumiaocn/mysql:5.7.16
ports:
- "32011:3306"
volumes:
- ./mysql/data/:/var/lib/mysql
- ./mysql/conf.d/:/etc/mysql/conf.d
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=liumiao123
restart: "no"
liumiaocn:mysql liumiao$
启动示例:
liumiaocn:mysql liumiao$ ls
docker-compose.yaml
liumiaocn:mysql liumiao$ docker-compose up -d
Creating network "mysql_default" with the default driver
Creating mysql_mysql_1 ... done
liumiaocn:mysql liumiao$
结果确认:
liumiaocn:mysql liumiao$ docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------
mysql_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:32011->3306/tcp
liumiaocn:mysql liumiao$
liumiaocn:mysql liumiao$ docker exec -it mysql_mysql_1 sh
# mysql -uroot -pliumiao123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.16 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>