MySql Cluster搭建

一:实验环境

 

 

二:实验步骤


1安装包下载

请从http://dev.mysql.com/downloads/cluster/ 选择GENERIC LINUX

我下载的是:mysql-cluster-gpl-7.2.15-linux2.6-x86_64.tar.gz

2:解压

在所有节点上进行解压:

tar -xvf mysql-cluster-gpl-7.2.15-linux2.6-x86_64.tar.gz 

mv mysql-cluster-gpl-7.2.15-linux2.6-x86_64 /usr/local/mysql

3管理节点的配置和启动

mkdir -p /data/mysql/mysql-cluster

cd /data/mysql/mysql-cluster

 

建立全局配置文件vi mgmd.cnf,内容为: 

[ndbd default]
 
NoOfReplicas=2
 
DataMemory=80M
 
IndexMemory=18M
 
[ndb_mgmd]
 
hostname=192.168.6.66
datadir=/data/mysql/mysql-cluster
 
[ndbd]
 
hostname=192.168.6.68
 
datadir=/usr/local/mysql/data
 
[ndbd]
 
hostname=192.168.6.69
 
datadir=/usr/local/mysql/data
 
[mysqld]
 
hostname=192.168.6.66
 
[mysqld]
 
hostname=192.168.6.67
 
[mysqld]

/*

NoOfReplicas表示数据节点的冗余数量。

如果是表有两份数据,如果是1表示只有一份数据NoOfReplicas=2你可以试验一下如果一个data node 停掉了,cluster 可以照常工作如果NoOfReplicas=1, 一个data node 停掉了,cluster 就会自动停掉.
*/

启动MGM node:

/usr/local/mysql/bin/ndb_mgmd -f mgmd.cnf 

ps -ef|grep mgmd|grep -v grep查看MGM node 进程是否已经起来。

[root@ser6-66 mysql-cluster]# ps -ef|grep mgmd|grep -v grep

root      1225     1  0 13:31 ?        00:00:00 /usr/local/mysql/bin/ndb_mgmd -f mgmd.cnf 

登录MGM控制台(ndb_mgm 192.168.6.66),用show命令查看各个节点的状态。

[root@ser6-66 mysql-cluster]# /usr/local/mysql/bin/ndb_mgm 192.168.6.66
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: 192.168.6.66:1186
Cluster Configuration
---------------------
[ndbd(NDB)]2 node(s)
id=2 (not connected, accepting connect from 192.168.6.68)
id=3 (not connected, accepting connect from 192.168.6.69)
 
[ndb_mgmd(MGM)]1 node(s)
id=1@192.168.6.66  (mysql-5.5.35 ndb-7.2.15)
 
[mysqld(API)]3 node(s)
id=4 (not connected, accepting connect from 192.168.6.66)
id=5 (not connected, accepting connect from 192.168.6.67)
id=6 (not connected, accepting connect from any host)

#默认管理节点使用的是1186端口。

 

4数据节点配置与启动

#我是先把/etc/my.cnf文件先删除,然后再创建的。

编辑/etc/my.cnf

内容如下:

[mysqld]
ndbcluster                          #运行NDB存储引擎
ndb-connectstring=192.168.6.66    #定位管理节点
[mysql_cluster]
ndb-connectstring=192.168.6.66    #定位管理节点

[root@ser6-68 ndbdata]# /usr/local/mysql/bin/ndbd  --initial --ndb-connectstring=192.168.6.66

2015-08-17 14:21:05 [ndbd] INFO     -- Angel connected to '192.168.6.66:1186'

2015-08-17 14:21:05 [ndbd] INFO     -- Angel allocated nodeid: 2

(注意:--initial只需在第一次启动时加该参数)

查看ndb进程启动了:

[root@ser6-68 ~]# ps -ef|grep ndb

root     26571     1  0 14:38 ?        00:00:01 /usr/local/mysql/bin/ndbd --ndb-connectstring=192.168.6.66

root     26572 26571  8 14:38 ?        00:10:50 /usr/local/mysql/bin/ndbd --ndb-connectstring=192.168.6.66

root     26921 26901  0 14:38 pts/1    00:00:00 grep ndb 

同理配置Data node 2ndbd2.cnf)……. 

在MGM node的控制台中show命令可以看到Data node不再是not connected状态了

ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]2 node(s)
id=2@192.168.6.68  (mysql-5.5.35 ndb-7.2.15, starting, Nodegroup: 0)
id=3@192.168.6.69  (mysql-5.5.35 ndb-7.2.15, starting, Nodegroup: 0)
 
[ndb_mgmd(MGM)]1 node(s)
id=1@192.168.6.66  (mysql-5.5.35 ndb-7.2.15)
 
[mysqld(API)]3 node(s)
id=4 (not connected, accepting connect from 192.168.6.66)
id=5 (not connected, accepting connect from 192.168.6.67)
id=6 (not connected, accepting connect from any host)

 

5sql节点配置与启动

#我是先把/etc/my.cnf文件先删除,然后再创建的。

以配置SQL node 1为例。

建基本库:

cd /usr/local/mysql
[root@ser6-66 mysql]# ./scripts/mysql_install_db --user=mysql
WARNING: The host 'ser6-66' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK
 
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
 
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
 
./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h ser6-66 password 'new-password'
 
Alternatively you can run:
./bin/mysql_secure_installation
 
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
 
See the manual for more instructions.
 
You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &
 
You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl
 
Please report any problems with the ./bin/mysqlbug script!

编辑 /etc/my.cnf 文件,添加内容如下。

[client]
socket=/usr/local/mysql/mysql.sock
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
ndbcluster
ndb-connectstring=192.168.6.66  #管理节点ip
[mysql_cluster]
ndb-connectstring=192.168.6.66  #管理节点ip

启动MySQL Server:

cd /usr/local/mysql/bin

[root@ser6-66 bin]# ./mysqld_safe  -user=mysql &

[1] 7338

[root@ser6-66 bin]# 150824 13:06:05 mysqld_safe Logging to '/usr/local/mysql/data/ser6-66.err'.

150824 13:06:05 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data  

或者可以这样启动:

cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql

service mysql start 

ndb_mgm> show

Cluster Configuration
---------------------
[ndbd(NDB)]2 node(s)
id=2@192.168.6.68  (mysql-5.5.35 ndb-7.2.15, Nodegroup: 0, *)
id=3@192.168.6.69  (mysql-5.5.35 ndb-7.2.15, Nodegroup: 1)
 
[ndb_mgmd(MGM)]1 node(s)
id=1@192.168.6.66  (mysql-5.5.35 ndb-7.2.15)
 
[mysqld(API)]2 node(s)
id=4@192.168.6.66  (mysql-5.5.35 ndb-7.2.15)
id=5@192.168.6.67  (mysql-5.5.35 ndb-7.2.15)
id=6 (not connected, accepting connect from any host)

 显示都连上了。

同理配置和启动SQL node 2。


6:配PATH

在各节点配置PATH

vi /root/.bash_profile

PATH这一行后面,添加/usr/local/mysql/bin

如:

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

source /root/.bash_profile

 

7:修改root密码

sql节点上修改:

/usr/local/mysql/bin/mysqladmin -u root password '123456' 

假如报错:

Enter password: 

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/mysql.sock' (2) 

[root@ser6-67 ~]# find / -name mysql.sock

/tmp/mysql.sock

[root@ser6-67 ~]# ln -s /tmp/mysql.sock /usr/local/mysql/mysql.sock

 

8:测试

登录sql节点192.168.6.67

 

8.1 测试sql节点是否同步

[root@ser6-67 bin]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.35-ndb-7.2.15-cluster-gpl MySQL Cluster Community Server (GPL)
 
Copyright (c) 2000, 2013, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| ndbinfo            |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
 
--看到mysql cluster比mysql多了一个ndbinfo数据库。
 
--测试sql节点是否同步
在6.67上建个dba库,在dba下建个表t,t2。
mysql> create database dba;
Query OK, 1 row affected (0.07 sec)
 
mysql> use dba;
Database changed
mysql> create table t(id int);
Query OK, 0 rows affected (0.02 sec)
mysql> create table t2(id int) engine=ndb;
Query OK, 0 rows affected (0.11 sec)
 
mysql> show tables;
+---------------+
| Tables_in_dba |
+---------------+
| t             |
| t2            |
+---------------+
2 rows in set (0.00 sec)
 
 
在6.66上登录mysql,看到存储引擎为ndb的t2同步过来了,t1没同步过来:
mysql> use dba;
Database changed
mysql> show tables;
Empty set (0.03 sec)
 
 
mysql> show tables;
+---------------+
| Tables_in_dba |
+---------------+
| t2            |
+---------------+
1 row in set (0.00 sec)

-- 总结:只有表存储引擎为 ndb 时,才能同步。

 

8.2 分片测试

mysql> insert into t2 values(1);
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into t2 values(2);
Query OK, 1 row affected (0.00 sec)
 
mysql> select partition_name,table_rows from information_schema.PARTITIONS where     table_name='t2' and table_schema='dba';
    
+----------------+------------+
| partition_name | table_rows |
+----------------+------------+
| p0             |          1 |
| p1             |          1 |
 
+----------------+------------+
2 rows in set (0.00 sec)

看到数据被自动分到了两个数据节点。


8.3 模拟NDB节点宕机

--ndb节点宕掉一个,其上面的数据还可以通过sql节点查询吗

影响后续的插入删除吗?

 

在192.168.6.68上杀掉数据节点:
mgm控制台会自动显示:
ndb_mgm> Node 2: Node shutdown completed. Initiated by signal 15.
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]	2 node(s)
id=2 (not connected, accepting connect from 192.168.6.68)
id=3	@192.168.6.69  (mysql-5.5.35 ndb-7.2.15, Nodegroup: 0, *)
 
[ndb_mgmd(MGM)]	1 node(s)
id=1	@192.168.6.66  (mysql-5.5.35 ndb-7.2.15)
 
[mysqld(API)]	2 node(s)
id=4	@192.168.6.66  (mysql-5.5.35 ndb-7.2.15)
id=5	@192.168.6.67  (mysql-5.5.35 ndb-7.2.15)
id=6 (not connected, accepting connect from any host)
 
看到其他节点没有宕掉。 
登录sql节点,发现数据都也还在:
mysql> select * from t2;
+------+
| id   |
+------+
|    2 |
|    1 |
+------+
2 rows in set (0.00 sec)
 
再插入测试数据,仍然可以正常插入。
mysql>  insert into t2 values(1);
Query OK, 1 row affected (0.00 sec)
 
mysql>  insert into t2 values(4);
Query OK, 1 row affected (0.00 sec)
 
mysql>  select partition_name,table_rows from information_schema.PARTITIONS where     table_name='t2' and table_schema='dba';
+----------------+------------+
| partition_name | table_rows |
+----------------+------------+
| p0             |          2 |
| p1             |          2 |
+----------------+------------+
2 rows in set (0.00 sec)
 
我们看到杀掉其中一个NDB节点,mysql依然高可用。
实验完毕,现在,我们再启动该数据节点。
/usr/local/mysql/bin/ndbd   --ndb-connectstring=192.168.6.66
--注意,这次启动,就不要再指定initial参数了。

 

8.4 模拟sql节点宕机

--测试关闭其中一个sql节点,看集群能否正常使用
[root@ser6-66 mysql-cluster]# service mysql stop
Shutting down MySQL....                                    [  OK  ]
 
192.168.6.67上看还能否正常插入:
mysql> use dba;
Database changed
 
mysql> insert into t2(id) values(3);
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into t2(id) values(5);
Query OK, 1 row affected (0.00 sec)
 
说明只存在一个sql节点,不影响使用,验证了集群的高可用。
启动6.66的sql节点:
[root@ser6-66 mysql-cluster]# service mysql start
Starting MySQL..                                           [  OK  ]
 
mysql> select * from t2;
No connection. Trying to reconnect...
Connection id:    2
Current database: dba
 
+------+
| id   |
+------+
|    3 |
|    5 |
|    1 |
|    2 |
|    1 |
|    4 |
+------+
6 rows in set (0.00 sec)

看到数据同步过来了。

--本篇文章主要参考:

http://blog.chinaunix.net/uid-25326-id-251681.html

http://blog.itpub.net/15415488/viewspace-620903/

http://dev.mysql.com/doc/mysql-cluster-excerpt/5.1/en/mysql-cluster-online-add-node-example.html


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值