兩臺互通的服務(wù)器使用Docker部署一主兩從MySQL8.0.35的方法
1. 使用Docker Overlay網(wǎng)絡(luò)(需Swarm模式)
首先,在兩臺服務(wù)器上創(chuàng)建一個 Docker 網(wǎng)絡(luò),確保容器可以通過該網(wǎng)絡(luò)相互通信。
在服務(wù)器1(172.25.0.19)上:
# 在服務(wù)器1初始化Swarm docker swarm init --advertise-addr 172.25.0.19 # 執(zhí)行后會輸出類似以下內(nèi)容: Swarm initialized: current node (hw0h813f85fgoonyifc5lsvpx) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-5jq69qtjwv9iod6hep02389p6lll2sa3grcp9xatnre15iwj33-7sr5tb2e9m28hbcztpe0qf5r3 172.25.0.19:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
在服務(wù)器2(172.25.0.20)上:
# 在服務(wù)器2加入Swarm docker swarm join --token SWMTKN-1-5jq69qtjwv9iod6hep02389p6lll2sa3grcp9xatnre15iwj33-7sr5tb2e9m28hbcztpe0qf5r3 172.25.0.19:2377 # 加入成功后,會顯示: This node joined a swarm as a worker.
創(chuàng)建 overlay 網(wǎng)絡(luò)(172.25.0.19):
在 Swarm 初始化后,需要在 Swarm 管理節(jié)點(即服務(wù)器1)上創(chuàng)建可附加的 overlay
網(wǎng)絡(luò),使用 --attachable 參數(shù):
# 創(chuàng)建overlay網(wǎng)絡(luò) docker network create -d overlay --attachable mysql-cluster
2. 部署主節(jié)點
在服務(wù)器1上部署 MySQL 主節(jié)點,并對外暴露端口。
docker run -d \ --name mysql-master \ --network mysql-cluster \ -e MYSQL_ROOT_PASSWORD='8th3xY]:NA' \ -e MYSQL_DATABASE=test \ -e MYSQL_USER=replication \ -e MYSQL_PASSWORD='@2X0wZY/rq' \ -p 3307:3306 \ mysql:8.0.35 \ --server-id=1 \ --log-bin=mysql-bin \ --bind-address=0.0.0.0 # 允許所有IP連接
3. 部署從節(jié)點1
在服務(wù)器1上部署 MySQL 從節(jié)點1,不對外暴露端口。
docker run -d \ --name mysql-slave1 \ --network mysql-cluster \ -e MYSQL_ROOT_PASSWORD='8th3xY]:NA' \ -e MYSQL_DATABASE=test \ -e MYSQL_USER=replication \ -e MYSQL_PASSWORD='@2X0wZY/rq' \ mysql:8.0.35 \ --server-id=2 \ --log-bin=mysql-bin
4. 部署從節(jié)點2
在服務(wù)器2上部署 MySQL 從節(jié)點2,不對外暴露端口。
docker run -d \ --name mysql-slave2 \ --network mysql-cluster \ -e MYSQL_ROOT_PASSWORD='8th3xY]:NA' \ -e MYSQL_DATABASE=test \ -e MYSQL_USER=replication \ -e MYSQL_PASSWORD='@2X0wZY/rq' \ mysql:8.0.35 \ --server-id=3 \ --log-bin=mysql-bin
5. 配置主從復(fù)制
在主節(jié)點上配置從節(jié)點1和從節(jié)點2的復(fù)制。
進入主節(jié)點容器:
docker exec -it mysql-master mysql -uroot -p'8th3xY]:NA'
在主節(jié)點上授權(quán)復(fù)制用戶:
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%'; FLUSH PRIVILEGES;
在主庫上修改復(fù)制用戶的認(rèn)證插件為 mysql_native_password
(5.7版本則不用執(zhí)行此步驟):
ALTER USER 'replication'@'%' IDENTIFIED WITH mysql_native_password BY '@2X0wZY/rq'; FLUSH PRIVILEGES;
查看主節(jié)點狀態(tài):
mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000003 | 1007 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
記錄 File
和 Position
的值,稍后會在從節(jié)點上使用。
進入從節(jié)點1容器:
docker exec -it mysql-slave1 mysql -uroot -p'8th3xY]:NA'
在從節(jié)點1上配置復(fù)制:
CHANGE MASTER TO MASTER_HOST='mysql-master', MASTER_USER='replication', MASTER_PASSWORD='@2X0wZY/rq', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=1007; START SLAVE;
進入從節(jié)點2容器:
docker exec -it mysql-slave2 mysql -uroot -p'8th3xY]:NA'
在從節(jié)點2上配置復(fù)制:
CHANGE MASTER TO MASTER_HOST='mysql-master', MASTER_USER='replication', MASTER_PASSWORD='@2X0wZY/rq', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=1007; START SLAVE;
6. 驗證復(fù)制狀態(tài)
在從節(jié)點1和從節(jié)點2上執(zhí)行以下命令,查看復(fù)制狀態(tài):
SHOW SLAVE STATUS\G
確保 Slave_IO_Running
和 Slave_SQL_Running
都為 Yes
。
例如:
mysql> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for source to send event Master_Host: mysql-master Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 1007 Relay_Log_File: acd704f632bf-relay-bin.000002 Relay_Log_Pos: 326 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 1007 Relay_Log_Space: 543 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: dc75461f-f3e7-11ef-844a-02420a000202 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: 1 row in set, 1 warning (0.00 sec)
到此這篇關(guān)于兩臺互通的服務(wù)器使用Docker部署一主兩從MySQL8.0.35的方法的文章就介紹到這了,更多相關(guān)Docker部署一主兩從MySQL8.0.35內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于 Docker 搭建 Anythingllm的詳細(xì)過程
本文詳細(xì)介紹了如何在本地Windows 11企業(yè)版上使用Docker搭建和部署Anythingllm,包括Ollama和Docker的安裝步驟,以及配置和使用Anythinllm的具體方法,感興趣的朋友一起看看吧2025-03-03docker配置openGauss數(shù)據(jù)庫的方法詳解
這篇文章主要介紹了docker配置openGauss數(shù)據(jù)庫,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03使用遠(yuǎn)程Docker進行集成測試的環(huán)境搭建過程
使用docker可以幫助我們快速的搭建項目依賴環(huán)境,但是本地化的docker依賴,依然讓我們的代碼在測試時,不夠純粹,對其各個運行環(huán)境,都有本地docker安裝的要求2021-07-07