(七) Docker 部署 MySql8.0 一主一从 高可用集群
参考并感谢
下载mysql镜像(不带tag标签则表示下载latest版本)
docker pull mysql/mysql-server
配置my.cnf 文件
my.cnf文件参考nginx的临时方案,从容器中拷贝出来
# 主库 [mysqld] log-bin=mysql-bin # [必须]启用二进制日志 server-id=1 # [必须]服务器唯一ID,默认是1,一般取IP最后一段,这里看情况分配 # 从库 [mysqld] log-bin=mysql-bin # [必须]启用二进制日志 server-id=2 # [必须]服务器唯一ID,默认是1,一般取IP最后一段,这里看情况分配
启动 mysql 主库
docker run -d \ -p 3306:3306 \ --privileged=true \ -e MYSQL_ROOT_PASSWORD="Mypwd@123456" \ --name mysql3306 \ --mount type=bind,source=/var/docker/configs/mysql/3306/my.cnf,target=/etc/my.cnf \ --mount type=bind,source=/var/docker/datas/mysql/3306,target=/var/lib/mysql \ --restart always \ mysql/mysql-server:latest
启动 mysql 从库
docker run -d \ -p 3307:3306 \ --privileged=true \ -e MYSQL_ROOT_PASSWORD="Mypwd@123456" \ --name mysql3307 \ --mount type=bind,source=/var/docker/configs/mysql/3307/my.cnf,target=/etc/my.cnf \ --mount type=bind,source=/var/docker/datas/mysql/3307,target=/var/lib/mysql \ --restart always \ mysql/mysql-server:latest
开放mysql端口并立即生效
firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --zone=public --add-port=3307/tcp --permanent firewall-cmd --reload
登录主库
docker exec -it mysql3306 bash mysql -u root -p Mypwd@123456 # 主库创建同步用户 CREATE USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY 'Mypwd@123456'; # 主库给同步用户授权 GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; # 主库创建用户并配置读写权限 CREATE USER 'madmars'@'%' IDENTIFIED WITH mysql_native_password BY 'Mypwd@123456'; GRANT ALL PRIVILEGES ON *.* TO 'madmars'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES; # 查询主库状态,并记录 File 的值和 Position 的值 SHOW MASTER STATUS;
登录从库
docker exec -it mysql3307 bash mysql -u root -p Mypwd@123456 # 配置slave (master_log_file 和 master_log_pos 是主库的file和position值) change master to master_host='172.17.0.1', master_user='repl', master_log_file='mysql-bin.000003', master_log_pos=1345, master_port=3306, master_password='Mypwd@123456'; # 启动salve START SLAVE; # 查看slave状态 SHOW SLAVE STATUS\G; # 从库用户配置只读权限 CREATE USER 'madmarsreadonly'@'%' IDENTIFIED WITH mysql_native_password BY 'Mypwd@123456'; GRANT SELECT ON *.* TO 'madmarsreadonly'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;
PS:
- 启动容器之前,需要创建好对应的宿主机的文件目录,宿主机缺少文件会导致容器启动失败
- docker环境部署,如果需要数据库持久化,必须要增加 privileged=true
《(七) Docker 部署 MySql8.0 一主一从 高可用集群》CSDN地址:https://blog.csdn.net/madmarszff/article/details/100904288
《(七) Docker 部署 MySql8.0 一主一从 高可用集群》博客园地址:https://www.cnblogs.com/godzff/p/11530713.html