docker环境安装MySQL-Cluster
前言
随着计算机和信息技术的迅猛发展和普及,行业应用系统的规模迅速扩大,行业应用所产生的数据量量呈爆炸式增长,动辄达到数百TB甚至数百PB规模,已远远超出现有的传统计算技术和信息系统的处理能力,而集中式数据库面对大规模数据处理逐渐表现出其局限性,因此,人们希望寻找一种能快速处理数据和及时响应用户访问的方法,也希望对数据进行集中分析、管理和维护。这已成为现实世界的迫切需求。
分布式数据库是在集中式数据库的基础上发展起来的,是计算机技术和网络技术结合的产物。分布式数据库是指数据在物理上分布而逻辑上集中管理的数据库系统。物理上分布指的是分布式数据库的数据分布在物理位置不同并由网络连接的节点或站点上;逻辑上集中是指各数据库之间在逻辑上是一个整体,并由统一的数据库管理系统管理。不同的节点分布可以跨不同的机房、城市甚至国家。
分布式数据库的主要特点如下:
- 透明性:用户不必关心数据的逻辑分区和物理位置的分布细节,也不必关心重复副本(冗余数据)的一致性问题,同时不必关心在局部场地上数据库支持哪种数据模型。对于系统开发工程师而言,当数据从一个场地移到另一个场地时不必改写应用程序,使用起来如同一个集中式数据库。
- 数据冗余性:分布式数据库通过冗余实现系统的可靠性、可用性,并改善其性能。多个节点存储数据副本,当某一节点的数据遭到破坏时,冗余的副本可保证数据的完整性;当工作的节点受损害时,可通过心跳等机制进行切换,系统整体不被破坏。还可以通过热点数据的就近分析原则减少网络通信的消耗,加快访问速度,改善性能。
- 易于扩展性:在分布式数据库中能够方便地通过水平扩展提高系统的整体性能,也能通过垂直扩展来提供性能,扩展并不需要修改系统程序。
- 自治性:各节点上的数据由本地的DBMS管理,具有自动处理能力,完成本场地的应用或局部应用。
MySQL-Cluster 介绍
关于MySQL-Cluster网上的资料很多很全面,我这里不做一一介绍,我把相关的引用地址粘贴在这里:
博客园-你背后的那个人: MySQL集群原理详解
这里面也有一些引用大家可以做参照。
正式安装
老规矩,安装前首先明确一点,我们需要使用docker安装,所以必须准备好安装环境,以下是我自己的环境。
安装环境
环境名称 | 环境版本 | 用途 |
---|---|---|
操作系统 | Ubuntu 20.04 | docker 宿主机 |
docker | 19.03.13 | 运行docker镜像必备 |
mysql集群镜像 | mysql/mysql-cluster:latest | 用于启动集群使用 |
MySQL镜像 | mysql:5.7.34 | 用于MySQL子节点服务启动 |
集群可视化管理工具 | adminer:latest | 管理集群,监控集群 |
docker swarm | docker swarm init | 用于融入整个部署的环境 |
装系统,拉镜像也就不说了。
安装步骤
做好了上面的准备工作就可以开始安装了,由于我本地的环境还有其他的业务服务,服务中使用Docker Swarm做的集群,所以需要初始化一个Docker Swarm出来,并且需要一个网络环境,指令如下:
sudo docker swarm init # 初始化swarm
sudo docker network create --driver=overlay --subnet=10.0.4.0/16 --gateway=10.0.4.1 --attachable prod-cluster # 创建可手动连接的swarm网络
安装脚本
又因为我们使用的是docker swarm集群的方式安装MySQL-Cluster,所以我们需要一个安装的docker-compose.yml启动脚本。脚本信息如下:
version: '3.1'
services:
management1:
image: mysql/mysql-cluster
volumes:
- ./mysql.conf/my.cnf:/etc/my.cnf
- ./mysql.conf/mysql-cluster.cnf:/etc/mysql-cluster.cnf
command: ndb_mgmd
networks:
mysqlcluster:
ipv4_address: 10.0.4.2
ndb1:
image: mysql/mysql-cluster
volumes:
- ./mysql.conf/my.cnf:/etc/my.cnf
- ./mysql.conf/mysql-cluster.cnf:/etc/mysql-cluster.cnf
#command: bash -c 'sleep 40; exec ndbd'
command: ndbd
depends_on:
- "management1"
networks:
mysqlcluster:
ipv4_address: 10.0.4.3
ndb2:
image: mysql/mysql-cluster
volumes:
- ./mysql.conf/my.cnf:/etc/my.cnf
- ./mysql.conf/mysql-cluster.cnf:/etc/mysql-cluster.cnf
command: ndbd
depends_on:
- "management1"
networks:
mysqlcluster:
ipv4_address: 10.0.4.4
mysql1:
image: mysql/mysql-cluster
ports:
- "3306:3306"
volumes:
- ./mysql.conf/my.cnf:/etc/my.cnf
- ./mysql.conf/mysql-cluster.cnf:/etc/mysql-cluster.cnf
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: bdteste
MYSQL_USER: teste
MYSQL_PASSWORD: teste
MYSQL_ROOT_HOST: '%'
#command: bash -c 'sleep 60; exec mysqld'
command: mysqld
depends_on:
- "management1"
- "ndb1"
- "ndb2"
networks:
mysqlcluster:
ipv4_address: 10.0.4.10
adminer:
image: adminer
restart: always
ports:
- 8081:8080
links:
- mysql1:db
networks:
mysqlcluster:
networks:
mysqlcluster:
driver: overlay
ipam:
config:
- subnet: 10.0.4.0/16
gateway: 10.0.4.1
当然,这个脚本的地址你也可以转站到cluster-mysql的github仓库里面找来研究。
这里我把脚本中用到的两个配置文件给贴出来,省的大家去找了。my.cnf
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
[mysqld]
ndbcluster
ndb-connectstring=172.28.0.2
user=mysql
skip_name_resolve
[mysql_cluster]
ndb-connectstring=172.28.0.2
这是mysql-cluster.cnf
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
[ndbd default]
NoOfReplicas=2
DataMemory=80M
IndexMemory=18M
[ndb_mgmd]
NodeId=1
hostname=172.28.0.2
datadir=/var/lib/mysql
[ndbd]
NodeId=2
hostname=172.28.0.3
datadir=/var/lib/mysql
[ndbd]
NodeId=3
hostname=172.28.0.4
datadir=/var/lib/mysql
[mysqld]
NodeId=4
hostname=172.28.0.10
至此脚本准备完成.
安装指令
安装指令也是很简单:
sudo docker stack deploy -c docker-compose.yml mysqlCluster
使用
有没有安装成功呢?那我们得来验证一下.
使用可视化工具adminer
在脚本里我们可以看到adminer是可视化管理端服务,端口为8081,所以我们可以通过IP+端口去访问这个页面:
输入相关的登录参数就可以对集群操作了。相关的操作可以自行百度,后面我会在本文继续更新。