要配置一个Cluster集群,至少得需要配置
1个MGM(管理结点)+1个SQL结点+2 Data 结点
MGM可以和SQL结点共用一台机器,数据结点单独占一台服务器。
这样最少也需要3台服务器。
有时候这样会觉得很麻烦,如果是学习阶段,可考虑在单台服务器搭建一个MySQL Cluster。
下文就是教你如何在一台服务器上搭建Cluster。
一,先确认Cluster的包已经正确安装
[root@cluster-one ~]# rpm -aq|grep -i mysql
MySQL-Cluster-client-gpl-7.2.10-1.el6.x86_64
MySQL-Cluster-server-gpl-7.2.10-1.el6.x86_64
MySQL-Cluster-shared-compat-gpl-7.2.10-1.el6.x86_64
MySQL-Cluster-devel-gpl-7.2.10-1.el6.x86_64
二,创建如下几个配置文件,下面启动时要用到
[root@cluster-one mysqlData]# more config.ini
[NDBD DEFAULT]
NoOfReplicas=2
[TCP DEFAULT]
#portnumber=3388
[NDB_MGMD]
NodeId = 1
hostname=localhost
datadir=/mysqlData/clustermgmd-data/
[NDBD]
NodeId = 2
hostname=localhost
datadir=/mysqlData/clusterndb1-data/
[NDBD]
NodeId = 3
hostname=localhost
datadir=/mysqlData/clusterndb2-data/
[MYSQLD]
NodeId = 4
hostname=localhost
[MYSQLD]
NodeId = 5
hostname=localhost
[MYSQLD]
[root@cluster-one mysqlData]# more my1.cnf
[mysqld]
ndbcluster
socket=/data/socket/n1.sock
pid-file=/data/socket/n1.pid
log-error = /data/log/n1_err.log
port = 3501
[mysql_cluster]
ndb-connectstring=localhost
[root@cluster-one mysqlData]# more my2.cnf
[mysqld]
ndbcluster
socket=/data/socket/n2.sock
pid-file=/data/socket/n2.pid
log-error = /data/log/n2_err.log
port = 3502
[mysql_cluster]
ndb-connectstring=localhost
[root@cluster-one mysqlData]# more /etc/my.cnf
[mysqld]
ndbcluster
socket=/var/lib/mysql/mysql.sock
pid-file=/data/socket/mysqld.pid
datadir=/mysqlData/clusterapi
log-bin=mysql-bin
log-bin-index=mysql-bin.index
binlog-format=ROW
server-id=1
port = 3401
[mysql_cluster]
ndb-connectstring=localhost
[client]
socket=/var/lib/mysql/mysql.sock
三,分别启动所有结点 (需要注意的是,如果是第一次启动,请在 管理结点/数据结点 的启动参数后面加上 --initial)
启动结点之前,请确保,上面配置文件指定的目录都存在。
不存在,请自行先创建一个,或是你修改配置文件指定其它目录。目录有:
/mysqlData/clustermgmd-data/
/mysqlData/clusterndb1-data/
/mysqlData/clusterndb2-data/
/mysqlData/clusterapi
/data/socket/mysqld.pid
/data/socket/
/data/log/
管理结点
[root@cluster-one ~]# ndb_mgmd -f /mysqlData/config.ini
MySQL Cluster Management Server mysql-5.5.29 ndb-7.2.10
数据结点1
[root@cluster-one ~]# ndbd --defaults-file=/mysqlData/my1.cnf
2013-12-20 02:35:42 [ndbd] INFO -- Angel connected to 'localhost:1186'
2013-12-20 02:35:42 [ndbd] INFO -- Angel allocated nodeid: 2
数据结点2
[root@cluster-one ~]# ndbd --defaults-file=/mysqlData/my2.cnf
2013-12-20 02:35:46 [ndbd] INFO -- Angel connected to 'localhost:1186'
2013-12-20 02:35:46 [ndbd] INFO -- Angel allocated nodeid: 3
查询结点,如果没有/etc/init.d/mysql这个文件,可以 把 “
/usr/share/mysql/mysql.server” 拷贝到/etc/init.d目录下,并改名为mysql
[root@cluster-one ~]# /etc/init.d/mysql start
Starting MySQL [ OK ]
四,启动成功后检查
检查结点连接状态
[root@cluster-one ~]# ndb_mgm -e show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @127.0.0.1 (mysql-5.5.29 ndb-7.2.10, Nodegroup: 0, Master)
id=3 @127.0.0.1 (mysql-5.5.29 ndb-7.2.10, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @127.0.0.1 (mysql-5.5.29 ndb-7.2.10)
[mysqld(API)] 3 node(s)
id=4 @127.0.0.1 (mysql-5.5.29 ndb-7.2.10)
id=5 (not connected, accepting connect from localhost)
id=6 (not connected, accepting connect from any host)
创建一个测试表
[root@cluster-one ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.29-ndb-7.2.10-cluster-gpl-log MySQL Cluster Community Server (GPL)
Copyright (c) 2000, 2012, 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> mysql> CREATE TABLE `sbtest` (
-> `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-> `k` int(10) unsigned NOT NULL DEFAULT '0',
-> `c` char(120) NOT NULL DEFAULT '',
-> `pad` char(60) NOT NULL DEFAULT '',
-> PRIMARY KEY (`id`),
-> KEY `k` (`k`),
-> KEY `idx_c` (`c`)
-> ) ENGINE=ndbcluster ;
Query OK, 0 rows affected (0.63 sec)
mysql> show create table sbtest;
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sbtest | CREATE TABLE `sbtest` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`k` int(10) unsigned NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k` (`k`),
KEY `idx_c` (`c`)
) ENGINE=ndbcluster DEFAULT CHARSET=latin1 |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)