一、环境准备
1.1系统IP设置
准备三台服务器安装Centos6.6 64位操作系统,IP地址及角色分配如下:
Node IP Address Decristion
管理节点 192.168.43.10 负责管理整个集群
SQL节点 192.168.43.20 负责操作数据库
数据节点 192.168.43.20 负责存储数据
SQL节点 192.168.43.30 负责操作数据库
数据节点 192.168.43.30 负责存储数据
说明:SQL节点和数据节点可以在同一个节点上
1.2 修改三台机器的 etc/hosts
192.168.43.10 leader
192.168.43.20 db
192.168.43.30 slave3
1.3卸载系统自带的Mysql软件(三台机同时检测是否有自带的mysql软件,有则进行卸载)
1.3.1 查找已安装的mysql版本
[root@leader ~]# rpm -qa|grep mysql
qt-mysql-4.6.2-28.el6_5.x86_64
mysql-server-5.1.73-3.el6_5.x86_64
mysql-libs-5.1.73-3.el6_5.x86_64
mysql-devel-5.1.73-3.el6_5.x86_64
mysql-connector-odbc-5.1.5r1144-7.el6.x86_64
mysql-5.1.73-3.el6_5.x86_64
1.3.2.查看mysql运行状态及mysql版本信息和进程信息
[root@leader ~]# service mysqld status
mysqld (pid 20327) is running...
[root@leader ~]# mysql --version
mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
[root@leader ~]# ps -aux|grep mysql
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 20219 0.0 0.0 108204 1448 pts/0 S 05:56 0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql 20327 0.0 1.3 377852 25344 pts/0 Sl 05:56 0:06 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 21575 0.0 0.0 103252 840 pts/3 S+ 11:47 0:00 grep mysql
[root@leader ~]# chkconfig --list mysqld
mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
1.3.3.卸载mysql
1.3.3.1 停止mysql运行
[root@leader ~]# service mysqld stop
Stopping mysqld: [ OK ]
[root@leader ~]# service mysqld status
mysqld is stopped
1.3.3.2卸载mysql,根据rpm qa结果数进行,打印信息忽略。
[root@leader ~]# yum -y remove qt-mysql-4.6.2-28.el6_5.x86_64
[root@leader ~]# yum -y remove mysql-server-5.1.73-3.el6_5.x86_64
[root@leader ~]# yum -y remove mysql-libs-5.1.73-3.el6_5.x86_64
[root@leader ~]# yum -y remove mysql-devel-5.1.73-3.el6_5.x86_64
[root@leader ~]# yum -y remove mysql-connector-odbc-5.1.5r1144-7.el6.x86_64
[root@leader ~]# yum -y remove mysql-5.1.73-3.el6_5.x86_64
1.3.3.3执行上述命令后,验证检查结果
[root@leader ~]# rpm -qa|grep mysql
[root@leader ~]# service mysqld status
mysqld: unrecognized service
卸载后/var/lib/mysql中的数据及/etc/my.cnf不会删除,如果确定没用后就手工删除
[root@leader ~]#rm -f /etc/my.cnf
[root@leader ~]#rm -rf /var/lib/mysql
通过下面命令看看是否还有没有删的安装路径
1.4 删除存在的Mysql文件夹(三台机)
find / -name mysql
/var/lib/mysql
/var/lib/mysql/mysql
rm -rf /var/lib/mysql/mysql
rm -rf /var/lib/mysql
1.5 准备好Mysql集群软件
集群软件下载地址:http://dev.mysql.com/downloads/cluster/
软件:mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64.tar.gz
集群软件有三种格式:
a、以tar.gz结尾的二制进格式
b、以rpm结尾的rpm包形式
c、以源文件编译安装方式
rpm包安装方式,由于是自动安装的,所以不方便配置软件安装位置和数据存储位置,
所以通常选用.tar.gz结尾的二进制格式的安装文件。
注意:mysql-cluster-gpl-7.4.9-linux-glibc2.5-x86_64.tar.gz包含了mysql数据库软件和NDB集群软件。
1.6将该软件复制到三台主机上
将压缩包mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64.tar.gz放到三台机器上并进行解压
二、软件安装
2.1新建mysql用户(三台机进行)
[root@leader ~]# groupadd mysql
[root@leader ~]# useradd -r -g mysql mysql
2.2安装NDB Mysql集群软件
2.2.1解压并授权
[root@leader mysql]# tar -zvxf mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64.tar.gz /usr/local/
[root@leader mysql]#cd /usr/local/
[root@leader local]#ln -s mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64 mysql
[root@leader mysql]# chown -R mysql:mysql mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64
2.2.2在两台从节点服务器上安装mysql软件 即db&slave数据节点
由于是二进制安装,所以mysql数据库软件就安装在/usr/local/mysql下,
但数据库的数据可以存放到其它地方,如/u01/app/mysql/data/下面
注意:以下命令在两台数据节点服务器上都要执行
[root@db mysql]# mkdir -p /u01/app/mysql/data
[root@db mysql]# chown -R mysql:mysql /u01/app/mysql
[root@db bin]# cd /usr/local/mysql/bin
--进行一波初始化
[root@db bin]# ./mysqld --initialize --basedir=/usr/local/mysql --datadir=/u01/app/mysql/data --user=mysql
[root@db bin]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/u01/app/mysql/data --user=mysql
--执行以下命令即可启动服务
[root@salve3 mysql]# cp /usr/local/mysql/bin/ndbmtd /usr/local/bin
[root@salve3 mysql]# cp /usr/local/mysql/bin/ndbd /usr/local/bin
[root@salve3 mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
手动创建 my.cnf文件
[root@salve3 mysql]#vi /etc/my.cnf
[mysqld]
ndbcluster
basedir=/usr/local/mysql
datadir=/u01/app/mysql/data
port=3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip-grant-tables
[mysql_cluster]
ndb-connectstring=192.168.43.10
[root@db mysql]# service mysqld stop
Shutting down MySQL..... SUCCESS!
[root@salve3 mysql]# service mysqld stop
Shutting down MySQL..... SUCCESS!
2.2.3管理节点安装
创建集群目录
[root@leader mysql]# mkdir mysql-cluster
[root@leader mysql]# cp bin/ndb_mgm* /usr/local/bin/
[root@leader mysql]# cd /var/lib/
[root@leader lib]# mkdir mysql-cluster
[root@leader lib]# cd mysql-cluster
--编辑管理节点配置文件
[root@leader mysql-cluster]# vi config.ini
[NDBD DEFAULT]
NoOfReplicas=1 #每个数据节点的镜像数量
DataMemory=500M #每个数据节点中给数据分配的内存
IndexMemory=18M #每个数据节点中给索引分配的内存
[TCP DEFAULT]
#portnumber=2202 #数据节点的默认连接端口
[ndb_mgmd] #配置管理节点
hostname=192.168.43.10
datadir=/var/lib/mysql-cluster/ #管理节点数据(日志)目录
[ndbd] #数据节点配置
hostname=192.168.43.20
datadir=/u01/app/mysql/data/ #数据节点目录
[ndbd]
hostname=192.168.43.30
datadir=/u01/app/mysql/data/ #数据节点目录
[mysqld] #SQL节点目录
hostname=192.168.43.20
[mysqld] #SQL节点目录
hostname=192.168.43.30
编辑完之后,启动集群管理服务,如果是第一次启动,执行如下命令:
[root@leader bin]# ./ndb_mgmd -f /var/lib/mysql-cluster/config.ini
MySQL Cluster Management Server mysql-5.7.27 ndb-7.6.11
若是有新增数据节点,则需要执行初始化命令,如下:
[root@leader bin]# ./ndb_mgmd -f /var/lib/mysql-cluster/config.ini --initial
查看集群状态
[root@leader ~]# 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.43.20)
id=3 (not connected, accepting connect from 192.168.43.30)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.43.10 (mysql-5.7.27 ndb-7.6.11)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.43.20)
id=5 (not connected, accepting connect from 192.168.43.30)
有上述信息可以看出,该集群环境有一个管理节点,两个数据节点和两个sql节点
启动数据节点,执行如下命令(在db和slave3执行):
[root@db ~]# ndbd --initial
2019-11-10 16:48:26 [ndbd] INFO -- Angel connected to '192.168.43.10:1186'
2019-11-10 16:48:26 [ndbd] INFO -- Angel allocated nodeid: 2
[root@salve3 ~]# ndbd --initial
2019-11-10 16:48:33 [ndbd] INFO -- Angel connected to '192.168.43.10:1186'
2019-11-10 16:48:33 [ndbd] INFO -- Angel allocated nodeid: 3
再查看集群状态,数据节点显示已经连接上
[root@leader bin]# 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 @192.168.43.20 (mysql-5.7.27 ndb-7.6.11, starting, Nodegroup: 0)
id=3 @192.168.43.30 (mysql-5.7.27 ndb-7.6.11, starting, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.43.10 (mysql-5.7.27 ndb-7.6.11)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.43.20)
id=5 (not connected, accepting connect from 192.168.43.30)
--启动SQL节点(在db和slave3执行):
若mysqld服务已经在运行,先关掉在执行下列命令:
[root@db ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql
2019-11-10T08:56:36.145694Z mysqld_safe Logging to '/u01/app/mysql/data/db.err'.
2019-11-10T08:56:36.290555Z mysqld_safe Starting mysqld daemon with databases from /u01/app/mysql/data
[root@salve3 ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql
2019-11-10T08:56:41.423330Z mysqld_safe Logging to '/u01/app/mysql/data/salve3.err'.
2019-11-10T08:56:41.614283Z mysqld_safe Starting mysqld daemon with databases from /u01/app/mysql/data
然后再查看集群状态,显示SQL节点也已经连上:
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.43.20 (mysql-5.7.27 ndb-7.6.11, Nodegroup: 0, *)
id=3 @192.168.43.30 (mysql-5.7.27 ndb-7.6.11, Nodegroup: 1)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.43.10 (mysql-5.7.27 ndb-7.6.11)
[mysqld(API)] 2 node(s)
id=4 @192.168.43.20 (mysql-5.7.27 ndb-7.6.11)
id=5 @192.168.43.30 (mysql-5.7.27 ndb-7.6.11)
到这里,集群就搭建完成!
2.3 关闭集群操作
2.3.1 先在db和沙拉ve关闭SQL节点
[root@db ~]# service mysqld stop
Shutting down MySQL.... SUCCESS!
[root@salve3 ~]# service mysqld stop
Shutting down MySQL..... SUCCESS!
2.3.2 在主节点leader上shutdown整个集群
[root@leader bin]# 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 @192.168.43.20 (mysql-5.7.27 ndb-7.6.11, Nodegroup: 0, *)
id=3 @192.168.43.30 (mysql-5.7.27 ndb-7.6.11, Nodegroup: 1)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.43.10 (mysql-5.7.27 ndb-7.6.11)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.43.20)
id=5 (not connected, accepting connect from 192.168.43.30)
ndb_mgm> shutdown
Node 2: Cluster shutdown initiated
Node 3: Cluster shutdown initiated
Node 2: Node shutdown completed.
Node 3: Node shutdown completed.
3 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.
2.4启动整个集群
2.4.1 启动集群管理服务
[root@leader ~]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini
MySQL Cluster Management Server mysql-5.7.27 ndb-7.6.11
[root@leader ~]# 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.43.20)
id=3 (not connected, accepting connect from 192.168.43.30)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.43.10 (mysql-5.7.27 ndb-7.6.11)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.43.20)
id=5 (not connected, accepting connect from 192.168.43.30)
ndb_mgm>
2.4.2启动数据节点
[root@db ~]# ndbd
2019-11-10 17:07:46 [ndbd] INFO -- Angel connected to '192.168.43.10:1186'
2019-11-10 17:07:46 [ndbd] INFO -- Angel allocated nodeid: 2
[root@salve3 ~]# ndbd
2019-11-10 17:07:49 [ndbd] INFO -- Angel connected to '192.168.43.10:1186'
2019-11-10 17:07:49 [ndbd] INFO -- Angel allocated nodeid: 3
2.4.3启动SQL节点
[root@salve3 ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql
2019-11-10T09:06:33.600522Z mysqld_safe Logging to '/u01/app/mysql/data/salve3.err'.
2019-11-10T09:06:33.693927Z mysqld_safe Starting mysqld daemon with databases from /u01/app/mysql/data
[root@db ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql
2019-11-10T09:06:27.397679Z mysqld_safe Logging to '/u01/app/mysql/data/db.err'.
2019-11-10T09:06:27.443878Z mysqld_safe Starting mysqld daemon with databases from /u01/app/mysql/data
查看集群状态:
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.43.20 (mysql-5.7.27 ndb-7.6.11, Nodegroup: 0, *)
id=3 @192.168.43.30 (mysql-5.7.27 ndb-7.6.11, Nodegroup: 1)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.43.10 (mysql-5.7.27 ndb-7.6.11)
[mysqld(API)] 2 node(s)
id=4 @192.168.43.20 (mysql-5.7.27 ndb-7.6.11)
id=5 @192.168.43.30 (mysql-5.7.27 ndb-7.6.11)
三、集群数据测试
1.做个简单的测试,在节点db创建数据库test1,并建表tab,再查看是否同步,然后再做删除操作!
2.做删除操作,删掉整个test1库。
3.到这里就测试完成了,但值得注意的是,要是在建表没有指定存储引擎的方式,是不会自动同步的。
即要指定engine=ndbcluster。