MySQL NDB Cluster 最新版7.6.7 安装部署
MySQL NDB Cluster安装
MySQL集群是一个无共享的(share-nothing)、分布式节点架构的存储方案,目的是提供容错性和高性能。
MySQL NDB Cluster架构如图:
MySQL NDB Cluster架构按照节点类型分为三部分:
- 管理节点:管理节点通过对配置文件conifg.ini的维护来对其他节点进行管理。该文件可以用来配置有多少副本需要维护、在数据节点上为数据和索引分配多少内存、数据节点的位置、数据节点上保存数据的磁盘的位置,SQL节点的位置信息等,管理节点只能有一个。
- SQL节点:SQL节点可以理解为应用程序和数据节点的一个桥梁,应用程序不能直接访问数据节点,只能先访问SQL节点,然后SQL节点再去访问数据节点来返回数据,Cluster中可以有多个SQL节点,通过每个SQL节点查询到的数据都是一致的,一般来说,SQL节点越多,分配到每个SQL节点的负载就越小,系统的整体性能就越好;
- 数据节点:数据节点用来存放数据,可有多个数据节点。
下面演示MySQL NDB Cluster的部署安装:
在5台VMware 虚拟机上安装部署
在每台虚拟机上执行
[[email protected] ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.133.131 mgr
172.16.133.132 ndb01
172.16.133.133 ndb02
172.16.133.134 ndb03
172.16.133.135 ndb04
使局域网内集群可以通过主机名访问
1、环境信息:
- 管理节点mgr:172.16.133.131
- SQL节点ndb01:172.16.133.132
- SQL节点ndb02:172.16.133.133
- 数据节点ndb03:172.16.133.134
- 数据节点ndb04:172.16.133.135
2、下载MySQL NDB Cluster软件;
3、安装SQL节点,在每个SQL节点都执行如下操作;
1)创建mysql用户和组,如果该用户和组不存在:
[[email protected] ~]# userdel mysql (因为是新系统,所以就把现有的MySQL用户给删除了)
[[email protected] ~]# groupadd mysql
[[email protected] ~]# useradd -g mysql mysql
[[email protected] ~]# passwd mysql (MySQL密码要设置的复杂一点,或者在创建用户的时候设置不让登陆系统
[[email protected] ~]# mkdir -p /data/mysql/db_file 在mysql 下创建bin_log 、log 目录
[[email protected] ~]# chown -R mysql:mysql /data
2)解压缩 mysql-cluster软件;
[[email protected] ~]# tar -xvf mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64.tar.gz
[[email protected] ~]# mv mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64 /usr/local/mysql
3)安装数据库系统;
[[email protected] ~]# su - mysql
[[email protected]~]$
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/db_file/
-----注意,初始化数据库会生成登录MySQL数据库的root密码,一定要记住,一会登录数据库要用
编辑SQL节点 my.cnf 文件
[mysqld]
character_set_server=utf8
collation_server=utf8_bin
max_allowed_packet = 128M
event_scheduler =1
open_files_limit = 20480
max_connections = 3000
default-storage-engine=ndbcluster
basedir=/usr/local/mysql/
datadir=/data/mysql/db_file
user=mysql
log-error=/data/mysql/log/sqlnode.err
ndbcluster
ndb-connectstring=mgr
[mysql_cluster]
ndb-connectstring=mgr
[[email protected] ~]# vi /etc/profile 在最下方加上
export PATH
PATH=$PATH:/usr/local/mysql/bin
保存退出
source /etc/profile 使环境生效
4)拷贝MySQL启动脚本;
[[email protected] ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.server
[[email protected] ~]$ /usr/local/mysql/support-files/mysql.server start
[[email protected] ~]$ /usr/local/mysql/bin/mysqld_safe --defaults-file=/ect/my.cnf --user=mysql & (效果一样)
5)进入数据库检查;
[[email protected] ~]$ /usr/local/mysql/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.18-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> set password='root'; (因为刚才使用的密码是个默认的登录密码,所以我们要修改)
mysql> flush privileges;
然后再重新登录MySQL即可,进行检查
这样,二进制的MySQL SQL节点就顺利安装完成了。
4、安装数据节点,在每个数据节点都执行如下操作;
[[email protected] ~]# userdel mysql (因为是新系统,所以就把现有的MySQL用户给删除了)
[[email protected] ~]# groupadd mysql
[[email protected] ~]# useradd -g mysql mysql
[[email protected] ~]# passwd mysql (MySQL密码要设置的复杂一点,或者在创建用户的时候设置不让登陆系统
[[email protected] ~]# tar -xvf mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64.tar.gz
[[email protected] ~]# mv mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64 /usr/local/mysql
创建数据文件存放路径
[[email protected] ~]# mkdir -p /data/mysql/db_file 在mysql 下创建log目录
编辑数据节点my.cnf
[mysqld]
basedir=/usr/local/mysql/
datadir=/data/mysql/db_file
user=mysql
log-error=/data/mysql/log/sqlnode.err
ndbcluster
ndb-connectstring=mgr
[mysql_cluster]
ndb-connectstring=mgr
vi /etc/profile 在最下方加上
export PATH
PATH=$PATH:/usr/local/mysql/bin
保存退出
source /etc/profile 使环境生效
建议用上述方式,因为以后使用我们可能会使用其他的管理工具查询集群状态,如 ndb_cofig 等
或者直接scp sql节点 将ndbd、ndbmtd拷贝至/usr/local/bin/目录中
5、安装管理节点;
[[email protected] ~]# tar -xvf mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64.tar.gz
[[email protected] ~]# mv mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64 /usr/local/mysql
[[email protected] ~]# mkdir -p /data/mysql-cluster 作为管理节点文件存放路径
[[email protected] ~]# vi /etc/profile 在最下方加上
export PATH
PATH=$PATH:/usr/local/mysql/bin
保存退出
source /etc/profile 使环境生效
建议用上述方式,因为以后使用我们可能会使用其他的管理工具查询集群状态,如 ndb_cofig 等
或者直接scp sql节点 将ndb_mgm、ndb_mgmd拷贝至/usr/local/bin/目录中
编辑管理节点config.ini
[[email protected] ~]# cat /data/mysql-cluster/config.ini
[ndbd default]
NoOfReplicas=2
DataMemory=1024M
StringMemory=5M
MaxNoOfTables = 1024
MaxNoOfAttributes = 5000000
MaxNoOfOrderedIndexes = 10000
MaxNoOfTriggers = 10240
MaxNoOfConcurrentTransactions = 4096
MaxNoOfLocalOperations=110000
MaxNoOfConcurrentOperations=100000
TransactionDeadLockDetectionTimeOut = 15000
[ndb_mgmd]
hostname=mgr
datadir=/data/mysql-cluster
[ndbd]
NodeId=2
hostname=ndb03
datadir=/data/mysql/db_file
StopOnError=0
[ndbd]
NodeId=3
hostname=ndb04
datadir=/data/mysql/db_file
StopOnError=0
[mysqld]
NodeId=4
hostname=ndb01
[mysqld]
NodeId=5
hostname=ndb02
[mysqld]
[mysqld]
各个参数作用不在详细介绍,可以自己google.根据自己的硬件及需求修改。
---注意:7.6.7版本不在支持IndexMemory,新版本将IndexMemory和DataMemory合并。
6、启动Cluster;
Cluster需要各个节点都进行启动后才可以运行,节点的启动顺序为管理节点——>数据节点——>SQL节点。
启动管理节点:
- [[email protected] mysql-cluster]# ndb_mgmd - f ./config.ini --initial (如修改config.ini中配置,需要添加该参数才生效)
- MySQL Cluster Management Server mysql-5.7.21 ndb-7.5.9
- [[email protected]]# ps aux |grep ndb_mgmd
- root 8155 2.5 0.6 504480 3380 ? Ssl 20:48 0:00 ndb_mgmd -f ./config.ini
- root 8166 13.0 0.1 112632 968 pts/0 S+ 20:48 0:00 grep --color=auto ndb_mgmd
- [[email protected] mysql-cluster]#
启动数据节点:
- [[email protected] local]# ndbd --initial (第一次启动是添加该参数,初始化数据节点,如在有数据情况下,该参数将删除所有数据,用于数据恢复时用,谨记!!!)
- 2018-09-06 20:51:40 [ndbd] INFO -- Angel connected to '10.24.32.180:1186'
- 2018-09-06 20:51:40 [ndbd] INFO -- Angel allocated nodeid: 2
启动SQL节点:
- [[email protected] ~]# service mysql.server start
- Starting MySQL........................................................................................................................................... SUCCESS!
7、节点全部成功启动后,在管理节点查看集群状态;
[[email protected] ~]# 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 @172.16.133.134 (mysql-5.7.23 ndb-7.6.7, Nodegroup: 0, *)
id=3 @172.16.133.135 (mysql-5.7.23 ndb-7.6.7, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @172.16.133.131 (mysql-5.7.23 ndb-7.6.7)
[mysqld(API)] 4 node(s)
id=4 @172.16.133.132 (mysql-5.7.23 ndb-7.6.7)
id=5 @172.16.133.133 (mysql-5.7.23 ndb-7.6.7)
id=6 (not connected, accepting connect from any host)
id=7 (not connected, accepting connect from any host)
ndb_mgm>
ndb_mgm是ndb_mgmd(MySQL Cluster Server)的客户端管理工具,通过它可以方便的检查Cluster的状态、启动备份、关闭Cluster等功能。
通过上面的状态信息,可以看到:
- 1)集群的管理服务端口是1186;
- Connected to Management Server at: localhost:1186
- 2)集群的数据节点有2个;
-
[ndbd(NDB)] 2 node(s)
id=2 @172.16.133.134 (mysql-5.7.23 ndb-7.6.7, Nodegroup: 0, *)
id=3 @172.16.133.135 (mysql-5.7.23 ndb-7.6.7, Nodegroup: 0) - 3)管理节点有一个;
- [ndb_mgmd(MGM)] 1 node(s)
id=1 @172.16.133.131 (mysql-5.7.23 ndb-7.6.7) - [ndb_mgmd(MGM)] 1 node(s)
- id=1 @10.24.32.180 (mysql-5.7.21 ndb-7.5.9)
- 4)SQL节点有4个,且处于连接状态;
- [mysqld(API)] 4 node(s)
id=4 @172.16.133.132 (mysql-5.7.23 ndb-7.6.7)
id=5 @172.16.133.133 (mysql-5.7.23 ndb-7.6.7)
id=6 (not connected, accepting connect from any host)
id=7 (not connected, accepting connect from any host)
8、测试Cluster;
使用Cluster,则表的存储引擎必须为NDB,其他类型存储引擎的数据不会保存到数据节点中,对于CLuster的一个重要功能就是防止单点故障,下面将分别进行测试:
1)NDB存储引擎测试
- --在任一SQL节点(本例用172.16.133.132)的test库创建测试表t_cluster,设置存储引擎为NDB:
- mysql> create table t_cluster(id int,name varchar(100),cdate datetime default now()) engine=NDB;
- Query OK, 0 rows affected (0.42 sec)
- mysql> insert into t_cluster(id,name) values(1,'Alen'),(2,'Cluster Test');
- Query OK, 2 rows affected (0.10 sec)
- Records: 2 Duplicates: 0 Warnings: 0
- mysql> select *from t_cluster;
- +------+--------------+---------------------+
- | id | name | cdate |
- +------+--------------+---------------------+
- | 1 | Alen | 2018-09-06 23:07:50 |
- | 2 | Cluster Test | 2018-09-06 23:07:50 |
- +------+--------------+---------------------+
- 2 rows in set (0.03 sec)
- mysql>
- --在另一SQL节点(本例用172.16.133.133)的test库查询表t_cluster,结果如下:
- mysql> select *from t_cluster;
- +------+--------------+---------------------+
- | id | name | cdate |
- +------+--------------+---------------------+
- | 1 | Alen | 2018-09-06 23:07:50 |
- | 2 | Cluster Test | 2018-09-06 23:07:50 |
- +------+--------------+---------------------+
- 2 rows in set (0.02 sec)
- mysql>
- --如上,两个SQL节点查询到的数据是一致的。
- --在SQL节点172.16.133.132将表t_cluster的存储引擎改为MyISAM,再次插入测试:
- mysql> alter table t_cluster engine=myisam;
- Query OK, 2 rows affected (0.66 sec)
- Records: 2 Duplicates: 0 Warnings: 0
- mysql> insert into t_cluster(id,name) values(3,'MyISAM Test');
- Query OK, 1 row affected (0.03 sec)
- mysql> select *from t_cluster;
- +------+--------------+---------------------+
- | id | name | cdate |
- +------+--------------+---------------------+
- | 2 | Cluster Test | 2018-09-06 23:07:50 |
- | 1 | Alen | 2018-09-06 23:07:50 |
- | 3 | MyISAM Test | 2018-09-06 23:15:02 |
- +------+--------------+---------------------+
- 3 rows in set (0.00 sec)
- mysql>
- --在SQL节点172.16.133.133再次查询表t_cluster,可以发现,表无法查询,结果如下:
- mysql> select *from t_cluster;
- ERROR 1412 (HY000): Table definition has changed, please retry transaction
- mysql>
- --在SQL节点172.16.133.132将表t_cluster的存储引擎改为NDB:
- mysql> alter table t_cluster engine=ndb;
- Query OK, 3 rows affected (0.32 sec)
- Records: 3 Duplicates: 0 Warnings: 0
- mysql>
- --在SQL节点172.16.133.133再次查询,结果如下:
- mysql> select *from t_cluster;
- +------+--------------+---------------------+
- | id | name | cdate |
- +------+--------------+---------------------+
- | 3 | MyISAM Test | 2018-03-06 23:15:02 |
- | 1 | Alen | 2018-03-06 23:07:50 |
- | 2 | Cluster Test | 2018-03-06 23:07:50 |
- +------+--------------+---------------------+
- 3 rows in set (0.01 sec)
- mysql>
- --显然,表t_cluster的数据再次同步到了数据节点,所有SQL节点又都可以正常查询数据。
9、Cluster的关闭;
- [[email protected] mysql-cluster]# ndb_mgm
- -- NDB Cluster -- Management Client --
- ndb_mgm> shutdown
- Connected to Management Server at: localhost:1186
- Node 2: Cluster shutdown initiated
- Node 3: Cluster shutdown initiated
- Node 3: Node shutdown completed.
- 3 NDB Cluster node(s) have shutdown.
- Disconnecting to allow management server to shutdown.
- Node 2: Node shutdown completed.
- ndb_mgm>
注意:集群关闭后,SQL节点不会关闭,需手工关闭;
经测试:虚拟机根本无法发挥集群高并发的性能,同时集群对网络要求很是苛刻,数据节点经常无故宕服务,弄得手忙脚乱,如果仓促上正式环境,只能说佛祖保佑!!!这也是mysql ndb cluster 没有广泛使用的原因,bug太多!!!
如有问题探讨,请加QQ群:695182221