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

How to build Mysql Cluster with docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces docker how to build Mysql cluster, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.

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

Docker run-it-d-p 4001 privileged 8888-p 4002 home/soft/haproxy:/usr/local/etc/haproxy 3306-v / name H2-privileged-net=net1-ip 172.18.0.7 haproxy

Enter the H2 container and start Haproxy

Docker exec-it H2 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

Thank you for reading this article carefully. I hope the article "how to build a Mysql Cluster in docker" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Servers

Wechat

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

12
Report