http://xuwensong.elastos.org/2014/01/13/ubuntu-%E4%B8%8Bmysql-cluster%E5%AE%89%E8%A3%85%E5%92%8C%E9%85%8D%E7%BD%AE/
MySQL-Cluster是一种技术,该技术允许在无共享的系统中部署“内存中”数据库的簇。通过无共享体系结构,系统能够使用廉价的硬件,而且对软硬件无特殊要求。此外,由于每个组件都有自己的内存和磁盘,所以不存在单点故障。
MySQL簇将标准的MySQL服务器与名为NDB的“内存中”的簇式存储引擎集成了起来。术语NDB指的是与存储引擎相关的设置部分,而术语“MySQL簇”指的是MySQL和NDB存储引擎的组合。
MySQL簇由一组计算机构成,每台计算机上均运行着多种进程,包括MySQL服务器,NDB簇的数据节点,管理服务器(MGM),以及(可能)专门的数据访问程序。关于簇中组件的关系,如下图:
所有这些程序一起构成了MySQL簇。将数据保存到NBD簇引擎中时,表将保存在数据节点内。能够从簇中所有其他MySQL服务器直接访问这些表。因此,假如在将数据保存在簇内的工资应用程序中,如果某一应用程序更新了一位雇员的工资,所有查询该数据的其他MySQL 服务器能立刻发现这种变化。
对于MySQL簇,保存在数据节点的数据可被映射,簇能够处理单独数据节点的故障,除了少数事物将因事物状态丢失而被放弃外,不会产生其他影响。由于事物性应用程序能够处理失败事宜,因而它不是问题源。
NDB 是一种“内存中”存储引擎,它具有可用性高和数据一致性好的特点。
能够使用多种故障切换和负载平衡选项配置NDB 存储引擎,但以簇层面上的存储引擎开始最简单。MySQL簇的NDB存储引擎包含完整的数据集,仅取决于簇本身内的其他数据。
下面名,我们将介绍设置由NDB存储引擎和一些MySQL服务器构成的MySQL簇的设置方法。
目前,MySQL簇的部分可以独立于MySQL服务器进行配置。在MySQL簇中,簇的每个部分被视为一个节点。
注释:在很多情况下,术语“节点”用于指计算机,但在讨论MySQL簇时,它表示的是进程。在单台计算机上可以有任意数目的节点,所以我们才有可能将多个不同功能的节点配置在同一台计算机上,为此,我们采用术语簇主机。
有三类簇节点,在最低的MySQL簇配置中,至少有三个节点,这三类节点分别是:
管理节点(MGM):这类节点的作用是管理MySQL簇内的其他节点,如提供配置数据,启动并停止节点,运行备份等。由于这类节点负责管理其他节点的配置,所以管理节点应在其他节点之前先启动。MGM节点是用命令ndb_mgm启动的。
数据节点(NDB):这类节点用于保存簇的数据。数据集点的数目与副本的数目相关,是片段的倍数。例如,对于两个副本,每个副本又两个片段,那么就有4个数据节点。没有必要有一个以上的副本。。数据节点是用命令ndbd来启动的。
SQL节点:这类节点是用来访问簇数据的节点。对于MySQL簇,客户端节点是使用NDB 簇存储引擎的传统MySQL服务器。典型情况下,SQL节点是使用命令mysql -ndbcluster来启动的,或将ndbcluster添加到my.cnf后面使用mysqld启动。
簇配置包括对簇中单独节点的配置,以及设置节点之间的单独通信链路。对于目前设计的MySQL簇,其意图在于,从处理器的能力,内存空间和宽带来讲,存储节点是同质的,此外,为了提供单一的配置点,作为整体,簇的所有配置均位于一个文件中。
管理服务器(MGM节点)负责管理簇配置文件和簇日志。簇中的每个节点从管理服务器检索配置数据,并请求确定管理服务器所在的位置的方式。当数据节点内出现有趣的事件时,节点将关于这类事件的信息传输到管理服务器,然后,啊经这类信息写入簇日志。
现在,我们计划建立有5个节点的MySQLCLuster体系,因此需要用到3台机器(sql和数据节点共用),分别做如下用途:
节点(用途) IP地址(主机名)
管理节点(MGM) 10.24.0.101(db1)nodeid = 1
数据节点1(NDBD1) 10.24.6.4 (db4) nodeid = 11
数据节点2(NDBD2) 10.24.6.6(db5) nodeid =12
SQL节点1(SQL1) 10.24.6.4(db2)nodeid = 21
SQL节点2(SQL2) 10.24.6.6(db3)nodeid = 22
对于MySQL-Cluster的安装包下载,下载地址见http://dev.mysql.com/downloads/cluster/
mysql-cluster-gpl-7.4.7-debian7-x86_64.deb
此外 如果之前安装过mysql-server,在进行此次实验之前,需要将mysql-server卸载,执行以下指令卸载mysql
sudo apt-get autoremove --purgemysql-server
sudo apt-get removemysql-server
sudo apt-get autoremovemysql-server
sudo apt-get remove mysql-common(非常重要)
sudo dpkg -imysql-cluster-gpl-7.4.7-debian7-x86_64.deb
安装目录/opt/mysql/server-5.6
SQL节点和数据节点的安装步骤基本相同,所以在设计为存储节点或SQL节点的的每一台机器上,以系统根用户身份执行以下步骤:
mysql组和mysql用户
检查/etc/passwd和/etc/group/文件,查看在系统上是否已经存在mysql组和mysql用户,这时因为某些操作系统会将其作为安装进程的一部分创建。可以使用以下指令查看:
cat show /etc/passwd
cat show /etc/group
如果它们不存在,则需要创建新的mysql用户组,然后为该组添加一个mysql用户
groupadd mysql
useradd -g mysql mysql
sudo/opt/mysql/server-5.6/scripts/mysql_install_db --user=mysql
如果此脚本不能运行,若报错为主机名不匹配的话,则很有可能是下载的版本不对,查看操作系统是32位还是64位,选择正确的版本。若报错找不到默认的文件,则很有可能是因为之前的mysql-server没有卸载,执行上文提供的指令,彻底卸载mysql-server
成功结果:
To start mysqld at boot time youhave to copy
support-files/mysql.server to theright place for your system
PLEASE REMEMBER TO SET A PASSWORDFOR THE MySQL root USER !
To do so, start the server,then issue the following commands:
/opt/mysql/server-5.6/bin/mysqladmin -u rootpassword 'new-password'
/opt/mysql/server-5.6/bin/mysqladmin -u root-h drbd01 password 'new-password'
Alternatively you can run:
/opt/mysql/server-5.6/bin/mysql_secure_installation
which will also give you theoption of removing the test
databases and anonymous usercreated by default. This is
strongly recommended forproduction servers.
See the manual for moreinstructions.
You can start the MySQL daemonwith:
cd /opt/mysql/server-5.6 ;/opt/mysql/server-5.6/bin/mysqld_safe &
You can test the MySQL daemonwith mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Support MySQL by buyingsupport/licenses at http://shop.mysql.com
WARNING: Found existing configfile /opt/mysql/server-5.6/my.cnf on the system.
Because this file might be inuse, it was not replaced,
but was used in bootstrap(unless you used --defaults-file)
and when you later start theserver.
The new default config file wascreated as /opt/mysql/server-5.6/my-new.cnf,
please compare it with yourfile and take the changes you need.
chown -R root .
chown -R mysql data
3 | chgrp -R mysql . |
sudo cp /opt/mysql/server-5.6/support-files/mysql.server/etc/init.d/mysql
chmod +x /etc/init.d/mysql
sudo cp /opt/mysql/server-5.6/my-new.cnf/etc/my.cnf
sudo vim /etc/my.cnf
sudo apt-get installmysql-client
mysqladmin -u rootflush-privileges password "123456"
对于管理(MGM)节点,不需要安装mysqld可执行文件,仅需要安装用于MGM服务器和客户端的二进制文件,这类文件可在下载的档案文件中找到。假定将下载的档案文件放在了/var/tmp文件下,以系统管理员的身份执行以下步骤,在簇管理节点主机上安装ndb_mgmd和ndb_mgm
ndb_mgmd:ndb管理服务器
ndb_mgm: ndb管理客户端
sudo dpkg -imysql-cluster-gpl-7.4.7-debian7-x86_64.deb
安装目录/opt/mysql/server-5.6
sudo mkdir /usr/local/mysql/
sudo cp /opt/mysql/server-5.6/bin/ndb_mgm*/usr/local/mysql/
注释:簇管理节点的默认端口是1186,数据节点的默认端后是2202。
cd /usr/local/mysql/
sudo vim config.ini:
# Options affecting ndbdprocesses on all data nodes:
[NDBD DEFAULT]
NoOfReplicas=2
DataMemory=80M
IndexMemory=18M
# TCP/IP options:
[TCP DEFAULT]
#portnumber=2202
# Management process options:
[NDB_MGMD]
nodeid=1
HostName=10.24.0.101
DataDir=/usr/local/mysql
# Options for data node :
[NDBD]
nodeid=11
HostName=10.24.6.4
DataDir=/opt/mysql/server-5.6/data/
# Options for data node :
[NDBD]
nodeid=12
HostName=10.24.6.6
DataDir=/opt/mysql/server-5.6/data/
# SQL node options:
[MYSQLD]
nodeid=21
HostName=10.24.6.4
[MYSQLD]
nodeid=22
HostName=10.24.6.6
完成配置后,启动簇并不困难。必须在数据节点所在的主机上分别启动每个簇节点进程。尽管能够按任何顺序启动节点,但还是建议,首先启动管理节点,然后启动存储节点,最后启动SQL节点。
可使用nbd_mgm指令登录到ndb_mgm客户端,登录后,可使用show指令来查看簇中个节点情况。
注意,启动MGM时,必须用-f或者–config-file选项,告诉ndb_mgmd到哪里找到配置文件。首次启动时必须选用–initial选项,或者更改了MGM节点的配置信息后,也需选用–initial选项。
sudo /usr/local/mysql/ndb_mgmd -f/usr/local/mysql/config.ini
Ndb客户端查看:
wiki@zoweewiki:/usr/local/mysql$/usr/local/mysql/ndb_mgm
-- NDB Cluster -- ManagementClient --
ndb_mgm>
ndb_mgm>
ndb_mgm>
ndb_mgm> show
Connected to Management Serverat: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=11 @10.24.6.4 (mysql-5.6.25 ndb-7.4.7, Nodegroup: 0, *)
id=12 @10.24.6.6 (mysql-5.6.25 ndb-7.4.7, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @10.24.0.101 (mysql-5.6.25 ndb-7.4.7)
[mysqld(API)] 2 node(s)
id=21 @10.24.6.4 (mysql-5.6.25 ndb-7.4.7)
id=22 @10.24.6.6 (mysql-5.6.25 ndb-7.4.7)
在每台数据节点主机上,对于首次启动,运行下述命令启动NDBD进程:
sudo/opt/mysql/server-5.6/bin/ndbd --initial
注意,应仅在首次启动时ndbd时,或者在备份/恢复或者配置变化后重启ndbd时使用“–initial”参数,这很重要,因为该参数会使数据节点删除由早期ndbd实例创建的,用于恢复的任何文件,包括恢复用日志文件。
sudo /etc/init.d/mysql restart
启动日志:
/opt/mysql/server-5.6/data/drbd02.err
151015 14:33:19 mysqld_safeStarting mysqld daemon with databases from /opt/mysql/server-5.6/data
2015-10-15 14:33:22 0 [Warning]TIMESTAMP with implicit DEFAULT value is deprecated. Please use--explicit_defaults_for_timestamp server option (see documentation for moredetails).
2015-10-15 14:33:22 0 [Note]/opt/mysql/server-5.6/bin/mysqld (mysqld 5.6.25-ndb-7.4.7-cluster-gpl-log)starting as process 15192 ...
2015-10-15 14:33:23 15192[Note] Plugin "FEDERATED' is disabled.
2015-10-15 14:33:23 15192[Note] InnoDB: Using atomics to ref count buffer pool pages
2015-10-15 14:33:23 15192[Note] InnoDB: The InnoDB memory heap is disabled
2015-10-15 14:33:23 15192[Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2015-10-15 14:33:23 15192[Note] InnoDB: Memory barrier is not used
2015-10-15 14:33:23 15192[Note] InnoDB: Compressed tables use zlib 1.2.3
2015-10-15 14:33:23 15192[Note] InnoDB: Using Linux native AIO
2015-10-15 14:33:23 15192[Note] InnoDB: Not using CPU crc32 instructions
2015-10-15 14:33:23 15192[Note] InnoDB: Initializing buffer pool, size = 128.0M
2015-10-15 14:33:23 15192[Note] InnoDB: Completed initialization of buffer pool
2015-10-15 14:33:24 15192[Note] InnoDB: Highest supported file format is Barracuda.
2015-10-15 14:33:24 15192[Note] InnoDB: 128 rollback segment(s) are active.
2015-10-15 14:33:24 15192[Note] InnoDB: Waiting for purge to start
2015-10-15 14:33:24 15192[Note] InnoDB: 5.6.25 started; log sequence number 1626027
2015-10-15 14:33:24 15192[Note] NDB: Changed global value of binlog_format from STATEMENT to MIXED
2015-10-15 14:33:24 15192[Note] NDB: NodeID is 22, management server '10.24.0.101:1186'
2015-10-15 14:33:25 15192[Note] NDB[0]: NodeID: 22, all storage nodes connected
2015-10-15 14:33:25 15192[Warning] NDB: server id set to zero - changes logged to bin log with server idzero will be logged with another server id by slave mysqlds
2015-10-15 14:33:25 15192[Note] NDB Binlog: Starting...
2015-10-15 14:33:25 15192[Note] NDB Util: Starting...
2015-10-15 14:33:25 15192[Note] NDB Index Stat: Starting...
2015-10-15 14:33:25 15192[Note] NDB Index Stat: Wait for server start completed
2015-10-15 14:33:25 15192[Note] NDB Util: Wait for server start completed
2015-10-15 14:33:25 15192[Note] NDB Binlog: Started
2015-10-15 14:33:25 15192[Note] NDB Binlog: Setting up
2015-10-15 14:33:25 15192[Note] NDB Binlog: Created schema Ndb object, reference: 0x80040016, name: 'NdbBinlog schema change monitoring'
2015-10-15 14:33:25 15192[Note] NDB Binlog: Created injector Ndb object, reference: 0x80050016, name:'Ndb Binlog data change monitoring'
2015-10-15 14:33:25 15192[Note] NDB Binlog: Setup completed
2015-10-15 14:33:25 15192[Note] NDB Binlog: Wait for server start completed
2015-10-15 14:33:25 15192[Note] Server hostname (bind-address): '*'; port: 3306
2015-10-15 14:33:25 15192[Note] IPv6 is available.
2015-10-15 14:33:25 15192[Note] - '::' resolves to '::';
2015-10-15 14:33:25 15192[Note] Server socket created on IP: '::'.
2015-10-15 14:33:25 15192[Note] Event Scheduler: Loaded 0 events
2015-10-15 14:33:25 15192[Note] /opt/mysql/server-5.6/bin/mysqld: ready for connections.
Version:'5.6.25-ndb-7.4.7-cluster-gpl-log' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL ClusterCommunity Server (GPL)
2015-10-15 14:33:25 15192[Note] NDB Util: Wait for cluster to start
2015-10-15 14:33:25 15192[Note] NDB Util: Started
2015-10-15 14:33:25 15192[Note] NDB Binlog: Check for incidents
2015-10-15 14:33:25 15192[Note] NDB Binlog: Wait for cluster to start
2015-10-15 14:33:25 15192[Note] NDB Index Stat: Wait for cluster to start
2015-10-15 14:33:25 15192[Note] ndb_index_stat_proc: Created Ndb object, reference: 0x80070016, name:'Ndb Index Statistics monitoring'
2015-10-15 14:33:25 15192[Note] NDB Index Stat: Started
2015-10-15 14:33:26 15192[Note] NDB Binlog: DISCOVER TABLE Event: REPL$mysql/ndb_schema
2015-10-15 14:33:26 15192[Note] NDB Binlog: logging ./mysql/ndb_schema (UPDATED,USE_WRITE)
2015-10-15 14:33:26 15192[Note] NDB Binlog: DISCOVER TABLE Event: REPL$mysql/ndb_apply_status
2015-10-15 14:33:26 15192[Note] NDB Binlog: logging ./mysql/ndb_apply_status (UPDATED,USE_WRITE)
2015-10-15 14:33:26 15192[Note] NDB: Cleaning stray tables from database 'ndb_12_fs'
2015-10-15 14:33:26 15192[Note] NDB: Cleaning stray tables from database 'ndbinfo'
2015-10-15 14:33:26 15192[Note] NDB: Cleaning stray tables from database 'performance_schema'
2015-10-15 14:33:26 15192[Note] NDB: Cleaning stray tables from database 'test'
2015-10-15 14:33:26 15192[Note] NDB Binlog: Wait for first event
2015-10-15 14:33:26 [NdbApi]INFO -- Flushing incomplete GCI:s< 4554/3
2015-10-15 14:33:26 [NdbApi]INFO -- Flushing incomplete GCI:s< 4554/3
2015-10-15 14:33:26 15192[Note] NDB Binlog: starting log at epoch 4554/3
2015-10-15 14:33:26 15192[Note] NDB Binlog: Got first event
2015-10-15 14:33:26 15192[Note] NDB Binlog: ndb tables writable
2015-10-15 14:33:26 15192[Note] NDB Binlog: Startup and setup completed
2015-10-15 14:33:26 15192[Note] NDB Schema dist: Data node: 11 reports subscribe from node 21,subscriber bitmask 0200000
2015-10-15 14:33:26 15192[Note] NDB Schema dist: Data node: 12 reports subscribe from node 21,subscriber bitmask 0200000
2015-10-15 14:34:03 15192[Warning] IP address '10.24.6.170' could not be resolved: Name or service notknown
2015-10-15 14:34:56 15192[Note] NDB Schema dist: Data node: 11 reports subscribe from node 21,subscriber bitmask 00
2015-10-15 14:34:56 15192[Note] NDB Schema dist: Data node: 12 reports subscribe from node 21,subscriber bitmask 00
2015-10-15 14:35:03 15192[Note] NDB Schema dist: Data node: 11 reports subscribe from node 21,subscriber bitmask 0200000
2015-10-15 14:35:03 15192[Note] NDB Schema dist: Data node: 12 reports subscribe from node 21,subscriber bitmask 0200000
2015-10-15 14:41:04 15192[Note] NDB Schema dist: Data node: 11 failed, subscriber bitmask 00
2015-10-15 14:42:36 15192[Note] NDB Schema dist: Data node: 12 reports subscribe from node 21, subscriberbitmask 00
为了让数据表能够在cluster中正常复制,创建数据表时必须指定为ndbcluster引擎(engine=ndb或engine=ndbcluster)。
登录到10.24.64的mysql,然后创建一个新数据库songzi,并创建一个ndbcluster引擎的数据表test(id int,namechar(10)),并向表中插入一条数据(0,songzi)。10.24.6.4上的操作及结果如下图:
然后登录10.24.6.6的mysql,可查看到数据已同步,并且新建的表及数据也存在。10.24.6.6上的操作及结果如下图:
终止10.24.6.4上的NDB进程,执行以下指令查看NDB进程情况
ps -ef | grep ndbd
kill 24077
ps -ef | grep ndbd
具体操作及结果见下图:
也可以在ndb_mgm管理节点客户端查看到10.24.0.101上的NDB节点已停掉
然后分别登录到10.24.6.4和10.24.6.6的mysql,可发现依然能够查询到数据。结果如下图
10.24.6.4
10.24.6.6
此结果说明测试成功,即当有一个NDB节点Crash后,整个MySQL环境仍可以正常服务。
在上10.24.6.4上终止mysqld进程,可执行以下指令:
killall mysqld
也可以在ndb_mgm管理节点客户端查看到10.24.6.4上的SQL节点已停掉
登录到10.24.6.6上的mysql,可查看到数据依然存在
此结果说明测试成功,及当有一个SQL节点Crash后,整个MySQL-Cluster环境仍可以工作。
至此,整个MySQL-Cluster安装及配置实验已完成。实验结束后,可使用指令shell>ndb_mgm -e shutdown或者ndb_mgm>shutdown来关闭簇中的所有节点。