在Docker上快速配置PerconaXtraDBCluster集群

版权声明:菩提本无树,明镜亦非台,本来无一物,何处惹尘埃 https://blog.csdn.net/Aria_Miazzy/article/details/84943785

在Docker上快速配置PerconaXtraDBCluster集群

 

创建Docker内部网络

# Docker创建内部网络 Create network 

 $ docker network create pxc-network 

创建多个PXC节点

# 初始化第一个节点 Init First node:  

$ docker run -d \
  -e MYSQL_ROOT_PASSWORD=root \
  -e CLUSTER_NAME=cluster1 \
  -p 3315:3306 \
  --name=node1 \
  --net=pxc-network \
  percona/percona-xtradb-cluster:5.7


#  加入第二个节点 Join the second node:

 docker run -d \
  -e MYSQL_ROOT_PASSWORD=root \ 
  -e CLUSTER_NAME=cluster1 \
  -e CLUSTER_JOIN=node1 \
  -p 3316:3306 \
  --name=node2 \
  --net=pxc-network \
  percona/percona-xtradb-cluster:5.7

#  加入第三个节点 Join the third node:

$ docker run -d \
  -e MYSQL_ROOT_PASSWORD=root \
  -e CLUSTER_NAME=cluster1 \
  -e CLUSTER_JOIN=node1 \
  -p 3317:3306 \
  --name=node3 \
  --net=pxc-network \
  percona/percona-xtradb-cluster:5.7

PS:PerconaXtraDBCluster最少要求三个节点,后续的节点可以不用再加入,但是如果您想用更多节点提供高负载,高吞吐量,就可以加入

#  加入第四个节点 Join the forth node 

$ docker run -d \
  -e MYSQL_ROOT_PASSWORD=root \
  -e CLUSTER_NAME=cluster1 \
  -e CLUSTER_JOIN=node1 \
  -p 3318:3306 \
  --name=node4 \
  --net=pxc-network \
  percona/percona-xtradb-cluster:5.7


# 加入第五个节点 Join the fifth node

$ docker run -d \
  -e MYSQL_ROOT_PASSWORD=root \
  -e CLUSTER_NAME=cluster1 \
  -e CLUSTER_JOIN=node1 \
  -p 3319:3306 \
  --name=node5 \
  --net=pxc-network \
  percona/percona-xtradb-cluster:5.7

# 加入第六个节点 Join the 6th node

 $ docker run -d \
  -e MYSQL_ROOT_PASSWORD=root \
  -e CLUSTER_NAME=cluster1 \
  -e CLUSTER_JOIN=node1 \
  -p 3320:3306 \
  --name=node6 \
  --net=pxc-network \
  percona/percona-xtradb-cluster:5.7

进入MySQL客户端创建用户

示例,进入节点1 :

$ sudo docker exec -it node1 /usr/bin/mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.19-17-57-log Percona XtraDB Cluster (GPL), Release rel17,
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql@node1>

create user 'perconausr'@'%' identified by '123456';

grant all privileges on *.* to 'perconausr'@'%' ;

flush privileges; 

 

在图形化客户端登录

# 使用Docker PS 命令查看各个节点3306端口暴露到宿主机的端口
MacdeMacBook-Pro:~ mac$ docker ps
CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                                                   NAMES
d6ae0fca296b        percona/percona-xtradb-cluster:5.7   "/entrypoint.sh "        5 seconds ago       Up 3 seconds        4567-4568/tcp, 0.0.0.0:3320->3306/tcp                   node6
4681b9047f91        percona/percona-xtradb-cluster:5.7   "/entrypoint.sh "        44 seconds ago      Up 42 seconds       4567-4568/tcp, 0.0.0.0:3319->3306/tcp                   node5
de9eec930dc2        percona/percona-xtradb-cluster:5.7   "/entrypoint.sh "        9 minutes ago       Up 9 minutes        4567-4568/tcp, 0.0.0.0:3318->3306/tcp                   node4
9fd0d786c1fb        percona/percona-xtradb-cluster:5.7   "/entrypoint.sh "        About an hour ago   Up About an hour    4567-4568/tcp, 0.0.0.0:3317->3306/tcp                   node3
692de88f91f4        percona/percona-xtradb-cluster:5.7   "/entrypoint.sh "        About an hour ago   Up About an hour    4567-4568/tcp, 0.0.0.0:3316->3306/tcp                   node2
50398c3bf454        percona/percona-xtradb-cluster:5.7   "/entrypoint.sh "        About an hour ago   Up About an hour    4567-4568/tcp, 0.0.0.0:3315->3306/tcp                   node1

通过上述信息得知,可以通过127.0.0.1:3320,127.0.0.1:3319,127.0.0.1:3318,127.0.0.1:3317,127.0.0.1:3316,127.0.0.1:3315这几个URL登录到不同的MYSQL节点,在任意一个节点上增删改查,都会同步到其他节点。

猜你喜欢

转载自blog.csdn.net/Aria_Miazzy/article/details/84943785