Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of building Mysql cluster by docker?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "what is the method for docker to build a Mysql cluster". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the method for docker to build a Mysql cluster" can help you solve your doubts.

Docker basic directives:

Update software package

Yum-y update

Install the docker virtual machine (centos 7)

Yum install-y docker

Run, restart, and shut down the docker virtual machine

Service docker startservice docker stop

Search for an image

Docker search image name

Download the image

Docker pull image name

View Mirror

Docker images

Delete Mirror

Docker rmi image name

Run the container

Docker run startup parameter image name

View a list of containers

Docker ps-a

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

Search: [root@vm_71_225_centos ~] # docker search javaindex name description stars official automateddocker.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 implementa... 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 implementation o... 1031 [ok] download: [root@vm_71_225_centos ~] # docker pull docker.io/javausing default tag: latesttrying to pull repository docker.io/library/java... latest: pulling from docker.io/library/java5040bd298390: downloading [= >] 1.572 mb/51.36 mb run: [root@vm_71_225_centos ~] # docker run-it-- name myjava docker.io/java bashroot@25623e12b759:/# java-i: run the container in interactive mode Usually used in conjunction with-t -t: reassign a pseudo-input terminal to the container, usually used in conjunction with-I

Install pxc cluster (instead of explaining the advantages and disadvantages of mysql pxc cluster and replication cluster, choose pxc cluster solution [multi-node backup and strong federation]:

Install the pxc image

Docker pull percona/percona-xtradb-cluster

View local image

[root@vm_71_225_centos] # docker imagesrepository tag image id created sizedocker.io/hello-world latest e38bc07ac18e 2 months ago 1.85 kbdocker.io/percona/percona-xtradb-cluster latest f1439de62087 3 months ago 413 mbdocker.io/java latest d23bdf5b1b1b 17 months ago 643 mb

Docker.io/percona/percona-xtradb-cluster is too long. Rename it:

[root@vm_71_225_centos ~] # docker tag percona/percona-xtradb-cluster PXC [root @ vm_71_225_centos ~] # docker imagesrepository tag image id created sizedocker.io/hello-world latest e38bc07ac18e 2 months ago 1.85 kbdocker.io/percona/percona-xtradb-cluster latest f1439de62087 3 months ago 413 mbpxc latest F1439de62087 3 months ago 413 mbdocker.io/java latest d23bdf5b1b1b 17 months ago 643 mb

Create a net1 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 v1docker volume create v2docker volume create v3docker volume create v4docker volume create v5

View the 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 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

After waiting for 2 minutes, create a second node and wait for the first node to be instantiated before you can open the second node instance, otherwise it will stop instantly.

Create additional nodes:

# create the second mysql node docker run-d-p 3307mysql_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 3308mysql_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 net=net1 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 3310Suzhou 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

The test creates a database on any mysql node:

Mysql-h 172.18.0.3-uroot-pabc123456mysql > create database test;query ok, 1 row affected (0.03 sec)

Log in to other node databases and see that synchronization has been carried out to form a simple mysql cluster.

Install haproxy for high availability and load balancing

Pull haproxy

Docker pull haproxy

Write haproxy configuration file

Vi / home/soft/haproxy.cfg

The configuration file is as follows:

Global # working directory chroot / usr/local/etc/haproxy # log files, using the local5 log device in rsyslog service (/ var/log/local5) Level info log 127.0.0.1 local5 info # daemon runs daemondefaults log global mode http # Log format option httplog # Log heartbeat detection record option dontlognull # connection timeout (millisecond) timeout connect 5000 # client timeout (millisecond) timeout client 5000 server timeout (millisecond) timeout server 5000 monitoring interface listen admin_stats # monitoring interface visit Ask about ip and port bind 0.0.0.0 uri 8888 # access protocol mode http # uri relative address stats uri / dbs # Statistical report format stats realm global\ statistics # login account information stats auth admin:abc123456# database load balancer listen proxy-mysql # access ip and port bind 0.0.0.0 Drexel 3306 # network protocol mode tcp # load balancing algorithm (round Polling algorithm) # polling algorithm: roundrobin # weight algorithm: static-rr # least connection algorithm: leastconn # request Source ip algorithm: source balance roundrobin # Log format option tcplog # create an unauthorized haproxy user in mysql The password is empty. Haproxy uses this account to detect the heartbeat of the mysql database option mysql-check user haproxy server mysql_1 172.18.0.2 check weight 3306 check weight 1 maxconn 2000 server mysql_2 172.18.0.3 maxconn 3306 check weight 1 maxconn 2000 server mysql_3 172.18.0.4 option mysql-check user haproxy server mysql_1 3306 check weight 1 maxconn 2000 server mysql_4 172.18.0.5 server mysql_4 3306 check weight 1 maxconn 2000 server mysql_5 172.18.0.6 maxconn 3306 check weight 1 maxconn 2000 # use keepalive to detect dead chain option tcpka

Create the first haproxy load balancing server

The copy code is as follows:

Docker run-it-d-p 4001 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 bashhaproxy-f / usr/local/etc/haproxy/haproxy.cfg

Check to see if the startup was successful:

Visit http://ip:4001/dbs

Install keepalive to realize double-click hot standby

After reading this, the article "what is the method for docker to build Mysql clusters" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report