What is the method for building a Mysql cluster with docker?
Docker basic instructions:
Update software package
yum -y update
Install docker virtual machine (centos 7)
yum install -y docker
Run, restart, and shut down docker Virtual machine
service docker start service docker stop
Search image
docker search 镜像名称
Download image
docker pull 镜像名称
View image
docker images
Delete image
docker rmi 镜像名称
Run container
docker run 启动参数 镜像名称
View the container list
docker ps -a
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]
下载: [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
运行: [root@vm_71_225_centos ~]# docker run -it --name myjava docker.io/java bash root@25623e12b759:/# java -i: 以交互模式运行容器,通常与 -t 同时使用; -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
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
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
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
Create net1 network segment:
docker network create --subnet=172.18.0.0/16 net1
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
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" } ]
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
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
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)
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
Write haproxy configuration file
vi /home/soft/haproxy.cfg
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
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
Check whether the startup is successful:
Visit http:// ip:4001/dbs
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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.

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

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 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 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.

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.
