安装要求:
安装环境:Centos6.5
安装方式:源码安装
软件名称: mysql-cluster-gpl-7.2.26-linux2.6-x86_64.tar.gz
软件安装位置:/usr/local/mysql
数据存放位置:/var/mysql/data
日志存放位置:/var/mysql/logs
集群设计:
首先设计集群的安装分配方式,至少需要三台服务器,sql节点和数据节点可以放在同一台服务器上,服务器分配如下:
管理节点:192.168.147.129
sql节点和数据节点1:192.168.147.130
sql节点和数据节点2:192.168.147.132
目前此设计存在的问题是管理节点是单点的,129管理节点服务器挂掉后整个集群就会瘫痪。
检查安装的Mysql:
检查系统中已经安装过的Mysql信息,如果有卸载之:
[[email protected] /]# rpm -qa | grep mysql #查看mysql信息
[[email protected] /]# service mysql status #查看mysql服务的状态
[[email protected] /]# /etc/init.d/mysqld stop #关闭目前的mysql服务
[[email protected] /]# ps -ef | grep mysql #检验mysql是否已经关闭 如果没关闭,执行kill -9 端口号
[[email protected] /]# rpm -e --allmatches --nodeps mysql mysql-server #执行删除或者用如下口令
[[email protected] /]# yun remove mysql mysql-*
[[email protected] /]#rm -rf /var/lib/mysql #删除Mysql的安装目录
管理节点的安装:
安装管理节点在(192.168.147.129)上
[[email protected] /]# groupadd mysql
[[email protected] /]# useradd mysql -g mysql
[[email protected] /]# cd /usr/local
[[email protected] local]# tar -zxvf mysql-cluster-gpl-7.2.26-linux2.6-x86_64.tar.gz
[[email protected] local]# mv mysql-cluster-gpl-7.2.26-linux2.6-x86_64 mysql
[root@wjx local]# chown -R mysql:mysql mysql
[[email protected] local]# cd mysql
[[email protected] mysql]# scripts/mysql_install_db --user=mysql
配置管理节点
[[email protected] ~]# mkdir /var/lib/mysql-cluster
[[email protected] ~]# cd /var/lib/mysql-cluster
[[email protected] mysql-cluster]# vi + /var/lib/mysql-cluster/config.ini
在config.ini中添加如下内容
[NDBD DEFAULT]
NoOfReplicas=1
[TCP DEFAULT]
portnumber=3306
[NDB_MGMD]
#设置管理节点服务器
HostName=192.168.147.129
DataDir=/var/mysql/data
[NDBD]
#设置存储节点服务器(NDB节点)
HostName=192.168.147.130
DataDir=/var/mysql/data
[NDBD]
#第二个NDB节点
HostName=192.168.147.132
DataDir=/var/mysql/data
[MYSQLD]
#设置SQL节点服务器
HostName=192.168.147.130
[MYSQLD]
#第二个SQL节点
HostName=192.168.147.132
管理节点启动
[[email protected] ~]# /usr/local/mysql/bin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini#启动节点显示如下内容
MySQL Cluster Management Server mysql-5.5.53 ndb-7.2.26
2016-10-22
00:40:32 [MgmtSrvr] INFO -- The default config directory
‘/usr/local/mysql/mysql-cluster‘ does not exist. Trying to create
it...
2016-10-22 00:40:32 [MgmtSrvr] INFO -- Sucessfully created config directory
2016-10-22 00:40:32 [MgmtSrvr] WARNING -- at line 4: [TCP] portnumber is deprecated
[[email protected] ~]# mkdir /var/mysql/logs
[[email protected] ~]# netstat -lntpu
[[email protected] mysql-cluster]# /usr/local/mysql/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 (not connected, accepting connect from 192.168.147.130)
id=3 (not connected, accepting connect from 192.168.147.132)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.147.129 (mysql-5.5.53 ndb-7.2.26)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.147.130)
id=5 (not connected, accepting connect from 192.168.147.132)
ndb_mgm>
管理节点的关闭:
[root@wjx /]# /usr/local/mysql/bin/ndb_mgm -e shutdown
数据节点的安装
数据节点1: 192.168.147.130
数据节点2: 192.168.147.132
[[email protected] /]# groupadd mysql
[[email protected] /]# useradd mysql -g mysql
[[email protected] /]# cd /usr/local
[[email protected] local]# tar -zxvf mysql-cluster-gpl-7.2.26-linux2.6-x86_64.tar.gz
[[email protected] local]# mv mysql-cluster-gpl-7.2.26-linux2.6-x86_64 mysql
[[email protected] local]# chown -R mysql:mysql mysql
[[email protected] local]# cd mysql
[[email protected] mysql]# scripts/mysql_install_db --user=mysql
[[email protected] mysql]# cp support-files/my-medium.cnf /etc/my.cnf
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld
配置数据节点
[[email protected] mysql]# mkdir -p /var/mysql/data
[[email protected] mysql]# mkdir /var/mysql/logs
[[email protected] mysql]# vi /etc/my.cnf #添加如下内容
[MYSQLD]
ndbcluster
ndb-connectstring=192.168.147.129
[MYSQL_CLUSTER]
ndb-connectstring=192.168.147.129
[NDB_MGM]
connect-string=192.168.147.129
数据节点的启动
[[email protected] mysql]# /usr/local/mysql/bin/ndbd --initial #只是在第一次启动或在备份/恢复或配置变化后重启ndbd时,才加–initial参数!
第一次启动如下:
2016-10-22 13:43:53 [ndbd] INFO -- Angel connected to ‘192.168.147.129:1186‘
2016-10-22 13:43:53 [ndbd] INFO -- Angel allocated nodeid: 2
[[email protected] mysql]# /usr/local/mysql/bin/ndbd #正常启动方式
数据节点的关闭
[[email protected] /]# /etc/rc.d/init.d/mysqld stop #或者
[[email protected] mysql]# /etc/init.d/mysql stop
Shutting down MySQL.. SUCCESS!
/usr/local/mysql/bin/mysqladmin -uroot shutdown
SQL 节点的安装
SQL节点和存储节点(NDB节点)安装相同,都执行以下操作;
sql节点1: 192.168.147.130
sql节点2: 192.168.147.132
[[email protected] /]# groupadd mysql
[[email protected] /]# useradd mysql -g mysql
[[email protected] /]# cd /usr/local
[[email protected] local]# tar -zxvf mysql-cluster-gpl-7.2.26-linux2.6-x86_64.tar.gz
[[email protected] local]# mv mysql-cluster-gpl-7.2.26-linux2.6-x86_64 mysql
[[email protected] local]# chown -R mysql:mysql mysql
[[email protected] local]# cd mysql
[[email protected] mysql]# scripts/mysql_install_db --user=mysql
[[email protected] mysql]# cp support-files/my-medium.cnf /etc/my.cnf
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld
SQL节点配置
[[email protected] mysql]# mkdir -p /var/mysql/data #创建存储数据的文件夹
[[email protected] mysql]# mkdir /var/mysql/logs #创建存储日志的文件夹
[[email protected] mysql]# vi /usr/local/mysql/my.cnf #修改配置文件
在my.cnf中追加以下内容
[MYSQLD]
ndbcluster
ndb-connectstring=192.168.147.129
[MYSQL_CLUSTER]
ndb-connectstring=192.168.147.129
[NDB_MGM]
connect-string=192.168.147.129
数据节点和sql节点安装在同一台服务器时3306端口会冲突导致数据库服务启动不了只需要在my.cnf中将端口号改了即可,如下
[client]
#password = your_password
port = 3308
socket = /tmp/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3308
socket = /tmp/mysql.sock
skip-external-locking
SQL节点启动
[[email protected] mysql]# service mysqld start
Starting MySQL.. SUCCESS!
SQL节点关闭
[[email protected] mysql]# /usr/local/mysql/bin/mysqladmin -uroot shutdown #最直接的方式
[[email protected] mysql]# service mysqld stop
[[email protected] mysql]# /etc/init.d/mysqld stop
#至此数据节点和SQL节点安装配置完毕只需要在.130和.132两台服务器上启动数据节点服务和mysql服务即可,启动后正常显示如下
[[email protected] ~]# /usr/local/mysql/bin/ndbd
2016-10-22 15:17:58 [ndbd] INFO -- Angel connected to ‘192.168.147.129:1186‘
2016-10-22 15:17:58 [ndbd] INFO -- Angel allocated nodeid: 3
[[email protected] ~]# service mysqld start
Starting MySQL SUCCESS!
功能测试
在管理节点(192.168.147.129)上查看服务状态
[[email protected] ~]# /usr/local/mysql/bin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini
MySQL Cluster Management Server mysql-5.5.53 ndb-7.2.26
[[email protected] ~]# /usr/local/mysql/bin/ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.147.130 (mysql-5.5.53 ndb-7.2.26, Nodegroup: 0, *)
id=3 @192.168.147.132 (mysql-5.5.53 ndb-7.2.26, no nodegroup)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.147.129 (mysql-5.5.53 ndb-7.2.26)
[mysqld(API)] 2 node(s)
id=4 @192.168.147.130 (mysql-5.5.53 ndb-7.2.26)
id=5 @192.168.147.132 (mysql-5.5.53 ndb-7.2.26)
可以看到这里的数据节点、管理节点、sql节点都是正常的。
注意:
1. 在建表的时候一定要用ENGINE=NDB或ENGINE=NDBCLUSTER指定使用NDB集群存储引擎,或用ALTER TABLE选项更改表的存储引擎。
2. NDB表必须有一个主键,因此创建表的时候必须定义主键,否则NDB存储引擎将自动生成隐含的主键
3. Sql节点的用户权限表仍然采用MYISAM存储引擎保存的,所以在一个Sql节点创建的MySql用户只能访问这个节点,如果要用同样的用户访问别的Sql节点,需要在对应的Sql节点追加用户。虽然在MySql Cluster7.2版本开始提供了”用户权限共享”。
数据同步测试
在一个数据节点上进行相关数据库的创建,然后到另外一个数据节点上看看数据是否同步。
第1步:SQL节点1(192.168.147.130)上增加数据:
[[email protected] mysql]# /etc/rc.d/init.d/mysqld status #检验mysql是否运行
[[email protected] mysql]# /etc/rc.d/init.d/mysqld start #启动mysql
[[email protected] mysql]# /usr/local/mysql/bin/mysql -u root -p
Enter password:
mysql> show databases;
mysql> create database testdb2;
mysql> use testdb2;
mysql> CREATE TABLE td_test2 (i INT) ENGINE=NDB; #这里必须指定数据库表的引擎为NDBCLUSTER,与配置文件中的名称相同
mysql> INSERT INTO td_test2() VALUES (1);
mysql> INSERT INTO td_test2() VALUES (152);
mysql> SELECT * FROM td_test2;
第2步:进入到SQL节点2(192.168.147.132)上查看数据
mysql> use testdb2;
Database changed
mysql> SELECT * FROM td_test2;
+------+
| i |
+------+
| 152|
| 1 |
+------+
2 rows in set (0.01 sec)
第3步:反向测试,SQL节点2(192.168.147.132)上增加数据:
mysql> create database bb;
mysql> use bb;
mysql> CREATE TABLE td_test3 (i INT) ENGINE=NDB;
mysql> INSERT INTO td_test3 () VALUES (98);
mysql> SELECT * FROM td_test3;
第四步:SQL节点1(192.168.15.232)上查看数据:
mysql> use bb;
Database changed
mysql> SELECT * FROM td_test3;
+------+
| i |
+------+
| 98 |
+------+
1 row in set (0.00 sec)
关闭集群
先关闭管理节点,然后关闭SQL节点和数据节点。
集群启动操作顺序
要再次启动集群,按照以下顺序执行:
管理节点 -> 数据节点 –> SQL节点 注意:此次启动数据节点时不要加”–initial”参数。
启动中的常见错误
错误提示:
Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘ (2)
解决办法1(端口占用) netstat -anp |grep 3306
kill -9 进程号
解决办法1(权限问题) [[email protected] mysql]# chown -R mysql:mysql /var/mysql //修改自定义文件夹的访问权限
修改root用户空密码语句:
use mysql;
update user setPassword=password(‘root‘) where User=‘root‘;
set PASSWORD FOR‘root‘@‘localhost‘ = PASSWORD(‘root‘);
赋予root用户所有权限:
grant allprivileges on *.* to ‘root‘@‘%‘identified by ‘root‘ with grant option;
权限即刻生效:
flush privileges;