MySQLCluster是sharednothing分布式架构,ndb存储引擎把数据放置于内存中。可以做到无单点故障。由运行于不同服务器上的的多种进程构成,组件包括SQL节点,NDBD数据节点,管理程序,以及(可能的)数据访问程序表(结构)存在SQL节点中。应用程序通过SQL节点访问这些数据表;NDBD数据节点用于保存数据;集群管理通过管理工具ndb_mgmd管理。
MySQLCluster具有高可用性、高性能和可缩放性的Cluster数据管理。
MySQL集群
1、软件准备
虚拟机搭建:
环境:CentOS6.6 minimal 版本
MySQL Cluster 7.4.4:
下载:http://dev.mysql.com/downloads/cluster/
自行选择 x86-32bit 或者 x86-64bit
我这里是实验是选择 x86-64bit
2、IP 规划
mysql cluster 5个节点,部署在2台机器上:
192.168.1.109 做为 数据节点、SQL节点、管理节点
192.168.1.110 作为 数据节点、SQL节点
3、查看服务器是否安装有mysql(2台服务器都检查)
[root@mysql1 ~]# rpm -qa|grep mysql
mysql-libs-5.1.71-1.el6.x86_64
[root@myqsql1 ~]#
4、删除
[root@mysql1 ~]# yum -y remove mysql-libs-5.1.71-1.el6.x86_64
【开始部署数据节点、SQL节点】
5、创建用户和组
[root@mysql1 ~]# groupadd mysql
[root@mysql1 ~]# useradd -r -g mysql mysql
6、解压MySQL Cluster(步骤1 已经下载并存放在 home 目录)
[root@mysql1 home]tar -zxvf mysql-cluster-advanced-7.4.4-linux-glibc2.5-x86_64.tar.gz
7、把解压出来的MySQL Cluster放在 /usr/local/mysql 目录下,修改相关用户权限
[root@mysql1 home]# mv mysql-cluster-advanced-7.4.4-linux-glibc2.5-x86_64 /usr/local/mysql
[root@mysql1 home]# cd /usr/local/mysql
[root@mysql1 mysql]# chown -R root:mysql .
[root@mysql1 mysql]# chown -R mysql data/
8、修改MySQL 配置文件 my.cnf
(1) 先把 /etc/my.cnf 备份
[root@mysql1 mysql]# mv /etc/my.cnf /etc/my.cnf.bak
(2) 把 supports-file 文件夹下的 my-default.cnf 重命名为 my.cnf 在[mysqld] 字段添加如下内容:
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
(3) 复制到 /etc/目录下:
[root@mysql1 mysql]# mv support-files/my.cnf /etc/my.cnf
9、开始创建MySQL 系统表
[root@mysql1 mysql]#
scripts/mysql_install_db basedir = /usr/local/mysql datadir = /usr/local/mysql/data --user=mysql
[root@mysql1 mysql]# yum -y install libaio-dev
系统表成功后,添加mysql root 密码:
(1)[root@mysql1 mysql]#
bin/mysqld_safe &
(2)
mysqladmin -u root -p password 'newpassword'
10、配置启动脚本
[root@mysql1 mysql]# cp support-files/mysql.server /etc/init.d/mysql
# 设置mysql服务为开机自启动
[root@mysql1 mysql]# chkconfig --add mysqld
【以上 3-10 步部署数据节点和SQL节点,在另一台规划机器做同样操作】
【部署管理节点】
管理节点只需ndb_mgm和ndb_mgmd两个文件和一个配置文件,这两个文件从数据节点上copy就行
ndb_mgmd是 mysql cluster管理服务器,ndb_mgm是客户端管理工具
11、复制管理节点所需文件
[root@mysql1 mysql]# cp bin/ndb_mgm* /usr/local/bin
12、配置MySQL服务器成数据节点和SQL节点
[root@mysql1 mysql]# vi /etc/my.cnf
改为如下内容:
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
ndbcluster
ndb-connectstring=192.168.1.109
default-storage-engine=ndbcluster
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysql_cluster]
ndb-connectstring=192.168.1.109
在另一个节点也做同样配置:
注意:两台服务器的配置要一致
13、配置管理节点(192.168.1.109)
[root@mysql1 mysql]#
mkdir /var/lib/mysql-cluster
[root@mysql1 mysql]# vi /var/lib/mysql-cluster/config.ini
加入以下内容:
[ndbd default]
NoOfReplicas=2
DataMemory=200M
IndexMemory=20M
[tcp default]
portnumber=2202
[ndb_mgmd]
#NodeId=1
hostname=192.168.1.109
datadir=/var/lib/mysql-cluster/
[ndbd]
#NodeId=2
hostname=192.168.1.109
datadir=/usr/local/mysql/data/
[ndbd]
#NodeId=3
hostname=192.168.1.110
datadir=/usr/local/mysql/data/
[mysqld]
hostname=192.168.1.109
[mysqld]
hostname=192.168.1.110
14、启动:管理节点--数据节点--SQL节点
关闭防火墙或者添加端口 1186 允许通过
[root@mysql1 mysql]# service iptables stop (我选择关闭)
启动管理节点:
[root@mysql1 mysql]# cd /usr/local/bin
[root@mysql1 bin]# yum -y install perl perl-devel (预防运行ndb_mgmd 出错)
[root@mysql1 bin]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini
MySQL Cluster Management Server mysql-5.6.23 ndb-7.4.4
[root@mysql1 bin]#
启动数据节点
[root@mysql1 bin]# ./ndbd --initial
2015-03-30 15:31:15 [ndbd] INFO -- Angel connected to '192.168.1.109:1186'
2015-03-30 15:31:15 [ndbd] INFO -- Angel allocated nodeid: 2
[root@mysql1 bin]#
启动第二个数据节点
[root@mysql2 bin]# ./ndbd --initial
2015-03-30 15:36:34 [ndbd] INFO -- Angel connected to '192.168.1.109:1186'
2015-03-30 15:36:34 [ndbd] INFO -- Angel allocated nodeid: 3
[root@myqClter2 bin]#
启动sql节点
[root@mysql1 bin]# /etc/init.d/mysql start
Starting MySQL.......................................... SUCCESS!
[root@mysql1 bin]#
[root@mysql1 bin]# ps -ef | grep mysql
[root@mysql1 bin]#
15、
客户端管理使用/usr/local/bin/ndb_mgm
[root@mysql1 bin]# cd /usr/local/bin
[root@mysql1 bin]#
[root@mysql1 bin]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: 192.168.1.109:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.1.109 (mysql-5.6.23 ndb-7.4.4, Nodegroup: 0, *)
id=3 @192.168.1.110 (mysql-5.6.23 ndb-7.4.4, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.109 (mysql-5.6.23 ndb-7.4.4)
[mysqld(API)] 2 node(s)
id=4 @192.168.1.109 (mysql-5.6.23 ndb-7.4.4)
id=5 @192.168.1.110 (mysql-5.6.23 ndb-7.4.4)
ndb_mgm>
#退出管理工具
ndb_mgm> quit
[root@mysql1 bin]#
16、测试
使用mysql cluster中需要注意两点:
一是表必须用ENGINE=NDB或ENGINE=NDBCLUSTER选项创建,使用ALTER TABLE选项更改也可以,以使用NDB Cluster存储引擎在 Cluster内复制它们。
二是每个NDB表必须要有一个主键,如果没有,NDB Cluster存储引擎将自动生成隐含主键。
连到sql节点1上,创建ndb引擎数据表t1,插入数据,连到sql节点2上,查看t1表数据,再插入数据,连到sql节点1查看,没问题就OK了。
连接sql节点1,创建下表并插入数据:
[root@mysql1 ~]# mysql -h 192.168.1.109 -p test
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.6.23-ndb-7.4.4-cluster-commercial-advanced MySQL Cluster Server - Advanced Edition (Commercial)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
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 databases;
+------------------------+
| Database |
+------------------------+
| information_schema |
| mysql |
| ndb_2_fs |
| ndbinfo |
| performance_schema |
| test |
+--------------------------+
6 rows in set (0.00 sec)
mysql>
mysql> use test
Database changed
mysql> create table t1(id int(11) not null auto_increment primary key,name varchar(10),password varchar(20),others varchar(128));
mysql> insert into t1 values(1, 'ndb','123456','1');
mysql>
insert into t1 values(2, 'ndb2','123456','2');
mysql>
insert into t1 values(3, 'nd3','123456','3');
mysql>
insert into t1 values(4, 'ndb4','123456','4');
mysql> select * from t1;
+----+------+----------+--------+
| id | name | password | others |
+----+------+----------+--------+
| 3 | ndb3 | 123456 | 3 |
| 1 | ndb | 123456 | 1 |
| 2 | ndb2 | 123456 | 2 |
| 4 | ndb4 | 123456 | 4 |
+----+------+----------+--------+
4 rows in set (0.01 sec)
mysql> exit
[root@mysql1 ~]
#
连到sql节点2上,查看t1表数据,再插入数据:
[root@mysql2 ~]
# mysql -h 192.168.1.110 -p test
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.23-ndb-7.4.4-cluster-commercial-advanced MySQL Cluster Server - Advanced Edition (Commercial)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
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 test;
Database changed
mysql> select * from t1;
+----+------+----------+--------+
| id | name | password | others |
+----+------+----------+--------+
| 1 | ndb | 123456 | 1 |
| 2 | ndb2 | 123456 | 2 |
| 4 | ndb4 | 123456 | 4 |
| 3 | ndb3 | 123456 | 3 |
+----+------+----------+--------+
4 rows in set (0.00 sec)
mysql> insert into t1 values(5, 'ndb5','123456','5');
Query OK, 1 row affected (0.00 sec)
mysql> exit
Bye
[root@mysql2 ~]
#
连到sql节点1查看,没问题就OK了。
[root@mysql1 ~]
# mysql -h 192.168.1.109 -p test
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.6.23-ndb-7.4.4-cluster-commercial-advanced MySQL Cluster Server - Advanced Edition (Commercial)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
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 test;
Database changed
mysql> select * from t1;
+----+------+----------+--------+
| id | name | password | others |
+----+------+----------+--------+
| 3 | ndb3 | 123456 | 3 |
| 5 | ndb5 | 123456 | 5 |
| 1 | ndb | 123456 | 1 |
| 2 | ndb2 | 123456 | 2 |
| 4 | ndb4 | 123456 | 4 |
+----+------+----------+--------+
5 rows in set (0.00 sec)
mysql> exit
Bye
[root@mysql1 ~]
#
17、关闭
管理节点--数据节点--SQL节点
命令ndb_mgm -e shutdown关闭管理节点和数据节点,sql节点/etc/init.d/mysqld stop关就可以了
[root@mysql1 ~]# ndb_mgm -e shutdown
Connected to Management Server at: 192.168.55.15:1186
3 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.
[root@mysql1 ~]#
[root@mysql1 ~]# service mysql stop
Shutting down MySQL..... SUCCESS!
[root@mysql1 ~]#
[root@mysql2 ~]# service mysql stop
Shutting down MySQL..... SUCCESS!
engine=ndbcluster或者engine=ndb的表才能使用Data node所存储。其他engine即放在SQL node本地,就像单机版一样。
这东西用着如何就根据实际场景测吧,其相关的bug可以到http://bugs.mysql.com/search.php查看