两台互通的服务器使用Docker部署一主两从MySQL8.0.35

文章目录

1. 使用Docker Overlay网络(需Swarm模式)

首先,在两台服务器上创建一个 Docker 网络,确保容器可以通过该网络相互通信。

在服务器1(172.25.0.19)上:
bash 复制代码
# 在服务器1初始化Swarm
docker swarm init --advertise-addr 172.25.0.19

# 执行后会输出类似以下内容:
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.
在服务器2(172.25.0.20)上:
bash 复制代码
# 在服务器2加入Swarm
docker swarm join --token SWMTKN-1-5jq69qtjwv9iod6hep02389p6lll2sa3grcp9xatnre15iwj33-7sr5tb2e9m28hbcztpe0qf5r3 172.25.0.19:2377

# 加入成功后,会显示:
This node joined a swarm as a worker.
创建 overlay 网络(172.25.0.19):

在 Swarm 初始化后,需要在 Swarm 管理节点(即服务器1)上创建可附加的 overlay 网络,使用 --attachable 参数:

bash 复制代码
# 创建overlay网络
docker network create -d overlay --attachable mysql-cluster

2. 部署主节点

在服务器1上部署 MySQL 主节点,并对外暴露端口。

bash 复制代码
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. 部署从节点1

在服务器1上部署 MySQL 从节点1,不对外暴露端口。

bash 复制代码
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. 部署从节点2

在服务器2上部署 MySQL 从节点2,不对外暴露端口。

bash 复制代码
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. 配置主从复制

在主节点上配置从节点1和从节点2的复制。

进入主节点容器:
ini 复制代码
docker exec -it mysql-master mysql -uroot -p'8th3xY]:NA'
在主节点上授权复制用户:
mysql 复制代码
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%';
FLUSH PRIVILEGES;

在主库上修改复制用户的认证插件为 mysql_native_password(5.7版本则不用执行此步骤):

mysql 复制代码
ALTER USER 'replication'@'%' IDENTIFIED WITH mysql_native_password BY '@2X0wZY/rq';
FLUSH PRIVILEGES;
查看主节点状态:
mysql 复制代码
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)

记录 FilePosition 的值,稍后会在从节点上使用。

进入从节点1容器:
bash 复制代码
docker exec -it mysql-slave1 mysql -uroot -p'8th3xY]:NA'
在从节点1上配置复制:
msyql 复制代码
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;
进入从节点2容器:
bash 复制代码
docker exec -it mysql-slave2 mysql -uroot -p'8th3xY]:NA'
在从节点2上配置复制:
mysql 复制代码
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. 验证复制状态

在从节点1和从节点2上执行以下命令,查看复制状态:

mysql 复制代码
SHOW SLAVE STATUS\G

确保 Slave_IO_RunningSlave_SQL_Running 都为 Yes

例如:

mysql 复制代码
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)
相关推荐
智慧源点3 分钟前
服务端获取远程ip的方法
服务器·网络·tcp/ip
程序员JerrySUN24 分钟前
从底层驱动到 OpenCV:深入解析 Linux 摄像头完整技术栈
linux·服务器·开发语言·人工智能·opencv·计算机视觉
猫吃了源码25 分钟前
CentOS系统安装NFS
linux·运维·centos
ADFVBM26 分钟前
MySQL自启动失败(MySQL不能开机自启)解决方案_MySQL开机自启疑难杂症解决,适用Win11Win10
数据库·mysql
EelBarb26 分钟前
ubuntu:桌面版磁盘合并扩容
linux·运维·ubuntu
m0_7482382727 分钟前
MySQL 实验1:Windows 环境下 MySQL5.5 安装与配置
windows·mysql·adb
magrich28 分钟前
Docker usage on ubuntu
ubuntu·docker·容器
雷神乐乐33 分钟前
分布式主键生成服务
java·服务器·微服务·主键生成·数据库乐观锁
心 -1 小时前
MySQL事务
数据库·mysql
令狐少侠20111 小时前
devops-Jenkins一键部署多台实例
运维·jenkins·devops