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创建的表在集群中存储数据。

发表评论

电子邮件地址不会被公开。