1. 程式人生 > >在Docker上快速配置PerconaXtraDBCluster叢集

在Docker上快速配置PerconaXtraDBCluster叢集

在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.
[email protected]>

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節點,在任意一個節點上增刪改查,都會同步到其他節點。