当前位置:网站首页>Docker uses PXC to build a MySQL Cluster (mysql:5.7.24)

Docker uses PXC to build a MySQL Cluster (mysql:5.7.24)

2022-06-11 03:49:00 linmengmeng_ one thousand three hundred and fourteen

 Insert picture description here

About MySQL colony , There are two common modes :Replication Cluster architecture ( Master slave copy ) and PXC Cluster architecture

  • PXC All nodes in the cluster scheme are readable and writable ,Replication Slave node cannot write , Because master-slave synchronization is one-way , from slave Node to master Point synchronization .
  • PXC The synchronization mechanism is synchronous , This is also the fundamental reason why it can ensure strong data consistency ,Replication Synchronous mechanism is asynchronous , If it stops syncing from the node , You can still insert data into the primary node , Correct return , Cause inconsistency between master and slave data .
  • PXC It's about sacrificing performance to ensure data consistency ,Replication It's better than PXC Of . So they are not the same .PXC It's for storing important information , for example : Order 、 User information, etc .Replication For general information storage , Able to tolerate data loss , for example : The shopping cart , User behavior log, etc .
     Insert picture description here
     Insert picture description here

Record the construction here PXC The process of clustering , The selected image is percona/percona-xtradb-cluster:5.7.24,

For the original document, see :About Percona XtraDB Cluster

Docker The cluster environment is :docker-79、docker-80、docker-81 Three machines , among docker-81 For the current cluster Leader node .
 Insert picture description here

1. Pull the mirror image :

docker pull percona/percona-xtradb-cluster:5.7.24

Here you can choose according to your own needs MySQL edition , Directly modify the following version number . Different versions MySQL, Normal should be able to pull down .

2. establish docker Swarm Internal network

docker network create -d overlay --attachable pxc_network

 Insert picture description here
It must be added here –attachable Otherwise, it will run later pxc When the container , Will report a mistake :XXX not manually attachable.

docker: Error response from daemon: Could not attach to network pxc-network: rpc error: code = PermissionDenied desc = network pxc-network not manually attachable.

If it is on a single server, it needs to be based on the network card of the host , Create a subnet segment :docker network create --subnet=172.18.0.0/24 net1 When used in the back , Appoint 172.18.0.0 To 172.18.0.24 Within the scope of IP As each pxc Node IP

Here are three servers , Just create a overlay Type of network

3. Creating a data volume volume

Create data volumes once on each machine volume_81 And backup data volumes backup

docker volume create volume_81
docker volume create backup

Then you can take a look at the newly created data volume , Location on the host :docker inspect Data volume name
 Insert picture description here
among :/var/lib/docker/volumes/volume_81/_data That is to say volume_81 Volume location . In fact, it is possible not to create it manually , At run time , If the data volume doesn't exist ,docker Will automatically help us create .

stay docker-81 On the machine , start-up pxc_81 node :

docker run --privileged=true \
    --restart=always \
    --name node_81 \
    -d -p 3306:3306 \
    -v volume_81:/var/lib/mysql \
    -v backup:/data \
    -e MYSQL_ROOT_PASSWORD=abc123456\
    -e CLUSTER_NAME=PXC_CLUSTER \
    -e XTRABACKUP_PASSWORD=abc123456\
    --net=pxc_network \
    percona/percona-xtradb-cluster:5.7.24

 Insert picture description here
After the node is running normally , You can use Navicat Database linking tool access try :
 Insert picture description here
 Insert picture description here
In addition, you can see on the host MySQL Container files :
MySQL Run the file
Be sure to use Navicat After successful connection , Then install from the node , Otherwise, even if the slave node is running normally , It may not be connected .

Then run the container on the other two servers .
docker-80 machine

docker run --privileged=true \
    --restart=always \
    --name node_80 \
    -d -p 3306:3306 \
    -v volume_80:/var/lib/mysql \
    -v backup:/data \
    -e MYSQL_ROOT_PASSWORD=huauN2021 \
    -e CLUSTER_NAME=PXC_CLUSTER \
    -e CLUSTER_JOIN=node_81 \
    -e XTRABACKUP_PASSWORD=huauN2021 \
    --net=pxc_network \
    percona/percona-xtradb-cluster:5.7.24

docker-79 machine

docker run --privileged=true \
    --restart=always \
    --name node_79 \
    -d -p 3306:3306 \
    -v volume_79:/var/lib/mysql \
    -v backup:/data \
    -e MYSQL_ROOT_PASSWORD=huauN2021 \
    -e CLUSTER_NAME=PXC_CLUSTER \
    -e CLUSTER_JOIN=node_81 \
    -e XTRABACKUP_PASSWORD=huauN2021 \
    --net=pxc_network \
    percona/percona-xtradb-cluster:5.7.24

Then you can try to create a new database on any node , Other nodes will automatically synchronize the database or data .

Then you can take a look at the network Usage situation :

docker network inspect pxc_network

 Insert picture description here
You can see three nodes IP.

Although we were the first to create node81 node , And it looks like it's the master node , Other nodes join the cluster according to its name , Here you can try to stop docker-81 On the machine MySQL Containers , And then verify ,docker-79 and docker-80 above MySQL You can still keep in sync .

New database :spring_boot_plus
You can see the automatic synchronization , Then import... On a certain computer SQL Script :
 Insert picture description here
After importing successfully , You can see that the other two libraries also synchronize the data information imported above .

原网站

版权声明
本文为[linmengmeng_ one thousand three hundred and fourteen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020552520153.html