MySQL Cluster

Part I MySQL Cluster

MySQL Cluster, which is ahigh-availability, high-redundancy version of MySQL adapted for the distributedcomputing environment. Recent releases of MySQL Cluster use version 7 of theNDBCLUSTER storage engine (also known as NDB) to enable running severalcomputers with MySQL servers and other software in a cluster; the latestreleases available for production use incorporate NDB version 7.3.

MySQL簇是MySQL适合于分布式计算环境的高实用、高冗余版本。它采用了NDB簇存储引擎,允许在1个簇中运行多个MySQL服务器。

 

1. 简述

MySQL Cluster is a technology that enables clustering of in-memory databases in a shared-nothingsystem. The shared-nothing architecture enables the system to work with veryinexpensive hardware, and with a minimum of specific requirements for hardwareor software.

MySQL簇是一种技术,该技术允许在无共享的系统中部署“内存中”数据库的簇。通过无共享体系结构,系统能够使用廉价的硬件,而且对软硬件无特殊要求。此外,由于每个组件有自己的内存和磁盘,不存在单点故障。

MySQL Cluster isdesigned 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 ofshared storage mechanisms such as network shares, network file systems, andSANs is not recommended or supported.

 

MySQL Clusterintegrates the standard MySQL server with an in-memory clustered storage enginecalled NDB (which stands for “Network DataBase”). In ourdocumentation, the term NDB refers to the part of the setup that is specific tothe storage engine, whereas “MySQL Cluster” refers to thecombination of one or more MySQL servers with the NDB storage engine.

MySQL簇将标准的MySQL服务器与名为NDB的“内存中”簇式存储引擎集成了起来。在我们的文档中,术语NDB指的是与存储引擎相关的设置部分,而术语“MySQL簇”指的是MySQL和NDB存储引擎的组合。

 

A MySQL Clusterconsists of a set of computers, known as hosts, each running one or moreprocesses. These processes, known as nodes, may include MySQL servers (foraccess to NDB data), data nodes (for storage of the data), one or moremanagement servers, and possibly other specialized data access programs. Therelationship of these components in a MySQL Cluster is shown here:

MySQL簇由一组计算机构成,每台计算机上均运行着多种进程,包括MySQL服务器,NDB簇的数据节点,管理服务器,以及(可能)专门的数据访问程序。关于簇中这些组件的关系,请参见下图:


所有这些程序一起构成了MySQL簇。将数据保存到NDB簇存储引擎中时,表将保存在数据节点内。能够从簇中所有其他MySQL服务器直接访问这些表。因此,在将数据保存在簇内的工资表应用程序中,如果某一应用程序更新了1位雇员的工资,所有查询该数据的其他MySQL服务器能立刻发现这种变化。

 

对于MySQL簇,保存在数据节点内的数据可被映射,簇能够处理单独数据节点的故障,除了少数事务将因事务状态丢失而被放弃外,不会产生其他影响。由于事务性应用程序能够处理事务失败事宜,因而它不是问题源。

 

通过将MySQL簇引入开放源码世界,MySQL为所有需要它的人员提供了具有高可用性、高性能和可缩放性的簇数据管理。

2. MySQL Cluster基本概念

NDB是一种“内存中”存储引擎,它具有可用性高和数据一致性好的特点。

 

能够使用多种故障切换和负载平衡选项配置NDB存储引擎,但以簇层面上的存储引擎开始最简单。MySQL簇的NDB存储引擎包含完整的数据集,仅取决于簇本身内的其他数据。

 

下面,我们介绍了设置由NDB存储引擎和一些MySQL服务器构成的MySQL簇的设置方法。

 

目前,MySQL簇的簇部分可独立于MySQL服务器进行配置。在MySQL簇中,簇的每个部分被视为1个节点。

 

注释:在很多情况下,术语“节点”用于指计算机,但在讨论MySQL簇时,它表示的是进程。在单台计算机上可以有任意数目的节点,为此,我们采用术语簇主机(cluster host)。

 

有三类簇节点,在最低的MySQL簇配置中,至少有三个节点,这三类节点分别是:

 

·管理节点(Management node):这类节点的作用是管理MySQL簇内的其他节点,如提供配置数据、启动并停止节点、运行备份等。由于这类节点负责管理其他节点的配置,应在启动其他节点之前首先启动这类节点。MGM节点是用命令ndb_mgmd启动的。

 

·数据节点(Data node):这类节点用于保存簇的数据。数据节点的数目与副本的数目相关,是片段的倍数。例如,对于两个副本,每个副本有两个片段,那么就有4个数据节点。没有必要有一个以上的副本。数据节点是用命令ndbd启动的。

 

·SQL节点(SQL node):这是用来访问簇数据的节点。对于MySQL簇,客户端节点是使用NDB簇存储引擎的传统MySQL服务器。典型情况下,SQL节点是使用命令mysqld –ndbcluster启动的,或将ndbcluster添加到my.cnf后使用mysqld启动。

 

簇配置包括对簇中单独节点的配置,以及设置节点之间的单独通信链路。对于目前设计的MySQL簇,其意图在于,从处理器的能力、内存空间和带宽来讲,存储节点是同质的,此外,为了提供单一的配置点,作为整体,簇的所有配置数据均位于1个配置文件中。

 

管理服务器(MGM节点)负责管理簇配置文件和簇日志。簇中的每个节点从管理服务器检索配置数据,并请求确定管理服务器所在位置的方式。当数据节点内出现有趣的事件时,节点将关于这类事件的信息传输到管理服务器,然后,将这类信息写入簇日志。

 

此外,可以有任意数目的簇客户端进程或应用程序。它们分为两种类型:

 

·标准MySQL客户端:对于MySQL簇,它们与标准的(非簇类)MySQL没有区别。换句话讲,能够从用PHP、Perl、C、C++、Java、Python、Ruby等编写的现有MySQL应用程序访问MySQL簇。

 

·管理客户端:这类客户端与管理服务器相连,并提供了优雅地启动和停止节点、启动和停止消息跟踪(仅对调试版本)、显示节点版本和状态、启动和停止备份等的命令。

Part II install MySQL Cluster

1.     架构

 我们将建立具有4个节点的簇,每个节点位于不同的主机上,而且在典型的以太网中具有固定的网络地址,如下所述:

节点

IP地址

管理(MGM)节点

192.168.0.10

MySQL服务器(SQL)节点

192.168.0.20

数据(NDBD)节点"A"

192.168.0.30

数据(NDBD)节点"B"

192.168.0.40

通过下图可更清楚的表明这点:



Each MySQL Cluster host computer must have the correctexecutable programs installed. A host running an SQL node must have installedon it a MySQL Server binary (mysqld).Management nodes require the management server daemon (ndb_mgmd); data nodes require the data node daemon (ndbd or ndbmtd). It is not necessary toinstall the MySQL Server binary on management node hosts and data node hosts.It is recommended that you also install the management client (ndb_mgm) on the management server host.

 

1.     安装二进制版

 

1.1    下载安装包

For setting up a cluster using precompiled binaries,the first step in the installation process for each cluster host is to downloadthe latest MySQL Cluster NDB 7.3 binary archive(mysql-cluster-gpl-7.3.3-linux-i686-glibc23.tar.gz) from the MySQL Clusterdownloads area. We assume that you have placed this file in each machine's/var/tmp directory.

只需下载:mysql-cluster-gpl-7.3.2-linux-glibc2.5-x86_64.tar.gz

 

1.2    安装 SQL 节点

SQL nodes.   Oneach of the machines designated to host SQL nodes, perform the following stepsas the system root user:

1. 创建mysql用户和组

shell> groupadd mysql

shell> useradd -g mysql mysql

2. 解压安装包

shell> cd /u02/

shell> tar -xzvfmysql-cluster-gpl-7.3.2-linux-glibc2.5-x86_64.tar.gz

3. 更改归属组

shell> chown –R mysql:mysql *

4. 安装

切换mysql用户

shell> cd mysql

shell> scripts/mysql_install_db --user=mysql

5. 设置自启动

shell> cp support-files/mysql.server/etc/rc.d/init.d/

shell> chmod +x /etc/rc.d/init.d/mysql.server

shell> chkconfig --add mysql.server

1.3    安装 Data 节点

shell> cd /var/tmp

shell> tar -zxvfmysql-5.6.11-ndb-7.3.3-linux-i686-glibc23.tar.gz

shell> cd mysql-5.6.11-ndb-7.3.3-linux-i686-glibc23

shell> cp bin/ndbd /usr/local/bin/ndbd

shell> cp bin/ndbmtd /usr/local/bin/ndbmtd

shell> cd /usr/local/bin

shell> chmod +x ndb*

 

使用mysql-cluster-gpl-7.3.2-linux-glibc2.5-x86_64.tar.gz安装包,不需要这一步,ndbd 和 ndbmtd 已经在上一步安装的/bin目录下了。

若SQL节点和Data节点在同一台主机上,则不需要做任何操作。

1.4    安装管理节点

shell> cd /var/tmp

shell> tar -zxvfmysql-5.6.11-ndb-7.3.3-linux2.6-i686.tar.gz

shell> cd mysql-5.6.11-ndb-7.3.3-linux2.6-i686

shell> cp bin/ndb_mgm* /usr/local/bin

shell> cd /usr/local/bin

shell> chmod +x ndb_mgm*

 

同样,使用mysql-cluster-gpl-7.3.2-linux-glibc2.5-x86_64.tar.gz安装包,不需要这一步,ndb_mgmd和 ndb_mgm已经在第一步安装的/bin目录下了。

若SQL节点和Management节点在同一台主机上,则不需要做任何操作。

 

2.     安装rpm版

略。

3.     配置Cluster

对于我们的4节点、4主机MySQL簇,需要编写4个配置文件,每个节点/主机1个。

 

·每个数据节点或SQl节点需要1个my.cnf文件,该文件提供了两类信息:connectstring(连接字符串),用于通知节点到哪里找到MGM节点;以及一行,用于通知该主机(容纳数据节点的机器)上的MySQL服务器运行在NDB模式下。

·管理节点需要config.ini文件,该文件通知节点有多少需要维护的副本,需要在每个数据节点上为数据和索引分配多少内存,数据节点的位置,在每个数据节点上保存数据的磁盘位置,以及SQL节点的位置。

 

配置Data节点和SQL节点:

数据节点所需的my.cnf文件相当简单。配置文件应位于/etc目录下,并能用任何文本编辑器进行编辑(如有必要,创建该文件),例如:

vi /etc/my.cnf

对于本示例中的每个数据节点和SQL节点,my.cnf文件类似于:

[mysql@rac1 mysql-cluster]$ pwd

/u02/mysql-cluster

[mysql@rac1 mysql-cluster]$ cat my.cnf

[mysqld]

# Options for mysqld process:

ndbcluster                      # run NDB storage engine

[mysql_cluster]

# Options for MySQL Cluster processes:

ndb-connectstring=172.16.28.241  # location of management server

 

在数据节点“A”、数据节点“B”和SQL节点的机器上分别进行配置。

 

配置管理节点:

配置MGM节点的第一步是创建目录,该目录用于存放配置文件,然后创建配置文件本身。例如(以根用户身份运行):

mkdir /var/lib/mysql-cluster

cd /var/lib/mysql-cluster

vi config.ini

本例中,

[mysql@rac1 mysql-cluster]$ pwd

/u02/mysql-cluster/lib/mysql-cluster

 

[mysql@rac1 mysql-cluster]$ pwd

/u02/mysql-cluster/lib/mysql-cluster

[mysql@rac1 mysql-cluster]$ cat config.ini

[ndbd default]                                                                

# Options affecting ndbd processes on all datanodes:                         

NoOfReplicas=2   # Number of replicas                                        

DataMemory=80M   # How much memory to allocate for data storage              

IndexMemory=18M  # 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 Cluster setup.                               

[tcp default]                                                                  

# TCP/IP options:                                                             

portnumber=2202  # This the default; however, you can use any                

                 # port that is free for all the hosts in the cluster        

                 # Note: It is recommended that you do not specify the port  

                 # number at all and simply allow the default value to be used

                 # instead                                                    

[ndb_mgmd]                                                                    

# Management process options:                                                 

hostname=172.16.28.241          # Hostname or IP address of MGMnode          

datadir=/u02/mysql-cluster/lib/mysql-cluster  # Directory for MGM node log files            

Id=1                          #此处可以不写,使用系统默认值

[ndbd]                                                                        

# Options for data node "A":                                                   

                                # (one [ndbd]section per data node)          

hostname=172.16.28.241          # Hostname or IP address                      

datadir=/u02/mysql-cluster/data   # Directory for this data node's datafiles   

[ndbd]                                                                        

# Options for data node "B":                                                  

hostname=172.16.28.242          # Hostname or IP address                      

datadir=/u02/mysql-cluster/data   # Directory for this data node's datafiles   

[mysqld]                                                              

# SQL node options:                                                    

hostname=172.16.28.241          # Hostname or IP address              

                                # (additionalmysqld connections can be

                                # specified forthis node for various 

                                # purposes such asrunning ndb_restore)

[mysql@rac1 mysql-cluster]$

 

4.     首次启动Cluster

完成配置后,启动簇并不很困难。必须在数据节点所在的主机上分别启动每个簇节点进程。尽管能够按任何顺序启动节点,但我们建议,应首先启动管理节点,然后启动存储节点,最后启动SQL节点:

1.在管理主机上,从系统shell发出下述命令以启动MGM节点进程:

shell> ndb_mgmd -f /var/lib/mysql-cluster/config.ini

注意,必须用“-f”或“--config-file”选项,告诉ndb_mgmd到哪里找到配置文件(详情请参见17.5.3节,“ndb_mgmd,“管理服务器”进程”)。

 

2.在每台数据节点主机上,对于首次启动,运行下述命令启动NDBD进程:

shell> ndbd --initial

注意,仅应在首次启动ndbd时,或在备份/恢复或配置变化后重启ndbd时使用“--initial”参数,这很重要。原因在于,该参数会使节点删除由早期ndbd实例创建的、用于恢复的任何文件,包括恢复用日志文件。

 

3.如果使用RPM在SQL节点所在的簇主机上安装了MySQL,能够(也应当)使用安装在/etc/init.d下的启动脚本在SQL节点上启动MySQL服务器进程。注意,要想运行“-max”服务器二进制文件,除了标准的RPM外,还需要安装-max服务器RPM。

 

如果一切顺利,并已正确设置了簇,那么簇现在应能运行。通过调用ndb_mgm管理节点客户端,可对其进行测试。采用参考文件中的命令

shell> ndb_mgmd -f/var/lib/mysql-cluster/config.ini

报错,需要加configdir参数:

[mysql@rac1 bin]$ ./ndb_mgmd--config-file=/u02/mysql-cluster/lib/mysql-cluster/config.ini

MySQL Cluster Management Server mysql-5.6.11 ndb-7.3.2

2013-11-13 11:34:07 [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

2013-11-13 11:34:07 [MgmtSrvr] ERROR    -- Could not create directory'/usr/local/mysql/mysql-cluster'. Either create it manually or specify adifferent directory with--configdir=<path>

 

启动过程测试:

启动:(在修改参数config.ini后若想使设置生效需加参数: –-init

[mysql@rac1 bin]$ ndb_mgmd-f /u02/mysql-cluster/lib/mysql-cluster/config.ini--configdir=/u02/mysql-cluster/lib/mysql-cluster

MySQL Cluster Management Server mysql-5.6.11ndb-7.3.2

2013-11-13 12:34:29 [MgmtSrvr] WARNING  -- at line 12: [tcp] portnumber is deprecated

2013-11-13 12:34:29 [MgmtSrvr] WARNING  -- at line 21: [MGM] Id is deprecated, useNodeId instead

2013-11-13 12:34:29 [MgmtSrvr] WARNING  -- at line 36: Cluster configuration warning:

 arbitrator with id 1 and db node with id 2 on same host 172.16.28.241

  Runningarbitrator on the same host as a database node may

  causecomplete cluster shutdown in case of host failure.

2013-11-13 12:34:29 [MgmtSrvr] WARNING  -- at line 12: [tcp] portnumber is deprecated

2013-11-13 12:34:29 [MgmtSrvr] WARNING  -- at line 21: [MGM] Id is deprecated, useNodeId instead

2013-11-13 12:34:29 [MgmtSrvr] WARNING  -- at line 36: Cluster configuration warning:

 arbitrator with id 1 and db node with id 2 on same host 172.16.28.241

  Runningarbitrator on the same host as a database node may

  causecomplete cluster shutdown in case of host failure.

若不是首次启动或修改了参数,则只会显示第一行:

MySQL Cluster Management Server mysql-5.6.11ndb-7.3.2

查看进程:

[mysql@rac1 bin]$ ps -ef |grep ndb

mysql   26495     1  0 12:34 ?        00:00:00 ./ndb_mgmd -f/u02/mysql-cluster/lib/mysql-cluster/config.ini--configdir=/u02/mysql-cluster/lib/mysql-cluster

mysql   26523 23559  0 12:35 pts/0    00:00:00 grep ndb

[mysql@rac1 bin]$          

查看Cluster状态

[mysql@rac1 bin]$ ndb_mgm

-- NDB Cluster -- Management Client --

ndb_mgm> show

Connected to Management Server at:172.16.28.241:1186

Cluster Configuration

---------------------

[ndbd(NDB)]    2 node(s)

id=2 (not connected, accepting connect from172.16.28.241)

id=3 (not connected, accepting connect from172.16.28.242)

 

[ndb_mgmd(MGM)] 1 node(s)

id=1   @172.16.28.241  (mysql-5.6.11ndb-7.3.2)

 

[mysqld(API)]  1 node(s)

id=4 (not connected, accepting connect from172.16.28.241)

 

ndb_mgm> exit

启动数据节点A:

[mysql@rac1 bin]$ ndbd

2013-11-13 12:42:33 [ndbd] INFO     -- Angel connected to '172.16.28.241:1186'

2013-11-13 12:42:33 [ndbd] INFO     -- Angel allocated nodeid: 2

查看Cluster状态

[mysql@rac1 bin]$ ndb_mgm

-- NDB Cluster -- Management Client --

ndb_mgm> show

Cluster Configuration

---------------------

[ndbd(NDB)]    2 node(s)

id=2   @172.16.28.241  (mysql-5.6.11ndb-7.3.2, starting, Nodegroup: 0)

id=3 (not connected, accepting connect from172.16.28.242)

 

[ndb_mgmd(MGM)] 1 node(s)

id=1   @172.16.28.241  (mysql-5.6.11ndb-7.3.2)

 

[mysqld(API)]  1 node(s)

id=4 (not connected, accepting connect from172.16.28.241)

 

ndb_mgm>

启动数据节点2后查看Cluster状态:

ndb_mgm> show

Cluster Configuration

---------------------

[ndbd(NDB)]    2 node(s)

id=2   @172.16.28.241  (mysql-5.6.11ndb-7.3.2, Nodegroup: 0, Master)

id=3   @172.16.28.242  (mysql-5.6.11ndb-7.3.2, Nodegroup: 0)

 

[ndb_mgmd(MGM)] 1 node(s)

id=1   @172.16.28.241  (mysql-5.6.11ndb-7.3.2)

 

[mysqld(API)]  1 node(s)

id=4 (not connected, accepting connect from172.16.28.241)

 

ndb_mgm>

 

启动SQL节点:

[mysql@rac1 mysql-cluster]$ mysqld_safe &

[1] 26865

[mysql@rac1 mysql-cluster]$ 131113 12:49:09mysqld_safe Logging to '/u02/mysql-cluster/data/rac1.zgq.com.err'.

131113 12:49:09 mysqld_safe Starting mysqlddaemon with databases from /u02/mysql-cluster/data

 

[mysql@rac1 mysql-cluster]$ ndb_mgm

-- NDB Cluster -- Management Client

查看Cluster状态

ndb_mgm> show

Connected to Management Server at:172.16.28.241:1186

Cluster Configuration

---------------------

[ndbd(NDB)]    2 node(s)

id=2   @172.16.28.241  (mysql-5.6.11ndb-7.3.2, Nodegroup: 0, Master)

id=3   @172.16.28.242  (mysql-5.6.11ndb-7.3.2, Nodegroup: 0)

 

[ndb_mgmd(MGM)] 1 node(s)

id=1   @172.16.28.241  (mysql-5.6.11ndb-7.3.2)

 

[mysqld(API)]  1 node(s)

id=4   @172.16.28.241  (mysql-5.6.11ndb-7.3.2)

 

ndb_mgm>

 

登录:

[mysql@rac1 mysql-cluster]$ mysql -uroot

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.6.11-ndb-7.3.2-cluster-gplMySQL Cluster Community Server (GPL)

 

Copyright (c) 2000, 2013, Oracle and/or itsaffiliates. All rights reserved.

 

Oracle is a registered trademark of OracleCorporation and/or its

affiliates. Other names may be trademarks of theirrespective

owners.

 

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

 

mysql>

 

5.     安全关闭和重启

要想关闭簇,可在MGM节点所在的机器上,在Shell中简单地输入下述命令:

 

shell> ndb_mgm-e shutdown

该命令将恰当地中止ndb_mgm、ndb_mgmd以及任何ndbd进程。使用mysqladminshutdown或其他方法,可中止SQL节点。注意,这里的“-e”选项用于将命令从shell传递到ndb_mgm客户端。

 

要想重启簇,可简单地运行下述命令:

 

·在管理主机上(本设置中为192.168.0.10):

shell> ndb_mgmd-f /var/lib/mysql-cluster/config.ini

·在每台数据节点主机上(192.168.0.30和192.168.0.40):

shell> ndbd

请记住,正常重启NDBD节点时,不要用“--initial”选项调用该命令。

 

·在SQL主机上(192.168.0.20):

shell> mysqld&

 

设置数据库引擎:

在建表时,若不设置数据库引擎,则建的表在本地数据库上,而不是在Cluster里。

因此需要声明存储引擎:

mysql> createtable zgq_test engine=ndbclusteras select host,user from mysql.user;

或者设置默认的引擎:

mysql> SETdefault_storage_engine=NDBCLUSTER;

Query OK, 0 rowsaffected (0.01 sec)

 

mysql> createtable test2 as select host,db,user from mysql.db;

Query OK, 2 rowsaffected (0.12 sec)

Records: 2  Duplicates: 0 Warnings: 0

 

mysql>

 

设置默认存储引擎前:

mysql> show variables like '%engine%';

+----------------------------+--------+

| Variable_name              | Value  |

+----------------------------+--------+

| default_storage_engine    | InnoDB |

|default_tmp_storage_engine | InnoDB |

|storage_engine             | InnoDB |

+----------------------------+--------+

3 rows in set(0.00 sec)

 

设置默认存储引擎后:

mysql> show variables like '%engine%';

+----------------------------+------------+

|Variable_name              | Value      |

+----------------------------+------------+

| default_storage_engine    | ndbcluster |

|default_tmp_storage_engine | InnoDB     |

|storage_engine             | ndbcluster |

+----------------------------+------------+

3 rows in set(0.01 sec)

 

  

 

参考文件:

MySQL 5.6 Reference Manual IncludingMySQL Cluster NDB 7.3 Reference Guide Chapter 17. MySQL Cluster NDB 7.3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值