首先,应以系统根用户身份通过执行下述命令创建配置目录,如/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 and API are 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]
现在,能够按下述方式启动管理服务器:
shell> cd /var/lib/mysql-cluster shell> ndb_mgmd
接下来,通过运行ndbd启动单个DB节点。首次为给定的DB节点启动ndbd时,应使用“—initial”选项,如下所示:
shell> ndbd --initial
对于后续的ndbd启动,通常不需要使用该选项:
shell> ndbd
这是因为,--initial选项将删除该数据节点的所有已有数据和日志文件(以及所有的表元数据),并创建新的数据和日志文件。该规则的一项例外是:添加了新数据节点后重启簇并从备份进行恢复之时。
默认情况下,ndbd将在端口1186上查找本地主机上的管理服务器。
注释:如果从二进制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 &
等待一段时间,确认MySQL服务器正在恰当运行。如果发现通知用mysql停止,请检查服务器的.err文件,找出错误。
如果到目前为止一切正常,可使用簇启动它:
shell> mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 to server version: 5.1.2-alpha-Max 键入’help;’或’\h’获取帮助。键入’\c’清空缓冲区。 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 ...
(注意,上例输出中显示的行号可能与你的系统上显示的不同,具体情况取决于使用的MySQL版本,以及配置它的方式)。
shell> mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 to server version: 5.1.2-alpha-Max 键入’help;’或’\h’获取帮助。键入’\c’清空缓冲区。 mysql> 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=latin1 1 row in set (0.00 sec)
要想检查是否恰当设置了节点,可启动管理客户端:
shell> ndb_mgm
随后,为了获得关于簇状态的报告,可从管理客户端内使用SHOW命令:
NDB> SHOW Cluster Configuration --------------------- [ndbd(NDB)] 1 node(s) id=2 @127.0.0.1 (Version: 3.5.3, Nodegroup: 0, Master) [ndb_mgmd(MGM)] 1 node(s) id=1 @127.0.0.1 (Version: 3.5.3) [mysqld(API)] 3 node(s) id=3 @127.0.0.1 (Version: 3.5.3) id=4 (not connected, accepting connect from any host) id=5 (not connected, accepting connect from any host)
此时,你成功地设置了工作的MySQL簇。现在,你能使用由ENGINE=NDBCLUSTER或其别名ENGINE=NDB创建的表,将数据保存到簇中。