IP规划
mgm管理节点:192.168.88.199
data1: 192.168.88.204
data2: 192.168.88.205
sql1: 192.168.88.202
sql2: 192.168.88.203
所有节点执行
[root@localhost Desktop]# service iptables stop
[root@localhost Desktop]# yum remove mysql-libs-5.1.71-1.el6.x86_64
[root@localhost Desktop]# rpm -qa|grep mysql
[root@localhost Desktop]#
同步mysql文件
添加mysql用户和组,这是必需的。
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd -g mysql mysql
解压mysql cluster
[root@localhost soft]# tar -zxf mysql-cluster-gpl-7.4.10-linux-glibc2.5-x86_64.tar.gz
[root@localhost soft]# cp -rp mysql-cluster-gpl-7.4.10-linux-glibc2.5-x86_64/ /usr/local/mysql
[root@localhost soft]# cd /usr/local/mysql
设置权限
[root@localhost mysql]# chown -R root .
[root@localhost mysql]# chown -R mysql data
[root@localhost mysql]# chgrp -R mysql .
初始表空间
[root@localhost mysql]# scripts/mysql_install_db --user=mysql
开机自启动
[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld
启动mysql服务,如果报错请参考
service mysqld start
在管理节点
[root@localhost mysql]# cp bin/ndb_mgm* /usr/local/bin
[root@localhost mysql]# cp bin/ndb_mgm* /usr/local/bin
[root@localhost mysql]# cd /usr/local/bin
[root@localhost bin]# chmod +x ndb_mgm*
配置
数据节点和SQL节点
[client]
port =3306
socket =/tmp/mysql.sock
[mysqld]
basedir =/usr/local/mysql/
datadir =/usr/local/mysql/data
user = mysql
log-error =/var/lib/mysql/mysqld.err
ndbcluster #运行NDB存储引擎
#指定管理节点 以上两行声明其为SQL节点
ndb-connectstring=192.168.88.199
[mysql_cluster]
#指定管理节点 以上两行声明其为数据节点
ndb-connectstring=192.168.88.199
管理节点:
[root@localhost bin]# cd /var/lib
[root@localhost lib]# mkdir mysql-cluster
[root@localhost lib]# cd mysql-cluster
[root@localhost mysql-cluster]# vi config.ini
[ndbd default]
NoOfReplicas=2
DataMemory=80M
IndexMemory=18M
[tcp default]
portnumber=2202
[ndb_mgmd]
hostname=192.168.88.199
datadir=/var/lib/mysql-cluster
[ndbd]
hostname=192.168.88.204
datadir=/usr/local/mysql/data
[ndbd]
hostname=192.168.88.205
datadir=/usr/local/mysql/data
[mysqld]
hostname=192.168.88.202
[mysqld]
hostname=192.168.88.203
[NDBD DEFAULT]:表示每个数据节点的默认配置在每个节点的[NDBD]中不用再写这些选项,只能有一个。
[NDB_MGMD]:表示管理节点的配置,只有一个。
[NDBD]:表示每个数据节点的配置,可以有多个。
[MYSQLD]:表示SQL节点的配置,可以有多个,分别写上不同SQL节点的IP地址,也可以什么都不写,只保留一个空节点,表示任意一个IP地址都可以进行访问,此节点的个数表明了可以用来连接数据节点的SQL节点总数。
四、启动
1、管理节点
mysql cluster 需要各个节点都 进行启动后才可以工作,节点的启动顺序为管理节点->数据节点->SQL节点。首先启动管理节点
[root@localhost mysql-cluster]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini
MySQL Cluster Management Server mysql-5.6.28 ndb-7.4.10
[root@localhost mysql-cluster]# ndb_mgm
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.88.204)
id=3 (not connected, accepting connect from 192.168.88.205)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.88.199 (mysql-5.6.28 ndb-7.4.10)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.88.202)
id=5 (not connected, accepting connect from 192.168.88.203)
命令行中的ndb_mgmd是mysql cluster的管理服务器,后面的-f表示后面的参数是启动的参数配置文件。如果在启动后过了几天又添加了一个数据节点,这时修改了配置文件启动时就必须加上--initial参数,不然添加的节点不会作用在mysql cluster中。
ndb_mgmd -f /var/lib/mysql-cluster/config.ini --initial
2、数据节点
安装后第一次启动数据节点时要加上--initial参数,其它时候不要加,除非是在备份、恢复或配置变化后重启时。
cd mysql/bin/ndbd --initial
如果显示以下信息说明启动完成
NDBD1
[root@localhost mysql]# ./bin/ndbd --initial
2016-02-18 05:00:38 [ndbd] INFO -- Angel connected to '192.168.88.199:1186'
2016-02-18 05:00:38 [ndbd] INFO -- Angel allocated nodeid: 3
NDBD1
[root@localhost mysql]# ./bin/ndbd --initial
2016-02-18 05:00:38 [ndbd] INFO -- Angel connected to '192.168.88.199:1186'
2016-02-18 05:00:38 [ndbd] INFO -- Angel allocated nodeid: 2
在管理节点查看链接状况
db_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.88.204 (mysql-5.6.28 ndb-7.4.10, Nodegroup: 0, *)
id=3 @192.168.88.205 (mysql-5.6.28 ndb-7.4.10, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.88.199 (mysql-5.6.28 ndb-7.4.10)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.88.202)
id=5 (not connected, accepting connect from 192.168.88.203)
启动mysql
[root@localhost mysql]# service mysqld start
Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid).
发现找不了日志
用mysqld——safe启动
[root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
[1] 4228
[root@localhost mysql]# 160218 05:20:37 mysqld_safe Logging to '/var/lib/mysql/mysqld.err'.
touch: cannot touch `/var/lib/mysql/mysqld.err': No such file or directory
chmod: cannot access `/var/lib/mysql/mysqld.err': No such file or directory
touch: cannot touch `/var/lib/mysql/mysqld.err': No such file or directory
chown: cannot access `/var/lib/mysql/mysqld.err': No such file or directory
160218 05:20:37 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
创建目录
mkdir /var/lib/mysql/
同样方法启动第二SQL个节点
[root@localhost data]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
查看状态
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.88.204 (mysql-5.6.28 ndb-7.4.10, Nodegroup: 0, *)
id=3 @192.168.88.205 (mysql-5.6.28 ndb-7.4.10, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.88.199 (mysql-5.6.28 ndb-7.4.10)
[mysqld(API)] 2 node(s)
id=4 @192.168.88.202 (mysql-5.6.28 ndb-7.4.10)
id=5 @192.168.88.203 (mysql-5.6.28 ndb-7.4.10)
测试
在SQL1节点上
[root@localhost bin]# /usr/local/mysql/bin/mysql
mysql> create database tt;
mysql> use tt
Database changed
mysql> create table teacher(t_id int) engine=ndb;#创建一个存储引擎为ndb的存储引擎
Query OK, 0 rows affected (0.28 sec)
ysql> insert into teacher values(133);#插入一条记录
Query OK, 1 row affected (0.01 sec)
在SQL2节点上
[root@localhost data]# /usr/local/mysql/bin/mysql
mysql> select * from tt.teacher;
+------+
| t_id |
+------+
| 133 |
+------+
1 row in set (0.07 sec)
2、模拟NDB节点Crash:
在节点2上终止掉NDB进程,然后再分别通过两个SQL节点去访问teacher表,查看是否可以正常访问,数据是否一致。如下:
查看进程
kill -9 3492杀掉一个ndb
db_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 (not connected, accepting connect from 192.168.88.204)###########被傻掉了
id=3 @192.168.88.205 (mysql-5.6.28 ndb-7.4.10, Nodegroup: 0, *)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.88.199 (mysql-5.6.28 ndb-7.4.10)
[mysqld(API)] 2 node(s)
id=4 @192.168.88.202 (mysql-5.6.28 ndb-7.4.10)
id=5 @192.168.88.203 (mysql-5.6.28 ndb-7.4.10)
ysql> select * from tt.teacher;############在sql2节点任然能够访问,数据是写在sql1上,
+------+
| t_id |
+------+
| 133 |
+------+
1 row in set (0.00 sec)
[root@localhost bin]# ps -ef |grep mysql
root 4405 2444 0 05:22 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql
mysql 4567 4405 2 05:22 pts/0 00:00:29 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql//lib/plugin --user=mysql --log-error=/var/lib/mysql/mysqld.err --pid-file=/usr/local/mysql/data/localhost.localdomain.pid
root 4670 4598 0 05:33 pts/1 00:00:00 /usr/local/mysql/bin/mysql
root 4752 4742 0 05:46 pts/2 00:00:00 grep mysql
[root@localhost bin]# kill -9 4567
[root@localhost bin]# kill -9 4670
SQL2节点任然能够访问,虽然insert在sql1上
mysql> select * from tt.teacher;
+------+
| t_id |
+------+
| 133 |
+------+
1 row in set (0.00 sec)