系统:centos6.4最小化
编译环境:yum -y groupinstall "Development tools" "Server Platform Development"
heartbeat版本:heartbeat-3.0.4-2.el6.x86_64.rpm heartbeat-libs-3.0.4-2.el6.x86_64.rpm
搭建高可用集群之前,首要前提条件:
1、节点之间必须能够ssh互信通信
2、节点间需传递集群事务信息,网段选择
3、节点名称与hostname(uname -n)一致,不要使用dns解析
4、各节点之间保持时间上的同步
######################################################################
节点选择:
node1:192.168.31.111
node2:192.168.31.112
VIP:192.168.31.113
NFS:192.168.31.114
######################################################################
node1:192.168.31.111
1、ssh互信通信
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
ssh-copy-id .ssh/id_rsa.pub [email protected]
node2:192.168.31.112同步此操作
2、节点名称与hostname
vim /ets/hosts
192.168.31.111 node1
192.168.31.112 node2
hostname node1
vim /etc/sysconfig/network
HOSTNAME=node1
node2:192.168.31.112同步以上操作
3、时间同步
# rdate -s time-b.nist.gov
启动例行计划任务
crontab -u root -e
*/2 * * * * /usr/bin/rdate -s time-b.nist.gov
node2:192.168.31.116同步以上操作
#######################################################
准备工作已就绪,开始下载需要的工具
heartbeat-3.0.4-2.el6.x86_64.rpm,heartbeat-libs-3.0.4-2.el6.x86_64.rpm
下载网址:https://dl.fedoraproject.org/pub/epel/6/x86_64/repoview/
安装:
yum -y --nogpgcheck localinstall heartbeat-3.0.4-2.el6.x86_64.rpm,heartbeat-libs-3.0.4-2.el6.x86_64.rpm
cd /etc/ha.d/
复制heartbeat需要的三个配置文件:
cp -a /usr/share/doc/heartbeat-3.0.4/{authkeys,ha.cf,haresources} /etc/ha.d/
cd /etc/ha.d/
1、配置authkeys
auth 1
1 md5 hhhkkloooo:后接一段随机密码
生成随机数:
dd if=/dev/random bs=512 count=1|md5sum
[root@node1 heartbeat-3.0.4]# dd if=/dev/random bs=512 count=1|md5sum
记录了0+1 的读入
记录了0+1 的写出
19字节(19 B)已复制,0.000102463 秒,185 kB/秒
1b3b81cba0cb16a43195af2508a48ff8
注意:修改密钥文件为600的权限最好
chmod 600 authkeys
2、配置主配置文件ha.cf
node node1
node node2
bcast eth0 # Linux
deadtime 30
keepalive 2
logfile /var/log/ha-log
简单配置这些就行
3、配置资源
vi haresources
添加如下一行:
node1 IPaddr::192.168.31.113/24/eth0 mysqld:定义主节点以及VIP以及服务
将配置完的三个文件一并复制到node2:192.168.31.112
scp ha.cf authkeys haresources node2:/etc/ha.d/
注意:检查两个节点的配置文件权限是否一致
NFS配置:
NFS:192.168.31.114
创建一个逻辑卷用来挂载本地/mydata
fdisk /dev/sda
[root@NFS ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f3e25
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 664 5120000 83 Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3 664 1301 5120000 83 Linux
/dev/sda4 1301 2611 10525696 5 Extended
/dev/sda5 1301 1429 1024000 82 Linux swap / Solaris
/dev/sda6 1429 1821 3155279 8e Linux LVM
[root@NFS ~]#
[root@NFS ~]# pvcreate /dev/sda6
Physical volume "/dev/sda6" successfully created
[root@NFS ~]# vgcreate myvg /dev/sda6
Volume group "myvg" successfully created
[root@NFS ~]# lvcreate -L 2G -n mylv myvg
Logical volume "mylv" created
[root@NFS ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
mylv myvg -wi-a---- 2.00g
然后格式化:
mke2fs -j /dev/myvg/mylv
编辑开机自动挂载:
/dev/myvg/mylv /mydata ext3 defaults 0 0
mount -a
mount
mkdir /mydata/data
groupadd -g 3306 mysql
useradd -g mysql -u 3306 -s /sbin/nologin -M mysql
chown -R mysql:mysql /mydata/data
[root@NFS ~]# ls -l /mydata/
总用量 20
drwxr-xr-x. 2 mysql mysql 4096 5月 15 01:11 data
drwx------. 2 root root 16384 5月 15 01:04 lost+found
vi /etc/exports
/mydata 192.168.31.0/24(no_root_squash,rw)
接下来配置节点上的mysql数据库
node1:192.168.31.111
选用的是通用二进制格式mysql:mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
[root@node1 ~]# tar xf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[root@node1 ~]# cd /usr/local/
[root@node1 local]# ls
bin etc games include lib lib64 libexec mysql-5.6.30-linux-glibc2.5-x86_64 sbin share src
[root@node1 local]# ln -s mysql-5.6.30-linux-glibc2.5-x86_64/ /usr/local/mysql
[root@node1 local]# cd mysql
[root@node1 mysql]# ls
bin COPYING data docs include lib man mysql-test README scripts share sql-bench support-files
在节点上各自也分别创建mysql用户与组
groupadd -g 3306 mysql
useradd -g mysql -u 3306 -s /sbin/nologin -M mysql
[root@node1 mysql]# chown -R root:mysql ./*
mkdir /mydata
挂载NFS,mount -t nfs 192.168.31.114:/mydata /mydata
然后初始化mysql
[root@node1 mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
vi my.cnf加上如下两行
socket = /tmp/mysql.sock
datadir = /mydata/data
[root@node1 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@node1 mysql]# /usr/local/mysql/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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 |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.09 sec)
mysql> grant all on *.* to root@'%' identified by 'redhat';
接下来配置node2:192.168.31.112
选用的是通用二进制格式mysql:mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
[root@node1 ~]# tar xf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[root@node1 ~]# cd /usr/local/
[root@node1 local]# ls
bin etc games include lib lib64 libexec mysql-5.6.30-linux-glibc2.5-x86_64 sbin share src
[root@node1 local]# ln -s mysql-5.6.30-linux-glibc2.5-x86_64/ /usr/local/mysql
[root@node1 local]# cd mysql
[root@node1 mysql]# ls
bin COPYING data docs include lib man mysql-test README scripts share sql-bench support-files
mkdir /mydata
由于数据文件在nfs服务器上面,及node2并不需要初始化mysql,只需要配备配置文件,以及启动脚本即可
于是停止node1上面的mysql以及卸载数据目录
service mysqld stop
umount /mydata
将配置文件以及启动脚本复制到节点2上
scp my.cnf node2:/etc/
scp /etc/init.d/mysqld node2:/etc/init.d/
mysql准备就绪后,就挂载数据目录
mount -t nfs 192.168.31.114:/mydata /mydata
然后可以开始启动mysql了
[root@node2 data]# service mysqld start
Starting MySQL..... SUCCESS!
[root@node2 data]# /usr/local/mysql/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | |
| root | node1 | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | node1 | |
| root | % | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
+------+-----------+-------------------------------------------+
7 rows in set (0.12 sec)
mysql> \q
可以看出启动成功,成功访问了刚刚创建的一个用户
开始配置heartbeat资源文件
停掉各节点上的mysql服务,以及挂载的目录
service mysqld stop
umount /mydata
vi /etc/ha.d/haresources
各节点都添加最好一行为:
node1 IPaddr::192.168.31.113/24/eth0 Filesystem::192.168.31.114:/mydata::/mydata mysqld
然后在node1上执行启动heartbeat服务
service heartbeat start
ssh node2 'service heartbeat start'
[root@node1 ~]# service heartbeat start
Starting High-Availability services: INFO: Resource is stopped
Done.
[root@node1 ~]# ssh node2 'service heartbeat start'
Starting High-Availability services: 2016/05/14_18:13:03 INFO: Resource is stopped
Done.
[root@node1 ~]#
接下来在nfs系统上安装mysql客户端
yum -y install mysql
[root@NFS mydata]# mysql -uroot -h192.168.31.113 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.30 MySQL 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| huang |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.21 sec)
mysql>
上述复制即可看出已经搭建成功了,基于mysql与heartbeat的高可用集群