时间:2021-07-01 10:21:17 帮助过:15人阅读
1、节点之间时间必须同步;
建议使用ntp协议进行;
2、节点之间必须要通过主机名互相通信;
建议使用hosts文件;
通信中使用的名字必须与其节点为上“uname -n”命令展示出的名字保持一致;
3、如果是2个节点,需要仲裁设备;
4、节点之间彼此root用户能基于ssh密钥方式进行通信;
注意:定义为集群服务中的任意资源都不能开机自动启动,因为它们将由CRM启动;
具体设置参考前文:http://dengaosky.blog.51cto.com/9215128/1964550
五. 方案实现
实验环境介绍
2.DRBD的安装
DRBD介绍
官方站点:http://www.drbd.org/
DRBD(DistributedReplicatedBlockDevice)是一个基于块设备级别在远程服务器直接同步和镜像数据的软件,用软件实现的、无共享的、服务器之间镜像块设备内容的存储复制解决方案。它可以实现在网络中两台服务器之间基于块设备级别的实时镜像或同步复制(两台服务器都写入成功)/异步复制(本地服务器写入成功),相当于网络的RAID1,由于是基于块设备(磁盘,LVM逻辑卷),在文件系统的底层,所以数据复制要比cp命令更快。DRBD已经被MySQL官方写入文档手册作为推荐的高可用的方案之一.
获取DRBD软件程序,CentOS 6.6的内核版本是2.6.32-504
[root@node1 ~]# uname -r
2.6.32-504.el6.x86_64
DRBD已经合并到linux kernel2.6.33及以后内核版本中,这里直接安装管理工具即可,若内核版本低于2.6.33时请额外安装DRBD内核模块,且与管理工具版本保持一致。
kmod-drbd84-8.4.5-504.1.el6.x86_64.rpm
drbd84-utils-8.9.1-1.el6.elrepo.x86_64.rpm
# rpm -ivhdrbd84-utils-8.9.1-1.el6.elrepo.x86_64.rpmkmod-drbd84-8.4.5-504.1.el6.x86_64.rpm
3.DRBD的配置
可以参考我的前一篇文章:http://dengaosky.blog.51cto.com/9215128/1964590
我这里就只帖配置了!
配置之前需要先使用fdisk对/dev/sdb进行分区。
# echo -n -e"n\np\n3\n\n+1G\nw\n" |fdisk /dev/sda
# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
配置文件的内容如下(node1和node2的配置一样):
[root@node1 drbd.d]# cat global_common.conf
# DRBD is the result of over a decade ofdevelopment by LINBIT.
# In case you need professional servicesfor DRBD or have
# feature requests visithttp://www.linbit.com
global {
usage-count no;
# minor-count dialog-refresh disable-ip-verification
}
common {
handlers {
# These are EXAMPLE handlersonly.
# They may have severeimplications,
# like hard resetting the nodeunder certain circumstances.
# Be careful when chosing yourpoison.
pri-on-incon-degr"/usr/lib/drbd/notify-pri-on-incon-degr.sh;/usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ;reboot -f";
pri-lost-after-sb "/usr/lib/drbd/notify-pri-lost-after-sb.sh;/usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ;reboot -f";
local-io-error"/usr/lib/drbd/notify-io-error.sh;/usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ;halt -f";
# fence-peer"/usr/lib/drbd/crm-fence-peer.sh";
# split-brain"/usr/lib/drbd/notify-split-brain.sh root";
# out-of-sync"/usr/lib/drbd/notify-out-of-sync.sh root";
# before-resync-target"/usr/lib/drbd/snapshot-resync-target-lvm.sh -p 15 -- -c 16k";
# after-resync-target/usr/lib/drbd/unsnapshot-resync-target-lvm.sh;
}
startup {
# wfc-timeout degr-wfc-timeoutoutdated-wfc-timeout wait-after-sb
}
options {
# cpu-maskon-no-data-accessible
}
disk {
on-io-error detach;
# size on-io-error fencingdisk-barrier disk-flushes
# disk-drain md-flushesresync-rate resync-after al-extents
# c-plan-ahead c-delay-targetc-fill-target c-max-rate
# c-min-rate disk-timeout
}
net {
protocol C;
cram-hmac-alg "sha1";
shared-secret"password";
# protocol timeoutmax-epoch-size max-buffers unplug-watermark
# connect-int ping-intsndbuf-size rcvbuf-size ko-count
# allow-two-primariescram-hmac-alg shared-secret after-sb-0pri
# after-sb-1pri after-sb-2prialways-asbp rr-conflict
# ping-timeoutdata-integrity-alg tcp-cork on-congestion
# congestion-fillcongestion-extents csums-alg verify-alg
# use-rle
}
syncer {
rate 1000M;
}
}
资源配置文件:
[root@node1 drbd.d]# cat mystore.res
resource mystore {
on node1.stu31.com {
device /dev/drbd0;
disk /dev/sda3;
address 172.16.31.10:7789;
meta-disk internal;
}
on node2.stu31.com {
device /dev/drbd0;
disk /dev/sda3;
address 172.16.31.11:7789;
meta-disk internal;
}
}
复制一份到node2:
[root@node1 drbd.d]# scp *node2:/etc/drbd.d/
4. DRBD的管理维护
创建DRBD资源
配置好drbd以后,就需要使用命令在node1和node2上创建配置的drbd资源,使用如下命令:
[root@node1 drbd.d]# drbdadm create-mdmystore # mystore为配置文件中定义的资源名
[root@node2 drbd.d]# drbdadm create-mdmystore
DRBD的启动和停止
#/etc/rc.d/init.d/drbd start #启动drbd
#/etc/rc.d/init.d/drbd stop #停止drbd
#/etc/rc.d/init.d/drbd restart #重启drbd
查看DRBD状态
#watch -n 1 ‘cat /proc/drbd‘
#/etc/init.d/drbd status
以上两种方式都可以查看drbd的状态
5. 设置当前节点为主节点,并进行格式化和挂载
由于默认没有主次节点之分,因而需要设置两个主机的主次节点,选择需要设置为主节点的主机,然后执行如下
node1为主节点
#强制设置主节点
[root@node1 ~]# drbdadm primary --force mystore
[root@node1 ~]# drbd-overview
0:mystore/0 SyncSource Primary/Secondary UpToDate/Inconsistent
[====>...............] sync‘ed: 29.0% (754064/1059216)K
完成后查看节点状态:
[root@node1 ~]# cat /proc/drbd
version: 8.4.5 (api:1/proto:86-101)
GIT-hash:1d360bde0e095d495786eaeb2a1ac76888e4db96 build by root@node1.magedu.com,2015-01-02 12:06:20
0:cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:1059216 nr:0 dw:0 dr:1059888 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:foos:0
6.格式化存储
[root@node1 ~]# mke2fs -t ext4 /dev/drbd0
挂载到一个目录:
[root@node1 ~]# mount /dev/drbd0 /mnt
复制一个文件到mnt:
[root@node1 ~]# cp /etc/issue /mnt
卸载存储:
[root@node1 ~]# umount /mnt
7.切换主节点为备节点,将node2提升为主节点
节点1设置为备节点:
[root@node1 ~]# drbdadm secondary mystore
[root@node1 ~]# drbd-overview
0:mystore/0 Connected Secondary/Secondary UpToDate/UpToDate
提升节点2为主节点:
[root@node2 ~]# drbdadm primary mystore
[root@node2 ~]# drbd-overview
0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate
挂载文件系统,查看文件是否存在:
[root@node2 ~]# mount /dev/drbd0 /mnt
[root@node2 ~]# ls /mnt
issue lost+found
卸载存储:
[root@node2 ~]# umount /mnt
注意:
(1)mount drbd设备以前必须把设备切换到primary状态。
(2)两个节点中,同一时刻只能有一台处于primary状态,另一台处于secondary状态。
(3)处于secondary状态的服务器上不能加载drbd设备。
(4)主备服务器同步的两个分区大小最好相同,这样不至于浪费磁盘空间,因为drbd磁盘镜像相当于网络raid 1。
8.初始化安装mariadb/mysql数据库
我们将drbd设备作为数据库的数据存储目录,我们在本地创建文件夹,将drbd0挂载到此文件夹。
[root@node1 ~]# mkdir /mydata
[root@node1 ~]# drbdadm primary all
[root@node1 ~]# drbd-overview
0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate
[root@node1 ~]# mount /dev/drbd0 /mydata
[root@node1 ~]# ls /mydata/
issue lost+found
挂载成功!
初始化安装mariadb必须在主节点进行:
创建用户mysql管理数据库及配置数据存储目录权限为mysql,两个节点都需要创建用户
# groupadd -r -g 306 mysql
# useradd -r -g 306 -u 306 mysql
创建数据存放目录:
[root@node1 ~]# mkdir /mydata/data
[root@node1 ~]# chown -R mysql:mysql/mydata/data/
获取mariadb数据库的二进制安装包:
mariadb-5.5.40-linux-x86_64.tar.gz
解压安装包到/usr/local:
[root@node1 ~]# tar xf mariadb-5.5.40-linux-x86_64.tar.gz -C /usr/local/
创建软链接:
[root@node1 ~]# cd /usr/local
[root@node1 local]# ln -sv mariadb-5.5.40-linux-x86_64/ mysql
`mysql‘ -> `mariadb-5.5.40-linux-x86_64/‘
进入安装目录:
[root@node1 local]# cd mysql
[root@node1 mysql]# pwd
/usr/local/mysql
[root@node1 mysql]# chown -R root:mysql ./*
初始化安装mariadb:
[root@node1 mysql]#scripts/mysql_install_db --user=mysql --datadir=/mydata/data
安装完成后查看数据目录:
[root@node1 mysql]# ls /mydata/data/
aria_log.00000001 aria_log_control mysql performance_schema test
安装成功!
mariadb配置文件的存放,如果我们希望一个节点的配置文件更改后,备节点同步更新,那么配置文件需要存放在drbd存储上是最合适的!
[root@node1 mysql]# cp support-files/my-large.cnf /mydata/data/my.cnf
[root@node1 mysql]# vim /mydata/data/my.cnf
[mysqld]
port = 3306
datadir = /mydata/data
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
# Try number of CPU‘s*2 forthread_concurrency
thread_concurrency = 8
innodb_file_per_table = on
skip_name_resolve = on
在本地创建软链接指向配置文件目录:
系统默认存在一个,不需要就删除:
[root@node1 mysql]# ls /etc/my.cnf
/etc/my.cnf
[root@node1 mysql]# rm -rf /etc/my.cnf
[root@node1 mysql]# ln -sv /mydata/data/my.cnf /etc/my.cnf
`/etc/my.cnf‘ -> `/mydata/data/my.cnf‘
服务脚本的创建:关闭开机自启动
[root@node1 mysql]# cp support-files/mysql.server/etc/init.d/mysqld
[root@node1 mysql]# chkconfig --add mysqld
[root@node1 mysql]# chkconfig mysqld off
启动服务测试:
[root@node1 mysql]# service mysqld start
Starting MySQL. [ OK ]
登录mysql创建数据库:
[root@node1 mysql]#/usr/local/mysql/bin/mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.40-MariaDB-log MariaDBServer
Copyright (c) 2000, 2014, Oracle, MontyProgram Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ toclear the current input statement.
MariaDB [(none)]> create databasetestdb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> \q
Bye
停止数据库服务:
[root@node1 mysql]# service mysqld stop
Shutting down MySQL. [ OK ]
现在开始到节点2配置mariadb数据库:
先降级节点1为从节点:
卸载存储:
[root@node1 mysql]# umount /mydata
降级:
[root@node1 mysql]# drbdadm secondarymystore
[root@node1 mysql]# drbd-overview
0:mystore/0 Connected Secondary/Secondary UpToDate/UpToDate
将节点2升级为主节点:
[root@node2 ~]# drbdadm primary mystore
[root@node2 ~]# drbd-overview
0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate
创建drbd设备挂载目录:
[root@node2 ~]# mkdir /mydata
[root@node2 ~]# chown -R mysql:mysql/mydata
挂载drbd设备:
[root@node2 ~]# mount /dev/drbd0 /mydata
[root@node2 ~]# ls /mydata/
data issue lost+found
[root@node2 ~]# ls /mydata/data/
aria_log.00000001 ib_logfile0 mysql node1.stu31.com.err testdb
aria_log_control ib_logfile1 mysql-bin.000001 performance_schema
ibdata1 my.cnf mysql-bin.index test
数据是同步了的!
解压二进制程序安装包:
[root@node2 ~]# tar xf mariadb-5.5.40-linux-x86_64.tar.gz -C /usr/local
创建软链接:
[root@node2 ~]# cd /usr/local
[root@node2 local]# ln -sv mariadb-5.5.40-linux-x86_64/ mysql
`mysql‘ ->`mariadb-5.5.40-linux-x86_64/‘
[root@node2 local]# cd mysql
[root@node2 mysql]# chown -R root:mysql ./*
创建服务脚本:只需要服务脚本了,其它都已经在node1上配好了。
[root@node2 mysql]# cp support-files/mysql.server/etc/init.d/mysqld
[root@node2 mysql]# chkconfig --add mysqld
配置文件需要软链接至/etc目录下:
[root@node2 mysql]# rm /etc/my.cnf
rm: remove regular file `/etc/my.cnf‘? y
[root@node2 mysql]# ln -sv/mydata/data/my.cnf /etc/my.cnf
`/etc/my.cnf‘ -> `/mydata/data/my.cnf‘
启动mysqld服务:
[root@node2 mysql]# service mysqld start
Starting MySQL... [ OK ]
查看在节点1上建立的数据库testdb是否存在:
[root@node2 mysql]#/usr/local/mysql/bin/mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.40-MariaDB-log MariaDBServer
Copyright (c) 2000, 2014, Oracle, MontyProgram Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ toclear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set (0.09 sec)
#授权远程用户登录
MariaDB [(none)]> grant all on *.* to‘root‘@‘172.16.%.%‘ identified by ‘oracle‘;
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> \q
Bye
安装成功!
停止mysqld服务:
[root@node2 mysql]# service mysqld stop
Shutting down MySQL.. [ OK ]
卸载DRBD设备:
[root@node2 mysql]# umount /dev/drbd0
关闭DRBD服务:
[root@node1 mysql]# service drbd stop
Stopping all DRBD resources: .
[root@node2 mysql]# service drbd stop
Stopping all DRBD resources: .
关闭数据库服务的自启动:两个节点都需要设置
# chkconfig mysqld off
下面就可以进行heartbeat的安装了!
###########################################################################################
9. 迁移mysql的数据到drdb(假设你的mysql已经在该服务器上安装好了)
DRBD已经安装并且能正常同步了,那么我们接下来要做的就是将本机的已安装的mysql的数据迁移到drbd上,具体的数据迁移步骤如下:
a) 关闭node1和node2的mysql,/etc/rc.d/init.d/mysqld stop
b) 在node1上创建存放数据库数据的目录:mkdir -pv /mydata/data
c) 在node1将配置文件放到drbd目录中:
#mv /etc/my.cnf /mydata/data
删除node1上的/etc/my.cnf,rm -f /etc/my.cnf
在node1和node2上执行如下命令创建软链接。
#ln -s /mydata/data/my.cnf /etc/my.cnf
d) 修改/etc/my.cnf的数据目录指向/mydata/data/
e) 将原来的mysql数据文件移动到/mydata/data/
f) 执行chown -R mysql.mysql /mydata/data/
g) 启动mysql
10. 手工切换DRBD
在没有安装配置drbd之前,drbd是不能自动切换的,我们可以写出过程来加深对drbd的理解,也更能明白heartbeat的工作流程,下面是手工切换的步骤:
a) 在node1上停止mysql,/etc/rc.d/init.d/mysqld stop。
b) 在node1上执行umount /dev/drbd0。
c) 在node1上执行drbdadm secondary all切换到从模式。当两个节点都是secondary模式的时候才可以将node2设置成primary。
d) 在node2上执行drbdadm primary all,将node2提升为主模式,并观察/proc/drbd是否正常。
e) 在node2上执行mount /dev/drbd0 /mydata挂在文件系统。
f) 启动node2的mysql,/etc/rc.d/init.d/mysqld start。
注意:node1和node2上的mysql用户的uid和gid要一样。不然切换后会导致mysql数据目录的属主不正确而启动失败。
11. 主从切换
主切换成从,需要先卸载文件系统,再执行降级为从的命令:
#umount /dev/drbd0
#drbdadm secondary all
从切换成主,要先执行升级成主的命令然后挂在文件系统:
#drbdadm primary all #如果不成功drbdsetup/dev/drbd0 primary -o
#mount /dev/drbd0 /drbd/
12. DRBD脑裂后的处理
当DRBD出现脑裂后,会导致drbd两边的磁盘不一致,处理方法如下:
在确定要作为从的节点上切换成secondary,并放弃该资源的数据:
#drbdadm secondary mystore
#drbdadm -- --discard-my-data connect mystore
在要作为primary的节点重新连接secondary(如果这个节点当前的连接状态为WFConnection的话,可以省略),使用如下命令连接:
#drbdadm connect mystore
###########################################################################################
13. heartbeat的安装
在DRBD调试没有问题之后,就可以开始安装和配置heartbeat了。需要确认DRBD和数据库服务都是关闭的。
Heartbeat介绍
官方站点:http://linux-ha.org/wiki/Main_Page
heartbeat可以资源(VIP地址及程序服务)从一台有故障的服务器快速的转移到另一台正常的服务器提供服务,heartbeat和keepalived相似,heartbeat可以实现failover功能,但不能实现对后端的健康检查.
获取heartbeat程序组:
[root@node1 heartbeat2]# ls
heartbeat-2.1.4-12.el6.x86_64.rpm
heartbeat-gui-2.1.4-12.el6.x86_64.rpm
heartbeat-ldirectord-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
安装程序包组:
node1和node2都需要安装;
前提条件需要安装如下依赖包:
[root@node1 heartbeat2]# yum install -ynet-snmp-libs libnet PyXML
安装heartbeat套件程序:
[root@node1 heartbeat2]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-gui-
2.1.4-12.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:heartbeat-pils ########################################### [ 25%]
2:heartbeat-stonith ########################################### [ 50%]
3:heartbeat ########################################### [ 75%]
4:heartbeat-gui ########################################### [100%]
14. Heartbeat的配置
Hearbeat的配置主要包括三个配置文件,authkeys,ha.cf和haresources的配置,下面就分别来看!
复制配置文件模版到/etc/ha.d目录下:
[root@node1 ~]# cd /usr/share/doc/heartbeat-2.1.4/
[root@node1 heartbeat-2.1.4]# cp auth keysha.cf haresources /etc/ha.d/
Authkerys的配置
这个文件用来配置密码认证方式,支持3种认证方式,crc,md5和sha1,从左到右安全性越来越高,消耗的资源也越多。因此如果heartbeat运行在安全的网路之上,比如私网,那么可以将验
证方式设置成crc,master和backup的authkeys配置一样。我的authkeys文件配置如下:
[root@node1 ~]# vim /etc/ha.d/authkeys
auth 2
2 sha1 password123
ha.cf的配置
需要先设置一下rsyslog:
配置rsyslog记录heartbeat的日志
[root@node1 ~]# vim /etc/rsyslog.conf
local0.* /var/log/heartbeat.log
[root@node1 ~]# scp /etc/rsyslog.conf node2:/etc/rsyslog.conf
rsyslog.conf 100%3203 3.1KB/s 00:00
ha.cf的配置文件:
[root@node1 ha.d]# cat /etc/ha.d/ha.cf|grep -v ^#
#定义heartbeat的日志记录
logfacility local0
#设定心跳(检测)时间为2秒
keepalive 2
#设置死亡时间为15秒
deadtime 30
warntime 10
initdead 120
#采用组播的方式传递信息
mcast eth0 225.135.121.21 694 1 0
#当Primary机器发生故障切换到Secondary机器后Primary恢复后是否进行切回操作
auto_failback on
node node1.stu31.com
node node2.stu31.com
ping 172.16.0.1
haresources的配置
haresources用来设置master的主机名、虚拟IP、服务以及磁盘挂载等,node1和node2的配置是一样的,下面的mysqld需要做成服务,放在/etc/rc.d/init.d/目录下,配置配置如下:
[root@node1 ha.d]# cat/etc/ha.d/haresources |grep -v ^#
node1.stu31.com IPaddr::172.16.31.180/24/eth0/172.16.31.255
drbddisk::mystore Filesystem::/dev/drbd0::/mydata::ext4 mysqld
注:该文件内IPaddr,Filesystem等脚本存放路径在/etc/ha.d/resource.d/下,也可在该目录下存放服务启动脚本(例如:mysqld),将相同脚本名称添到/etc/ha.d/haresources内容中,从而跟随heartbeat启动而启动该脚本。
#cp /etc/init.d/mysqld /etc/ha.d/resource.d/
IPaddr::172.16.31.180/24/eth0:用IPaddr脚本配置浮动VIP
drbddisk::mystore:用drbddisk脚本实现DRBD主从节点资源组的挂载和卸载
Filesystem::/dev/drbd0::/mydata::ext4:用Filesystem脚本实现磁盘挂载和卸载
设置完成后,将authkeys文件的权限改为400或600,再复制配置文件到节点2:
[root@node1 ha.d]# chmod 400 authkeys
[root@node1 ha.d]# scp authkeys ha.cf haresources node2:/etc/ha.d/
authkeys 100% 678 0.7KB/s 00:00
ha.cf 100% 10KB 10.3KB/s 00:00
haresources 100%6011 5.9KB/s 00:00
先启动drbd服务:
两个节点都启动drbd服务:
#service drbd start
#设置主节点为node1
[root@node1 ha.d]# drbd-overview
0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate
不用挂载。
启动heartbeat服务:
[root@node1 ha.d]# service heartbeat start;ssh node2 ‘service heartbeat start‘
logd is already running
Starting High-Availability services:
2015/01/04_10:38:05 INFO: Resource is stopped
Done.
logd is already running
Starting High-Availability services:
2015/01/04_10:38:05 INFO: Resource is stopped
Done.
需要注意的问题:
(1).在启动Heartbeat服务之前,一定要先启动drbd服务。
(2).用chkconfig mysqld off关闭mysqld服务自启动,这个服务的启动与关闭应由heartbeat负责执行。
查看挂载:
[root@node1 ha.d]# mount
/dev/mapper/vg0-root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts(rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc typebinfmt_misc (rw)
/dev/drbd0 on /mydata type ext4 (rw)
[root@node1 ha.d]# ls /mydata
data issue lost+found
查看监听端口是否启动:
[root@node1 ha.d]# ss -tunl |grep 3306
tcp LISTEN 0 50 *:3306 *:*
查看drbd状态:
[root@node1 ha.d]# drbd-overview
0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate /mydata ext4 987M 31M 904M4%
查看heartbeat的日志文件:
[root@node1 ha.d]# tail/var/log/heartbeat.log
Jan 4 12:09:20 node1 Filesystem[4905]: INFO: Resource is stopped
Jan 4 12:09:20 node1 ResourceManager[4659]: info: Running/etc/ha.d/resource.d/Filesystem /dev/drbd0 /mydata ext4 start
Jan 4 12:09:20 node1 ResourceManager[4659]: debug: Starting/etc/ha.d/resource.d/Filesystem /dev/drbd0 /mydata ext4 start
Jan 4 12:09:20 node1 Filesystem[4986]: INFO: Running start for /dev/drbd0 on/mydata
Jan 4 12:09:20 node1 Filesystem[4986]: INFO: Starting filesystem check on/dev/drbd0
Jan 4 12:09:20 node1 Filesystem[4975]: INFO: Success
Jan 4 12:09:20 node1 ResourceManager[4659]: debug:/etc/ha.d/resource.d/Filesystem /dev/drbd0 /mydata ext4 start done. RC=0
Jan 4 12:09:20 node1 ResourceManager[4659]: info: Running/etc/ha.d/resource.d/mysqld start
Jan 4 12:09:20 node1 ResourceManager[4659]: debug: Starting/etc/ha.d/resource.d/mysqld start
Jan 4 12:09:22 node1 ResourceManager[4659]: debug:/etc/ha.d/resource.d/mysqld start done.RC=0
客户端连接数据库服务器进行测试:
[root@nfs ~]# mysql -h 172.16.31.180 -uroot -poracle
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.40-MariaDB-log MariaDBServer
Copyright (c) 2000, 2013, Oracle and/or itsaffiliates. All rights reserved.
Oracle is a registered trademark of OracleCorporation and/or its
affiliates. Other names may be trademarksof their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ toclear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set (0.08 sec)
mysql> \q
Bye
15.Heartbeat+DRBD测试
环境搭建好以后,就需要进行周密的测试,看是否实现了预期的功能:
停掉master的heartheat看看是否能正常切换。
停掉master的网络或者直接将master系统shutdown,看看能否正常切换。
启动master的heartbeat看看是否能正常切换回来。
重新启动master看看能否切换过程是否OK。
注意:这里说的切换是指是否已经将mysql停掉、是否卸载了文件系统等等。
我们将节点1的heartbeat服务关闭:
[root@node1 ha.d]# service heartbeat stop
Stopping High-Availability services:
Done.
查看DRBD状态:成了备节点
[root@node1 ha.d]# drbd-overview
0:mystore/0 Connected Secondary/Primary UpToDate/UpToDate
mysql服务关闭:查看mysqld服务监听端口:
[root@node1 ha.d]# ss -tunl |grep 3306
到node2查看:
heartbeat服务状态:
[root@node2 ~]# service heartbeat status
heartbeat OK [pid 4296 et al] is running onnode2.stu31.com [node2.stu31.com]...
DRDB服务状态:
[root@node2 ~]# drbd-overview
0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate /mydata ext4 987M 31M 904M4%
mysqld服务监听端口启动:
[root@node2 ~]# ss -tunl |grep 3306
tcp LISTEN 0 50 *:3306 *:*
文件系统挂载成功:
[root@node2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-root 20G 507M 19G 3% /
tmpfs 499M 0 499M 0% /dev/shm
/dev/sda1 190M 28M 153M 16% /boot
/dev/mapper/vg0-usr 9.8G 3.0G 6.4G 32% /usr
/dev/mapper/vg0-var 20G 255M 19G 2% /var
/dev/drbd0 987M 31M 904M 4% /mydata
再次进行远程连接:
[root@nfs ~]# mysql -h 172.16.31.180 -uroot -poracle
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.40-MariaDB-log MariaDBServer
Copyright (c) 2000, 2013, Oracle and/or itsaffiliates. All rights reserved.
Oracle is a registered trademark of OracleCorporation and/or its
affiliates. Other names may be trademarksof their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ toclear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set (0.07 sec)
mysql> \q
Bye
在node1上再次启动heartbeat服务:
[root@node1 ha.d]# service heartbeat start
Starting High-Availability services:
2015/01/04_13:33:07 INFO: Resource is stopped
Done.
[root@node1 ha.d]# drbd-overview
0:mystore/0 Connected Secondary/Secondary UpToDate/UpToDate
[root@node1 ha.d]# drbd-overview
0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate /mydata ext4 987M 31M 904M 4%
[root@node1 ha.d]# ss -tunl |grep 3306
tcp LISTEN 0 50 *:3306 *:*
节点1恢复后会自动成为主节点。
测试成功。
至此,一个heartbeat+DRBD+mysql构成的高可用集群就完成了!
本文出自 “眼眸刻着你的微笑” 博客,请务必保留此出处http://dengaosky.blog.51cto.com/9215128/1964598
heartbeat v1(CRM)+DRBD实现数据库服务器高可用集群搭建
标签:drbd mariadb heartbeat