您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何使用Docker部署MySQL 5.7&8.0主從集群,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
鏡像版本 5.7
1、創建 overlay 網絡
docker network create --driver overlay common-network --attachable
2、編輯兩個配置文件 master.cnf 與 slave.cnf
!includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mysql.conf.d/ [mysqld] log-bin=mysql-bin server-id=1 gtid-mode=ON enforce-gtid-consistency=ON
!includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mysql.conf.d/ [mysqld] server-id=2 gtid-mode=ON enforce-gtid-consistency=ON
3、啟動 2 個 MYSQL:mysql-master 、mysql-slave
docker run -d \ --name mysql-master \ --network common-network \ -e MYSQL_ROOT_PASSWORD=Passw0rd \ -v `pwd`/master.cnf:/etc/mysql/my.cnf \ -p 3306:3306 \ -d mysql:5.7
docker run -d \ --name mysql-slave \ --network common-network \ -e MYSQL_ROOT_PASSWORD=Passw0rd \ -v `pwd`/slave.cnf:/etc/mysql/my.cnf \ -p 3307:3306 \ -d mysql:5.7
4、添加從庫用于復制的用戶
docker run -it --rm --network common-network mysql mysql -hmysql-master -uroot -pPassw0rd \ -e "CREATE USER 'repl'@'%' IDENTIFIED BY 'password' REQUIRE SSL; " \ -e "GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';"
5、連接 master & slave
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -pPassw0rd \ -e "CHANGE MASTER TO MASTER_HOST='mysql-master', MASTER_PORT=3306, MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_AUTO_POSITION=1, MASTER_SSL=1;" \ -e "START SLAVE;"
6、驗證 slave 狀態
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -pPassw0rd -e "show slave status\G"
如下狀態為正常:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
> 部署 MySQL 8.0 集群 master & slave(僅測試用)
鏡像版本 mysql:8.0
1、創建 overlay 網絡
docker network create --driver overlay common-network --attachable
2、啟動 2 個 MYSQL:mysql-master 、mysql-slave
docker run -d \ --name mysql-master \ --network common-network \ -e MYSQL_ROOT_PASSWORD=Passw0rd \ -p 3306:3306 \ -d mysql --default-authentication-plugin=mysql_native_password
docker run -d \ --name mysql-slave \ --network common-network \ -e MYSQL_ROOT_PASSWORD=Passw0rd \ -p 3307:3306 \ -d mysql --default-authentication-plugin=mysql_native_password
3、配置 master & slave
docker run -it --rm --network common-network mysql mysql -hmysql-master -uroot -pPassw0rd \ -e "SET PERSIST server_id=1;" \ -e "SET PERSIST_ONLY gtid_mode=ON;" \ -e "SET PERSIST_ONLY enforce_gtid_consistency=true; " \ -e "CREATE USER 'repl'@'%' IDENTIFIED BY 'password' REQUIRE SSL; " \ -e "GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';"
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -pPassw0rd \ -e "SET PERSIST server_id=2;" \ -e "SET PERSIST_ONLY gtid_mode=ON;" \ -e "SET PERSIST_ONLY enforce_gtid_consistency=true; "
4、重啟 master & slave
docker restart mysql-master mysql-slave
5、連接 master & slave
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -pPassw0rd \ -e "CHANGE MASTER TO MASTER_HOST='mysql-master', MASTER_PORT=3306, MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_AUTO_POSITION=1, MASTER_SSL=1;" \ -e "START SLAVE;"
6、驗證 slave 狀態
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -pPassw0rd -e "show slave status\G"
如下狀態為正常:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
> 驗證數據同步
在 master 創建數據庫 anoyi
docker run -it --rm --network common-network mysql mysql -hmysql-master -uroot -pPassw0rd \ -e "create database anoyi default character set utf8mb4 collate utf8mb4_general_ci;"
在 slave 查看數據庫列表
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -pPassw0rd \ -e "show databases;" mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | anoyi | | information_schema | | mysql | | performance_schema | | sys | +--------------------+
感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何使用Docker部署MySQL 5.7&8.0主從集群”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。