MySQL集群搭建

MySQL集群概述和安装环境

MySQL Cluster是MySQL适合于分布式计算环境的高实用、高冗余版本。Cluster的汉语是“集群”的意思。它采用了NDB Cluster 存储引擎,允许在1个 Cluster 中运行多个MySQL服务器。
MySQL Cluster 是一种技术,该技术允许在无共享的系统中部署“内存中”数据库的 Cluster 。通过无共享体系结构,系统能够使用廉价的硬件,而且对软硬件无特殊要求。此外,由于每个组件有自己的内存和磁盘,不存在单点故障。

mysql的一种常见集群
这里写图片描述
SQL节点: 给上层应用层提供sql访问。

管理节点(MGM): 管理整个集群。 启动,关闭集群。 通过ndb_mgmd命令启动集群 存储/数据节点: 保存cluster中的数据。
数据节点,可以提供副本。实现数据冗余。 NDB引擎:是一种 “内存中”的存储引擎 , 它具有可用性高和数据一致性好的特点。

NDB引擎

MySQL Cluster 使用了一个专用的基于内存的存储引擎——NDB引擎,这样做的好处是速度快,
没有磁盘I/O的瓶颈,但是由于是基于内存的,所以数据库的规模受系统总内存的限制, 如果运行NDB的MySQL服务器一定要内存够大,比如4G,
8G, 甚至16G。NDB引擎是分布式的,它可以配置在多台服务器上来实现数据的可靠性和扩展性,理论上
通过配置2台NDB的存储节点就能实现整个数据库集群的冗余性和解决单点故障问题。

缺陷

基于内存,数据库的规模受集群总内存的大小限制 基于内存,断电后数据可能会有数据丢失,这点还需要通过测试验证。
多个节点通过网络实现通讯和数据同步、查询等操作,因此整体性受网络速度影响,因此速度也比较慢

优点

多个节点之间可以分布在不同的地理位置,因此也是一个实现分布式数据库的方案。 扩展性很好,增加节点即可实现数据库集群的扩展。
冗余性很好,多个节点上都有完整的数据库数据,因此任何一个节点宕机都不会造成服务中断。

Mysql cluster的下载地址:http://dev.mysql.com/downloads/cluster/

在我们做的实验中mysql cluster集群各机器角色如下分配:
mysql管理结点:test63.cn IP:192.168.1.63
mysql 数据结点:test63.cn IP:192.168.1.63
mysql 数据结点:test64.cn IP:192.168.1.64
msyql SQL结点:test63.cn IP:192.168.1.63
msyql SQL结点:test64.cn IP:192.168.1.64

将MySQL-Cluster-gpl-7.3.7-1.el6.x86_64.rpm-bundle.tar 上传到test63和test64上。

实战:MySQL集群搭建
环境清理以及安装
test63上执行下面内容:
首先我们要清除旧版本,然后安装mysql cluster,最后是文件权限管理。

  1. mysql旧版本清除
    首先使用如下命令来清理之前操作系统自带的mysql安装:
yum -y remove mysql

然后使用如下命令:

rpm -qa | grep mysql*

对于找到的2个剩余mysql包,请按照如下的命令格式予以删除:

rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64

2.mysql cluster版本安装准备
将MySQL-Cluster-gpl-7.3.4-1.el6.x86_64.rpm-bundle.tar放到某个目录下(譬如/package) 下面,并且执行如下命令解压:
tar -xvf MySQL-Cluster-gpl-7.3.4-1.el6.x86_64.rpm-bundle.tar
得到如下文件清单:

MySQL-Cluster-client-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-devel-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-embedded-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-server-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-shared-compat-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-shared-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-test-gpl-7.3.4-1.el6.x86_64.rpm

3.创建文件夹
(分如下3个类来创建对应的文件夹)

数据结点存放数据,:mkdir /var/lib/mysql
管理节点:mkdir /var/lib/mysql-cluster     SQL节点:可不用  文件夹授权
进程DIR:    mkdir /var/run/mysqld        

4.安装mysql集群软件包(客户端)

[root@test63 ~]#rpm -ivh MySQL-Cluster-server-gpl-7.3.4-1.el6.x86_64. rpm
[root@test63 ~]#rpm -ivh MySQL-Cluster-client-gpl-7.3.4-1.el6.x86_64. rpm
注意:当安装完毕MySQL-Cluster-server-gpl包后,将出现如下提示信息,提醒我们整个cluster安装后的初次超级账户密码存在/root/.mysql_secret这个文件当中。
---------------------------------------------------------------------------------------------------------
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
  /usr/bin/mysql_secure_installation
which will also give you the option of removing the test database.
This is strongly recommended for production servers.
-----------------------------------------------------------------
使用如下的命令来变更权限保证可写入:
[root@test63 ~]# id mysql
uid=495(mysql) gid=489(mysql) groups=489(mysql)
[root@test63 ~]# chown mysql:mysql -R /var/lib/mysql
[root@test63 ~]# chown mysql:mysql -R /var/lib/mysql-cluster/
[root@test63 ~]# chown mysql:mysql -R /var/run/mysqld/
[root@test63 ~]# chown mysql:mysql -R /usr/mysqld/  RPM安装路径

test64的大体步骤如同test63,我们首先要清除旧版本mysql。

5.test64 清除旧版本mysql:
1. mysql旧版本清除:
首先使用如下命令来清理之前操作系统自带的mysql安装:

[root@test64 ~]# yum remove mysql -y
[root@test63 ~]# rpm -qa | grep mysql*
mysql-libs-5.1.52-1.el6_0.1.x86_64
对于找到的1个剩余mysql包,请按照如下的命令格式予以删除:
[root@test63 ~]# rpm -e --nodeps mysql-libs
[root@test63 ~]# rpm -qa | grep mysql*

6.复制软件包
[root@test63 ~]# scp MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64.rpm 192.168.1.64:/ root/
root@192.168.1.64's password:
[root@test63 ~]# scp MySQL-Cluster-client-gpl-7.3.7-1.el6.x86_64.rpm  192.168.1.64:/root
7.test64安装mysql集群相关的软件包
[root@test64 ~]# rpm -ivh MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64. rpm
[root@test63 ~]# rpm -ivh MySQL-Cluster-client-gpl-7.3.7-1.el6.x86_64. rpm
注意:MySQL-Cluster-server和MySQL-Cluster-client 的关系和mysql-server同mysql 包的关系一样。数据结点存放数据: /var/lib/mysql  会自动创建。
8.创建文件夹
存放进程PID文件:mkdir /var/run/mysqld    
[root@test64 ~]# id mysql
uid=495(mysql) gid=489(mysql) groups=489(mysql)
[root@test64 ~]# mkdir /var/run/mysqld
[root@test64 ~]# chown mysql:mysql -R /var/run/mysqld
注意:当安装完毕MySQL-Cluster-server-gpl包后,将出现如下提示信息,提醒我们整个cluster安装后的初次超级账户密码存在/root/.mysql_secret这个文件当中。
---------------------------------------------------------------------------------------
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test database.
This is strongly recommended for production servers.

9.查看mysql root用户密码
[root@test64 ~]# cat /root/.mysql_secret

# The random password set for the root user at Wed Apr  1 21:10:53 2015 (local time): NSblG9hMkThTgFHY
会生成两个主要的命令文件:ndb_mgmd 和ndb_mgm 
[root@test63 ~]# which ndb_mgm
/usr/bin/ndb_mgm
[root@test63 ~]# rpm -qf /usr/bin/ndb_mgm
MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64
[root@test63 ~]# which ndb_mgmd
/usr/sbin/ndb_mgmd
[root@test63 ~]# rpm -qf /usr/sbin/ndb_mgmd
MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64
还会生成用户:mysql
[root@test63 ~]# id mysql
uid=495(mysql) gid=489(mysql) groups=489(mysql)

搭建mysql集群
经过复杂的初始配置,我们终于要搭建集群了。我们按配置,启动,测试的顺序来整理这一部分。
45.2.1 各个机器上的配置
我们先将需求配置到各台机器上
1.test63创建管理结点上配置文件

[root@test63 ~]                        # cd /var/lib/mysql-cluster
[root@test63 ~]                        # vi config.ini   #写入以下内容
[ndbd default]
NoOfReplicas=2                          #数据写入数量。2表示两份
DataMemory=200M
IndexMemory=100M                            #索引给100M
[ndb_mgmd]
id=1
datadir=/var/lib/mysql-cluster      # 管理结点的日志
HostName=192.168.1.63                   #管理结点的IP地址。本机IP
###### data node options:           #存储结点
[ndbd]
HostName=192.168.1.63   
DataDir=/var/lib/mysql                  #mysql数据存储路径
id=2
[ndbd]
HostName=192.168.1.64  
DataDir=/var/lib/mysql                  #mysql数据存储路径
id=3
# SQL node options:                     #关于SQL结点
[mysqld]
HostName=192.168.1.63   
id=4
[mysqld]
HostName=192.168.1.64  
id=5
在这个文件里,我们分别给五个节点分配了ID,这有利于更好的管理和区分各个节点。当然,要是不指定,MySQL也会动态分配一个

2.test63数据节点+SQL节点配置文档

[root@test63 /]# vim  /etc/my.cnf       #写入以下内容
[mysqld]
datadir=/var/lib/mysql                          #mysql数据存储路径
ndbcluster                                      #启动ndb引擎
ndb-connectstring=192.168.1.63              # 管理节点IP地址 
[mysqld_safe]  
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql_cluster] 
ndb-connectstring=192.168.1.63              #管理节点IP地址
----------------注:如果test63 只做 SQL节点,则配置文档为:------------
[root@test63 /]# cat cat /etc/my.cnf
[mysqld]
ndbcluster                                          #启动ndb引擎
ndb-connectstring=192.168.1.63              # 管理节点IP地址 
 [mysqld_safe]  
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
 [mysql_cluster] 
ndb-connectstring=192.168.1.63              #管理节点IP地址
说明:数据节点和SQL结点配置文件区别 ,就多一行
数据结点有:datadir=/var/lib/mysql            #mysql数据存储路径。
SQL节点上没有。

3.test64配置数据结点和SQL结点

[root@test63 mysql-cluster]# scp /etc/my.cnf 192.168.1.64:/etc/
root@192.168.1.64's password: 
my.cnf                                  100%  215     0.2KB/s   00:00   
注意:test63和test64的/my.cnf的内容一样,可直接从test63复制过去
[root@test64 /]# vim  /etc/my.cnf   #写入以下内容。默认没有my.cnf文件
[mysqld]
datadir=/var/lib/mysql      #mysql数据存储路径
ndbcluster     #启动ndb引擎
ndb-connectstring=192.168.1.63    # 管理节点IP地址 
[mysqld_safe]  
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql_cluster] 
ndb-connectstring=192.168.1.63    #管理节点IP地址

45.2.2 MySQL Cluster启动
初次启动命令以及用户密码更改调整:(请严格按照次序启动)
先启动:管理结点服务->数据结点服务->sql结点服务
关闭:关闭管理结点服务,关闭管理结点服务后,nbdb数据结点服务会自动关闭->手动把sql结点服务关了。
执行初次启动前请先确认 将两台机器的防火墙关闭(service iptables stop 或者 设定 防火墙端口可通,两个端口即通讯端口1186、数据端口3306 )
1.test63上启动管理结点命令

ndb_mgmd -f /var/lib/mysql-cluster/config.ini   # mysql cluster 后台运行进程
#尽量不要把 管理结点、 数据结点、存储结点 配置在同一台机子上,否则一个挂了,就全挂了。
查看端口号:
[root@test63 ~]# netstat  -antup | grep 1186
tcp     0   0 0.0.0.0:1186      0.0.0.0:*           LISTEN    7057/ndb_mgmd
tcp     0   0 127.0.0.1:1186    127.0.0.1:60324 ESTABLISHED 7057/ndb_mgmd 
tcp     0   0 127.0.0.1:60324   127.0.0.1:1186      ESTABLISHED 7057/ndb_mgmd 

2.test63和test 64启动数据结点服务

[root@test63 ~]# ndbd --initial
2015-04-01 21:57:58 [ndbd] INFO     -- Angel connected to '192.168.1.63: 1186'
2015-04-01 21:57:58 [ndbd] INFO     -- Angel allocated nodeid: 2
 [root@test64 ~]# ndbd --initial
2014-12-02 22:24:31 [ndbd] INFO     -- Angel connected to '192.168.1.63: 1186'
2014-12-02 22:24:31 [ndbd] INFO     -- Angel allocated nodeid: 3
 [root@test63 ~]# vim /var/lib/mysql-cluster/config.ini   #查看数据结点对应的ID
[ndbd]
HostName=192.168.1.63
DataDir=/var/lib/mysql
id=2
[ndbd]
HostName=192.168.1.64
DataDir=/var/lib/mysql
id=3

3.test63和test64启动SQL结点服务

[root@test63 ~]#mysqld_safe --defaults-file=/etc/my.cnf  &
[root@test64 ~]#mysqld_safe --defaults-file=/etc/my.cnf  &
查看mysql 集群状态:
[root@test64 ~]#ndb_mgm
45.2.3  数据同步
因为默认密码比较坑人,我们就需要在此之前改一下两台机器mysql的密码。
1.test63 修改root密码:

[root@test63 /]# cat /root/.mysql_secret 
# The random password set for the root user at Tue Dec  2 21:25:59 2014 (local time): rWG1av6XjzT4ghfd
[root@test63 /]# mysql -uroot -prWG1av6XjzT4ghfd
mysql> show databases;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
mysql> set password=password('123456');
Query OK, 0 rows affected (0.72 sec)
mysql> exit;
Bye
[root@test63 /]# mysql -uroot -p123456

2.test64 修改root密码:

[root@test64 ~]# cat /root/.mysql_secret 
# The random password set for the root user at Tue Dec  2 21:31:11 2014 (local time): 2nOsamIw5wkBNhDm
[root@test64 ~]# mysql -uroot -p2nOsamIw5wkBNhDm
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.21-ndb-7.3.7-cluster-gpl
Copyright (c) 2000, 2014, 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> set password=password('123456');
Query OK, 0 rows affected (0.42 sec)
mysql> exit;
Bye
[root@test64 ~]# mysql -uroot -p123456
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.21-ndb-7.3.7-cluster-gpl MySQL Cluster Community Server (GPL)
Copyright (c) 2000, 2014, 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;
+--------------------+
3.插入数据:
test63:
mysql> create database mk;
test64:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mk                 |
| mysql              |
| ndb_3_fs           |
| ndbinfo            |
| performance_schema |
| test               |

45.2.4 关闭服务
关闭mysql集群顺序: 关闭管理节点服务-》 关闭管理节点时,数据结点服务自动关闭 –》 需要手动关闭SQL结点服务

  [root@test63 /]   # ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> shutdown
Node 2: Cluster shutdown initiated
Node 3: Cluster shutdown initiated
3 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.
Node 2: Node shutdown completed.
ndb_mgm> exit
ps -axu | grep  ndbd        #查看不到,说明数据节点已经被关

手动关闭SQL结点服务
test64上,手动关闭SQL结点服务

[root@test63 ~]# ps -axu | grep mysql

Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root      7617  0.0  0.1 106280  1424 pts/0    S    21:52   0:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf

mysql     7743  0.2 38.9 1342572 453680 pts/0  Sl   21:52   0:03 /usr/sbin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid

root      7889  0.0  0.2 125868  2364 pts/3    S+   22:11   0:00 mysql -uroot -px xxxx
root      7916  0.0  0.0 103300   852 pts/1    S+   22:17   0:00 grep mysql
[root@test63 ~]# kill -9 7617
[root@test63 ~]# kill -9 7743
[root@test63 ~]# ps -axu | grep mysql
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root      7889  0.0  0.2 125868  2364 pts/3    S+   22:11   0:00 mysql -uroot -px xxxx
root      7920  0.0  0.0 103300   852 pts/1    S+   22:18   0:00 grep mysql

test64上,手动关闭SQL结点服务

[root@test64 ~]# ps -axu | grep mysql
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root      5576  0.0  0.0 106284   644 pts/0    S    21:53   0:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf
mysql     5701  0.8 37.6 1352816 437908 pts/0  Sl   21:53   0:10 /usr/sbin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid
root      5801  0.0  0.2 125860  2740 pts/0    S+   22:10   0:00 mysql -uroot -px xxxx
root      5826  0.0  0.0 103300   844 pts/1    S+   22:15   0:00 grep mysql
[root@test64 ~]# kill -9  5576
 [root@test64 ~]# kill -9  5701
[root@test64 ~]# ps -axu | grep mysql
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root      5801  0.0  0.2 125860  2740 pts/0    S+   22:10   0:00 mysql -uroot -px xxxx
root      5836  0.0  0.0 103300   844 pts/1    S+   22:16   0:00 grep mysql

45.2.5 总结
再次启动,msyql集群启动:

 [root@test63 /]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini 
MySQL Cluster Management Server mysql-5.6.21 ndb-7.3.7
 [root@test63 /]#ndbd
2014-12-02 22:41:59 [ndbd] INFO     -- Angel connected to '192.168.1.63:1186'
2014-12-02 22:41:59 [ndbd] INFO     -- Angel allocated nodeid: 2
[root@test64 /]#ndbd  
2014-12-02 22:41:59 [ndbd] INFO     -- Angel connected to '192.168.1.63:1186'
2014-12-02 22:41:59 [ndbd] INFO     -- Angel allocated nodeid: 2
[root@test63 /]# mysqld_safe --defaults-file=/etc/my.cnf &
[root@test64 /]# mysqld_safe --defaults-file=/etc/my.cnf &

[root@test63 /]# mysqld_safe --defaults-file=/etc/my.cnf &
[root@test64 /]# mysqld_safe --defaults-file=/etc/my.cnf &

测试:
查看mysql 集群状态:

[root@test63 /]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show 
Connected to Management Server at: 192.168.1.63:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2    @192.168.1.63  (mysql-5.6.21 ndb-7.3.7, Nodegroup: 0, *)
id=3    @192.168.1.64  (mysql-5.6.21 ndb-7.3.7, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @192.168.1.63  (mysql-5.6.21 ndb-7.3.7)

[mysqld(API)]   2 node(s)
id=4    @192.168.1.63  (mysql-5.6.21 ndb-7.3.7)
id=5    @192.168.1.64  (mysql-5.6.21 ndb-7.3.7)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值