为了让您熟悉基础知识,我们描述了一个功能性 NDB 集群的最简单配置。在此之后,您应该能够根据本章其他相关部分提供的信息设计所需的设置。To familiarize you with the basics, we describe the simplest possible configuration for a functional NDB Cluster. After this, you should be able to design your desired setup from the information provided in the other relevant sections of this chapter.
首先,您需要创建一个配置目录,例如 /var/lib/mysql-cluster,通过以系统 root 用户执行以下命令来完成:First, you need to create a configuration directory such as
/var/lib/mysql-cluster, by executing the
following command as the system root user:
$> mkdir /var/lib/mysql-cluster
在此目录中,创建一个名为 config.ini 的文件,其中包含以下信息。根据您系统的实际情况,替换适当的 HostName 和 DataDir 值。In this directory, create a file named
config.ini that contains the following
information. Substitute appropriate values for
HostName and DataDir as
necessary for your system.
# 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 do not know the hostname for your machine, use localhost. # The DataDir parameter also has a default value, but it is recommended to # set it explicitly. # [api] and [mgm] are aliases for [mysqld] and [ndb_mgmd], respectively. [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:You can now start the ndb_mgmd management
server. By default, it attempts to read the
config.ini file in its current working
directory, so change location into the directory where the file is
located and then invoke ndb_mgmd:
$>cd /var/lib/mysql-cluster$>ndb_mgmd
然后通过运行 ndbd 启动一个单独的数据节点:Then start a single data node by running ndbd:
$> ndbd
默认情况下,ndbd 会在端口 1186 的 localhost 查找管理服务器。By default, ndbd looks for the management
server at localhost on port 1186.
如果你是从二进制 tar 包安装 MySQL,你必须明确指定 ndb_mgmd 和 ndbd 服务器的路径。(通常,这些可以在 /usr/local/mysql/bin 中找到。)If you have installed MySQL from a binary tarball, you must to
specify the path of the ndb_mgmd and
ndbd servers explicitly. (Normally, these can
be found in /usr/local/mysql/bin.)
最后,切换到 MySQL 数据目录(通常是 /var/lib/mysql 或 /usr/local/mysql/data),并确保 my.cnf 文件包含启用 NDB 存储引擎所需的选项:Finally, change location to the MySQL data directory (usually
/var/lib/mysql or
/usr/local/mysql/data), and make sure that
the my.cnf file contains the option necessary
to enable the NDB storage engine:
[mysqld] ndbcluster
现在可以像往常一样启动 MySQL 服务器:You can now start the MySQL server as usual:
$> mysqld_safe --user=mysql &
稍等片刻以确保 MySQL 服务器正常运行。如果看到通知 mysql ended,请检查服务器的 .err 文件以了解出了什么问题。Wait a moment to make sure the MySQL server is running properly.
If you see the notice mysql ended, check the
server's .err file to find out what went
wrong.
如果到目前为止一切顺利,现在可以开始使用集群了。连接到服务器并验证 NDBCLUSTER 存储引擎已启用:If all has gone well so far, you now can start using the cluster.
Connect to the server and verify that the
NDBCLUSTER storage engine is enabled:
$>mysqlWelcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 to server version: 8.4.9 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>SHOW ENGINES\G... *************************** 12. row *************************** Engine: NDBCLUSTER Support: YES Comment: Clustered, fault-tolerant, memory-based tables *************************** 13. row *************************** Engine: NDB Support: YES Comment: Alias for NDBCLUSTER ...
前面示例输出中显示的行号可能与您系统上显示的不同,这取决于服务器的配置方式。The row numbers shown in the preceding example output may be different from those shown on your system, depending upon how your server is configured.
尝试创建一个 NDBCLUSTER 表:Try to create an NDBCLUSTER table:
$>mysqlmysql>USE test;Database changed mysql>CREATE TABLE ctest (i INT) ENGINE=NDBCLUSTER;Query OK, 0 rows affected (0.09 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=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.00 sec)
要检查节点是否正确设置,请启动管理客户端:To check that your nodes were set up properly, start the management client:
$> ndb_mgm
在管理客户端中使用 SHOW 命令获取集群状态报告:Use the SHOW command from within the management client to obtain a report on the cluster's status:
ndb_mgm> SHOW
Cluster Configuration
---------------------
[ndbd(NDB)] 1 node(s)
id=2 @127.0.0.1 (Version: 8.4.7-ndb-8.4.7, Nodegroup: 0, *)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @127.0.0.1 (Version: 8.4.7-ndb-8.4.7)
[mysqld(API)] 3 node(s)
id=3 @127.0.0.1 (Version: 8.4.7-ndb-8.4.7)
id=4 (not connected, accepting connect from any host)
id=5 (not connected, accepting connect from any host)
此时,您已成功设置了一个可用的 NDB 集群。现在可以通过使用任何创建了 ENGINE=NDBCLUSTER 或其别名 ENGINE=NDB 的表,将数据存储在集群中。At this point, you have successfully set up a working NDB Cluster.
You can now store data in the cluster by using any table created
with ENGINE=NDBCLUSTER or its alias
ENGINE=NDB.