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

mysql集群的配置

2015-07-22 07:47 555 查看
最近在 学习MYSQL集群的配置,首先要了解什么集群,为什么要使用集群,以及安装集群的软件是什么和集群中的进程有哪些。
1.什么是集群?
集群是一组服务器提供相同的服务。
2.为什么要使用集群?
解决单点故障和数据的备份问题。
操作系统以及mysql配置文件的说明。
Linux系统:redhat6.5
安装集群软件: MySQL-Cluster-gpl-7.3.3-1.el6.x86_64.rpm-bundle.tar
服务器的角色:

实验要求:5台服务器,分别ip地址192.168.1.10做管理节点,192.168.1.20/192.168.1.30做数据节点使数据能够同时备份当其中任意一台服务器宕机后,对数据库进行操作,当宕机的服务器恢复后自动同步数据,192.168.1.40/192.168.1.50做sql节点当任意一台mysql节点故障后,都可以登陆数据库。

192.168.1.10(mgmd)
192.168.1.20(ndbd)

192.168.1.30 (ndbd)
192.168.1.40 (sql)
192.168.1.50 (sql)
一。在所有服务器上安装提供集群服务的软件 mysql-cluster (.rpm 源码)
最简单是的PRM包,下面采用二进制包的方法进行安装
1.解压软件包。
[root@fanxh opt]# tar -xvf MySQL-Cluster-gpl-7.3.3-1.el6.x86_64.rpm-bundle.tar
2.安装软件包,tar解压出来的是rpm包,直接安装就可以。
[root@fanxh opt]# rpm -Uvh MySQL-Cluster-*.rpm
3.查看软件包是否已安装。

[root@fanxh opt]# rpm -qa | grep -i mysql
MySQL-Cluster-shared-compat-gpl-7.3.3-1.el6.x86_64
MySQL-Cluster-devel-gpl-7.3.3-1.el6.x86_64
MySQL-Cluster-embedded-gpl-7.3.3-1.el6.x86_64
perl-DBD-MySQL-4.013-3.el6.x86_64
MySQL-Cluster-test-gpl-7.3.3-1.el6.x86_64
MySQL-Cluster-server-gpl-7.3.3-1.el6.x86_64
MySQL-Cluster-client-gpl-7.3.3-1.el6.x86_64
MySQL-Cluster-shared-gpl-7.3.3-1.el6.x86_64
4.在192.168.1.10上配置管理节点。
管理节点运行的是管理进程,运行时加载自己的主配置文件,主配置文件需要自己写。
例如:配置文件为config.ini 配置文件的内容包括:

1 数据节点的公共配置[ndbd default]
2 指定管理节点[ndb_mgmd]
3 指定数据节点 [ndbd]
4 指定sql节点 [mysqld]
[root@fanxh ~]# vim /etc/config.ini
[ndbd default]
NoOfReplicas=2 (表示数据节点的份数,有几个数据节点就写几)
DataMemory=80M (表示数据缓存的大小)
IndexMemory=18M (表示索引缓存的大小)
[ndb_mgmd]
nodeid=1 ( 用来设置当前主机在集群中的编号)
hostname=192.168.1.10 (指定管理节点的IP地址)
datadir=/var/log/mysql-cluster (指定把运行中的信息放在/var/log/mysql-cluster,这个文件夹 必须在系统中存在,如果没有创建该文件夹。)
[ndbd]
nodeid=2 (数据节点的编号)
hostname=192.168.1.20 (数据节点的IP地址)
datadir=/var/log/mysql-cluster/data (存储数据的位置)
[ndbd]
nodeid=3
hostname=192.168.1.30
datadir=/var/log/mysql-cluster/data
[mysqld]
nodeid=4
hostname=192.168.1.40
[mysqld]
nodeid=5
hostname=192.168.1.50

在192.168.1.10创建/var/log/mysql-cluster文件夹

[root@fanxh ~]# mkdir -p /var/log/mysql-cluster/
5.在192.168.1.20/192.168.1.30上配置数据节点

在192.168.1.20和192.168.1.30上 分别创建/var/log/mysql-cluster文件夹用来存储数据的位置

[root@fanxh ~]# mkdir -p /var/log/mysql-cluster/
创建主配置文件。
[root@fanxh ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/log/mysql-cluster/data (指定数据库目录)
ndb-connectstring=192.168.1.10 (指定连接管理集群的服务器IP)
ndbcluster (表的存储引擎必须是ndbcluster)
[mysql_cluster] ( 指定管理集群的机器)
ndb-connectstring=192.168.1.10(指定谁来管理集群的服务器IP)

6.在192.168.1.40/192.168.1.50上配置sql节点
[root@fanxh ~]# vim /etc/my.cnf
[mysqld]
ndbcluster
default-storage-engine=ndbcluster (指定默认的存储引擎)
ndb-connectstring=192.168.1.10 (指定连接管理集群的服务器IP)
[mysql_cluster]
ndb-connectstring=192.168.1.10 (指定谁来管理集群的服务器IP)

7.启动不同角色服务对应进程(有启动顺序,按照以下顺序进行启动)。
1 启动管理进程在192.168.1.10上。
[root@fanxh ~]# ndb_mgmd -f /etc/config/init 启动mysql cluster服务
MySQL Cluster Management Server mysql-5.6.14 ndb-7.3.3

[root@fanxh ~]# netstat -anuptl | grep :1186 查看集群端口是否开启
tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 4341/ndb_mgmd
tcp 0 0 127.0.0.1:38177 127.0.0.1:1186 ESTABLISHED 4341/ndb_mgmd
tcp 0 0 127.0.0.1:1186 127.0.0.1:38177 ESTABLISHED 4341/ndb_mgmd
tcp 0 0 192.168.1.10:1186 192.168.1.50:43075 ESTABLISHED 4341/ndb_mgmd
tcp 0 0 192.168.1.10:1186 192.168.1.40:37459 ESTABLISHED 4341/ndb_mgmd

[root@fanxh ~]# 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.1.20)
id=3 (not connected, accepting connect from 192.168.1.30)

[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.10 (mysql-5.6.14 ndb-7.3.3)

[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.1.40)
id=5 (not connected, accepting connect from 192.168.1.50)
2 启动192.168.1.20/192.168.1.30上的数据进程
[root@fanxh ~]# ndbd 在192.168.1.20启动数据进程的命令
2015-07-21 15:49:24 [ndbd] INFO -- Angel connected to '192.168.1.10:1186'
2015-07-21 15:49:24 [ndbd] INFO -- Angel allocated nodeid: 2 3
[root@localhost ~]# ndbd 在192.168.1.30启动数据进程的命令
2015-07-21 10:16:16 [ndbd] INFO -- Angel connected to '192.168.1.10:1186'
2015-07-21 10:16:16 [ndbd] INFO -- Angel allocated nodeid: 3 service mysql start

3.启动完数据进程后在管理节点192.168.1.10上查看数据进程是否已经启动。

ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.1.20 (mysql-5.6.14 ndb-7.3.3, starting, Nodegroup: 0, *)
id=3 @192.168.1.30 (mysql-5.6.14 ndb-7.3.3, starting, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.10 (mysql-5.6.14 ndb-7.3.3)

[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.1.40)
id=5 (not connected, accepting connect from 192.168.1.50)

ndb_mgm> Node 2: Started (version 7.3.3)
Node 3: Started (version 7.3.3)
4.在192.168.1.40/192.168.1.50启动mysql数据库服务。
[root@fanxh ~]# service mysql start
Starting MySQL SUCCESS!
5.启动完mysql数据库服务后在管理节点192.168.1.10上查看mysql进程是否已经启动。
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.1.20 (mysql-5.6.14 ndb-7.3.3, Nodegroup: 0, *)
id=3 @192.168.1.30 (mysql-5.6.14 ndb-7.3.3, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.10 (mysql-5.6.14 ndb-7.3.3)

[mysqld(API)] 2 node(s)
id=4 @192.168.1.40 (mysql-5.6.14 ndb-7.3.3)
id=5 @192.168.1.50 (mysql-5.6.14 ndb-7.3.3)

8.客户端测试
1.访问节点的单点故障。
在mysql 数据库服务上建表建库,进行查看。
登陆mysql服务器192.168.1.40/192.168.1.50
[root@fanxh~]# cat /root/.mysql_secret
# The random password set for the root user at Mon Jul 6 04:54:27 2015 (local time): umdVqWxz
[root@fanxh ~]# mysql -hlocalhost -uroot -pumdVqWxz
mysql>set password for root@localhost=password("123")

mysql>quit
[root@fanxh ~]# mysql -hlocalhost -uroot -p123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.14-ndb-7.3.3-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 stateme nt.

查看存储引擎默认是不是dbcluster
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ndbcluster | DEFAULT | Clustered, fault-tolerant tables | YES | NO | NO |

关闭192.168.1.50上的 mysql服务
[root@fanxh ~]# service mysql stop
查看192.168.1.10上的信息
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.1.20 (mysql-5.6.14 ndb-7.3.3, Nodegroup: 0, *)
id=3 @192.168.1.30 (mysql-5.6.14 ndb-7.3.3, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.10 (mysql-5.6.14 ndb-7.3.3)

[mysqld(API)] 2 node(s)
id=4 @192.168.1.40 (mysql-5.6.14 ndb-7.3.3)
id=5 (not connected, accepting connect from 192.168.1.50)

用192.168.1.40查看是否可以登陆数据库,如果可以登陆说明解决单点故障。
[root@localhost ~]# mysql -hlocalhost -uroot -p123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.14-ndb-7.3.3-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>

2.数据节点单点故障。(关闭1个数据服务,在表中进行插入,查看等一些操作,当另外一个恢复服务后,看能不能进行同步数据,如果可以说明就解决单点故障)
验证数据节点单点故障的步骤:
1.关闭192.168.1.20的数据节点的服务。

[root@fanxh ~]# pkill -9 ndbd

2. 查看管理节点的192.168.1.20的状态。

ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 (not connected, accepting connect from 192.168.1.20)
id=3 @192.168.1.30 (mysql-5.6.14 ndb-7.3.3, Nodegroup: 0, *)

[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.10 (mysql-5.6.14 ndb-7.3.3)

[mysqld(API)] 2 node(s)
id=4 @192.168.1.40 (mysql-5.6.14 ndb-7.3.3)
id=5 @192.168.1.50 (mysql-5.6.14 ndb-7.3.3)

3.在sql节点上查看数据库的信息,当192.168.1.20宕机后查看的信息是192.168.1.30的信息。
在数据库上对表进行select ,insert into 等信息后,当192.168.1.30恢复后能不能同步数据,如果可以说明可以解决单点故障。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MYSQL