MySQL 配置NDB集群

配置NDB集群
作为NDB集群一部分的MySQL服务器与普通的(非集群的)MySQL服务器有一个主要的区别,那就是它使用了NDB存储引擎。这个引擎有时也被称为 NDBCLUSTER,不过NDB是首选。

为避免不必要的资源分配,服务器默认配置为关闭NDB存储引擎。要启用NDB,必须修改服务器的my.cnf配置文件,或者使用–ndbcluster选项启 动服务器。这个MySQL服务器是集群的一部分,因此它也必须知道如何访问管理节点以获取集群配置数据。默认行为是在localhost上查找管理节点。但是, 如果您需要指定它的位置在其他地方,可以在my.cnf中完成,或者使用mysql客户端。在使用NDB存储引擎之前,必须保证至少有一个管理节点和 任意一个数据节点处于正常运行状态。

首先,以系统root用户执行如下命令,创建一个配置目录/var/lib/mysql-cluster,例如:

shell> mkdir /var/lib/mysql-cluster

在该目录下,创建一个名为config.ini的文件,该文件包含以下信息。根据系统需要,为HostName和DataDir替换适当的值。

# file "config.ini" - showing minimal setup consisting of 1 data node,
# 1 management server, and 3 MySQL servers.
# The empty default sections are not required, and are shown only for
# the sake of completeness.
# Data nodes must provide a hostname but MySQL Servers are not required
# to do so.
# If you don't know the hostname for your machine, use localhost.
# The DataDir parameter also has a default value, but it is recommended to
# set it explicitly.
# Note: [db], [api], and [mgm] are aliases for [ndbd], [mysqld], and [ndb_mgmd],
# respectively. [db] is deprecated and should not be used in new installations.
[ndbd default]
NoOfReplicas= 1

[mysqld default]
[ndb_mgmd default]
[tcp default]

[ndb_mgmd]
HostName= myhost.example.com

[ndbd]
HostName= myhost.example.com
DataDir= /var/lib/mysql-cluster

[mysqld]
[mysqld]
[mysqld]

现在可以启动ndb_mgmd管理服务器。默认情况下,它尝试读取当前工作目录下的config.ini文件,因此进入文件所在的目录,然后调用ndb_mgmd :

shell> cd /var/lib/mysql-cluster
shell> ndb_mgmd

然后执行命令ndbd启动单个数据节点。

shell> ndbd

默认情况下,ndbd在端口1186的localhost上查找管理服务器。

如果您从二进制tarball安装MySQL,则需要显式指定ndb_mgmd和ndbd服务器的路径。(通常,这些将在/ usr/local/mysql/bin中找到。

最后,将位置更改为MySQL数据目录(通常为/var/lib/mysql或/usr/local/mysql/ data),并确保my.cnf文件包含启用NDB存储引擎所需的选项 :

[mysqld]
ndbcluster

现在你可以像往常一样启动MySQL服务器了:

shell> mysqld_safe --user=mysql &

或者

shell>service mysql.server start

等待片刻,确保MySQL服务器正常运行。如果您看到mysql结束的通知,请检查服务器的.err文件以找出出错的地方。

如果到目前为止一切顺利,那么现在可以开始使用集群了。连接到服务器并验证开启NDBCLUSTER存储引擎:

[root@mysqld /]# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.48-ndb-7.5.36-cluster-gpl MySQL Cluster Community Server (GPL)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW ENGINES\G
*************************** 1. row ***************************
      Engine: ndbcluster
     Support: YES
     Comment: Clustered, fault-tolerant tables
Transactions: YES
          XA: NO
  Savepoints: NO
*************************** 2. row ***************************
      Engine: CSV
     Support: YES
     Comment: CSV storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 3. row ***************************
      Engine: InnoDB
     Support: DEFAULT
     Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
          XA: YES
  Savepoints: YES
*************************** 4. row ***************************
      Engine: ndbinfo
     Support: YES
     Comment: MySQL Cluster system information storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 5. row ***************************
      Engine: MyISAM
     Support: YES
     Comment: MyISAM storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 6. row ***************************
      Engine: PERFORMANCE_SCHEMA
     Support: YES
     Comment: Performance Schema
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 7. row ***************************
      Engine: ARCHIVE
     Support: YES
     Comment: Archive storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 8. row ***************************
      Engine: MRG_MYISAM
     Support: YES
     Comment: Collection of identical MyISAM tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 9. row ***************************
      Engine: FEDERATED
     Support: NO
     Comment: Federated MySQL storage engine
Transactions: NULL
          XA: NULL
  Savepoints: NULL
*************************** 10. row ***************************
      Engine: BLACKHOLE
     Support: YES
     Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 11. row ***************************
      Engine: MEMORY
     Support: YES
     Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
          XA: NO
  Savepoints: NO
11 rows in set (0.00 sec)

前面示例输出中显示的行号可能与系统中显示的行号不同,这取决于服务器的配置方式。

尝试创建一个NDBCLUSTER表:

[root@mysqld /]# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.48-ndb-7.5.36-cluster-gpl MySQL Cluster Community Server (GPL)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use jycs
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> CREATE TABLE ctest (i INT) ENGINE=NDBCLUSTER;
Query OK, 0 rows affected (0.19 sec)

mysql> SHOW CREATE TABLE ctest \G
*************************** 1. row ***************************
       Table: ctest
Create Table: CREATE TABLE `ctest` (
  `i` int(11) DEFAULT NULL
) ENGINE=ndbcluster DEFAULT CHARSET=latin1
1 row in set (0.01 sec)

mysql>

要检查节点是否正确设置,请启动管理客户端:

[root@mgmd /]# ndb_mgm
-- NDB Cluster -- Management Client --

在管理客户端中使用SHOW命令获取集群状态报告:

ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2    @10.138.130.234  (mysql-5.7.48 ndb-7.5.36, Nodegroup: 0, *)
id=3    @10.138.130.235  (mysql-5.7.48 ndb-7.5.36, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @10.138.130.232  (mysql-5.7.48 ndb-7.5.36)

[mysqld(API)]   1 node(s)
id=4    @10.138.130.233  (mysql-5.7.48 ndb-7.5.36)

至此,你已经成功设置好了一个可以工作的NDB集群。现在,你可以使用ENGINE=NDBCLUSTER或别名ENGINE=NDB创建的表在集群中存储数据。

MySQL NDB集群安全关机和重启

NDB集群安全关机和重启
要关闭集群,请在管理节点所在机器的shell中输入以下命令:

[root@mgmd /]# ndb_mgm -e shutdown
Connected to Management Server at: localhost:1186
3 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.

这里的-e选项用于从shell向ndb_mgm客户端传递命令。该命令将导致ndb_mgm、ndb_mgmd以及任何ndbd或ndbmtd进程正常终止。任何SQL节点都可 以使用mysqladmin shutdown和其他方式终止。在Windows平台上,假设您已经将SQL节点安装为Windows服务,您可以使用。NET STOP MYSQL。

[root@mysqld world-db]# service mysql.server stop
Shutting down MySQL.... SUCCESS!

要在Unix平台上重新启动集群,请运行以下命令:
.在管理主机上:

[root@mgmd /]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini --configdir=/var/lib/mysql-cluster
MySQL Cluster Management Server mysql-5.7.48 ndb-7.5.36

.在每个数据节点主机上:

[root@ndbda /]# ndbd
2025-05-23 00:43:01 [ndbd] INFO     -- Angel connected to '10.138.130.232:1186'
2025-05-23 00:43:01 [ndbd] INFO     -- Angel allocated nodeid: 2

[root@ndbdb /]# ndbd
2025-05-23 00:43:09 [ndbd] INFO     -- Angel connected to '10.138.130.232:1186'
2025-05-23 00:43:09 [ndbd] INFO     -- Angel allocated nodeid: 3

.使用ndb_mgm客户端验证两个数据节点是否已经成功启动。

[root@mgmd /]# 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    @10.138.130.234  (mysql-5.7.48 ndb-7.5.36, Nodegroup: 0, *)
id=3    @10.138.130.235  (mysql-5.7.48 ndb-7.5.36, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @10.138.130.232  (mysql-5.7.48 ndb-7.5.36)

[mysqld(API)]   1 node(s)
id=4 (not connected, accepting connect from 10.138.130.233)

.在SQL主机上:

[root@mysqld world-db]# service mysql.server start
Starting MySQL.. SUCCESS!

在Windows平台上,假设您已经将所有NDB集群进程安装为Windows服务,使用默认的服务名称可以按照如下步骤重新启动集群:
.在管理主机上,执行以下命令:

C:\> NET START ndb_mgmd

.分别在数据节点主机上执行如下命令:

C:\> NET START ndbd

.在管理节点主机上,使用ndb_mgm客户端验证管理节点和两个数据节点是否已经成功启动

.在SQL节点主机上执行如下命令:

C:\> NET START mysql

在生产环境中,通常不希望完全关闭集群。在许多情况下,即使是在进行配置更改或对集群硬件或软件(或两者)执行升级(这需要关闭单个主 机)时,也可以通过执行集群的滚动重启而不关闭整个集群。

MySQL NDB集群表和数据的示例

NDB集群表和数据的示例

在NDB Cluster中处理数据库表和数据与在标准中这样做没有太大的不同MySQL。有两个关键点需要记住:
.在集群中复制表时,必须使用NDBCLUSTER存储引擎。创建表时可以使用ENGINE=NDBCLUSTER或ENGINE=NDB选项指定:

CREATE TABLE tbl_name (col_name column_definitions) ENGINE=NDBCLUSTER;

或者,对于使用不同存储引擎的现有表,使用ALTER TABLE将表更改为使用NDBCLUSTER:

ALTER TABLE tbl_name ENGINE=NDBCLUSTER;

每个NDBCLUSTER表都有一个主键。如果在创建表时没有用户定义主键,NDBCLUSTER存储引擎会自动生成一个隐藏的主键。这样的键和其他表索引 一样会占用空间。(由于没有足够的内存容纳这些自动创建的索引,遇到问题的情况并不少见。)

如果使用mysqldump的输出从现有数据库中导入表,则可以在文本编辑器中打开SQL脚本,并将ENGINE选项添加到任何表创建语句中,或者替换任 何现有的引擎选项。假设你把world sample数据库放在另一台不支持NDB集群的MySQL服务器上,并且想导出City表:

[root@my239 world-db]# mysqldump -uroot -p123456 --add-drop-table world City > city_table.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

生成的city_table。sql文件将包含这个表创建语句(以及导入表数据所需的INSERT语句):

[root@my239 world-db]# ll
total 568
-rw-r--r--. 1 root root 179242 May 22 19:50 city_table.sql
-rw-r--r--. 1 root root 398629 May  1 06:05 world.sql
[root@my239 world-db]# more city_table.sql
-- MySQL dump 10.13  Distrib 5.7.26, for Linux (x86_64)
--
-- Host: localhost    Database: world
-- ------------------------------------------------------
-- Server version       5.7.26-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `city`
--

DROP TABLE IF EXISTS `city`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `city` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` char(35) NOT NULL DEFAULT '',
  `CountryCode` char(3) NOT NULL DEFAULT '',
  `District` char(20) NOT NULL DEFAULT '',
  `Population` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`ID`),
  KEY `CountryCode` (`CountryCode`),
  CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`)
) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `city`
INSERT INTO `city` VALUES (1,'Kabul','AFG','Kabol',1780000),(2,'Qandahar','AFG','Qandahar',237500), (3,'Herat','AFG','Herat',186800)
...............

你需要确保MySQL对该表使用NDBCLUSTER存储引擎。有两种方法可以实现这一点。其中之一是在将表导入集群数据库之前修改表的定义。以City 表为例,修改定义中的ENGINE选项,如下所示:

[root@mysqld world-db]# vi city_table.sql
-- MySQL dump 10.13  Distrib 5.7.48-ndb-7.5.36, for linux-glibc2.12 (x86_64)
--
-- Host: localhost    Database: world
-- ------------------------------------------------------
-- Server version       5.7.48-ndb-7.5.36-cluster-gpl

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `city`
--

DROP TABLE IF EXISTS `city`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `city` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` char(35) NOT NULL DEFAULT '',
  `CountryCode` char(3) NOT NULL DEFAULT '',
  `District` char(20) NOT NULL DEFAULT '',
  `Population` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`ID`),
  KEY `CountryCode` (`CountryCode`),
  CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`)
) ENGINE=NDBCLUSTER AUTO_INCREMENT=4080 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;


对于要成为集群数据库一部分的每个表的定义,必须这样做。要做到这一点,最简单的方法是在包含定义的文件上进行搜索替换,将 TYPE=engine_name或ENGINE=engine_name的所有实例替换为ENGINE=NDBCLUSTER。如果您不想修改文件,可以使用未修改的文件来创建表,然后 使用ALTER TABLE来更改它们的存储引擎。详情将在本节稍后给出。

假设你已经在集群的SQL节点上创建了一个名为world的数据库,然后可以使用mysql命令行客户端来读取city_table。Sql,并按照通常的方式创 建并填充相应的表:

[root@mysqld world-db]# mysql -uroot -p123456  world < city_table.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

非常重要的是要记住,前面的命令必须在SQL节点正在运行(在本例中,在IP地址为10.138.130.233的机器上)。

要在SQL节点上创建整个world数据库的副本,请在非集群服务器上使用mysqldump将数据库导出为名为world. SQL的文件;例如,在/tmp目录下 。然后修改表定义,并将文件导入集群的SQL节点,如下所示:

shell> mysql -uxxxx -pxxxx world < /tmp/world.sql

如果将文件保存到其他位置,请相应地调整前面的说明。

在SQL节点上运行SELECT查询与在任何其他MySQL服务器实例上运行它们没有什么不同。要在命令行中运行查询,首先需要以通常的方式登录 MySQL监视器(在Enter password:提示符中指定root密码):

[root@mysqld /]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.48-ndb-7.5.36-cluster-gpl MySQL Cluster Community Server (GPL)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

我们只是简单地使用MySQL服务器的root帐户,并假设你已经遵循了安装MySQL服务器的标准安全预防措施,包括设置一个强的root密码。

值得注意的是,集群节点在访问其他节点时不使用MySQL特权系统。设置或更改MySQL用户帐户(包括root帐户)只影响访问SQL节点的应用程序 ,而不会影响节点之间的交互。

如果在导入SQL脚本之前没有修改表定义中的ENGINE子句,那么此时应该运行以下语句:

mysql> USE world;
mysql> ALTER TABLE City ENGINE=NDBCLUSTER;
mysql> ALTER TABLE Country ENGINE=NDBCLUSTER;
mysql> ALTER TABLE CountryLanguage ENGINE=NDBCLUSTER;

选择一个数据库并对该数据库中的表运行SELECT查询也是以通常的方式完成的,就像退出MySQL Monitor一样:

mysql> use world
Database changed
mysql> SELECT Name, Population FROM city ORDER BY Population DESC LIMIT 5;
+-----------------+------------+
| Name            | Population |
+-----------------+------------+
| Mumbai (Bombay) |   10500000 |
| Seoul           |    9981619 |
| S?o Paulo       |    9968485 |
| Shanghai        |    9696300 |
| Jakarta         |    9604900 |
+-----------------+------------+
5 rows in set (0.03 sec)

mysql> \q
Bye
[root@mysqld /]#

MySQL NDB集群初始配置

NDB集群初始配置
在本节中,我们将通过创建和编辑配置文件来讨论已安装的NDB集群的手动配置。
群集节点和主机。
节点 IP地址

管理节点(mgmd)                        10.10.10.102
SQL节点(mysqld)                       10.10.10.103
数据节点A(ndbd)                       10.10.10.104
数据节点B(ndbd)                       10.10.10.105

NDB Cluster还提供了一个GUI安装程序,可用于执行配置,而无需在单独的应用程序中编辑文本文件。要了解更多信息,请参见第21.2.1 节“NDB集群”Auto-Installer”。

对于我们的四节点、四主机的NDB集群(请参阅集群节点和主机),有必要编写四个配置文件,每个节点主机一个。
.每个数据节点或SQL节点都需要一个my.cnf文件,该文件提供两条信息:一个连接字符串,告诉节点在哪里找到管理节点,另一行告诉该主机( 承载数据节点的机器)上的MySQL服务器启用NDBCLUSTER存储引擎。

.管理节点需要一个config.ini文件,告诉它要维护多少个副本、为每个数据节点上的数据和索引分配多少内存、在哪里找到数据节点、在哪里 将数据保存到每个数据节点的磁盘上,以及在哪里找到SQL节点。

配置数据节点和SQL节点。
数据节点所需的my.cnf文件相当简单。配置文件应该位于/etc目录下,可以使用任何文本编辑器进行编辑。(如果该文件不存在,则创建该文件 。)例如:

shell> vi /etc/my.cnf

对于我们的示例设置中的每个数据节点和SQL节点,my.cnf应该是这样的:

[mysqld]
# Options for mysqld process:
ndbcluster # run NDB storage engine

[mysql_cluster]
# Options for NDB Cluster processes:
ndb-connectstring=10.10.10.102 # location of management server

输入上述信息后,保存该文件并退出文本编辑器。对承载数据节点“A”、数据节点“B”和SQL节点的机器执行此操作。
SQL节点:

[root@mysqld /]# vi /etc/my.cnf
[mysqld]
# Options for mysqld process:
ndbcluster # run NDB storage engine

[mysql_cluster]
# Options for NDB Cluster processes:
ndb-connectstring=10.10.10.102 # location of management server

数据节点A:

[root@ndbda /]# vi /etc/my.cnf
[mysqld]
# Options for mysqld process:
ndbcluster # run NDB storage engine

[mysql_cluster]
# Options for NDB Cluster processes:
ndb-connectstring=10.10.10.102 # location of management server

数据节点B:

[root@ndbdb /]# vi /etc/my.cnf
[mysqld]
# Options for mysqld process:
ndbcluster # run NDB storage engine

[mysql_cluster]
# Options for NDB Cluster processes:
ndb-connectstring=10.10.10.102 # location of management server

如前所示,一旦你用my.cnf文件的[mysqld]和[mysql_cluster]部分中的ndbcluster和ndbconnectstring参数启动了mysqld进程,你就不能在没 有实际启动集群之前执行任何CREATE TABLE或ALTER TABLE语句。否则,这些语句将失败并报错。这是设计的结果。

配置管理节点。
配置管理节点的第一步是创建可以找到配置文件的目录,然后创建文件本身。例如(以root身份运行):

[root@mgmd /]# mkdir /var/lib/mysql-cluster
[root@mgmd /]# cd /var/lib/mysql-cluster

对于我们的典型设置,config.ini文件应该如下所示:

[root@mgmd mysql-cluster]# vi config.ini
[ndbd default]
# Options affecting ndbd processes on all data nodes:
NoOfReplicas=2      # Number of replicas
DataMemory=1024M    # How much memory to allocate for data storage
IndexMemory=256M    # 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.
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.10.10.102            # Hostname or IP address of MGM node
DataDir=/var/lib/mysql-cluster     # Directory for MGM node log files
[ndbd]
# Options for data node "A":
HostName=10.10.10.104            # Hostname or IP address
NodeId=2                           # Node ID for this data node
DataDir=/usr/local/mysql/data      # Directory for this data node's data files
[ndbd]
# Options for data node "B":
HostName=10.10.10.105            # Hostname or IP address
NodeId=3                           # Node ID for this data node
DataDir=/usr/local/mysql/data      # Directory for this data node's data files
[mysqld]
# SQL node options:
HostName=10.10.10.103            # Hostname or IP address
                                   # (additional mysqld connections can be
                                   # specified for this node for various
                                   # purposes such as running ndb_restore)
~

在创建了所有配置文件并指定了这些最小选项之后,就可以继续启动集群并验证所有进程都在运行。

集群管理节点的默认端口为1186;数据节点默认端口为“2202”。但是,集群可以从已经空闲的端口中自动为数据节点分配端口。

NDB集群初始启动
在配置完集群之后,启动集群并不是很困难。每个集群节点进程必须在其所在的主机上单独启动。首先应该启动管理节点,然后是数据节点,最 后是任何SQL节点:
1.在管理主机上,在系统shell中执行如下命令启动管理节点进程:

[root@mgmd /]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini
MySQL Cluster Management Server mysql-5.7.48 ndb-7.5.36
2025-05-22 18:26:23 [MgmtSrvr] INFO     -- The default config directory '/usr/local/mysql/mysql-cluster' does not exist.  Trying to create it...
Failed to create directory '/usr/local/mysql/mysql-cluster', error: 2
2025-05-22 18:26:23 [MgmtSrvr] ERROR    -- Could not create directory '/usr/local/mysql/mysql-cluster'. Either create it  manually or specify a different directory with --configdir=
[root@mgmd /]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini --configdir=/var/lib/mysql-cluster
MySQL Cluster Management Server mysql-5.7.48 ndb-7.5.36
[root@mgmd /]# netstat -ltnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1527/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      13043/cupsd
tcp        0      0 0.0.0.0:1186            0.0.0.0:*               LISTEN      26104/ndb_mgmd
tcp6       0      0 :::22                   :::*                    LISTEN      1527/sshd
tcp6       0      0 ::1:631                 :::*                    LISTEN      13043/cupsd

在第一次启动ndb_mgmd时,必须使用-f或——config-file选项告诉它在哪里可以找到它的配置文件

2.在所有数据节点主机上执行如下命令启动ndbd进程:
数据节点A:

[root@ndbda /]# ndbd
2025-05-22 18:34:35 [ndbd] INFO     -- Angel connected to '10.10.10.102:1186'
2025-05-22 18:34:35 [ndbd] INFO     -- Angel allocated nodeid: 2
[root@ndbda /]# netstat -ltnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1527/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      11765/cupsd
tcp        0      0 10.10.10.104:2202     0.0.0.0:*               LISTEN      25455/ndbd
tcp6       0      0 :::22                   :::*                    LISTEN      1527/sshd
tcp6       0      0 ::1:631                 :::*                    LISTEN      11765/cupsd

数据节点B:

[root@ndbdb /]# ndbd
2025-05-22 18:35:08 [ndbd] INFO     -- Angel connected to '10.10.10.102:1186'
2025-05-22 18:35:08 [ndbd] INFO     -- Angel allocated nodeid: 3
[root@ndbdb /]# netstat -ltnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1526/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      13086/cupsd
tcp        0      0 10.10.10.105:2202     0.0.0.0:*               LISTEN      26140/ndbd
tcp6       0      0 :::22                   :::*                    LISTEN      1526/sshd
tcp6       0      0 ::1:631                 :::*                    LISTEN      13086/cupsd

3.如果您使用RPM文件在SQL节点所在的集群主机上安装MySQL,则可以(并且应该)使用提供的启动脚本在SQL节点上启动MySQL服务器进程。

[root@mysqld mysql]# service mysql.server start
Starting MySQL.Logging to '/usr/local/mysql/data/mysqld.err'.
. SUCCESS!

[root@mysqld mysql]# netstat -ltnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1529/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      13038/cupsd
tcp6       0      0 :::3306                 :::*                    LISTEN      26418/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      1529/sshd
tcp6       0      0 ::1:631                 :::*                    LISTEN      13038/cupsd

如果一切顺利,并且集群已经正确设置,那么集群现在应该可以运行了。你可以通过调用ndb_mgm管理节点客户端来测试这一点。输出应该如下 所示,不过你可能会发现不同版本的MySQL的输出略有不同:

[root@mgmd /]# 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    @10.10.10.104  (mysql-5.7.48 ndb-7.5.36, Nodegroup: 0, *)
id=3    @10.10.10.105  (mysql-5.7.48 ndb-7.5.36, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @10.10.10.102  (mysql-5.7.48 ndb-7.5.36)

[mysqld(API)]   1 node(s)
id=4    @10.10.10.103  (mysql-5.7.48 ndb-7.5.36)

ndb_mgm>

SQL节点在这里被引用为[mysqld(API)],这反映了mysqld进程作为NDB集群API节点的事实。

SHOW命令输出中显示的给定NDB Cluster SQL或其他API节点的IP地址是SQL或API节点连接到集群数据节点的地址,而不是连接到任何管理节点的 地址。

现在您应该已经准备好处理NDB Cluster中的数据库、表和数据了

Linux下安装二进制版本的NDB集群

Linux下安装二进制版本的NDB集群
本节介绍从Oracle提供的预编译二进制文件中为每种类型的Cluster节点安装正确的可执行文件所需的步骤。
群集节点和主机。
节点 IP地址

管理节点(mgmd)                        10.10.10.102
SQL节点(mysqld)                       10.10.10.103
数据节点A(ndbd)                       10.10.10.104
数据节点B(ndbd)                       10.10.10.105

要使用预编译的二进制文件设置集群,安装过程中的第一步是从NDB cluster下载区域(https://dev.mysql.com/downloads/cluster/)下载最新 的NDB cluster 7.5二进制归档文件(mysql-cluster-gpl-7.5.9-linuxi686- glibc23.tar.gz)。我们假设您已经将这个文件放在每台机器 的/var/tmp目录中。

完成安装后,不要启动任何二进制文件。我们将在节点配置之后向您展示如何这样做。

SQL节点。
在指定用于承载SQL节点的每台机器上,以root用户的身份执行以下步骤:
1.检查/etc/passwd和/etc/group文件(或者使用操作系统提供的用于管理用户和组的工具),看看系统上是否已经有mysql组和mysql用户。一 些OS发行版在操作系统安装过程中创建了这些文件。如果没有,创建一个新的mysql用户组,然后添加一个mysql用户到这个组:

shell> groupadd mysql
shell> useradd -g mysql -s /bin/false mysql

useradd和groupadd的语法在不同版本的Unix上可能略有不同,或者它们可能具有不同的名称,例如adduser和addgroup。

2.将位置更改为包含下载文件的目录,解压缩归档文件,并创建一个名为mysql的符号链接到mysql目录。实际的文件和目录名称会根据NDB Cluster的版本号而有所不同。

[root@mysqld /]# cd /soft
[root@mysqld soft]# tar -C /usr/local -xzvf mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64.tar.gz
[root@mysqld soft]# ln -s /usr/local/mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64 /usr/local/mysql
[root@mysqld local]# cd /usr/local
[root@mysqld local]# ll
total 4
drwxr-xr-x.  2 root root    6 May  8  2014 bin
drwxr-xr-x.  2 root root    6 May  8  2014 etc
drwxr-xr-x.  2 root root    6 May  8  2014 games
drwxr-xr-x.  2 root root    6 May  8  2014 include
drwxr-xr-x.  2 root root    6 May  8  2014 lib
drwxr-xr-x.  2 root root    6 May  8  2014 lib64
drwxr-xr-x.  2 root root    6 May  8  2014 libexec
lrwxrwxrwx.  1 root root   58 May 20 18:43 mysql -> /usr/local/mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64
drwxr-xr-x. 10 root root 4096 May 20 18:36 mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64
drwxr-xr-x.  2 root root    6 May  8  2014 sbin
drwxr-xr-x.  5 root root   46 Oct 12  2017 share
drwxr-xr-x.  2 root root    6 May  8  2014 src
[root@mysqld local]#

3.将位置更改为mysql目录,并使用mysqld—initialize设置系统数据库,如下所示:

[root@mysqld local]# cd mysql
[root@mysqld mysql]# bin/mysqld --initialize
2025-05-20T10:55:27.199844Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use -- explicit_defaults_for_timestamp server option (see documentation for more details).
2025-05-20T10:55:27.381088Z 0 [Warning] InnoDB: New log files created, LSN=45790
2025-05-20T10:55:27.419222Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2025-05-20T10:55:27.475136Z 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: f068b8ae-3568-11f0-89cb-005056a04e7f.
2025-05-20T10:55:27.476044Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2025-05-20T10:55:28.436286Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2025-05-20T10:55:28.436307Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2025-05-20T10:55:28.439870Z 0 [Warning] CA certificate ca.pem is self signed.
2025-05-20T10:55:28.910740Z 1 [Note] A temporary password is generated for root@localhost: w9fh;K>W-tH2


[root@mysqld mysql]# ll
total 284
drwxr-xr-x.  2 root root    4096 May 20 18:36 bin
drwxr-x---.  6 root root    4096 May 20 18:55 data
drwxr-xr-x.  2 root root      69 May 20 18:36 docs
drwxr-xr-x.  4 root root    4096 May 20 18:35 include
drwxr-xr-x.  5 root root    4096 May 20 18:36 lib
-rw-r--r--.  1 7161 31415 255108 Sep 26  2024 LICENSE
drwxr-xr-x.  4 root root      28 May 20 18:36 man
drwxr-xr-x. 10 root root    4096 May 20 18:36 mysql-test
-rw-r--r--.  1 7161 31415    566 Sep 26  2024 README
-rw-r--r--.  1 7161 31415    566 Sep 26  2024 README-test
drwxr-xr-x. 29 root root    4096 May 20 18:36 share
drwxr-xr-x.  2 root root      86 May 20 18:36 support-files

这将为MySQL root账户生成一个随机密码。如果不希望生成随机密码,可以用——initialize-insecure选项替换——initialize。

或者,你可以将位置更改为mysql目录,然后运行mysql_install_db来创建系统数据库:

shell> cd mysql
shell> bin/mysql_install_db --user=mysql

但是,由于mysql_install_db已被废弃,因此不推荐使用此方法,因此在未来的版本中可能会被删除。

4.设置MySQL服务器和数据目录的必要权限:

[root@mysqld mysql]# chown -R root .
[root@mysqld mysql]# chown -R mysql data
[root@mysqld mysql]# chgrp -R mysql .
[root@mysqld mysql]# ll
total 284
drwxr-xr-x.  2 root  mysql   4096 May 20 18:36 bin
drwxr-x---.  6 mysql mysql   4096 May 20 18:55 data
drwxr-xr-x.  2 root  mysql     69 May 20 18:36 docs
drwxr-xr-x.  4 root  mysql   4096 May 20 18:35 include
drwxr-xr-x.  5 root  mysql   4096 May 20 18:36 lib
-rw-r--r--.  1 root  mysql 255108 Sep 26  2024 LICENSE
drwxr-xr-x.  4 root  mysql     28 May 20 18:36 man
drwxr-xr-x. 10 root  mysql   4096 May 20 18:36 mysql-test
-rw-r--r--.  1 root  mysql    566 Sep 26  2024 README
-rw-r--r--.  1 root  mysql    566 Sep 26  2024 README-test
drwxr-xr-x. 29 root  mysql   4096 May 20 18:36 share
drwxr-xr-x.  2 root  mysql     86 May 20 18:36 support-files

5.将MySQL启动脚本复制到相应的目录,使其可执行,并设置为在操作系统启动时启动:

[root@mysqld mysql]# cp support-files/mysql.server /etc/rc.d/init.d/
[root@mysqld mysql]# chmod +x /etc/rc.d/init.d/mysql.server
[root@mysqld mysql]# chkconfig --add mysql.server

(启动脚本目录可能因操作系统和版本而异——例如,在某些Linux发行版中,它是/etc/init.d。)

这里我们使用Red Hat的chkconfig来创建指向启动脚本的链接;在您的平台上使用任何适合于此目的的方法,例如在Debian上的update-rc。

请记住,必须在驻留SQL节点的每台机器上重复上述步骤。

数据节点。
数据节点的安装不需要mysqld二进制文件。只需要NDB集群数据节点可执行文件ndbd(单线程)或ndbmtd(多线程)。这些二进制文件也可以 在.tar.gz存档文件中找到。同样,我们假设您已将此归档文件放在/soft中

作为系统root用户(即使用sudo、su root或系统中同等的工具暂时拥有系统管理员帐户的权限),执行以下步骤在数据节点主机上安装数据节 点二进制文件:
数据节点A:
1.将位置更改为/soft目录,并将ndbd和ndbmtd二进制文件从归档文件中提取到合适的目录中,例如/usr/local/bin:

[root@ndbda ~]# cd /soft
[root@ndbda soft]# tar -zxvf mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64.tar.gz
[root@ndbda soft]# cd mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64
[root@ndbda mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64]# cp bin/ndbd /usr/local/bin/ndbd
[root@ndbda mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64]# cp bin/ndbmtd /usr/local/bin/ndbmtd
[root@ndbda mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64]# ll /usr/local/bin/ndb*
-rwxr-xr-x. 1 root root 43410615 May 22 00:28 /usr/local/bin/ndbd
-rwxr-xr-x. 1 root root 43697940 May 22 00:28 /usr/local/bin/ndbmtd

(在将ndb_mgm和ndb_mgmd复制到可执行目录后,您可以通过解压缩下载的存档文件及其包含的文件,从/soft中安全地删除创建的目录。)

2.将位置更改为复制文件的目录,然后使它们都可执行:

[root@ndbda mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64]# cd /usr/local/bin
[root@ndbda bin]# chmod +x ndb*
[root@ndbda bin]# ll ndb*
-rwxr-xr-x. 1 root root 43410615 May 22 00:28 ndbd
-rwxr-xr-x. 1 root root 43697940 May 22 00:28 ndbmtd

上述步骤应在每个数据节点主机上重复执行。
数据节点B:
1.将位置更改为/soft目录,并将ndbd和ndbmtd二进制文件从归档文件中提取到合适的目录中,例如/usr/local/bin:

[root@ndbdb ~]# cd /soft
[root@ndbdb soft]# tar -zxvf mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64.tar.gz
[root@ndbdb soft]# cd mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64
[root@ndbdb mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64]# cp bin/ndbd /usr/local/bin/ndbd
[root@ndbdb mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64]# cp bin/ndbmtd /usr/local/bin/ndbmtd
[root@ndbdb mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64]# ll /usr/local/bin/ndb*
-rwxr-xr-x. 1 root root 43410615 May 22 00:28 /usr/local/bin/ndbd
-rwxr-xr-x. 1 root root 43697940 May 22 00:28 /usr/local/bin/ndbmtd

(在将ndb_mgm和ndb_mgmd复制到可执行目录后,您可以通过解压缩下载的存档文件及其包含的文件,从/soft中安全地删除创建的目录。)

2.将位置更改为复制文件的目录,然后使它们都可执行:

[root@ndbda mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64]# cd /usr/local/bin
[root@ndbda bin]# chmod +x ndb*
[root@ndbdb bin]# ll ndb*
-rwxr-xr-x. 1 root root 43410615 May 22 00:44 ndbd
-rwxr-xr-x. 1 root root 43697940 May 22 00:45 ndbmtd

管理节点。
管理节点的安装不需要mysqld二进制文件。只需要NDB集群管理服务器(ndb_mgmd);您很可能还想安装管理客户端(ndb_mgm)。这两个二进制 文件也可以在.tar.gz存档文件中找到。同样,我们假设您已将此归档文件放在/soft中。

以系统root用户在管理节点主机上安装“ndb_mgmd”和“ndb_mgm”。

1.将位置更改为/soft目录,并将ndb_mgm和ndb_mgmd从归档文件中提取到合适的目录,例如/usr/local/bin:

[root@mgmd /]# cd /soft
[root@mgmd soft]# tar -zxvf mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64.tar.gz
[root@mgmd soft]# cd mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64
[root@mgmd mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64]# cp bin/ndb_mgm* /usr/local/bin
[root@mgmd mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64]# ll /usr/local/bin/ndb_mgm*
-rwxr-xr-x. 1 root root 11862623 May 22 16:41 /usr/local/bin/ndb_mgm
-rwxr-xr-x. 1 root root 21989718 May 22 16:41 /usr/local/bin/ndb_mgmd

(一旦将ndb_mgm和ndb_mgmd拷贝到可执行文件目录,你就可以安全地从/var/tmp目录中删除下载的归档文件及其包含的文件。)

2.将位置更改为复制文件的目录,然后使它们都可执行:

[root@mgmd mysql-cluster-gpl-7.5.36-linux-glibc2.12-x86_64]# cd /usr/local/bin
[root@mgmd bin]# chmod +x ndb_mgm*
[root@mgmd bin]# ll ndb_mgm*
-rwxr-xr-x. 1 root root 11862623 May 22 16:41 ndb_mgm
-rwxr-xr-x. 1 root root 21989718 May 22 16:41 ndb_mgmd