1 NDB Cluster说明
MySQL NDB Cluster is a high-availability, high-redundancy version of MySQL adapted for the distributed computing environment. Recent NDB Cluster release series use version 7 of the NDB storage engine (also known as NDBCLUSTER) to enable running several computers with MySQL servers and other software in a cluster.
注意:MySQL NDB Cluster does not support InnoDB cluster, which must be deployed using MySQL Server 5.7 with the InnoDB storage engine as well as additional applications that are not included in the NDB Cluster distribution. MySQL Server 5.7 binaries cannot be used with MySQL NDB Cluster.
NDB Cluster is a technology that enables clustering of in-memory databases in a shared-nothing system. The shared-nothing architecture enables the system to work with very inexpensive hardware, and with a minimum of specific requirements for hardware or software.
有限资源干更多的活。
NDB Cluster is designed not to have any single point of failure. In a shared-nothing system, each component is expected to have its own memory and disk, and the use of shared storage mechanisms such as network shares, network file systems, and SANs is not recommended or supported.
NDB Cluster无单点故障,每个组件都需要拥有自己的内存和磁盘
更多内容移步官方文档:https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster.html
2 NDB Cluster限制
1. Noncompliance with SQL Syntax in NDB Cluster
2. Limits and Differences of NDB Cluster from Standard MySQL Limits
3. Limits Relating to Transaction Handling in NDB Cluster
4. NDB Cluster Error Handling
5. Limits Associated with Database Objects in NDB Cluster
6. Unsupported or Missing Features in NDB Cluster
7. Limitations Relating to Performance in NDB Cluster
8. Issues Exclusive to NDB Cluster
9. Limitations Relating to NDB Cluster Disk Data Storage
10. Limitations Relating to Multiple NDB Cluster Nodes
官方文档中都有非常详细的说明:篇幅有限,无法一一说明
https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster-limitations.html
3 NDB Cluster组件

4 搭建NDB Cluster
官方网站:https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster-install-linux.html
NDB Cluster分为存储节点,计算节点和管理节点。
官网的例子:
Node | IP Address |
Management node (mgmd) | 198.51.100.10 |
SQL node (mysqld) | 198.51.100.20 |
Data node "A" (ndbd) | 198.51.100.30 |
Data node "B" (ndbd) | 198.51.100.40 |
如图:

4.1 实验环境
4.1.1 架构
节点 | IP 地址 |
Management node (mgmd) | 198.168.1.120 |
SQL 节点1 (mysqld) | 198.168.1.121 |
SQL 节点2 (mysqld) | 198.168.1.122 |
Data 节点 "A" (ndbd) | 198.168.1.123 |
Data 节点 "B" (ndbd) | 198.168.1.124 |
我这里准备两台存储节点和两台计算节点,为了测试单点故障的。
注意:搭建 MySQL Cluster首先需要至少一个管理节点主机来实现管理功能,一个SQL节点主机来实现MySQL server功能和两个ndb节点主机实现NDB Cluster的功能。
4.2 安装方式
有四种安装方式:
l NDB Cluster编译好的二进制包
l NDB Cluster RPM包
l NDB Cluster .deb文件
l NDB Cluster源码
我们这里采用第一种安装方式,其他安装方式参考官方文档。实际上安装方法尽可能去查看官方文档,毕竟每个人的安装环境不太一样,而且做笔记过程中有可能会省略一些步骤,导致安装失败。
4.3 下载安装包
最新版本:7.5.8
https://cdn.mysql.com//Downloads/MySQL-Cluster-7.5/mysql-cluster-gpl-7.5.8-linux-glibc2.12-x86_64.tar.gz
4.4 安装SQL节点
安装非常简单,只要安装步骤来就不会有什么问题。
4.4.1 创建用户
[root@NDB-SQL1 software]# groupadd mysql
[root@NDB-SQL1 software]# useradd -g mysql -s /bin/false mysql
4.4.2 解压安装
#解压到/usr/local目录
[root@NDB-SQL1 software]#tar -C /usr/local -xzvf mysql-cluster-gpl-7.5.8-linux2.6.tar.gz
#软连接
[root@NDB-SQL1 software]# ln -s /usr/local/mysql-cluster-gpl-7.5.8-linux-glibc2.12-x86_64/ /usr/local/mysql
4.4.3 初始化
[root@NDB-SQL1 mysql]# cd /usr/local/mysql
[root@NDB-SQL1 mysql]# bin/mysqld --initialize
2017-12-13T02:03:32.877092Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-12-13T02:03:33.405760Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-12-13T02:03:33.521281Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-12-13T02:03:33.653017Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d2c2ac28-dfa9-11e7-b684-08002776b9a5.
2017-12-13T02:03:33.655089Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-12-13T02:03:33.677295Z 1 [Note] A temporary password is generated for root@localhost: -HTf9>eK>zri --记住该密码
4.4.4 创建data目录并赋权
[root@NDB-SQL1 mysql]# chown -R mysql.mysql /usr/local/mysql
4.4.5 复制相关脚本并添加服务,开启开机启动
[root@NDB-SQL1 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/
[root@NDB-SQL1 mysql]# chmod +x /etc/rc.d/init.d/mysql.server
[root@NDB-SQL1 mysql]# chkconfig --add mysql.server
[root@NDB-SQL1 mysql]# chkconfig mysql.server on
4.4.6 修改配置文件
如果没有该文件则创建一个
[root@NDB-SQL1 mysql]# vi /etc/my.cnf
#添加如下内容
[mysqld]
datadir=/usr/local/mysql/data
basedir= /usr/local/mysql
ndbcluster # 启动NDB存储引擎
[mysql_cluster]
# Options for NDB Cluster processes:
ndb-connectstring=198.168.1.120 # 管理节点地址
4.5 安装数据节点
数据节点不需要mysqld的二进制可执行文件,只需要NDB的ndbd或ndbmtd的可执行文件,这些文件都在.tar.gz文件中。只需要将这两个文件复制出来即可,其他文件可以删除。
4.5.1 创建目录
[root@NDB-DATA1 mysql]# mkdir -p /usr/local/mysql/data
[root@NDB-SQL1 mysql]# chown -R mysql.mysql /usr/local/mysql
4.5.2 复制相关可执行文件
[root@NDB-DATA1 mysql]# cp bin/ndbd /usr/local/bin/ndbd
[root@NDB-DATA1 mysql]# cp bin/ndbmtd /usr/local/bin/ndbmtd
4.5.3 赋可执行权限
[root@NDB-DATA1 mysql]# cd /usr/local/bin
[root@NDB-DATA1 mysql]# chmod +x ndb*
4.5.4 修改配置文件
#添加如下内容
[mysqld]
ndbcluster # 启动NDB存储引擎
ndb-connectstring=NDB-MGM:1186
[mysql_cluster]
ndb-connectstring=NDB-MGM:1186 # 管理节点地址
4.6 安装管理节点
同样,管理节点只需要ndb_mgmd和ndb_mgm两个可执行文件,同样也是在压缩包中。复制出来即可
4.6.1 复制相关可执行文件
[root@NDB-MGM mysql]# cp bin/ndb_mgm* /usr/local/bin
4.6.2 赋可执行权限
[root@NDB-MGM mysql]# cd /usr/local/bin
[root@NDB-MGM mysql]# chmod +x ndb*
4.6.3 修改配置文件
4.6.3.1 创建必要目录
[root@NDB-MGM mysql]# mkdir /var/lib/mysql-cluster
4.6.3.2 创建配置文件
[root@NDB-MGM mysql]# cd /var/lib/mysql-cluster
[root@NDB-MGM mysql]# vi config.ini
#添加如下内容
[ndbd default]
#配置会影响所有数据节点,而且参数是不能动态修改的
NoOfReplicas=2 --数据的备份数,至少是2,才能保证冗余
DataMemory=80M --用于存储数据的内存大小
IndexMemory=18M -用于存储索引的内存大小
ServerPort=2202
[ndb_mgmd]
# Management process options:
HostName=NDB-MGM --管理节点(可以是主机名/IP)
NodeId=1
DataDir=/var/lib/mysql-cluster
[ndbd]
# Options for data node "A":
HostName=NDB-DATA1 --数据节点(可以是主机名/IP)
NodeId=2
DataDir=/usr/local/mysql/data
[ndbd]
# Options for data node "B":
HostName=NDB-DATA2 --数据节点(可以是主机名/IP)
NodeId=3
DataDir=/usr/local/mysql/data
[mysqld]
# SQL node options:
HostName=NDB-SQL1 --SQL计算节点(可以是主机名/IP)
NodeId=4
[mysqld]
# SQL node options:
HostName=NDB-SQL2 --SQL计算节点(可以是主机名/IP)
NodeId=5
4.7 启动NDB Cluster
分别启动每个节点的进程。首先启动管理节点,然后是数据节点,最后启动SQL计算节点。
启动顺序为:管理节点->数据节点->SQL节点(很重要)
4.7.1 配置/etc/hosts
注意:所有节点都配置
如下:
192.168.1.120 NDB-MGM
192.168.1.121 NDB-SQL1
192.168.1.122 NDB-SQL2
192.168.1.123 NDB-DATA1
192.168.1.124 NDB-DATA2
4.7.2 启动管理节点
[root@NDB-MGM mysql-cluster]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini
MySQL Cluster Management Server mysql-5.7.20 ndb-7.5.8
注意:如果启动不起来,尝试将配置文件中的IP改为主机名
4.7.2.1 查看端口是否正常
[root@NDB-MGM mysql-cluster]# netstat -ntlp | grep 1186
tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 27192/ndb_mgmd
4.7.2.2 查看所有节点是否加进来
[root@NDB-MGM mysql-cluster]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 (not connected, accepting connect from NDB-DATA1)
id=3 (not connected, accepting connect from NDB-DATA2)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.120 (mysql-5.7.20 ndb-7.5.8)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from NDB-SQL1)
id=5 (not connected, accepting connect from NDB-SQL2)
可以看到SQL节点和数据节点都加进来了。由于SQL节点和数据节点还没启动,所有显示“not connected”
4.7.3 启动数据节点
#数据节点1
[root@NDB-DATA1 bin]# ndbd
2017-12-13 14:04:31 [ndbd] INFO -- Angel connected to '192.168.1.120:1186'
2017-12-13 14:04:31 [ndbd] INFO -- Angel allocated nodeid: 2
#数据节点2
[root@NDB-DATA2 bin]# ndbd
2017-12-13 14:25:48 [ndbd] INFO -- Angel connected to 'NDB-MGM:1186'
2017-12-13 14:25:48 [ndbd] INFO -- Angel allocated nodeid: 3
4.7.4 启动SQL计算节点
#SQL计算节点1
[root@NDB-SQL2 mysql]# cd /usr/local/mysql
[root@NDB-SQL1 mysql]# support-files/mysql.server start
Starting MySQL.. [ OK ]
#SQL计算节点2
[root@NDB-SQL2 mysql]# cd /usr/local/mysql
[root@NDB-SQL2 mysql]# support-files/mysql.server start
Starting MySQL.. [ OK ]
4.8 再次查看集群状态
在管理节点查看
[root@NDB-MGM ~]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.1.123 (mysql-5.7.20 ndb-7.5.8, Nodegroup: 0)
id=3 @192.168.1.124 (mysql-5.7.20 ndb-7.5.8, Nodegroup: 0, *)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.120 (mysql-5.7.20 ndb-7.5.8)
[mysqld(API)] 2 node(s)
id=4 @192.168.1.121 (mysql-5.7.20 ndb-7.5.8)
id=5 @192.168.1.122 (mysql-5.7.20 ndb-7.5.8)
可以看到五台节点都加入进来了。至此集群就搭建完毕了,下面就是测试单点故障了。
5 测试
注意:创建表的引擎类型必须是NDB或NDBCLUSTER
语法:
CREATE TABLE tbl_name (col_name column_definitions) ENGINE=NDBCLUSTER;
ALTER TABLE tbl_name ENGINE=NDBCLUSTER;
5.1 验证存储的引擎
验证其他存储引擎是否能正常使用。
5.1.1 在SQL计算节点创建测试库和表,并插入数据
在SQL节点1(192.168.1.121)上操作
mysql> create database cndba;
Query OK, 1 row affected (0.10 sec)
mysql> use cndba;
Database changed
mysql> create table sihong(id int,name varchar(50)) engine=NDB;
Query OK, 0 rows affected (0.59 sec)
mysql> insert into sihong values(1,'ruyan');
Query OK, 1 row affected (0.06 sec)
mysql> insert into sihong values(2,'wenqing');
Query OK, 1 row affected (0.00 sec)
mysql> select * from sihong;
+------+---------+
| id | name |
+------+---------+
| 2 | wenqing |
| 1 | ruyan |
+------+---------+
2 rows in set (0.00 sec)
5.1.2 在另外一个SQL节点去查看是否有数据
再另一个SQL节点2(192.168.1.122)上查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cndba |
| mysql |
| ndbinfo |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
mysql> use cndba;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from sihong;
+------+---------+
| id | name |
+------+---------+
| 2 | wenqing |
| 1 | ruyan |
+------+---------+
2 rows in set (0.00 sec)
可以看到数据都正常的。
5.1.3 将sihong表存储引擎改为MyISAM,再插入数据
还是在SQL计算节点1上操作(192.168.1.121)
mysql> alter table sihong engine=myisam;
Query OK, 2 rows affected (0.46 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into sihong values(3,'dongsheng');
Query OK, 1 row affected (0.00 sec)
mysql> insert into sihong values(4,'dongsheng');
Query OK, 1 row affected (0.00 sec)
mysql> select * from sihong;
+------+-----------+
| id | name |
+------+-----------+
| 1 | ruyan |
| 2 | wenqing |
| 3 | dongsheng |
| 4 | dongsheng |
+------+-----------+
4 rows in set (0.00 sec)
5.1.4 去另一个SQL节点查看
mysql> select * from sihong;
ERROR 1412 (HY000): Table definition has changed, please retry transaction
mysql> show tables;
Empty set (0.00 sec)
直接报错了,提示表定义已经修改了。而且没有这个表了。
5.1.5 再次把表的存储引擎改到NDB
mysql> alter table sihong engine=ndbcluster;
mysql> show tables;
+-----------------+
| Tables_in_cndba |
+-----------------+
| sihong |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from sihong;
+------+-----------+
| id | name |
+------+-----------+
| 1 | ruyan |
| 4 | dongsheng |
| 3 | dongsheng |
| 2 | wenqing |
+------+-----------+
4 rows in set (0.06 sec)
两个节点的数据保持一致。
5.2 测试单节点故障-SQL计算节点
为了测试一个SQL计算节点挂掉,是否对集群有影响。
5.2.1 把SQL计算节点1(192.168.1.121)上的mysql服务停掉
[root@NDB-SQL1 mysql]# support-files/mysql.server stop
Shutting down MySQL..... [ OK ]
5.2.2 查看集群状态
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.1.123 (mysql-5.7.20 ndb-7.5.8, Nodegroup: 0)
id=3 @192.168.1.124 (mysql-5.7.20 ndb-7.5.8, Nodegroup: 0, *)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.120 (mysql-5.7.20 ndb-7.5.8)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from NDB-SQL1)
可以看到集群已经有一个节点处于未连接状态。
5.2.3 再另一个节点上查看表的数据是否正常
mysql> select * from sihong;
+------+-----------+
| id | name |
+------+-----------+
| 3 | dongsheng |
| 2 | wenqing |
| 1 | ruyan |
| 4 | dongsheng |
+------+-----------+
4 rows in set (0.00 sec)
可以正常查询,对于应用来说就是将操作从故障节点上转移到其他正常节点上(需要程序处理,集群无法自动处理)。
5.3 测试单节点故障-数据节点
为了验证一个数据节点挂掉是否会影响集群的正常运行。
注意:这里要测试数据节点的单点故障,必须将NoOfReplicas参数值设为2及以上。否则数据只保存一份,无法达到冗余的效果。
5.3.1 停掉数据节点1(192.168.1.123)
直接kill掉ndb进程。
[root@NDB-DATA1 bin]# ps -ef|grep ndb
root 27661 1 0 14:27 ? 00:00:00 ndbd
root 27662 27661 1 14:27 ? 00:00:36 ndbd
root 27825 2227 0 15:20 pts/0 00:00:00 grep ndb
[root@NDB-DATA1 bin]# pkill -9 ndbd
[root@NDB-DATA1 bin]# ps -ef|grep ndb
root 27828 2227 0 15:20 pts/0 00:00:00 grep ndb
可以看到,进程已经被kill了。
5.3.2 查看集群状态
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 (not connected, accepting connect from NDB-DATA1)
可以看到,id=2的数据节点已经断开。
5.3.3 再SQL节点上查看表数据是否正常
mysql> select * from sihong;
+------+-----------+
| id | name |
+------+-----------+
| 3 | dongsheng |
| 2 | wenqing |
| 1 | ruyan |
| 4 | dongsheng |
+------+-----------+
4 rows in set (0.00 sec)
可以看到,可以正常访问表中的数据。
至此NDB Cluster就搭建完成了。