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

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

May 29, 2023 pm 01:55 PM
mysql docker

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

How do containerization technologies (like Docker) affect the importance of Java's platform independence? How do containerization technologies (like Docker) affect the importance of Java's platform independence? Apr 22, 2025 pm 06:49 PM

Containerization technologies such as Docker enhance rather than replace Java's platform independence. 1) Ensure consistency across environments, 2) Manage dependencies, including specific JVM versions, 3) Simplify the deployment process to make Java applications more adaptable and manageable.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Docker on Linux: Containerization for Linux Systems Docker on Linux: Containerization for Linux Systems Apr 22, 2025 am 12:03 AM

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

How does MySQL differ from Oracle? How does MySQL differ from Oracle? Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

See all articles