mysql+drbd+heartbeat高可用综合
在企业的实际生产中, 一主多从的mysql数据库架构是最常用的DB架构方案,该架构方案部署简单,维护方便
并且通过配置简单的代理或者通过程序的方式就可以实现应用服务器对主从库的读写分离,且多个从库还可以通过lv或者
haproxy等代理实现对多个从库的负载均衡,分担读的压力,同时排除单点
但是,在以上的mysql数据库架构中,我们不难发现,虽然从库是多个,但是主库仅有一个,也就是说一旦主库宕机,所有的
写业务都会终止,而从库宕机一个就没有什么大的影响,那么如何解决这个主库单点的问题呢,其实,最简单的方案就是做好监控
,然后主库宕机后,有管理员人为的手工选择最快的从库改为主,然后让其他从库和新主库同步,这个方式简单易行,但是需要人工
处理,对有些要求高的场合高度不够,那有没有不需要人工处理的方案呢
mysql高可用生产需求
假设有三台数据库服务器data-1-1/data-1-2/data-1-3,其实际ip分别为10.0.0.7(data-1-1)
10.0.0.8(data-1-2),10.0.0.9(data-1-3机器)
data-1-1的数据库文件目录为/data,对前端提供访问的vip为10.0.0.17
配置目标:一旦主数据库data-1-1宕机,服务器上的mysql数据库服务和虚拟ip会自动切换到服务器data-1-2
从而达到mysql数据库高可用无业务影响的目的,
这里会有一个特别的问题,就是以前的多个从库如何自动和新的主库同步,经过实践,通过drbd的方式同步的数据库,以及做从库时使用和
主库对外提供的vip为同步vip,当主库宕机后,vip飘逸到热备主库,默认情况60秒内,从库就可以连接到新的
vip,从而自动和新的主库同步,这里需要强调下,通过mysql同步做双主的方式,是难以做到主库宕机从库和新的
主库自动同步
生产配置场景
删除两台虚拟机的硬盘,从新添加两块两G 的硬盘,从新添加两块两G硬盘
[root@data-1-2 ~]# fdisk -l
2031616 82 Linux swap / Solaris
[root@data-1-1 ~]# tail -2 /etc/hosts
192.168.1.107 data-1-1
192.168.1.108 data-1-2
[root@data-1-2 ~]# tail -2 /etc/hosts
192.168.1.107 data-1-1
192.168.1.108 data-1-2
[root@data-1-2 ~]# fdisk -l
/dev/sda3 2358 2611 2031616 82 Linux swap / Solaris
配置好heartbeat
[root@data-1-1 ~]# ip addr|grep 192.168
inet 192.168.1.107/24 brd 192.168.1.255 scope global eth1
inet 192.168.0.107/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.7/24 brd 192.168.0.255 scope global secondary
[root@data-1-2 ~]# ip addr|grep 192.168
inet 192.168.1.108/24 brd 192.168.1.255 scope global eth1
inet 192.168.0.108/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.8/24 brd 192.168.0.255 scope globa
[root@data-1-1 ~]# parted /dev/sdb mklabel gpt#大于2T的进行分区
Information: You may need to update /etc/fstab.
[root@data-1-1 ~]# parted /dev/sdb mkpart primary 0 1024
Warning: The resulting partition is not properly
aligned for best performance.
Ignore/Cancel? Ignore##
Information: You may need to update /etc/fstab.
[root@data-1-1 ~]# parted /dev/sdb mkpart primary 1025 2146
Warning: The resulting partition is not properly
aligned for best performance.
Ignore/Cancel? Ignore###
Information: You may need to update /etc/fstab.
[root@data-1-1 ~]# parted /dev/sdb p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 1024MB 1024MB primary
2 1025MB 2146MB 1121MB primary
[root@data-1-2 ~]# parted /dev/sdb mklabel gpt
Information: You may need to update /etc/fstab.
[root@data-1-2 ~]# parted /dev/sdb mkpart primary 0 1024
Warning: The resulting partition is not properly
aligned for best performance.
Ignore/Cancel? Ignore
Information: You may need to update /etc/fstab.
[root@data-1-2 ~]# parted /dev/sdb mkpart primary 1025 2146
Warning: The resulting partition is not properly
aligned for best performance.
Ignore/Cancel? Ignore
Information: You may need to update /etc/fstab.
[root@data-1-2 ~]# parted /dev/sdb p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 1024MB 1024MB primary
2 1025MB 2146MB 1121MB primary
2下载epel包
3#安装heartbeat
4##安装drbd
heartbeat不要自启动
5.##安装drbd加载到内核自启动
echo "modprobe drbd >/dev/null 2>&1" >/etc/sysconfig/modules/drbd.modules
6配置drbd.conf
[root@data-1-1 ~]# ll /etc/dr安装完成后会有drbd.conf,drbd.d/
dracut.conf drbd.conf drirc
dracut.conf.d/ drbd.d/
7#初始化drbd
[root@data-1-1 ~]# drbdadm create-md data
Writing meta data...
initializing activity log
NOT initializing bitmap
New drbd meta data block successfully created.
[root@data-1-2 ~]# drbdadm create-md data
Writing meta data...
initializing activity log
NOT initializing bitmap
New drbd meta data block successfully created.
[root@data-1-1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@data-1-1, 2016-01-08 22:44:43
0: cs:WFConnection ro:Secondary/Unknown ds:Inconsistent/DUnknown C r----s
ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:999984
[root@data-1-2 ~]# drbdadm up data
[root@data-1-1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)[版本]
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@data-1-1, 2016-01-08 22:44:43【编译信息】
0【设备/dev/drbd0,若一,表示设备/dev/data-1-1】:
cs[]:Connected[可通过drbdadm cstate all查询]
ro:Secondary/Secondary
ds:Inconsistent/Inconsistent diskstatus
ns:0 (network send)
nr:0 (network recive)
dw:0(disk write )
dr:0 (disk read)
al:0 (active log)
bm:0 (bit map)
lo:0 (local count)
pe:0 (pending)请求到对方还没有应答
ua:0 (unacknowledged)对端通过网络接收到请求还没有应答
ap:0 (application pending)block i/o request 已经转发到drbd,但是还没有收到应答
ep:1 (epochs)
wo:f
oos:999984
8设置主,同步数据到对端
drbdadm -- --overwrite-data-of-peer primary data
[root@data-1-1 ~]# mkdir /data -p
[root@data-1-2 ~]# mkdir /data -p
[root@data-1-1 ~]# mkfs -t ext4 -b 4096 /dev/drbd0
[root@data-1-1 ~]# mount /dev/drbd0 /data
[root@data-1-1 soft]# ls
drbd-8.4.4 drbd-8.4.4.tar.gz
[root@data-1-1 soft]# cd /data
[root@data-1-1 data]# cp ~/soft/drbd-8.4.4.tar.gz /data
[root@data-1-1 data]# cat /proc/drbd
[root@data-1-2 ~]# drbdadm down data
[root@data-1-2 ~]# mount /dev/sdb1 /mnt
[root@data-1-2 ~]# ll /mnt
total 712
-rw-r--r--. 1 root root 708879 Jan 9 18:39 drbd-8.4.4.tar.gz
drwx------. 2 root root 16384 Jan 9 18:34 lost+fo
[root@data-1-2 ~]# umount /mnt
[root@data-1-2 ~]# drbdadm up data
[root@data-1-1 etc]# cd /etc/ha.d/
drbddisk::data Filesystem::/dev/drbd0::/data::ext4 rsdata
[root@data-1-1 ha.d]# vi haresources
data-1-1 IPaddr::192.168.0.7/24/eth0 drbddisk::data Filesystem::/dev/drbd0::/data::ext4
data-1-2 IPaddr::192.168.0.8/24/eth0
[root@data-1-2 ~]# cd /etc/ha.d/
[root@data-1-2 ha.d]# ls
authkeys ha.cf.bak haresources.bak resource.d
authkeys.bak harc rc.d shellfuncs
ha.cf haresources README.config
[root@data-1-2 ha.d]# vi haresources
data-1-1 IPaddr::192.168.0.7/24/eth0 drbddisk::data Filesystem::/dev/drbd0::/data::ext4
data-1-2 IPaddr::192.168.0.8/24/eth0
[root@data-1-1 ha.d]# /etc/init.d/heartbeat stop
Stopping High-Availability services: Done.
[root@data-1-1 ha.d]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
INFO: Resource is stopped
Done.
[root@data-1-1 ha.d]# ip addr|grep 192.168
[root@data-1-2 ha.d]# /etc/init.d/heartbeat stop
Stopping High-Availability services: Done.
[root@data-1-2 ha.d]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
INFO: Resource is stopped
Done.
[root@data-1-2 ha.d]# ip addr|grep 192.168
[root@data-1-1 ha.d]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@data-1-1, 2016-01-08 22:44:43
0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:1033245 nr:8 dw:33270 dr:1003086 al:11 bm:62 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@data-1-2 ha.d]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@data-1-2, 2016-01-08 22:45:30
0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----
ns:8 nr:24 dw:32 dr:1039 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@data-1-1 ha.d]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 18G 2.7G 14G 16% /
tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 291M 34M 242M 13% /boot
/dev/drbd0 962M 18M 895M 2% /data
12
[root@data-1-1 ha.d]# tail -f /var/log/ha-log
[root@data-1-1 ha.d]# /etc/init.d/heartbeat stop
oot@data-1-2 ha.d]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 18G 2.7G 14G 16% /
tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 291M 34M 242M 13% /boot
/dev/drbd0 962M 18M 895M 2% /data接管了
[root@data-1-1 ha.d]# /etc/init.d/heartbeat start
[root@data-1-2 ha.d]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 18G 2.7G 14G 16% /
tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 291M 34M 242M 13% /boot