Home > Database > Mysql Tutorial > body text

What is the method for building a Mysql cluster with docker?

WBOY
Release: 2023-05-29 13:55:06
forward
1566 people have browsed it

Docker basic instructions:

Update software package

yum -y update
Copy after login

Install docker virtual machine (centos 7)

yum install -y docker
Copy after login

Run, restart, and shut down docker Virtual machine

service docker start
service docker stop
Copy after login

Search image

docker search 镜像名称
Copy after login

Download image

docker pull 镜像名称
Copy after login

View image

docker images
Copy after login

Delete image

docker rmi 镜像名称
Copy after login

Run container

docker run 启动参数 镜像名称
Copy after login

View the container list

docker ps -a
Copy after login

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

搜索:
[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 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]
Copy after login
下载:
[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
Copy after login
运行:
[root@vm_71_225_centos ~]# docker run -it --name myjava docker.io/java bash
root@25623e12b759:/# java


-i: 以交互模式运行容器,通常与 -t 同时使用;


-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
Copy after login

Install the pxc cluster (no mysql pxc cluster and replication here) The advantages and disadvantages of the cluster solution are explained. Here we choose the pxc cluster solution [multi-node backup and strong federation]):

Install the pxc image

docker pull percona/percona-xtradb-cluster
Copy after login

View the 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
Copy after login

docker.io/percona/percona-xtradb-cluster is too long, change the name:

[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
Copy after login

Create net1 network segment:

docker network create --subnet=172.18.0.0/16 net1
Copy after login

Create five data volumes (pxc cannot be accessed directly The data of the host group 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
Copy after login

Check 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"
  }
]
Copy after login

Create a 5-node pxc cluster

#创建第1个mysql节点
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
Copy after login

Wait for 2 minutes , then create a second node, and wait until the first node is instantiated before opening the second node instance, otherwise it will stop instantly

Create other nodes:

#创建第2个mysql节点
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
#创建第3个mysql节点
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
#创建第4个mysql节点
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
#创建第5个mysql节点
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
Copy after login

Test at any time Mysql node creates a database:

mysql -h 172.18.0.3 -uroot -pabc123456

mysql> create database test;
query ok, 1 row affected (0.03 sec)
Copy after login

Log in to other node databases and you can 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
Copy after login

Write haproxy configuration file

vi /home/soft/haproxy.cfg
Copy after login

The configuration file is as follows:

global
  #工作目录
  chroot /usr/local/etc/haproxy
  #日志文件,使用rsyslog服务中local5日志设备(/var/log/local5),等级info
  log 127.0.0.1 local5 info
  #守护进程运行
  daemon

defaults
  log global
  mode  http
  #日志格式
  option httplog
  #日志中不记录负载均衡的心跳检测记录
  option dontlognull
  #连接超时(毫秒)
  timeout connect 5000
  #客户端超时(毫秒)
  timeout client 50000
  #服务器超时(毫秒)
  timeout server 50000

#监控界面  
listen admin_stats
  #监控界面的访问的ip和端口
  bind 0.0.0.0:8888
  #访问协议
  mode    http
  #uri相对地址
  stats uri  /dbs
  #统计报告格式
  stats realm   global\ statistics
  #登陆帐户信息
  stats auth admin:abc123456
#数据库负载均衡
listen proxy-mysql
  #访问的ip和端口
  bind 0.0.0.0:3306 
  #网络协议
  mode tcp
  #负载均衡算法(轮询算法)
  #轮询算法:roundrobin
  #权重算法:static-rr
  #最少连接算法:leastconn
  #请求源ip算法:source 
  balance roundrobin
  #日志格式
  option tcplog
  #在mysql中创建一个没有权限的haproxy用户,密码为空。haproxy使用这个账户对mysql数据库心跳检测
  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
  #使用keepalive检测死链
  option tcpka
Copy after login

Create the first haproxy load balancing server

Copy code The code is 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
Copy after login

Check whether the startup is successful:

Visit http:// ip:4001/dbs

What is the method for building a Mysql cluster with docker?

Install keepalive to achieve double-click hot backup

The above is the detailed content of What is the method for building a Mysql cluster with docker?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template