NDB Cluster 是一种在无共享系统中启用内存数据库集群的技术。 无共享架构使系统能够使用非常便宜的硬件,并且对硬件或软件的特定要求最低。
NDB Cluster 旨在没有任何单点故障。 在无共享系统中,每个组件都应该有自己的内存和磁盘,不推荐也不支持使用网络共享、网络文件系统和 SAN 等共享存储机制。NDB Cluster 由一组称为主机的计算机组成,每台计算机运行一个或多个进程。 这些称为节点的进程可能包括 MySQL 服务器(用于访问 NDB 数据)、数据节点(用于存储数据)、一个或多个管理服务器,以及可能的其他专用数据访问程序。 NDB Cluster 中这些组件的关系如下所示:
MySQL NDB Cluster由三种节点构成,SQL节点、数据节点及管理节点。
接下来,本文使用如下方式进行集群搭建。
3台服务器:
管理节点,:10.217.35.108
SQL节点a, 数据节点a:10.217.35.109
SQL节点b, 数据节点b:10.217.35.110
在管理节点、SQL节点、数据节点、执行操作:
1.为MySQL NDB Cluster添加MySQL Yum存储库,所有节点服务器上执行:
$> mkdir -p /data/mysql
$> cd /data/mysql/
$> yum -y install wget
$> wget https://dev.mysql.com/get/mysql80-community-release-el7-6.noarch.rpm
$> rpm -Uvh mysql80-community-release-el7-6.noarch.rpm
2.选择MySQL NDB集群子库:
$> yum -y install yum-utils
$> sudo yum-config-manager --disable mysql80-community
$> sudo yum-config-manager --enable mysql-cluster-7.6-community
$> yum repolist enabled | grep mysql
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community 359
mysql-connectors-community/x86_64 MySQL Connectors Community 192
mysql-tools-community/x86_64 MySQL Tools Community 90
3. 安装MySQL NDB集群:
$> rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
3.1在管理节点执行操作:
$> yum -y install mysql-cluster-community-management-server
3.2在SQL节点执行操作:
手动安装依赖perl-Class-MethodMaker(此依赖CentOS中不存在,无法自动找到,使用的是epel el7 x86_64):
$> wget https://mirrors.aliyun.com/epel/7/x86_64/Packages/p/perl-Class-MethodMaker-2.20-1.el7.x86_64.rpm
$> yum -y install perl-Class-MethodMaker-2.20-1.el7.x86_64.rpm
安装SQL节点的组件:
关闭防火墙,关闭selinux
$> systemctl stop firewalld.service #停止firewall
$> systemctl disable firewalld.service #禁止firewall开机启动
$> setenforce 0
查询状态
$> getenforce
$> vi /etc/sysconfig/selinux
安装sql节点
$> yum -y install mysql-cluster-community-server
停止mysql节点服务,如果无法停止 需要杀死 ps -ef|grep mysql kill -9 ....
$> systemctl stop mysqld
移动数据文件到/data/mysql/data目录
$> cp -rf /var/lib/mysql /data/mysql/data/
设置数据权限,非常重要否则无法启动
$> chown -R mysql:mysql /var/lib/mysql
$> chown -R mysql:mysql /data/mysql/data/
3.3安装数据节点:
$> sudo yum -y install mysql-cluster-community-data-node
3.4 在各个sql节点,数据节点上配置my.conf
$> vi /etc/my.cnf
#-----------------------------------------------------
#添加如下内容:
[mysqld]
# Options for mysqld process:
ndbcluster # run NDB storage engine
#修改如下内容
#datadir=/var/lib/mysql
#修改为
datadir=/data/mysql/data
[mysql_cluster]
# Options for NDB Cluster processes:
ndb-connectstring=10.217.35.108 # location of management server
#-----------------------------------------------------
3.5配置管理节点
$> mkdir /var/lib/mysql-cluster
$> mkdir /data/mysql/mysql-cluster
$> cd /var/lib/mysql-cluster
$> vi config.ini
#--------------------------------------------------------------------------------------
[ndbd default]
# Options affecting ndbd processes on all data nodes:
NoOfReplicas=2 # Number of fragment replicas
DataMemory=80M # How much memory to allocate for data storage
#IndexMemory=18M # How much memory to allocate for index storage
# For DataMemory and IndexMemory, we have used the
# default values. Since the "world" database takes up
# only about 500KB, this should be more than enough for
# this example NDB Cluster setup.
# NOTE: IndexMemory is deprecated in NDB 7.6 and later; in
# these versions, resources for all data and indexes are
# allocated by DataMemory and any that are set for IndexMemory
# are added to the DataMemory resource pool
ServerPort=2202 # This the default value; however, you can use any
# port that is free for all the hosts in the cluster
# Note1: It is recommended that you do not specify the port
# number at all and simply allow the default value to be used
# instead
# Note2: The port was formerly specified using the PortNumber
# TCP parameter; this parameter is no longer available in NDB
# Cluster 7.5.
[ndb_mgmd]
# Management process options:
HostName=10.217.35.108 # Hostname or IP address of management node
DataDir=/data/mysql/mysql-cluster # Directory for management node log files
[ndbd]
# Options for data node "A":
# (one [ndbd] section per data node)
HostName=10.217.35.109 # Hostname or IP address
NodeId=2 # Node ID for this data node
DataDir=/data/mysql/data # Directory for this data node's data files
[ndbd]
# Options for data node "B":
HostName=10.217.35.110 # Hostname or IP address
NodeId=3 # Node ID for this data node
DataDir=/data/mysql/data # Directory for this data node's data files
[mysqld]
# SQL node options:
HostName=10.217.35.109 # Hostname or IP address
# (additional mysqld connections can be
# specified for this node for various
# purposes such as running ndb_restore)
NodeId=4
[mysqld]
# SQL node options:
HostName=10.217.35.110 # Hostname or IP address
# (additional mysqld connections can be
# specified for this node for various
# purposes such as running ndb_restore)
NodeId=5
#--------------------------------------------------------------------------------------
3.6 管理节点上配置
在管理节点执行操作:
打开端口1186:
$> firewall-cmd --zone=public --add-port=1186/tcp --permanent
$> firewall-cmd --zone=public --add-port=2202/tcp --permanent
$> systemctl restart firewalld.service
$> firewall-cmd --list-ports
首次启动 如果修改配置文件后重新初始化,下面命令后增加 --initial 参数
$> ndb_mgmd -f /var/lib/mysql-cluster/config.ini
在数据节点上启动,顺序不能错
$> ndbd
3.5 在sql节点启动服务
$> systemctl start mysqld.service
观察集群情况:在任意sql节点或管理节点上执行
# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: 10.217.35.108:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @10.217.35.109 (mysql-5.7.38 ndb-7.6.22, Nodegroup: 0, *)
id=3 @10.217.35.110 (mysql-5.7.38 ndb-7.6.22, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @10.217.35.108 (mysql-5.7.38 ndb-7.6.22)
[mysqld(API)] 2 node(s)
id=4 @10.217.35.109 (mysql-5.7.38 ndb-7.6.22)
id=5 @10.217.35.110 (mysql-5.7.38 ndb-7.6.22)
ndb_mgm>
看到上述显示表示集群已经正常运行
其他常用命令
关闭集群
$> ndb_mgm -e shutdown
在sql节点查看mysql默认密码
$> grep 'temporary password' /var/log/mysqld.log
常见问题
1.[Warning] NDB : Tables not available after 30 seconds. Consider increasing --ndb-wait-setup value
防火墙或selinux未关闭。
2.sql节点连接不上
关闭所有节点,重新按照顺序启动,集群节点--数据节点--sql节点
3.sql节点启动不起来
多半是目录权限问题,必要时 yum删除sql节点重新创建。
参考资料:
MySQL :: MySQL 5.7 Reference Manual :: 21.2 NDB Cluster Overview