您的位置:首页 > 数据库 > MySQL

MySQL Cluster 3台机器搭建集群环境

2015-05-25 15:28 465 查看


转自:http://blog.csdn.net/chengfei112233/article/details/7703812
系统:Centos6.1
mysql软件:mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz

下载地址: http://cdn.mysql.com/Downloads/MySQL-Cluster-7.2/mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz

1. 集群配置如下
(3台机器)

管理节点:192.168.6.134

数据节点1:192.168.6.135
数据节点2:192.168.6.136
sql节点1:192.168.6.135
sql节点2:192.168.6.136

我使用3台机器进行配置,其中两台机器上的数据节点与sql节点在一起

2. 管理节点安装(192.168.6.134)
安装

[plain]
view plaincopyprint?

shell> groupadd mysql  
shell> useradd mysql -g mysql  
shell> mv mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz /usr/local/  
shell> cd /usr/local/  
shell> tar zxvf mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz /usr/local/  
shell> mv mysql-cluster-gpl-7.2.6-linux2.6-x86_64 mysql  
shell> chown -R mysql:mysql mysql  
shell> cd mysql  
shell> scripts/mysql_install_db --user=mysql  

shell> groupadd mysql
shell> useradd mysql -g mysql
shell> mv mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz /usr/local/
shell> cd /usr/local/
shell> tar zxvf mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz /usr/local/
shell> mv mysql-cluster-gpl-7.2.6-linux2.6-x86_64 mysql
shell> chown -R mysql:mysql mysql
shell> cd mysql
shell> scripts/mysql_install_db --user=mysql

配置管理节点

[plain]
view plaincopyprint?

shell> mkdir /var/lib/mysql-cluster  
shell> cd /var/lib/mysql-cluster  

shell> mkdir /var/lib/mysql-cluster
shell> cd /var/lib/mysql-cluster

vi config.ini 添加以下内容

[plain]
view plaincopyprint?

[ndbd default]  
NoOfReplicas=2  
DataMemory=80M  
IndexMemory=18M  
[ndb_mgmd]  
NodeId=1  
Hostname=192.168.6.134  //管理IP  
datadir=/usr/local/mysql/logs  
[ndbd]  
NodeId=2  
Hostname=192.168.6.135           //数据节点1  
datadir=/usr/local/mysql/data/      //数据文件位置  
[ndbd]  
NodeId=3  
Hostname=192.168.6.136              //数据节点2  
datadir=/usr/local/mysql/data/        //数据文件位置  
[MYSQLD]  
[MYSQLD]  

[ndbd default]
NoOfReplicas=2
DataMemory=80M
IndexMemory=18M
[ndb_mgmd]
NodeId=1
Hostname=192.168.6.134  //管理IP
datadir=/usr/local/mysql/logs
[ndbd]
NodeId=2
Hostname=192.168.6.135           //数据节点1
datadir=/usr/local/mysql/data/      //数据文件位置
[ndbd]
NodeId=3
Hostname=192.168.6.136              //数据节点2
datadir=/usr/local/mysql/data/        //数据文件位置
[MYSQLD]
[MYSQLD]


3. 数据节点安装(192.168.6.135,[b]192.168.6.136)[/b]

执行相同的安装操作

[plain]
view plaincopyprint?

shell> groupadd mysql  
shell> useradd mysql -g mysql  
shell> mv mysql-cluster-gpl-7.2.6-linux2.6-x86_64 /usr/local/  
shell> cd /usr/local/  
shell> tar zxvf mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz  
shell> mv mysql-cluster-gpl-7.2.6-linux2.6-x86_64 mysql  
shell> chown -R mysql:mysql mysql  
shell> cd mysql  
shell> scripts/mysql_install_db --user=mysql  

shell> groupadd mysql
shell> useradd mysql -g mysql
shell> mv mysql-cluster-gpl-7.2.6-linux2.6-x86_64 /usr/local/
shell> cd /usr/local/
shell> tar zxvf mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz
shell> mv mysql-cluster-gpl-7.2.6-linux2.6-x86_64 mysql
shell> chown -R mysql:mysql mysql
shell> cd mysql
shell> scripts/mysql_install_db --user=mysql


[plain]
view plaincopyprint?

shell> cp support-files/mysql.server /etc/init.d/mysqld  

shell> cp support-files/mysql.server /etc/init.d/mysqld


配置数据节点
vi /etc/my.cnf 添加入如下内容

[plain]
view plaincopyprint?

[mysqld]  
ndbcluster                        # run NDB storage engine  
ndb-connectstring=192.168.6.134   # location of management server  
  
# Options for ndbd process:  
[mysql_cluster]  
ndb-connectstring=192.168.6.134   # location of management server  

[mysqld]
ndbcluster                        # run NDB storage engine
ndb-connectstring=192.168.6.134   # location of management server

# Options for ndbd process:
[mysql_cluster]
ndb-connectstring=192.168.6.134   # location of management server


4. 启动管理节点服务(192.168.6.134)

[plain]
view plaincopyprint?

shell> /usr/local/mysql/bin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini --initial  
shell> netstat -lntpu  
看到1186开放说明启动正常  

shell> /usr/local/mysql/bin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini --initial
shell> netstat -lntpu
看到1186开放说明启动正常


[plain]
view plaincopyprint?

★必须注意:只是在第一次启动或在备份/恢复或配置变化后重启ndbd时,才加–initial参数!  

★必须注意:只是在第一次启动或在备份/恢复或配置变化后重启ndbd时,才加–initial参数!


5. 启动数据节点服务[b]192.168.6.135,192.168.6.136)
 Data (NDBD) node[/b]

[plain]
view plaincopyprint?

shell> /usr/local/mysql/bin/ndbd --initial  

shell> /usr/local/mysql/bin/ndbd --initial
备注: 启动此处时,需要为防火墙开启1186,3306端口。

在两台机器上启动服务, 执行相同操作
★必须注意:只是在第一次启动或在备份/恢复或配置变化后重启ndbd时,才加–initial参数!
启动结果如下:

[plain]
view plaincopyprint?

2012-06-30 05:01:11 [ndbd] INFO     -- Angel connected to '192.168.6.134:1186'  
2012-06-30 05:01:11 [ndbd] INFO     -- Angel allocated nodeid: 3  

2012-06-30 05:01:11 [ndbd] INFO     -- Angel connected to '192.168.6.134:1186'
2012-06-30 05:01:11 [ndbd] INFO     -- Angel allocated nodeid: 3


6. 启动SQL节点服务192.168.6.135,192.168.6.136)
 MySQL server (SQL) node

[plain]
view plaincopyprint?

shell> service mysqld start  

shell> service mysqld start


如果在创建数据库时遇到如下错误

[plain]
view plaincopyprint?

Access denied for user ''@'localhost' to database 'mysql  

Access denied for user ''@'localhost' to database 'mysql
解决方法:

[plain]
view plaincopyprint?

首先停止mysqld服务,然后后台运行  
shell> /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &  
shell> service mysqld start  
再用root登录,就一切正常  

首先停止mysqld服务,然后后台运行
shell> /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &
shell> service mysqld start
再用root登录,就一切正常


看到nbdb服务和3306端口说明启动OK

[b]7. 功能测试[/b]

在管理节点(192.168.6.134)上查看服务状态

[plain]
view plaincopyprint?

shell> /usr/local/mysql/bin/ndb_mgm  
ndb_mgm> show  

shell> /usr/local/mysql/bin/ndb_mgm
ndb_mgm> show


[plain]
view plaincopyprint?

Connected to Management Server at: localhost:1186  
Cluster Configuration  
---------------------  
[ndbd(NDB)]     2 node(s)  
id=2    @192.168.6.135  (mysql-5.5.22 ndb-7.2.6, Nodegroup: 0, Master)  
id=3    @192.168.6.136  (mysql-5.5.22 ndb-7.2.6, Nodegroup: 0)  
  
[ndb_mgmd(MGM)] 1 node(s)  
id=1    @192.168.6.134  (mysql-5.5.22 ndb-7.2.6)  
  
[mysqld(API)]   2 node(s)  
id=4    @192.168.6.135  (mysql-5.5.22 ndb-7.2.6)  
id=5    @192.168.6.136  (mysql-5.5.22 ndb-7.2.6)  

Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2    @192.168.6.135  (mysql-5.5.22 ndb-7.2.6, Nodegroup: 0, Master)
id=3    @192.168.6.136  (mysql-5.5.22 ndb-7.2.6, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @192.168.6.134  (mysql-5.5.22 ndb-7.2.6)

[mysqld(API)]   2 node(s)
id=4    @192.168.6.135  (mysql-5.5.22 ndb-7.2.6)
id=5    @192.168.6.136  (mysql-5.5.22 ndb-7.2.6)


可以看到这里的数据节点、管理节点、sql节点都是正常的

★非常重要:

1.在建表的时候一定要用ENGINE=NDB或ENGINE=NDBCLUSTER指定使用NDB集群存储引擎,或用ALTER TABLE选项更改表的存储引擎。

2.NDB表必须有一个主键,因此创建表的时候必须定义主键,否则NDB存储引擎将自动生成隐含的主键。

3.Sql节点的用户权限表仍然采用MYISAM存储引擎保存的,所以在一个Sql节点创建的MySql用户只能访问这个节点,如果要用同样的用户访问别的Sql节点,需要在对应的Sql节点追加用户。虽然在MySql Cluster7.2版本开始提供了”用户权限共享”.

测试一:

现在我们在其中一个数据节点上进行相关数据库的创建,然后到另外一个数据节点上看看数据是否同步

在数据节点1(192.168.6.135)上执行:

[plain]
view plaincopyprint?

shell> /usr/local/mysql/bin/mysql -u root -p  
mysql>show databases;  
mysql>create database aa;  
mysql>use aa;  
mysql>CREATE TABLE ctest2 (i INT) ENGINE=NDB; //这里必须指定数据库表的引擎为NDB,否则同步失败  
mysql> INSERT INTO ctest2 () VALUES (1);  
mysql> SELECT * FROM ctest2;  

shell> /usr/local/mysql/bin/mysql -u root -p
mysql>show databases;
mysql>create database aa;
mysql>use aa;
mysql>CREATE TABLE ctest2 (i INT) ENGINE=NDB; //这里必须指定数据库表的引擎为NDB,否则同步失败
mysql> INSERT INTO ctest2 () VALUES (1);
mysql> SELECT * FROM ctest2;
然后在数据节点2上看数据是否同步过来了

经过测试,在非master上创建数据,可以同步到master上

查看表的引擎是不是NDB,>show create table 表名;

测试二、关闭一个数据节点 ,在另外一个节点写输入,开启关闭的节点,看数据是否同步过来

首先把数据节点1重启,然后在节点2上添加数据

在节点2(192.168.6.136)上操作如下:
mysql> create database bb;

[plain]
view plaincopyprint?

mysql> use bb  
mysql> CREATE TABLE ctest3 (i INT) ENGINE=NDB;  
mysql> use aa  
mysql> INSERT INTO ctest2 () VALUES (3333);mysql> SELECT * FROM ctest2;  

mysql> use bb
mysql> CREATE TABLE ctest3 (i INT) ENGINE=NDB;
mysql> use aa
mysql> INSERT INTO ctest2 () VALUES (3333);mysql> SELECT * FROM ctest2;


[plain]
view plaincopyprint?

等节点1启动完毕,启动节点1的服务#/usr/local/mysql/bin/ndbd --initial#service mysqld start然后登录进去查看数据# /usr/local/mysql/bin/mysql -u root –p可以看到数据已经同步过来了,说明数据可以双向同步了。  

等节点1启动完毕,启动节点1的服务#/usr/local/mysql/bin/ndbd --initial#service mysqld start然后登录进去查看数据# /usr/local/mysql/bin/mysql -u root –p可以看到数据已经同步过来了,说明数据可以双向同步了。


8. 关闭集群

1.关闭管理节点和数据节点,只需要在管理节点(ClusterMgm--134)里执行:

[plain]
view plaincopyprint?

shell> /usr/local/mysql/bin/ndb_mgm -e shutdown  

shell> /usr/local/mysql/bin/ndb_mgm -e shutdown
显示

[plain]
view plaincopyprint?

Connected to Management Server at: localhost:1186  
2 NDB Cluster node(s) have shutdown.  
Disconnecting to allow management server to shutdown.  

Connected to Management Server at: localhost:1186
2 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.


2.然后关闭Sql节点(135,136),分别在2个节点里运行:

[plain]
view plaincopyprint?

shell> /etc/init.d/mysql stop  
Shutting down MySQL... SUCCESS!  

shell> /etc/init.d/mysql stop
Shutting down MySQL... SUCCESS!


注意:要再次启动集群,就按照第4,5,6步的启动步骤即可,不过这次启动数据节点的时候就不要加”–initial”参数了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: