【单机/多机】安装 Docker swarm 步骤 + 安装PXC集群

安装pxc集群步骤

单机集群配置

第一步:拉取镜像

docker pull percona/percona-xtradb-cluster:5.7.30

第二步:自定义标签

docker tag percona/percona-xtradb-cluster:5.7.30 pxc

第三步:创建网络

docker network create --subnet=172.30.0.0/24 pxc-network 

第四步:搭建PXC[MySQL]集群

准备创建3个数据卷
docker volume create --name v1

docker volume create --name v2

docker volume create --name v3 

docker volume create  --name v1_conf_v

docker volume create  --name v2_conf_v

docker volume create  --name v3_conf_v

第五步:查看当前的ip地址

docker network inspect pxc-network

第六步:运行三个PXC容器

注意:在创建完第一个node1,需要等待一段时间,大概1分钟左右,等node1启动初始化完成,才能创建node2和node3,不然会出错,大家一定要注意哦

创建第一个node1 mysql,自定义ip地址, 必须在ip地址查看以内
docker run -d -p 3301:3306 -v v1:/var/lib/mysql -v v1_conf_v:/etc/mysql/  -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 --privileged --name=pxc-node1 --net=pxc-network --ip 172.30.0.11 pxc
过了1分钟后第一个mysql链接上了在执行下面的命令,创建第二个mysql同上
docker run -d -p 3302:3306 -v v2:/var/lib/mysql -v v2_conf_v:/etc/mysql/  -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 -e CLUSTER_JOIN=pxc-node1 --privileged --name=pxc-node2 --net=pxc-network --ip 172.30.0.12 pxc
创建第三个mysql同上命令
docker run -d -p 3303:3306 -v v3:/var/lib/mysql -v v3_conf_v:/etc/mysql/ -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 -e CLUSTER_JOIN=pxc-node1 --privileged --name=pxc-node3 --net=pxc-network --ip 172.30.0.13 pxc

多机集群pxc 配置

1. 准备三台服务器 ,内网相通,然后修改/etc/hosts

192.168.0.1  slave01
192.168.0.2  slave02
192.168.0.3  slave03

2. 下载docker容器镜像 swarm

[root@izuf65psc9rebqdd0o6sbvz ~]# docker pull swarm
Using default tag: latest
latest: Pulling from library/swarm
38e5683d7755: Pull complete 
083aff163606: Pull complete 
2064f1a73c6b: Pull complete 
Digest: sha256:2de8883e2933840ed7ee7360ea1eed314bf8aeac37c0692b9ca651630fde3b7f
Status: Downloaded newer image for swarm:latest
docker.io/library/swarm:latest

3. 初始化docker swarm

[root@izuf65psc9rebqdd0o6sbvz ~]# docker swarm init --advertise-addr  192.168.0.1
Swarm initialized: current node (o6xalvl620mxuefrux1rhjas5) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-0jgv1x8wwp0c7eb50w7eyv5hcj0nsrcx3su64ikuifghzgd36j-12iruzk75cc9yxekoi26tjnvk 192.168.0.1:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

上面命令执行后,该机器自动加入到swarm集群。这个会创建一个集群token,获取全球唯一的 token,作为集群唯一标识。后续将其他节点加入集群都会用到这个token值。

4. slave节点加入swarm集群

slave01: 192.168.0.2
slave02: 192.168.0.3
两台从节点机子依次执行上面步骤复制得join 脚本

    docker swarm join --token SWMTKN-1-0jgv1x8wwp0c7eb50w7eyv5hcj0nsrcx3su64ikuifghzgd36j-12iruzk75cc9yxekoi26tjnvk 192.168.0.1:2377

结果查看:

[root@izuf65psc9rebqdd0o6sbwz ~]# docker swarm join --token SWMTKN-1-0jgv1x8wwp0c7eb50w7eyv5hcj0nsrcx3su64ikuifghzgd36j-12iruzk75cc9yxekoi26tjnvk 192.168.0.1:2377
This node joined a swarm as a worker.

5. 查看集群列表

[root@izuf65psc9rebqdd0o6sbvz ~]# docker node list
ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
o6xalvl620mxuefrux1rhjas5 *   swarm01    Ready     Active         Leader           20.10.8
1c7hfcr870umzxrbhlqnfgkqf     swarm02    Ready     Active                          20.10.8
op30wpyz9381s9a0gguyhwxqr     swarm03    Ready     Active                          20.10.8

安装pxc集群

  1. 下载mysql 的集群镜像
[root@izbp176acsirlvx1jfu9uxz ~]# docker pull percona/percona-xtradb-cluster:5.7.30
5.7.30: Pulling from percona/percona-xtradb-cluster
524b0c1e57f8: Pull complete 
27b947d6be19: Pull complete 
26efa431217c: Pull complete 
7d4afafca533: Pull complete 
05e05cf6b007: Pull complete 
868bb5b06c86: Pull complete 
f0794e3eeb9c: Pull complete 
3a988f4f1cd9: Pull complete 
46f38e1635eb: Pull complete 
75805c1fcaed: Pull complete 
575a6d0fc78f: Pull complete 
c8bb3fa6ebc4: Pull complete 
Digest: sha256:b03a060e9261b37288a2153c78f86dcfc53367c36e1bcdcae046dd2d0b0721af
Status: Downloaded newer image for percona/percona-xtradb-cluster:5.7.30
docker.io/percona/percona-xtradb-cluster:5.7.30

  1. 给镜像修改一个标签
docker tag percona/percona-xtradb-cluster:5.7.30 pxc
  1. 准备容器网络统一,只需在master 创建网络即可:
docker network create -d  overlay --attachable pxc-network
docker run -d --restart=always -p 3306:3306 -v mysql_v:/var/lib/mysql  -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 --privileged --name=pxc-node1 --net=pxc-network pxc



docker run -d --restart=always -p 3306:3306 -v mysql_v:/var/lib/mysql  -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 -e CLUSTER_JOIN=pxc-node1 --privileged --name=pxc-node2 --net=pxc-network pxc


docker run -d --restart=always -p 3306:3306 -v mysql_v:/var/lib/mysql  -v mysql_conf_v:/etc/mysql/ -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 -e CLUSTER_JOIN=pxc-node1 --privileged --name=pxc-node3 --net=pxc-network pxc
  1. 进入容器,给mysql 授权远程访问
 GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "test.888";
 
 flush privileges;

在这里插入图片描述

ok,完毕!!!

你可能感兴趣的:(docker,mysql,docker)