MySQL Cluster的基本概念
NDB是一种“内存中”存储引擎,它具有可用性高和数据一致性好的特点。。
在很多情况下,术语“节点”用于指计算机,但在讨论MySQL Cluster时,它表示的是进程。在单台计算机上可以有任意数目的节点。
有三类Cluster节点,在最低的MySQL Cluster配置中,至少有三个节点,这三类节点分别是:
-
管理(MGM)节点
这类节点的作用是管理MySQL Cluster内的其他节点,如提供配置数据、启动并停止节点、运行备份等。由于这类节点负责管理其他节点的配置,应在启动其他节点之前首先启动这类节点。MGM节点是用命令ndb_mgmd启动的。 -
数据节点
这类节点用于保存Cluster的数据。数据节点的数目与副本的数目相关,是片段的倍数。例如,对于两个副本,每个副本有两个片段,那么就有4个数据节点。没有必要有一个以上的副本。数据节点是用命令ndbd启动的。 - SQL节点
这是用来访问Cluster数据的节点。对于MySQL Cluster,客户端节点是使用NDB存储引擎的传统MySQL服务器。典型情况下,SQL节点是使用命令mysqld –ndbcluster启动的,或将ndbcluster添加到my.cnf后使用mysqld启动。
环境介绍
IP地址 | 节点类型 | 描述 |
---|---|---|
192.168.1.250 | Mgmt Node | Master管理节点 |
192.168.1.251 | SQL Node | SQL节点 |
192.168.1.252 | SQL Node | SQL节点 |
192.168.1.251 | Data Node | 数据节点 |
192.168.1.252 | Data Node | 数据节点 |
安装MySQL Cluster
[192.168.1.250:SQL节点操作]
创建用户
shell> groupadd mysql
shell> useradd -r -g mysql mysql
shell> id mysql
uid=497(mysql) gid=500(mysql) 组=500(mysql)
shell> mkdir -p /opt/mysql
shell> mkdir -p /opt/mysql/etc
shell> tar zxvf mysql-cluster-gpl-7.4.4.tar.gz
shell> cd mysql-cluster-gpl-7.4.4
shell> cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql \
-DSYSCONFDIR=/opt/mysql/etc \
-DMYSQL_DATADIR=/opt/mysql/data \
-DMYSQL_TCP_PORT=3306 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_NDB_JAVA=OFF \
-DWITH_NDBCLUSTER_STORAGE_ENGINE=ON
shell> make && make install
shell> chown -R mysql.mysql /opt/mysql/
shell> ./scripts/mysql_install_db --user=mysql --basedir=/opt/mysql/ --datadir=/opt/mysql/data/
修改root密码并配置环境变量
查看ndb数据引擎
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ndbcluster | NO | Clustered, fault-tolerant tables | NULL | NULL | NULL |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| ndbinfo | YES | MySQL Cluster system information storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
11 rows in set (0.00 sec)
关闭服务打包安装文件并解压安装到其他节点
其他节点依次执行
1. 创建用户
2. 创建目录
3. 设置环境变量
配置MySQL Cluster
配置Data Node和SQL Node
shell> vi /opt/mysql/ect/my.cnf
==================================================
[mysqld]
ndbcluster #run NDB storage engine
[mysql_cluster]
ndb-connectstring=192.168.1.250 #location of management server
==================================================
配置管理节点
shell> vi /opt/mysql/ect/config.cnf
==================================================
[ndbd default]
# Options affecting ndbd processes on all data nodes:
NoOfReplicas=1 # Number of replicas
DataMemory=80M # memory to allocate for data storage
IndexMemory=18M # memory to allocate for index storage
[tcp default]
# TCP/IP options:
portnumber=2202
[ndb_mgmd]
# Management process options:
Nodeid=1
hostname=192.168.1.250 # Hostname or IP address of MGM node
datadir=/opt/mysql/log # Directory for MGM node log files
[ndbd]
# Options for data node "A":
hostname=192.168.1.251 # Hostname or IP address
datadir=/opt/mysql/mysql_ndb # Directory for this data node's data file
[ndbd]
# Options for data node "B":
hostname=192.168.1.252 # Hostname or IP address
datadir=/opt/mysql/mysql_ndb # Directory for this data node's data files
[mysqld]
# SQL node options:
hostname=192.168.1.251
[mysqld]
hostname=192.168.1.252
==================================================
启动MySQL Cluster
启动管理节点
shell> ndb_mgmd -f /opt/mysql/etc/config.cnf
MySQL Cluster Management Server mysql-5.6.23 ndb-7.4.4
使用ndb_mgm工具(MySQL Cluster Server的客户端管理工具)管理ndb_mgmd
shell> ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 (not connected, accepting connect from 192.168.1.251)
id=3 (not connected, accepting connect from 192.168.1.252)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.250 (mysql-5.6.23 ndb-7.4.4)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.1.251)
id=5 (not connected, accepting connect from 192.168.1.252)
启动Data Node
在每个Data Node的主机,执行如下命令启动ndbd进程:
shell> ndbd
2015-03-03 22:40:28 [ndbd] INFO -- Angel connected to '192.168.1.250:1186'
2015-03-03 22:40:28 [ndbd] INFO -- Angel allocated nodeid: 2
shell> ndbd --initial
2015-03-04 06:12:34 [ndbd] INFO -- Angel connected to '192.168.1.250:1186'
2015-03-04 06:12:34 [ndbd] INFO -- Angel allocated nodeid: 3
启动SQL Node
在每个SQL Node的主机,启动mysql数据库。
shell> /opt/mysql/bin/mysqld_safe --defaults-file=/opt/mysql/etc/my.cnf &
验证MySQL Cluster
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.1.251 (mysql-5.6.23 ndb-7.4.4, Nodegroup: 0, *)
id=3 @192.168.1.252 (mysql-5.6.23 ndb-7.4.4, Nodegroup: 1)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.250 (mysql-5.6.23 ndb-7.4.4)
[mysqld(API)] 2 node(s)
id=4 @192.168.1.251 (mysql-5.6.23 ndb-7.4.4)
id=5 @192.168.1.252 (mysql-5.6.23 ndb-7.4.4)
4号SQL Node节点执行如下操作:
shell> mysql -u root –p
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ndbcluster | YES | Clustered, fault-tolerant tables | YES | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| ndbinfo | YES | MySQL Cluster system information storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
11 rows in set (0.00 sec)
mysql> create database ndb;
Query OK, 1 row affected (0.07 sec)
mysql> use ndb;
Database changed
mysql> create table t1 (id int not null primary key ,name varchar(100)) engine=ndb;
Query OK, 0 rows affected (0.16 sec)
mysql> insert into t1 values(1,'svoid'),(2,'tom');
Query OK, 2 rows affected (0.06 sec)
Records: 2 Duplicates: 0 Warnings: 0
5号SQL Node节点进行数据查询:
shell> mysql -u root -p
mysql> select * from ndb.t1;
+----+-------+
| id | name |
+----+-------+
| 1 | svoid |
| 2 | tom |
+----+-------+
2 rows in set (0.04 sec)
测试正常,MySQL Cluster配置完成。
关闭
ndb_mgm> shutdown
Node 2: Cluster shutdown initiated
Node 3: Cluster shutdown initiated
Node 3: Node shutdown completed.
Node 2: Node shutdown completed.
3 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.
关闭管理节点与数据节点。
整理自网络
Svoid
2015-03-04