Example of how to build a Mysql cluster with docker

Example of how to build a Mysql cluster with docker

Docker basic instructions:

Update Packages

yum -y update

Install Docker virtual machine (centos 7)

yum install -y docker

Run, restart, and shut down the Docker virtual machine

service docker start
service docker stop

Search Mirror

docker search image name

Download image

docker pull image name

View Mirror

docker images

Deleting an image

docker rmi image name

Running the container

docker run startup parameter image name

View container list

docker ps -a

When we want to use the Java environment, we can do this:

search:
[root@VM_71_225_centos ~]# docker search java
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/node Node.js is a JavaScript-based platform for... 5752 [OK]
docker.io docker.io/tomcat Apache Tomcat is an open source implemen... 1891 [OK]
docker.io docker.io/java Java is a concurrent, class-based, and obj... 1745 [OK]
docker.io docker.io/openjdk OpenJDK is an open-source implemen... 1031 [OK]
download:
[root@VM_71_225_centos ~]# docker pull docker.io/java
Using default tag: latest
Trying to pull repository docker.io/library/java ...
latest: Pulling from docker.io/library/java
5040bd298390: Downloading [=> ] 1.572 MB/51.36 MB
run:
[root@VM_71_225_centos ~]# docker run -it --name myjava docker.io/java bash
root@25623e12b759:/# java


-i: Run the container in interactive mode, usually used with -t;


-t: reallocate a pseudo input terminal for the container, usually used together with -i;

Install PXC cluster (I will not explain the advantages and disadvantages of MySQL PXC cluster and replication cluster here, and choose PXC cluster solution [multi-node backup and strong association]):

Install PXC Image

docker pull percona/percona-xtradb-cluster

View local image

[root@VM_71_225_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest e38bc07ac18e 2 months ago 1.85 kB
docker.io/percona/percona-xtradb-cluster latest f1439de62087 3 months ago 413 MB
docker.io/java latest d23bdf5b1b1b 17 months ago 643 MB

docker.io/percona/percona-xtradb-cluster is too long, so rename it:

[root@VM_71_225_centos ~]# docker tag percona/percona-xtradb-cluster pxc
[root@VM_71_225_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest e38bc07ac18e 2 months ago 1.85 kB
docker.io/percona/percona-xtradb-cluster latest f1439de62087 3 months ago 413 MB
pxc latest f1439de62087 3 months ago 413 MB
docker.io/java latest d23bdf5b1b1b 17 months ago 643 MB

Create the net1 network segment:

docker network create --subnet=172.18.0.0/16 net1

Create five data volumes (pxc cannot directly access the data of the host machine, so create five docker data volumes)

docker volume create v1
docker volume create v2
docker volume create v3
docker volume create v4
docker volume create v5

View the data volume location:

[root@VM_71_225_centos code]# docker inspect v1
[
  {
    "Driver": "local",
    "Labels": {},
    "Mountpoint": "/var/lib/docker/volumes/v1/_data",
    "Name": "v1",
    "Options": {},
    "Scope": "local"
  }
]

Create a 5-node PXC cluster

#Create the first MySQL node docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=abc123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=abc123456 -v v1:/var/lib/mysql -v backup:/data --privileged --name=node1 --net=net1 --ip 172.18.0.2 pxc

Wait for 2 minutes before creating the second node. Wait until the first node is instantiated before starting the second node instance, otherwise it will stop instantly.

Create additional nodes:

#Create the second MySQL node docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=abc123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=abc123456 -e CLUSTER_JOIN=node1 -v v2:/var/lib/mysql -v backup:/data --privileged --name=node2 --net=net1 --ip 172.18.0.3 pxc
#Create the third MySQL node docker run -d -p 3308:3306 -e MYSQL_ROOT_PASSWORD=abc123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=abc123456 -e CLUSTER_JOIN=node1 -v v3:/var/lib/mysql --privileged --name=node3 --net=net1 --ip 172.18.0.4 pxc
#Create the fourth MySQL node docker run -d -p 3309:3306 -e MYSQL_ROOT_PASSWORD=abc123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=abc123456 -e CLUSTER_JOIN=node1 -v v4:/var/lib/mysql --privileged --name=node4 --net=net1 --ip 172.18.0.5 pxc
#Create the fifth MySQL node docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=abc123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=abc123456 -e CLUSTER_JOIN=node1 -v v5:/var/lib/mysql -v backup:/data --privileged --name=node5 --net=net1 --ip 172.18.0.6 pxc

Test to create a database on any MySQL node:

mysql -h 172.18.0.3 -uroot -pabc123456

mysql> create database test;
Query OK, 1 row affected (0.03 sec)

Log in to other node databases and you can see that they have been synchronized to form a simple MySQL cluster.

Install Haproxy for high availability and load balancing

Pull haproxy

docker pull haproxy

Writing Haproxy configuration file

vi /home/soft/haproxy.cfg

The configuration files are as follows:

global
  #Working directory chroot /usr/local/etc/haproxy
  #Log file, use the local5 log device (/var/log/local5) in the rsyslog service, level info
  log 127.0.0.1 local5 info
  #Daemon process running daemon
​
defaults
  log global
  mode http
  #Log format option httplog
  #Do not record the heartbeat detection record of load balancing in the log option dontlognull
  #Connection timeout (milliseconds)
  timeout connect 5000
  #Client timeout (milliseconds)
  timeout client 50000
  #Server timeout (milliseconds)
  timeout server 50000
​
#Monitoring interface listen admin_stats
  #Monitoring interface access IP and port bind 0.0.0.0:8888
  #Access protocol mode http
  #URI relative address stats uri /dbs
  #Statistics report formatstats realm Global\ statistics
  #Login account information stats auth admin:abc123456
#Database load balancing listen proxy-mysql
  #Access IP and port bind 0.0.0.0:3306 
  #Network protocol mode tcp
  #Load balancing algorithm (round robin algorithm)
  # Polling algorithm: roundrobin
  #Weight algorithm: static-rr
  # Least connection algorithm: leastconn
  #Request source IP algorithm: source 
  Balance Round Robin
  #Log format option tcplog
  #Create a haproxy user with no permissions in MySQL and set the password to empty. Haproxy uses this account to perform heartbeat detection on the MySQL database option mysql-check user haproxy
  server MySQL_1 172.18.0.2:3306 check weight 1 maxconn 2000 
  server MySQL_2 172.18.0.3:3306 check weight 1 maxconn 2000 
  server MySQL_3 172.18.0.4:3306 check weight 1 maxconn 2000 
  server MySQL_4 172.18.0.5:3306 check weight 1 maxconn 2000
  server MySQL_5 172.18.0.6:3306 check weight 1 maxconn 2000
  #Use keepalive to detect dead links option tcpka

Create the first Haproxy load balancing server

Copy the code as follows:
docker run -it -d -p 4001:8888 -p 4002:3306 -v /home/soft/haproxy:/usr/local/etc/haproxy --name h1 --privileged --net=net1 --ip 172.18.0.7 haproxy

Enter the h1 container and start Haproxy

docker exec -it h1 bash
haproxy -f /usr/local/etc/haproxy/haproxy.cfg

Check whether the startup is successful:

Visit http://ip:4001/dbs

Install keepalive to implement double-click hot standby

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of building a Mysql container + Tomcat container connection environment through Docker
  • Detailed explanation of using Docker to build externally accessible MySQL
  • The whole process of Docker building MySQL and mounting data

<<:  How to use ssh tunnel to connect to mysql server

>>:  Vue event's $event parameter = event value case

Recommend

javascript to switch by clicking on the picture

Clicking to switch pictures is very common in lif...

Example of pre-rendering method for Vue single page application

Table of contents Preface vue-cli 2.0 version vue...

HTML uses canvas to implement bullet screen function

Introduction Recently, I needed to make a barrage...

Detailed explanation of the use of find_in_set() function in MySQL

First, let’s take an example: There is a type fie...

Detailed View of Hidden Columns in MySQL

Table of contents 1. Primary key exists 2. No pri...

How to configure mysql5.6 to support IPV6 connection in Linux environment

Introduction: This article mainly introduces how ...

Detailed explanation of NodeJS modularity

Table of contents 1. Introduction 2. Main text 2....

Implementation of CSS sticky footer classic layout

What is a sticky footer layout? Our common web pa...

How to Rename Multiple Files at Once in Linux

Preface In our daily work, we often need to renam...

Solution to the conflict between two tabs navigation in HTML

Let's start with a description of the problem...