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

RHEL5.7上MySQL-Cluster-7.2.13双机集群安装部署手册

2013-11-16 21:36 696 查看
一、介绍

这篇文档旨在介绍如何安装配置基于2台服务器的MySQL集群。并且实现任意一台服务器出现问题或宕机时MySql集群依然能够继续运行。

1.MySQL-Cluster简介

MySQL-Cluster主要有三种类型节点:

Data node,数据节点将保存数据库,它会自动复制所有的数据节点;

Daemon node,守护节点相当于是SQL数据库和客户端之间的接口,它提供从数据节点查询等操作,类似于"网关";

Management node,管理节点,用以监控和管理整个集群。

2.安装环境及软件包

2台PC机,通过VMWare创建虚拟机,然后安装操作系统

软件包:MySQL-Cluster-client-gpl-7.2.13-1.rhel5.i386.rpm

MySQL-Cluster-server-gpl-7.2.13-1.rhel5.i386.rpm

操作系统:Redhat.Enterprise.Linux.v5.UPDATE.7.X86.DVD-HOTiSO.iso

serverA:192.168.0.180

serverB:192.168.0.181

数据库管理节点:192.168.0.180,192.168.0.181

数据库数据节点:192.168.0.180,192.168.0.181

数据库SQL节点(API节点):192.168.0.180,192.168.0.181

二、在serverA和serverB上安装MySQL-cluster

以下步骤需要在serverA和serverB上各做一次

1.首先检查linux操作系统中是否安装了自带的mysql数据库

一般linux自带mysql版本都比较低一些,因此要安装比较新的版本需要先将自带的低版本卸载。

检查操作系统中是否安装了mysql的命令:rpm -qa |grep -i mysql

卸载mysql的命令:rpm -e mysqlXXXX

或者使用yum -remove mysqlXXX来删除,这样可以避免卸载过程中由于各个包之间的依赖造成卸载停止

2.创建mysql用户和组

groupadd mysql

useradd -g mysqlmysql

usermod-d /home/mysql mysql

3.将MySQL-Cluster上传到/home/mysql/目录下,开始安装MySQL-Cluster

[root@serverAsbin]# rpm -ivh MySQL-Cluster-server-gpl-7.2.13-1.rhel5.i386.rpm

InstallingMySQL system tables...

13102811:00:50 [Warning] Forcing shutdown of 3 plugins

OK

Fillinghelp tables...

13102811:00:50 [Warning] Forcing shutdown of 3 plugins

OK

To startmysqld at boot time you have to copy

support-files/mysql.serverto the right place for your system

PLEASEREMEMBER 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 serverB password 'new-password'

Alternativelyyou can run:

./bin/mysql_secure_installation

whichwill also give you the option of removing the test

databasesand anonymous user created by default. This is

stronglyrecommended for production servers.

See themanual for more instructions.

You canstart the MySQL daemon with:

cd . ;./bin/mysqld_safe &

You cantest the MySQL daemon with mysql-test-run.pl

cd./mysql-test ; perl mysql-test-run.pl

Pleasereport any problems with the ./bin/mysqlbug script!

Thelatest information about MySQL is available at http://www.mysql.com/
SupportMySQL by buying support/licenses from http://shop.mysql.com/
[root@serverAsbin]# rpm -ivh MySQL-Cluster-client-gpl-7.2.13-1.rhel5.i386.rpm

Preparing... ########################################### [100%]

1:MySQL-Cluster-client-gp ###########################################[100%]

4.安装完成之后创建连接

#ln -s /usr/sbin/ndbd /usr/bin

#ln -s /usr/sbin/ndb_mgmd /usr/bin

#ln -s /usr/sbin/mysqld /usr/bin

三、安装并配置节点

以下步骤需要在serverA和serverB上各做一次

1.配置管理节点配置文件

# mkdir /var/lib/mysql-cluster

# cd/var/lib/mysql-cluster

# vi config.ini

在config.ini中添加如下内容:

[ndbd default]

NoOfReplicas= 2

MaxNoOfConcurrentOperations=10000

# Amount of memoryrequired=(SizeofDatabase * NumberOfReplicas * 1.1 ) / NumberOfDataNodes

DataMemory= 128M

IndexMemory= 24M

TimeBetweenWatchDogCheck=30000

DataDir=/usr/local/mysql/data

MaxNoOfOrderedIndexes=512

StartPartialTimeout=100

StartPartitionedTimeout=100

ArbitrationTimeout=5000

TransactionDeadlockDetectionTimeout=5000

HeartbeatIntervalDbDb=5000

StopOnError=0

[ndb_mgmd default]

DataDir=/usr/local/mysql/data

[ndb_mgmd]

NodeId=1

HostName=192.168.0.180

[ndb_mgmd]

NodeId=2

HostName=192.168.0.181

[ndbd]

NodeId= 3

HostName=192.168.0.180

[ndbd]

NodeId= 4

HostName=192.168.0.181

[mysqld]

ArbitrationRank=2

[mysqld]

ArbitrationRank=2

[mysqld]

[mysqld]

[tcp default]

portnumber=2279

2.配置通用my.cnf文件

# vi /etc/my.cnf

在my.cnf中添加如下内容:

[mysqld]

datadir=/usr/local/mysql/data

socket=/var/lib/mysql/mysql.sock

# Default to usingold password format for compatibility with mysql 3.x

# clients (thoseusing the mysqlclient10 compatibility package).

old_passwords=1

default-storage-engine=ndbcluster

ndbcluster

ndb-connectstring=192.168.0.180,192.168.0.181

[ndbd]

connect-string=192.168.0.180,192.168.0.181

[ndb_mgm]

connect-string=192.168.0.180,192.168.0.181

[ndb_mgmd]

config-file=/var/lib/mysql-cluster/config.ini

[mysql_cluster]

ndb-connectstring=192.168.0.180,192.168.0.181

[mysql.server]

user=mysql

basedir=/usr #这个目录是mysqld_safe所在的上一级目录mysqld_safe在usr/bin/目录下

[mysqld_safe]

log-error=/var/log/mysqld.log

#pid-file=/var/run/mysqld/mysqld.pid

[mysql]

#socket=/usr/local/var/mysql.sock

[mysqladmin]

#socket=/usr/local/var/mysql.sock

[ndb_restoredefault]

保存退出后

四、启动管理节点

MySQL安装目录初始化

执行/usr/bin/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data即可。注意这里的datadir要和config.ini和my.cnf中配置的datadir一致

[root@serverAsbin]# ndb_mgmd --ndb_nodeid=1

MySQL ClusterManagement Server mysql-5.5.31 ndb-7.2.13

2013-10-2915:45:36 [MgmtSrvr] INFO -- Thedefault config directory '/usr/mysql-cluster' does not exist. Trying to createit...

2013-10-2915:45:36 [MgmtSrvr] INFO -- Sucessfullycreated config directory

2013-10-29 15:45:36[MgmtSrvr] WARNING -- at line 38: [tcp]portnumber is deprecated

2013-10-29 15:45:36[MgmtSrvr] WARNING -- at line 38:Cluster configuration warning:

arbitrator with id1 and db node with id 3 on same host 192.168.0.180

arbitrator with id2 and db node with id 4 on same host 192.168.0.181

arbitrator with id5 has no hostname specified

arbitrator with id6 has no hostname specified

Running arbitratoron the same host as a database node may

cause completecluster shutdown in case of host failure.

注:在启动时几个警告提示

2013-10-2915:45:36 [MgmtSrvr] WARNING -- at line38: [tcp] portnumber is deprecated这个警告提示可以不必理会,不影响使用;

2013-10-2915:45:36 [MgmtSrvr] WARNING -- at line 38:Cluster configuration warning这个警告提示说节点1和3,2和4的arbitrator一样,可能引起整个集群失败,由于是双管理节点所示此警告可以不用放在心上

[root@serverAsbin]# netstat -lntpu

ActiveInternet connections (only servers)

ProtoRecv-Q Send-Q Local Address ForeignAddress State PID/Program name

tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 9484/ndb_mgmd

看到1186端口开放了说明启动是正常的。

启动serverB上的管理节点:

[root@SerB ~]#ndb_mgmd --ndb_nodeid=2

启动管理节点的服务还可以使用如下的命令:

ndb_mgmd -f/var/lib/mysql-cluster/config.ini

启动成功的提示信息如下所示:

[root@serverAmysql-cluster]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini

MySQLCluster Management Server mysql-5.5.31 ndb-7.2.13

2013-10-3016:48:02 [MgmtSrvr] WARNING -- at line38: [tcp] portnumber is deprecated

2013-10-3016:48:02 [MgmtSrvr] WARNING -- at line38: Cluster configuration warning:

arbitrator with id 1 and db node with id 3 onsame host 192.168.0.180

arbitrator with id 2 and db node with id 4 onsame host 192.168.0.181

arbitrator with id 5 has no hostnamespecified

arbitrator with id 6 has no hostname specified

Running arbitrator on the same host as adatabase node may

cause complete cluster shutdown in case ofhost failure.

2013-10-3016:48:02 [MgmtSrvr] WARNING -- at line38: [tcp] portnumber is deprecated

2013-10-3016:48:02 [MgmtSrvr] WARNING -- at line38: Cluster configuration warning:

arbitrator with id 1 and db node with id 3 onsame host 192.168.0.180

arbitrator with id 2 and db node with id 4 onsame host 192.168.0.181

arbitrator with id 5 has no hostnamespecified

arbitrator with id 6 has no hostnamespecified

Running arbitrator on the same host as adatabase node may

cause complete cluster shutdown in case ofhost failure.

五、初始化集群

在serverA中

[root@serverAsbin]# ndbd --ndb_nodeid=3 --initial

2013-10-3009:37:07 [ndbd] INFO -- Angelconnected to '192.168.0.180:1186'

2013-10-3009:37:07 [ndbd] INFO -- Angelallocated nodeid: 3

执行netstat –lntpu,如果查勘列表中有端口为2279,表示初始化成功

[root@serverAmysql-cluster]# netstat -lntpu

Active Internetconnections (only servers)

Proto Recv-QSend-Q Local Address ForeignAddress State PID/Program name

tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 13713/ndb_mgmd

tcp 0 0 192.168.0.180:2279 0.0.0.0:* LISTEN 13735/ndbd

在serverB中

[root@serverBmysql]# ndbd --ndb_nodeid=4 --initial

2013-10-3009:37:49 [ndbd] INFO -- Angelconnected to '192.168.0.180:1186'

2013-10-3009:37:49 [ndbd] INFO -- Angelallocated nodeid: 4

注:只有在第一次启动ndbd时或者对config.ini进行改动后才需要使用--initial参数!

初始化集群也可以直接输入ndbd –initial命令,而不必输入节点号

检查工作状态

在任意一台机子上启动管理终端:

[root@serverBmysql]# ndb_mgm -e show

Connected toManagement Server at: 192.168.0.180:1186

ClusterConfiguration

---------------------

[ndbd(NDB)] 2node(s)

id=3 @192.168.0.180 (mysql-5.5.31 ndb-7.2.13, Nodegroup: 0,Master)

id=4 @192.168.0.181 (mysql-5.5.31 ndb-7.2.13, Nodegroup: 0)

[ndb_mgmd(MGM)] 2node(s)

id=1 @192.168.0.180 (mysql-5.5.31 ndb-7.2.13)

id=2 @192.168.0.181 (mysql-5.5.31 ndb-7.2.13)

[mysqld(API)] 4 node(s)

id=5 (not connected,accepting connect from any host)

id=6 (notconnected, accepting connect from any host)

id=7 (notconnected, accepting connect from any host)

id=8 (notconnected, accepting connect from any host)

如果上面没有问题,现在开始加入mysqld(API)

六、加入mysqld(API)

在serverA 中:

[root@serverA sbin]#mysqld_safe --ndb_nodeid=5 --user=mysql &

[1] 12495

[root@serverAsbin]# 131030 10:26:00 mysqld_safe Logging to '/var/log/mysqld.log'.

131030 10:26:00mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data/

在serverB 中:

[root@serverB mysql]#mysqld_safe --ndb_nodeid=6 --user=mysql &

[1] 11756

[root@serverB sbin]#13103010:18:56 mysqld_safe Logging to '/var/log/mysqld.log'.

131030 10:18:57mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data/

再次检查工作状态,看mysql节点是否加入成功,出现下面的提示信息表示添加成功

[root@serverAsbin]# ndb_mgm -e show

Connected toManagement Server at: 192.168.0.180:1186

ClusterConfiguration

---------------------

[ndbd(NDB)] 2node(s)

id=3 @192.168.0.180 (mysql-5.5.31 ndb-7.2.13, Nodegroup: 0,Master)

id=4 @192.168.0.181 (mysql-5.5.31 ndb-7.2.13, Nodegroup: 0)

[ndb_mgmd(MGM)] 2node(s)

id=1 @192.168.0.180 (mysql-5.5.31 ndb-7.2.13)

id=2 @192.168.0.181 (mysql-5.5.31 ndb-7.2.13)

[mysqld(API)] 4 node(s)

id=5 @192.168.0.181 (mysql-5.5.31 ndb-7.2.13)

id=6 @192.168.0.180 (mysql-5.5.31 ndb-7.2.13)

id=7 (notconnected, accepting connect from any host)

id=8 (notconnected, accepting connect from any host)

此步也可以通过执行mysql服务器启动命令来代替

[root@serverAmysql-cluster]# service mysql start

StartingMySQL.......................[确定]......................

然后再执行ndb_mgm -e show查看各个节点工作状态

设置开机自动启动

将一下内容加入到/etc/rc.d/rc.local文件的最后一行,就可以使得ndb_mgmd和ndbd开机自动启动了

ndb_mgmd -f/var/lib/mysql-cluster/config.ini

ndbd

七、设置数据库的用户名和密码

执行如下命令(两个节点都要执行):

usr/bin/mysqladmin-u root password 'new-password'

usr/bin/mysqladmin-u root -h serverB password 'new-password'

八、功能测试

到管理节点查看下相关服务状态

# ndb_mgm

ndb_mgm> show

Connected toManagement Server at: 192.168.0.180:1186

ClusterConfiguration

---------------------

[ndbd(NDB)] 2node(s)

id=3 @192.168.0.180 (mysql-5.5.31 ndb-7.2.13, Nodegroup: 0,Master)

id=4 @192.168.0.181 (mysql-5.5.31 ndb-7.2.13, Nodegroup: 0)

[ndb_mgmd(MGM)] 2node(s)

id=1 @192.168.0.180 (mysql-5.5.31 ndb-7.2.13)

id=2 @192.168.0.181 (mysql-5.5.31 ndb-7.2.13)

[mysqld(API)] 4 node(s)

id=5 @192.168.0.181 (mysql-5.5.31 ndb-7.2.13)

id=6 @192.168.0.180 (mysql-5.5.31 ndb-7.2.13)

id=7 (notconnected, accepting connect from any host)

id=8 (notconnected, accepting connect from any host)

可以看到这里的数据节点、管理节点、sql节点都是正常的。现在我们在其中一个数据节点上进行相关数据库的创建,然后到另外一个数据节点上看看数据是否同步。

[root@serverBusr]# mysql -u root -p

Password:

Welcome to theMySQL monitor. Commands end with ; or\g.

Your MySQLconnection id is 2

Server version:5.5.31-ndb-7.2.13-cluster-gpl MySQL Cluster Community Server (GPL)

Copyright (c)2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is aregistered trademark of Oracle Corporation and/or its

affiliates. Othernames may be trademarks of their respective

owners.

Type 'help;' or'\h' for help. Type '\c' to clear the current input statement.

mysql> showdatabases;

+--------------------+

| Database |

+--------------------+

|information_schema |

| mysql |

| ndb_4_fs |

| ndbinfo |

|performance_schema |

| test |

+--------------------+

6 rows in set(0.08 sec)

mysql> createdatabase aa;

Query OK, 1 rowaffected (3.56 sec)

mysql> use aa

Database changed

mysql> CREATETABLE ctest2 (i INT) ;

Query OK, 0 rowsaffected (24.71 sec)

mysql> INSERTINTO ctest2 () VALUES (1);

Query OK, 1 rowaffected (0.54 sec)

mysql> SELECT *FROM ctest2;

+------+

| i |

+------+

| 1 |

+------+

1 row in set (0.01sec)

现在到另外一个数据节点serverA查看下aa数据库是否同步过来了。

[root@serverAmysql-cluster]# mysql -u root –p

Password:

mysql> showdatabases;

+--------------------+

| Database |

+--------------------+

|information_schema |

| aa |

| mysql |

| ndb_3_fs |

| ndbinfo |

|performance_schema |

| test |

+--------------------+

7 rows in set(4.80 sec)

mysql> use aa

Reading tableinformation for completion of table and column names

You can turn offthis feature to get a quicker startup with -A

Database changed

mysql> select *from ctest2;

+------+

| i |

+------+

| 1 |

+------+

1 row in set (0.01sec)

从上面可以看到数据已经同步了,mysql集群环境已经搭建完成。

九、破坏性测试

在上面可以看到192.168.0.180作为主的数据节点,现在把192.168.0.180这台机器关闭,有如下的结果:

[root@serverA mysql-cluster]#shutdown -h now

Broadcast messagefrom root (pts/2) (Thu Oct 31 14:00:56 2013):

The system isgoing down for system halt NOW!

[root@serverB ~]#ndb_mgm -e show

Connected toManagement Server at: 192.168.0.181:1186

ClusterConfiguration

---------------------

[ndbd(NDB)] 2 node(s)

id=3 (notconnected, accepting connect from 192.168.0.180)

id=4 @192.168.0.181 (mysql-5.5.31 ndb-7.2.13, Nodegroup: 0,Master)

[ndb_mgmd(MGM)] 2node(s)

id=1 @192.168.0.180 (mysql-5.5.31 ndb-7.2.13)

id=2 @192.168.0.181 (mysql-5.5.31 ndb-7.2.13)

[mysqld(API)] 4 node(s)

id=5 @192.168.0.181 (mysql-5.5.31 ndb-7.2.13)

id=6 (notconnected, accepting connect from any host)

id=7 (notconnected, accepting connect from any host)

id=8 (notconnected, accepting connect from any host)

从上面可以发现现在192.168.0.180这台机器的数据节点和sql节点已经连接不上了,192.168.0.181成为了主数据节点,现在在192.168.0.181数据节点上创建一个表,然后恢复192.168.0.180的数据节点,查看是否能够把数据同步过来了。

先在192.168.0.181数据节点做如下操作:

mysql> createtable ctest3(id int(11)) ;

mysql> showtables;

mysql> createtable ctest3(id int(11)) ;

Query OK, 0 rowsaffected (0.20 sec)

mysql> showtables;

+--------------+

| Tables_in_aa |

+--------------+

| ctest2 |

| ctest3 |

+--------------+

2 rows in set(0.00 sec)

mysql> insertinto ctest3 values(1);

Query OK, 1 rowaffected (0.02 sec)

mysql> select *from ctest3;

+------+

| id |

+------+

| 1 |

+------+

1 row in set (0.01sec)

然后我们恢复192.168.0.180数据节点,查看下ctest3数据是否同步过来了。

[root@serverA ~]#mysql -u root

mysql> showdatabases;

+--------------------+

| Database |

+--------------------+

|information_schema |

| aa |

| mysql |

| ndb_3_fs |

| ndbinfo |

|performance_schema |

| test |

+--------------------+

7 rows in set(0.19 sec)

mysql> use aa

Reading tableinformation for completion of table and column names

You can turn offthis feature to get a quicker startup with -A

Database changed

mysql> showtables;

+--------------+

| Tables_in_aa |

+--------------+

| ctest2 |

| ctest3 |

+--------------+

2 rows in set(0.00 sec)

mysql> select *from ctest3;

+------+

| id |

+------+

| 1 |

+------+

1 row in set (0.06sec)

可以看到192.168.0.189数据节点已经把192.168.0.181数据节点的数据同步过来了,说明mysql集群是没有问题的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: