首頁  >  文章  >  資料庫  >  如何使用Docker部署MySQL5.7&8.0主從集群

如何使用Docker部署MySQL5.7&8.0主從集群

王林
王林轉載
2023-06-01 22:43:041120瀏覽

> 部署mysql 5.7 叢集master & slave (僅測試用)

鏡像版本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 叢集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部署MySQL5.7&8.0主從集群的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除