參考並感謝
下載mysql鏡像(不帶tag標籤則表示下載latest版本)
docker pull mysql/mysql-server配置my.cnf 文件
my.cnf文件參考nginx的臨時方案,從容器中拷貝出來html
# 主庫 [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:mysql
- 啓動容器以前,須要建立好對應的宿主機的文件目錄,宿主機缺乏文件會致使容器啓動失敗
- docker環境部署,若是須要數據庫持久化,必需要增長 privileged=true
《(七) Docker 部署 MySql8.0 一主一從 高可用集羣》CSDN地址:https://blog.csdn.net/madmarszff/article/details/100904288nginx
《(七) Docker 部署 MySql8.0 一主一從 高可用集羣》博客園地址:http://www.javashuo.com/article/p-hlfymezu-kd.htmlsql