Heartbeat V2+NFS实现MySQL的高可用
实验拓扑:
实验环境:
OS:CentOS-6.7-x86_64
Node1:
IP:eth0,192.168.1.4
VIP:eth0:0,192.168.1.3
Node2:
IP:eth0,192.168.1.5
VIP:eth0:0,192.168.1.3
NFS+NTP:
IP:192.168.1.6
需要资源:VIP、mysqld、存储
实验流程:
配置高可用集群的前提:
1、时间必须保持同步:使用ntp服务器
[root@node3 ~]# yum install ntp
[root@node3 ~]# vim /etc/ntp.conf
restrict 192.168.1.0 mask 255.255.255.0 nomodify
server 127.127.1.0
fudge 127.127.1.0 stratum 1
启动服务:
[root@node3 ~]# chkconfig ntpd on
[root@node3 ~]# service ntpd start
各节点添加对时任务:
[root@node1 ~]# crontab -e
*/5 * * * * /usr/sbin/ntpdate 192.168.1.6 &> /dev/null
2、节点必须名称互相通信:
编辑/etc/host解析节点名称:
[root@node1 ~]# vim /etc/hosts
192.168.1.4 node1.chencer.org node1
192.168.1.5 node2.chencer.org node2
3、ssh密钥认证进行通信;
节点间ssh互信认证:
[root@node1 ~]# ssh-keygen -t rsa
[root@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node2
NFS:
提供共享存储:
[root@node3 ~]# mkdir /data/mydata
[root@node3 ~]# groupadd -g 306 -r mysql
[root@node3 ~]# useradd -u 306 -g 306 -r mysql
[root@node3 ~]# chown -R mysql.mysql /data/
安装服务,设置共享目录:
[root@node3 ~]# yum install nfs-utils
[root@node3 ~]# vim /etc/exports
/data 192.168.1.0/24(rw,async,no_root_squash)
启动服务:
[root@node3 ~]# chkconfig nfs on
[root@node3 ~]# service nfs start
Node1和Node2:
安装配置nfs工具并挂载共享目录:
[root@node1 ~]# yum install nfs-utils
[root@node1 ~]# mkdir /data
[root@node1 ~]# mount -t nfs 192.168.1.6:/data /data
二进制格式安装Mysql-5.5:
解决软件依赖关系:
[root@node1 ]# yum install libaio
创建mysql用户:
[root@node1 ~]# groupadd -g 306 -r mysql
[root@node1 ~]# useradd -u 306 -g 306 -r mysql
二进制安装设置:
[root@node1 ~]# tar xf mysql-5.5.47-linux2.6-x86_64.tar.gz -C /usr/local/
[root@node1 ~]# cd /usr/local/
[root@node1 local]# ln -sv mysql-5.5.47-linux2.6-x86_64 mysql
"mysql" -> "mysql-5.5.47-linux2.6-x86_64"
[root@node1 local]# cd mysql/
[root@node1 mysql]# chown -R root.mysql ./*
初始化数据:只需在1个节点执行1次;
[root@node1 mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mydata/
导出头文件、库文件、man手册:
[root@node1 mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql
"/usr/include/mysql" -> "/usr/local/mysql/include"
[root@node1 mysql]# vim /etc/ld.so.conf.d/mysql-x86_64.conf
/usr/local/mysql/lib
[root@node1 mysql]# ldconfig
[root@node1 mysql]# vim /etc/man.config
MANPATH /usr/local/mysql/man
添加并重读环境变量:
[root@node1 mysql]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@node1 mysql]# source /etc/profile.d/mysql.sh
提供配置文件和服务启动脚本:
[root@node1 mysql]# mkdir -p /etc/mysql
[root@node1 mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf
[root@node1 mysql]# vim /etc/mysql/my.cnf
[mysqld]
datadir = /data/mydata
thread_concurrency = 2
[root@node1 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@node1 mysql]# chmod +x /etc/rc.d/init.d/mysqld
加入服务列表,保证开机不自动启动:
[root@node1 mysql]# chkconfig --add mysqld
[root@node1 mysql]# chkconfig mysqld off
测试启动数据库:
[root@node1 mysql]# service mysqld start
Starting MySQL... SUCCESS!
[root@node1 mysql]# service mysqld stop
Shutting down MySQL. SUCCESS!
node1卸载共享存储,挂载到node2测试启动数据库:
[root@node1 mysql]# umount /data/
[root@node2 mysql]# mount -t nfs 192.168.1.6:/data /data
[root@node2 mysql]# service mysqld start
Starting MySQL... SUCCESS!
[root@node2 mysql]# service mysqld stop
Shutting down MySQL. SUCCESS!
[root@node2 mysql]# umount /data/
为数据库创建并授权测试用户:
[root@node1 mysql]# mysql
mysql> grant all on *.* to root@'192.168.1.%' identified by 'rootroot';
Query OK, 0 rows affected (1.65 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
安装heartbeat依赖的软件:
[root@node1 ~]# yum install PyXML pygtk2-devel pygtk2-libglade perl-TimeDate gettext-devel libtool-ltdl libnet net-snmp-libs
注意:libnet来自于epel源;
安装heartbeat:
[root@node1 heartbeat2]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm heartbeat-gui-2.1.4-12.el6.x86_64.rpm
提供配置文件:
[root@node1 ~]# cp /usr/share/doc/heartbeat-2.1.4/{authkeys,ha.cf} /etc/ha.d/
配置heartbeat服务:
生成密钥并配置密钥文件,:
[root@node1 ~]# openssl rand -hex 8
dc84d1ca8af1b269
[root@node1 ~]# vim /etc/ha.d/authkeys
auth 1
1 md5 dc84d1ca8af1b269
[root@node1 ~]# chmod 600 /etc/ha.d/authkeys
编辑主配置文件:
[root@node1 ~]# vim /etc/ha.d/ha.cf
logfile /var/log/ha-log
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport 694
mcast eth0 225.0.0.1 694 1 0
auto_failback on
node node1.chencer.org
node node2.chencer.org
ping 192.168.1.1
compression bz2
compression_threshold 2
crm on
将以上两个文件复制到另一节点:
[root@node1 ~]# scp -r /etc/ha.d/{authkeys,ha.cf} node2:/etc/ha.d/
heartbeat-gui会自动创建一个用户hacluster,在2个节点上为hacluster用户设置密码:
[root@node1 ~]# passwd hacluster
启动heartbeat服务:
[root@node1 ~]# service heartbeat start;ssh node2 'service heartbeat start'
查看集群状态:
[root@node2 ~]# crm_mon
启动gui管理器:
[root@node1 ~]# hb_gui &
添加资源: 首先添加一个组:
添加VIP:
添加共享存储:
添加Mysql服务:
添加资源约束:
vip和sharestore在同一节点上:
sharestore和mysqlserver在同一个节点上:
添加启动顺序:VIP --> sharestore --> mysqlserver
启用所有资源:
访问测试:
[root@node3 ~]# mysql -uroot -h192.168.1.3 -p
Enter password:
node2切换为备用节点,node1为主节点:
继续测试链接数据库:
[root@node3 ~]# mysql -uroot -h192.168.1.3 -p
Enter password: