欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

corosync+drbd+mysql实现的高可用_MySQL

程序员文章站 2022-03-28 11:54:56
...
corosync+drbd+mysql实现的高可用_MySQL

要求:

一、能够在同一网段内直接通信

二、节点名称,要和uname的结果一样,并保证可以根据节点名称解析到节点的IP地址,配置本地/etc/hosts

三、SSH互信通信

四、保证时间同步

环境准备配置:

test1,192.168.10.55配置

1、配置IP地址

[root@test1~]#vim/etc/sysconfig/network-scripts/ifcfg-eth0

2、配置主机名

[root@test1~]#uname-n
[root@test1~]#hostnamemaster1.local#临时生效
[root@test1~]#vim/etc/sysconfig/network#永久生效

3、配置主机名解析

[root@test1~]#vim/etc/hosts
添加:
192.168.10.55master1.local
192.168.10.56master2.local

3.2、测试主机名通信

[root@test1~]#pingmaster1.local
[root@test1~]#pingmaster2.local

4、配置SSH互信认证

[root@test1~]#ssh-keygen-trsa-P''
[root@test1~]#ssh-copy-id-i.ssh/id_rsa.pubroot@192.168.10.55

5、使用ntp同步时间

在crontab中加入每5分钟执行一次ntpdate命令,用来保证服务器时间是同步的

[root@test1~]#crontab-e
*/5****/sbin/ntpdate192.168.10.1&>/dev/null

test2,192.168.10.56配置

1、配置IP地址

[root@test2~]#vim/etc/sysconfig/network-scripts/ifcfg-eth0

2、配置主机名

[root@test2~]#uname-n
[root@test2~]#hostnametest2.local#临时生效
[root@test2~]#vim/etc/sysconfig/network#永久生效

3、配置主机名解析

[root@test2~]#vim/etc/hosts
添加:
192.168.10.55test1.localtest1
192.168.10.56test2.localtest2

3.2、测试主机名通信

[root@test2~]#pingtest1.local
[root@test2~]#pingtest1

4、配置SSH互信认证

[root@test2~]#ssh-keygen-trsa-P''
[root@test2~]#ssh-copy-id-i.ssh/id_rsa.pubroot@192.168.10.56

5、使用ntp同步时间

在crontab中加入每5分钟执行一次ntpdate命令,用来保证服务器时间是同步的

[root@test2~]#crontab-e
*/5****/sbin/ntpdate192.168.10.1&>/dev/null

安装配置heartbeat

CentOS直接yum安装报错,提示找不到可用的软件包

解决办法:

[root@test1src]#wgethttp://mirrors.sohu.com/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
[root@test1src]#rpm-ivhepel-release-6-8.noarch.rpm

6.1、安装heartbeat:

[root@test1src]#yuminstallheartbeat

6.2、copy配置文件:

[root@test1src]#cp/usr/share/doc/heartbeat-3.0.4/{ha.cf,authkeys,haresources}/etc/ha.d/

6.3、配置认证文件:

[root@test1src]#ddif=/dev/randomcount=1bs=512|md5sum#生成随机数
[root@test1src]#vim/etc/ha.d/authkeys
auth1
1md5d0f70c79eeca5293902aiamheartbeat
[root@test1src]#chmod600authkeys

test2节点的heartbeat安装和test1一样,此处略过。

6.4、heartbeat主配置文件参数:

[root@test2~]#vim/etc/ha.d/ha.cf
#debugfile/var/log/ha-debug#排错日志
logfile#日志位置
keepalive2#多长时间发送一次心跳检测,默认2秒,可以使用ms
deadtime30#多长时间检测不到主机就认为挂掉
warntime10#如果没有收到心跳信息,那么在等待多长时间就认为对方挂掉
initdead120#第一个节点起来后,等待其他节点的时间
baud19200#串行线缆的发送速率是多少
auto_failbackon#故障恢复后是否转移回来
ping10.10.10.254#pingnode,万一节点主机不通,要ping哪个主机
ping_groupgroup110.10.10.25410.10.10.253#pingnodegroup,只要组内有一台主机能ping通就可以
respawnhacluster/usr/lib/heartbeat/ipfail#当一个heartbeat服务停止了,会重启对端的heartbeat服务
deadping30#pingnodes多长时间ping不通,就真的故障了
#serialserialportname...#串行设备是什么
serial/dev/ttyS0#Linux
serial/dev/cuaa0#FreeBSD
serial/dev/cuad0#FreeBSD6.x
serial/dev/cua/a#Solaris
#Whatinterfacestobroadcastheartbeatsover?#如果使用以太网,定义使用单播、组播还是广播发送心跳信息
bcasteth0#广播
mcasteth0225.0.0.169410#组播
ucasteth0192.168.1.2#单播,只有两个节点的时候才用单播
#定义stonith主机
stonith_host*baytech10.0.0.3myloginmysecretpassword
stonith_hostken3rps10/dev/ttyS1kathy0
stonith_hostkathyrps10/dev/ttyS1ken30
#Tellwhatmachinesareinthecluster#告诉集群中有多少个节点,
每一个节点用node和主机名写一行,主机名要和uname-n保持一致
nodeken3
nodekathy
一般只要定义心跳信息的发送方式、和集群中的节点就行。
bcasteth0
nodetest1.local
nodetest2.local
6.5、定义haresources资源配置文件:
[root@test2~]#vim/etc/ha.d/haresources
#node110.0.0.170Filesystem::/dev/sda1::/data1::ext2#默认用作主节点的主机名,要跟uname-n一样。VIP是多少。
自动挂载哪个设备,到哪个目录下,文件类型是什么。资源类型的参数要用双冒号隔开
#just.linux-ha.org135.9.216.110http#和上面一样,这里使用的资源是在/etc/rc.d/init.d/下面的,
默认先到/etc/ha.d/resource.d/目录下找资源,找不到在到/etc/rc.d/init.d/目录找
master1.localIPaddr::192.168.10.2/24/eth0mysqld
master1.localIPaddr::192.168.10.2/24/eth0drbddisk::dataFilesystem::/dev/drbd1::/data::ext3mysqld
#使用IPaddr脚本来配置VIP

6.6、拷贝master1.local的配置文件到master2.local上

[root@test1~]#scp-pha.cfharesourcesauthkeysmaster2.local:/etc/ha.d/

7、启动heartbeat

[root@test1~]#serviceheartbeatstart
[root@test1~]#sshmaster2.local'serviceheartbeatstart'#一定要在test1上通过ssh的方式启动test2节点的heartbeat

7.1、查看heartbeat启动日志

[root@test1~]#tail-f/var/log/messages
Feb1615:12:45test-1heartbeat:[16056]:info:Configurationvalidated.Startingheartbeat3.0.4
Feb1615:12:45test-1heartbeat:[16057]:info:heartbeat:version3.0.4
Feb1615:12:45test-1heartbeat:[16057]:info:Heartbeatgeneration:1455603909
Feb1615:12:45test-1heartbeat:[16057]:info:glib:UDPBroadcastheartbeatstartedonport694(694)interfaceeth0
Feb1615:12:45test-1heartbeat:[16057]:info:glib:UDPBroadcastheartbeatclosedonport694interfaceeth0-Status:1
Feb1615:12:45test-1heartbeat:[16057]:info:glib:pingheartbeatstarted.
Feb1615:12:45test-1heartbeat:[16057]:info:G_main_add_TriggerHandler:Addedsignalmanualhandler
Feb1615:12:45test-1heartbeat:[16057]:info:G_main_add_TriggerHandler:Addedsignalmanualhandler
Feb1615:12:45test-1heartbeat:[16057]:info:G_main_add_SignalHandler:Addedsignalhandlerforsignal17
Feb1615:12:45test-1heartbeat:[16057]:info:Localstatusnowsetto:'up'
Feb1615:12:45test-1heartbeat:[16057]:info:Link192.168.10.1:192.168.10.1up.
Feb1615:12:45test-1heartbeat:[16057]:info:Statusupdatefornode192.168.10.1:statusping
Feb1615:12:45test-1heartbeat:[16057]:info:Linktest1.local:eth0up.
Feb1615:12:51test-1heartbeat:[16057]:info:Linktest2.local:eth0up.
Feb1615:12:51test-1heartbeat:[16057]:info:Statusupdatefornodetest2.local:statusup
Feb1615:12:51test-1harc(default)[16068]:info:Running/etc/ha.d//rc.d/statusstatus
Feb1615:12:52test-1heartbeat:[16057]:WARN:1lostpacket(s)for[test2.local][3:5]
Feb1615:12:52test-1heartbeat:[16057]:info:Nopktsmissingfromtest2.local!
Feb1615:12:52test-1heartbeat:[16057]:info:Comm_now_up():updatingstatustoactive
Feb1615:12:52test-1heartbeat:[16057]:info:Localstatusnowsetto:'active'
Feb1615:12:52test-1heartbeat:[16057]:info:Statusupdatefornodetest2.local:statusactive
Feb1615:12:52test-1harc(default)[16086]:info:Running/etc/ha.d//rc.d/statusstatus
Feb1615:13:02test-1heartbeat:[16057]:info:localresourcetransitioncompleted.
Feb1615:13:02test-1heartbeat:[16057]:info:Initialresourceacquisitioncomplete(T_RESOURCES(us))
Feb1615:13:02test-1heartbeat:[16057]:info:remoteresourcetransitioncompleted.
Feb1615:13:02test-1/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.10.2)[16138]:INFO:Resourceisstopped
Feb1615:13:02test-1heartbeat:[16102]:info:LocalResourceacquisitioncompleted.
Feb1615:13:02test-1harc(default)[16219]:info:Running/etc/ha.d//rc.d/ip-request-respip-request-resp
Feb1615:13:02test-1ip-request-resp(default)[16219]:receivedip-request-respIPaddr::192.168.10.2/24/eth0OKyes
Feb1615:13:02test-1ResourceManager(default)[16238]:info:Acquiringresourcegroup:test1.localIPaddr::192.168.10.2/24/eth0mysqld
Feb1615:13:02test-1/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.10.2)[16264]:INFO:Resourceisstopped
Feb1615:13:03test-1ResourceManager(default)[16238]:info:Running/etc/ha.d/resource.d/IPaddr192.168.10.2/24/eth0start
Feb1615:13:03test-1IPaddr(IPaddr_192.168.10.2)[16386]:INFO:Addinginetaddress192.168.10.2/24withbroadcastaddress192.168.10.255todeviceeth0
Feb1615:13:03test-1IPaddr(IPaddr_192.168.10.2)[16386]:INFO:Bringingdeviceeth0up
Feb1615:13:03test-1IPaddr(IPaddr_192.168.10.2)[16386]:INFO:/usr/libexec/heartbeat/send_arp-i200-r5-p/var/run/resource-agents/send_arp-192.168.10.2eth0192.168.10.2autonot_usednot_used
Feb1615:13:03test-1/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.10.2)[16360]:INFO:Success
Feb1615:13:03test-1ResourceManager(default)[16238]:info:Running/etc/init.d/mysqldstart
Feb1615:13:04test-1ntpd[1605]:Listennormallyon15eth0192.168.10.2UDP123

说明:

1、Link test1.local:eth0 up、Link test2.local:eth0 up #两个节点连接成功并为UP状态。

2、Link 192.168.10.1:192.168.10.1 up #ping节点的IP也已经启动

3、info: Running /etc/init.d/mysqld start #mysql启动成功

4、Listen normally on 15 eth0 192.168.10.2 UDP 123 #VIP启动成功

7.2、查看heartbeat的VIP

[root@test1ha.d]#ipadd|grep"10.2"
inet192.168.10.55/24brd192.168.10.255scopeglobaleth0
inet192.168.10.2/24brd192.168.10.255scopeglobalsecondaryeth0
[root@test-2ha.d]#ipadd|grep"10.2"
inet192.168.10.56/24brd192.168.10.255scopeglobaleth0

注:可以看到现在VIP是在master1.local主机上。而master2.local上没有VIP

8、测试效果

8.1、正常情况下连接mysql

[root@test3ha.d]#mysql-uroot-h'192.168.10.2'-p
Enterpassword:
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis2
Serverversion:5.5.44Sourcedistribution
Copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>showvariableslike'server_id';
+---------------+-------+
|Variable_name|Value|
+---------------+-------+
|server_id|1|
+---------------+-------+
1rowinset(0.00sec)
mysql>

8.2、关闭master1.local上的heartbeat

[root@test1ha.d]#serviceheartbeatstop
StoppingHigh-Availabilityservices:Done.
[root@test1ha.d]#ipadd|grep"192.168.10.2"
inet192.168.10.55/24brd192.168.10.255scopeglobaleth0
[root@test2ha.d]#ipadd|grep"192.168.10.2"
inet192.168.10.56/24brd192.168.10.255scopeglobaleth0
inet192.168.10.2/24brd192.168.10.255scopeglobalsecondaryeth0

注:这个时候VIP已经漂移到了master2.local主机上,我们在来看看连接mysql的server_id

[root@test2ha.d]#mysql-uroot-h'192.168.10.2'-p
Enterpassword:
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis2
Serverversion:5.5.44Sourcedistribution
Copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>showvariableslike'server_id';
+---------------+-------+
|Variable_name|Value|
+---------------+-------+
|server_id|2|
+---------------+-------+
1rowinset(0.00sec)
mysql>

注:server_id已经从1变成了2,证明此时访问的是master2.local主机上的mysql服务

测试完毕。下面配置drbd让两台mysql服务器之间使用同一个文件系统,以实现mysql的写高可用。

9、配置DRBD

DRBD:(Distributed Replicated Block Device)分布式复制块设备,是linux内核中的一个模块。DRBD作为磁盘镜像来讲,它一定是主从架构的,它决不允许两个节点同时读写,仅允许一个节点能读写,从节点不能读写和挂载,

但是DRDB有双主的概念,主、从的角色可以切换。DRBD分别将位于两台主机上的硬盘或磁盘分区做成镜像设备,当我们客户端的程序向主节点发起存储请求的时候,这个数据会在底层以TCP/IP协议按位同布一份到备节点,

所以这能保证只要我们在主节点上存的数据,备节点上在按位一定有一模一样的一份数据。这是在两台主机上实现的,这意味着DRBD是工作在内核模块当中。不像RAID1的镜像是在同一台主机上实现的。

DRBD双主模型的实现:一个节点在数据访问的时候,它一定会将数据、元数据载入内存的,而且它对于某个文件内核中加锁的操作,另一个节点的内核是看不到的,那如果它能将它自己施加的锁通知给另一个节点的内核就可以了。

在这种情况下,我们就只能通过message layer(heartbeat、corosync都可)、pathmaker(把DRBD定义成资源),然后把这两个主机上对应的镜像格式化成集群文件系统(GFS2/OCFS2)。

这就是基于结合分布式文件锁管理器(DLM Distributed Lock Manager)以及集群文件系统所完成的双主模型。DRBD集群只允许有两个节点,要么双主,要么主从。

9.1、DRBD的三种工作模型

A、数据在本地DRBD存储完成后向应用程序返回存储成功的消息,异步模型。效率高,性能好。数据不安全

B、数据在本地DRBD存储完成后并且通过TCP/IP把所有数据发送到从DRBD中,才向本地的应用程序返回存储成功的消息,半同步模型。一般不用。

C、数据在本地DRBD存储完成后,通过TCP/IP把所有数据发送到从DRBD中,从DRBD存储完成后才向应用程序返回成功的消息,同步模型。效率低,性能若,但是数据安全可靠性大,用的最多。

9.2、DRBD的资源

1、资源名称,可以是任意的ascii码不包含空格的字符

2、DRBD设备,在双方节点上,此DRBD设备的设备文件,一般为/dev/drbdN,其主设备号相同都是147,此设备号用来标识不通的设备

3、磁盘配置,在双方节点上,各自提供的存储设备,可以是个分区,可以是任何类型的块设备,也可以是lvm

4、网络配置,双方数据同步时,所使用的网络属性

9.3、安装DRBD

drbd在2.6.33开始,才整合进内核的。

9.3.1、下载drbd

[root@test1~]#wget-O/usr/local/srchttp://oss.linbit.com/drbd/8.4/drbd-8.4.3.tar.gz

9.3.2、安装drbd软件

[root@test1~]#cd/usr/local/src
[root@test1src]#tar-zxvfdrbd-8.4.3.tar.gz
[root@test1src]#cd/usr/local/src/drbd-8.4.3
[root@test1drbd-8.4.3]#./configure--prefix=/usr/local/drbd--with-km
[root@test1drbd-8.4.3]#makeKDIR=/usr/src/kernels/2.6.32-573.18.1.el6.x86_64
[root@test1drbd-8.4.3]#makeinstall
[root@test1drbd-8.4.3]#mkdir-p/usr/local/drbd/var/run/drbd
[root@test1drbd-8.4.3]#cp/usr/local/drbd/etc/rc.d/init.d/drbd/etc/rc.d/init.d/

9.3.3、安装drbd模块

[root@test1drbd-8.4.3]#cddrbd/
[root@test1drbd]#makeclean
[root@test1drbd]#makeKDIR=/usr/src/kernels/2.6.32-573.18.1.el6.x86_64
[root@test1drbd]#cpdrbd.ko/lib/modules/`uname-r`/kernel/lib/
[root@test1drbd]#modprobedrbd
[root@test1drbd]#lsmod|grepdrbd

9.3.4、为drbd创建新分区

[root@test1drbd]#fdisk/dev/sdb
WARNING:DOS-compatiblemodeisdeprecated.It'sstronglyrecommendedto
switchoffthemode(command'c')andchangedisplayunitsto
sectors(command'u').
Command(mforhelp):n
Commandaction
eextended
pprimarypartition(1-4)
p
Partitionnumber(1-4):1
Firstcylinder(1-1305,default1):
Usingdefaultvalue1
Lastcylinder,+cylindersor+size{K,M,G}(1-1305,default1305):+9G
Command(mforhelp):w
Thepartitiontablehasbeenaltered!
Callingioctl()tore-readpartitiontable.
AWRNING:Re-readingthepartitiontablefailedwitherror16:Deviceorresourcebusy.
Thekernelstillusestheoldtable.
Thenewtablewillbeusedatthenextreboot.
Syncingdisks.
[root@test1drbd]#partprobe/dev/sdb

test2节点的drbd安装和分区配置步骤略过,和test1上一样安装,test2节点的drbd配置文件保证和test1节点一样,使用scp传到test2节点即可

10、配置drbd

10.1、配置drbd的通用配置文件

[root@test1drbd.d]#cd/usr/local/drbd/etc/drbd.d
[root@test1drbd.d]#vimglobal_common.conf
global{#global是全局配置
usage-countno;#官方用来统计有多少个客户使用drbd的
#minor-countdialog-refreshdisable-ip-verification
}
common{#通用配置,用来配置所有资源那些相同属性的。为drbd提供默认属性的
protocolC;#默认使用协议C,即同步模型。
handlers{#处理器段,用来配置drbd的故障切换操作
#TheseareEXAMPLEhandlersonly.
#Theymayhavesevereimplications,
#likehardresettingthenodeundercertaincircumstances.
#Becarefulwhenchosingyourpoison.
pri-on-incon-degr"/usr/lib/drbd/notify-pri-on-incon-degr.sh;/usr/lib/drbd/notify-emergency-reboot.sh;echob>/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;echob>/proc/sysrq-trigger;reboot-f";#脑裂之后的操作
local-io-error"/usr/lib/drbd/notify-io-error.sh;/usr/lib/drbd/notify-emergency-shutdown.sh;echoo>/proc/sysrq-trigger;halt-f";#本地i/o错误之后的操作
#fence-peer"/usr/lib/drbd/crm-fence-peer.sh";
#split-brain"/usr/lib/drbd/notify-split-brain.shroot";
#out-of-sync"/usr/lib/drbd/notify-out-of-sync.shroot";
#before-resync-target"/usr/lib/drbd/snapshot-resync-target-lvm.sh-p15---c16k";
#after-resync-target/usr/lib/drbd/unsnapshot-resync-target-lvm.sh;
}
startup{
#wfc-timeoutdegr-wfc-timeoutoutdated-wfc-timeoutwait-after-sb#设备启动时,两个节点要同步,设置节点的等待时间,超时时间等
}
options{
#cpu-maskon-no-data-accessible
}
disk{
on-io-errordetach;#一旦发生i/o错误,就把磁盘卸载。不继续进行同步
#sizemax-bio-bvecson-io-errorfencingdisk-barrierdisk-flushes
#disk-drainmd-flushesresync-rateresync-afteral-extents
#c-plan-aheadc-delay-targetc-fill-targetc-max-rate
#c-min-ratedisk-timeout
}
net{#设置网络的buffers/cache大小,初始化时间等
#protocoltimeoutmax-epoch-sizemax-buffersunplug-watermark
#connect-intping-intsndbuf-sizercvbuf-sizeko-count
#allow-two-primariescram-hmac-algshared-secretafter-sb-0pri
#after-sb-1priafter-sb-2prialways-asbprr-conflict
#ping-timeoutdata-integrity-algtcp-corkon-congestion
#congestion-fillcongestion-extentscsums-algverify-alg
#use-rle
cram-hmac-alg"sha1";#数据加密使用的算法
shared-secret"mydrbd1fa2jg8";#验证密码
}
syncer{
rate200M;#定义数据传输速率
}
}

10.2、配置资源文件,资源配置文件的名字要和资源文件中的一样

[root@test1drbd.d]#vimmydrbd.res
resourcemydrbd{#资源名称,可以是任意的ascii码不包含空格的字符
ontest1.local{#节点1,每个节点必须要能使用名称的方式解析对方节点
device/dev/drbd0;#drbd设备的文件名叫什么
disk/dev/sdb1;#分区设备是哪个
address192.168.10.55:7789;#节点ip和监听的端口
meta-diskinternal;#drbd的meta(原数据)放在什么地方,internal是放在设备内部
}
ontest2.local{
device/dev/drbd0;
disk/dev/sdb1;
address192.168.10.56:7789;
meta-diskinternal;
}
}

10.3、两个节点的配置文件一样,使用工具把配置文件传到另一个节点

[root@test1drbd.d]#scp-r/usr/local/drbd/etc/drbd.*test2.local:/usr/local/drbd/etc/

10.4、在每个节点上初始化已定义的资源

[root@test1drbd.d]#drbdadmcreate-mdmydrbd
--==Thankyouforparticipatingintheglobalusagesurvey==--
Theserver'sresponseis:
Writingmetadata...
initializingactivitylog
NOTinitializedbitmap
Newdrbdmetadatablocksuccessfullycreated.
[root@test1drbd.d]#

[root@test2drbd.d]#drbdadmcreate-mdmydrbd
--==Thankyouforparticipatingintheglobalusagesurvey==--
Theserver'sresponseis:
Writingmetadata...
initializingactivitylog
NOTinitializedbitmap
Newdrbdmetadatablocksuccessfullycreated.
[root@test2drbd.d]#

10.5、分别启动两个节点的drbd服务

[root@test1drbd.d]#servicedrbdstart
[root@test2drbd.d]#servicedrbdstart

11、测试drbd的同步

11.1、查看drbd的启动状态

[root@test1drbd.d]#cat/proc/drbd
version:8.4.3(api:1/proto:86-101)
GIT-hash:89a294209144b68adb3ee85a73221f964d3ee515buildbyroot@test1.local,2016-02-2310:23:03
0:cs:Connectedro:Secondary/Secondaryds:Inconsistent/InconsistentCr-----#两个节点都是从,将来可以把一个提升为主。Inconsistent处于没有同步的状态
ns:0nr:0dw:0dr:0al:0bm:0lo:0pe:0ua:0ap:0ep:1wo:boos:0

11.2、提升一个节点为主,并覆盖从节点的drbd分区数据。在要提升为主的节点上执行

[root@test1drbd.d]#drbdadm----overwrite-data-of-peerprimarymydrbd

11.3、查看主节点同步状态

[root@test1drbd.d]#watch-n1cat/proc/drbd
Every1.0s:cat/proc/drbdTueFeb2317:10:552016
version:8.4.3(api:1/proto:86-101)
GIT-hash:89a294209144b68adb3ee85a73221f964d3ee515buildbyroot@test1.local,2016-02-2310:23:03
0:cs:SyncSourcero:Primary/Secondaryds:UpToDate/InconsistentCr---n-
ns:619656nr:0dw:0dr:627840al:0bm:37lo:1pe:8ua:64ap:0ep:1wo:boos:369144
[=============>.......]sync'ed:10.3%(369144/987896)K
finish:0:00:12speed:25,632(25,464)K/sec

11.4、查看从节点的状态

[root@test2drbd]#cat/proc/drbd
version:8.4.3(api:1/proto:86-101)
GIT-hash:89a294209144b68adb3ee85a73221f964d3ee515buildbyroot@test2.local,2016-02-2216:05:34
0:cs:Connectedro:Secondary/Primaryds:UpToDate/UpToDateCr-----
ns:4nr:9728024dw:9728028dr:1025al:1bm:577lo:0pe:0ua:0ap:0ep:1wo:foos:0

11.5、在主节点格式化分区并挂在写入数据测试

[root@test1drbd]#mke2fs-j/dev/drbd0
[root@test1drbd]#mkdir/mydrbd
[root@test1drbd]#mount/dev/drdb0/mydrbd
[root@test1drbd]#cd/mydrbd
[root@test1mydrbd]#touchdrbd_test_file
[root@test1mydrbd]#ls/mydrbd/
drbd_test_filelost+found

11.6、把主节点降级为从,把从节点提升为主。查看数据是否同步

11.1、主节点操作

11.1.1、卸载分区,注意卸载的时候要退出挂在目录,否则会显示设备忙,不能卸载

[root@test1mydrbd]#cd~
[root@test1~]#umount/mydrbd
[root@test1~]#drbdadmsecondarymydrbd

11.1.2、查看现在的drbd状态

[root@test1~]#cat/proc/drbd
version:8.4.3(api:1/proto:86-101)
GIT-hash:89a294209144b68adb3ee85a73221f964d3ee515buildbyroot@test2.local,2016-02-2216:05:34
0:cs:Connectedro:Secondary/Secondaryds:UpToDate/UpToDateCr-----
ns:4nr:9728024dw:9728028dr:1025al:1bm:577lo:0pe:0ua:0ap:0ep:1wo:foos:0

注:可以看到,现在drbd的两个节点的状态都是secondary的,下面把从节点提升为主

11.2、从节点操作

11.2.1、提升操作

[root@test1~]#drdbadmprimarymydrbd

11.2.2、挂在drbd分区

[root@test1~]#mkdir/mydrbd
[root@test1~]#mount/dev/drbd0/mydrbd/

11.2.3、查看是否有数据

[root@test2~]#ls/myddrbd/
drbd_test_filelost+found

注:可以看到从节点切换成主后,已经同步了数据。drbd搭建完成。下面结合corosync+mysql配置双主高可用。

12、结合corosync+drbd+mysql实现数据库双主高可用

将drbd配置为corosync双节点高可用集群中的资源,能够实现主从角色的自动切换,注意,要把某一个服务配置为高可用集群的资源,一定不能让这个服务开机自动启动。

12.1、关闭两台节点的drbd开机自启动

12.1.1、主节点操作

[root@test1drbd.d]#chkconfigdrbdoff
[root@test1drbd.d]#chkconfig--list|grepdrbd
drbd0:off1:off2:off3:off4:off5:off6:off

12.1.2、从节点操作

[root@test2drbd.d]#chkconfigdrbdoff
[root@test2drbd.d]#chkconfig--list|grepdrbd
drbd0:off1:off2:off3:off4:off5:off6:off

12.2、卸载drbd的文件系统并把主节点降级为从节点

12.2.1、从节点操作,注意,这里的从节点刚才提升为主了。现在把他降级

[root@test2drbd]#umount/mydata/
[root@test2drbd]#drbdadmsecondarymydrbd
[root@test2drbd]#cat/proc/drbd
version:8.4.3(api:1/proto:86-101)
GIT-hash:89a294209144b68adb3ee85a73221f964d3ee515buildbyroot@test2.local,2016-02-2216:05:34
0:cs:Connectedro:Secondary/Secondaryds:UpToDate/UpToDateCr-----
ns:8nr:9728024dw:9728032dr:1073al:1bm:577lo:0pe:0ua:0ap:0ep:1wo:foos:0

注:确保两个节点都是secondary

12.3、停止两个节点的drbd服务

12.3.1、从节点操作

[root@test2drbd]#servicedrbdstop
StoppingallDRBDresources:.
[root@test2drbd]#

12.3.2、主节点操作

[root@test1drbd.d]#servicedrbdstop
StoppingallDRBDresources:.
[root@test1drbd.d]#

12.4、安装corosync并创建日志目录

12.4.1、主节点操作

[root@test1drbd.d]#wget-P/etc/yum.repos.dhttp://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/network:ha-clustering:Stable.repo
[root@test1drbd.d]#yuminstallcorosyncpacemakercrmsh
[root@test1drbd.d]#mkdir/var/log/cluster

12.4.2、从节点操作

[root@test1drbd.d]#wget-P/etc/yum.repos.dhttp://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/network:ha-clustering:Stable.repo
[root@test2drbd.d]#mkdir/var/log/cluster
[root@test2drbd.d]#yuminstallcorosyncpacemakercrmsh

12.5、corosync配置文件

12.5.1、主节点操作

[root@test1drbd.d]#cd/etc/corosync/
[root@test1corosync]#cpcorosync.conf.examplecorosync.conf

12.6、配置主节点配置文件,生成corosync秘钥文件并复制给从节点(包括主配置文件)

12.6.1、主节点操作

[root@test1corosync]#vimcorosync.conf
#Pleasereadthecorosync.conf.5manualpage
compatibility:whitetank
totem{
version:2#secauth:Enablemutualnodeauthentication.Ifyouchooseto
#enablethis("on"),thendoremembertocreateashared
#secretwith"corosync-keygen".
secauth:on
threads:2
#interface:defineatleastoneinterfacetocommunicate
#over.Ifyoudefinemorethanoneinterfacestanza,youmust
#alsosetrrp_mode.
interface{
#Ringsmustbeconsecutivelynumbered,startingat0.
ringnumber:0
#Thisisnormallythe*network*addressofthe
#interfacetobindto.Thisensuresthatyoucanuse
#identicalinstancesofthisconfigurationfile
#acrossallyourclusternodes,withouthavingto
#modifythisoption.
bindnetaddr:192.168.10.0
#However,ifyouhavemultiplephysicalnetwork
#interfacesconfiguredforthesamesubnet,thenthe
#networkaddressaloneisnotsufficienttoidentify
#theinterfaceCorosyncshouldbindto.Inthatcase,
#configurethe*host*addressoftheinterface
#instead:
bindnetaddr:192.168.10.0
#Whenselectingamulticastaddress,considerRFC
#2365(which,amongotherthings,specifiesthat
#239.255.x.xaddressesarelefttothediscretionof
#thenetworkadministrator).Donotreusemulticast
#addressesacrossmultipleCorosyncclusterssharing
#thesamenetwork.
mcastaddr:239.212.16.19
#CorosyncusestheportyouspecifyhereforUDP
#messaging,andalsotheimmediatelypreceding
#port.Thusifyousetthisto5405,Corosyncsends
#messagesoverUDPports5405and5404.
mcastport:5405
#Time-to-liveforclustercommunicationpackets.The
#numberofhops(routers)thatthisringwillallow
#itselftopass.Notethatmulticastroutingmustbe
#specificallyenabledonmostnetworkrouters.
ttl:1#每一个数据报文不允许经过路由
}
}
logging{
#Logthesourcefileandlinewheremessagesarebeing
#generated.Whenindoubt,leaveoff.Potentiallyusefulfor
#debugging.
fileline:off
#Logtostandarderror.Whenindoubt,settono.Usefulwhen
#runningintheforeground(wheninvoking"corosync-f")
to_stderr:no
#Logtoalogfile.Whensetto"no",the"logfile"option
#mustnotbeset.
to_logfile:yes
logfile:/var/log/cluster/corosync.log
#Logtothesystemlogdaemon.Whenindoubt,settoyes.
to_syslog:no
#Logdebugmessages(veryverbose).Whenindoubt,leaveoff.
debug:off
#Logmessageswithtimestamps.Whenindoubt,settoon
#(unlessyouareonlyloggingtosyslog,wheredouble
#timestampscanbeannoying).
timestamp:on
logger_subsys{
subsys:AMF
debug:off
}
}
service{
ver:0
name:pacemaker
}
aisexec{
user:root
group:root
}
[root@test1corosync]#corosync-keygen
[root@test1corosync]#scp-pauthkeycorosync.conftest2.local:/etc/corosync/

12.7、启动corosync

12.7.1、主节点操作(注意:两个corosync的启动操作都要在主节点上进行)

[root@test1corosync]#servicecorosyncstart
StartingCorosyncClusterEngine(corosync):[OK]
[root@test1corosync]#sshtest2.local'servicecorosyncstart'
root@test2.local'spassword:
StartingCorosyncClusterEngine(corosync):[OK]

12.8、查看集群状态

12.8.1、问题:安装之后系统没有crm命令,不能使用crm的交互式模式

[root@test1corosync]#crmstatus
-bash:crm:commandnotfound

解决办法:安装ha-cluster的yum源,在安装crmsh软件包

[root@test1corosync]#wget-P/etc/yum.repos.d/http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/network:ha-clustering:Stable.repo

参考文档:http://www.dwhd.org/20150530_014731.html 中的第二,安装crmsh步骤

http://serverfault.com/questions/487309/crm-commandcluster-managment-for-pacemaker-not-found-in-latest-centos-6

安装完成后就可以使用crm命令行模式了

12.8.2、查看集群节点状态

[root@test1corosync]#crmstatus
Lastupdated:WedFeb2413:47:172016
Lastchange:WedFeb2411:26:062016
Stack:classicopenais(withplugin)
CurrentDC:test2.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
0Resourcesconfigured
Online:[test1.localtest2.local]
Fulllistofresources:

注:显示有2个nodes已配置并处于在线状态

12.8.3、查看pacemaker是否启动

[root@test1corosync]#greppcmk_startup/var/log/cluster/corosync.log
Feb2411:05:15corosync[pcmk]info:pcmk_startup:CRM:Initialized
Feb2411:05:15corosync[pcmk]Logging:Initializedpcmk_startup

12.8.4、检查集群引擎是否启动

[root@test1corosync]#grep-e"CorosyncClusterEngine"-e"configurationfile"/var/log/cluster/corosync.log
Feb2411:04:16corosync[MAIN]CorosyncClusterEngine('1.4.7'):startedandreadytoprovideservice.
Feb2411:04:16corosync[MAIN]Successfullyreadmainconfigurationfile'/etc/corosync/corosync.conf'.

注:经过上面的步骤,可以确认corosync的服务已经启动并没有问题了

12.9、配置corosync的属性

12.9.1、禁用STONITH设备以保证verify不会出错

[root@test1corosync]#crmconfigure
crm(live)configure#propertystonith-enabled=false
crm(live)configure#verify
crm(live)configure#commit

12.9.2、配置当不具备法定票数的时候不能关闭服务

crm(live)configure#propertyno-quorum-policy=ignore
crm(live)configure#verify
crm(live)configure#commit

12.9.3、配置资源默认粘性值

crm(live)configure#rsc_defaultsresource-stickiness=100
crm(live)configure#verify
crm(live)configure#commit

12.9.3、查看当前的配置

crm(live)configure#show
nodetest1.local
nodetest2.local
propertycib-bootstrap-options:\
dc-version=1.1.11-97629de\
cluster-infrastructure="classicopenais(withplugin)"\
expected-quorum-votes=2\
stonith-enabled=false\
no-quorum-policy=ignore
rsc_defaultsrsc-options:\
resource-stickiness=100

12.10、配置集群资源

12.10.1、定义drbd的资源

crm(live)configure#primitivemysqldrbdocf:linbit:drbdparamsdrbd_resource=mydrbdopstarttimeout=240opstoptimeout=100opmonitorrole=Masterinterval=10timeout=20opmonitorrole=Slaveinterval=20timeout=20
注:ocf:资源代理,这里用linbit代理drbd。drbd_resource=mydrbd:drbd的资源名字。starttimeout:启动超时时间。stoptimeout:停止超时时间。monitorrole=Master:定义主节点的监控时间,interval:监控间隔,timeout:超时时间。monitorrole=Slave:定义从节点的监控时间,interval:监控间隔,timeout:超时时间

crm(live)configure#show#查看配置
nodetest1.local
nodetest2.local
primitivemydrbdocf:linbit:drbd\
paramsdrbd_resource=mydrbd\
opstarttimeout=240interval=0\
opstoptimeout=100interval=0\
opmonitorrole=Masterinterval=10stimeout=20s\
opmonitorrole=Slaveinterval=20stimeout=20s
propertycib-bootstrap-options:\
dc-version=1.1.11-97629de\
cluster-infrastructure="classicopenais(withplugin)"\
expected-quorum-votes=2\
stonith-enabled=false\
no-quorum-policy=ignore
rsc_defaultsrsc-options:\
resource-stickiness=100
crm(live)configure#verify#验证配置

12.10.2、定义drbd的主从资源

crm(live)configure#msms_mysqldrbdmysqldrbdmetamaster-max=1master-node-max=1clone-max=2clone-node-max=1notify=true
注:msms_mydrbdmydrbd:把mydrbd做成主从,这个资源名称叫ms_mydrbd。meta:定义元数据属性。master-max=1:最多有1个主资源,master-node-max=1:最多有1个主节点,clone-max=2:最多有2个克隆,clone-node-max=1:每个节点上,可以启动几个克隆资源。
crm(live)configure#verify
crm(live)configure#commit
crm(live)configure#show
nodetest1.local
nodetest2.local
primitivemydrbdocf:linbit:drbd\
paramsdrbd_resource=mydrbd\
opstarttimeout=240interval=0\
opstoptimeout=100interval=0\
opmonitorrole=Masterinterval=10stimeout=20s\
opmonitorrole=Slaveinterval=20stimeout=20s
msms_mydrbdmydrbd\
metamaster-max=1master-node-max=1clone-max=2clone-node-max=1
propertycib-bootstrap-options:\
dc-version=1.1.11-97629de\
cluster-infrastructure="classicopenais(withplugin)"\
expected-quorum-votes=2\
stonith-enabled=false\
no-quorum-policy=ignore
rsc_defaultsrsc-options:\
resource-stickiness=100

注:可以看到现在配置有一个primitive mydrbd的资源,一个ms的主从类型。

12.10.3、查看集群节点状态

crm(live)configure#cd
crm(live)#status
Lastupdated:ThuFeb2514:45:522016
Lastchange:ThuFeb2514:44:442016
Stack:classicopenais(withplugin)
CurrentDC:test1.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
2Resourcesconfigured
Online:[test1.localtest2.local]
Fulllistofresources:
Master/SlaveSet:ms_mysqldrbd[mysqldrbd]
Masters:[test1.local]
Slaves:[test2.local]

注:正常状态,已经可以看到主从状态,test1是master。

错误状态:

Master/SlaveSet:ms_mydrbd[mydrbd]
mydrbd(ocf::linbit:drbd):FAILEDtest2.local(unmanaged)
mydrbd(ocf::linbit:drbd):FAILEDtest1.local(unmanaged)
Failedactions:
mydrbd_stop_0ontest2.local'notconfigured'(6):call=87,status=complete,last-rc-change='ThuFeb2514:17:342016',queued=0ms,exec=34ms
mydrbd_stop_0ontest2.local'notconfigured'(6):call=87,status=complete,last-rc-change='ThuFeb2514:17:342016',queued=0ms,exec=34ms
mydrbd_stop_0ontest1.local'notconfigured'(6):call=72,status=complete,last-rc-change='ThuFeb2514:17:342016',queued=0ms,exec=34ms
mydrbd_stop_0ontest1.local'notconfigured'(6):call=72,status=complete,last-rc-change='ThuFeb2514:17:342016',queued=0ms,exec=34ms解决办法:定义资源的时候要注意,drbd_resource=mydrbd的名字是drbd资源的名字且主从资源的名称不能和drbd资源的名称一样,还有各种超时设置中不要加s。测试了很久,才找到这个问题。。。。如有不同看法,请各位大神赐教,谢谢。

12.10.4、验证主从的切换

[root@test1~]#crmnodestandbytest1.local#将主节点下线
[root@test1~]#crmstatus注:查看状态,显示主节点已经不在线,而test2成为了master
Lastupdated:ThuFeb2514:51:582016
Lastchange:ThuFeb2514:51:442016
Stack:classicopenais(withplugin)
CurrentDC:test1.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
2Resourcesconfigured
Nodetest1.local:standby
Online:[test2.local]
Fulllistofresources:
Master/SlaveSet:ms_mysqldrbd[mysqldrbd]
Masters:[test2.local]
Stopped:[test1.local]
[root@test1~]#crmnodeonlinetest1.local#重新将test1上线
[root@test1~]#crmstatus#查看状态,显示test依旧为master,而test1成为了slave
Lastupdated:ThuFeb2514:52:552016
Lastchange:ThuFeb2514:52:392016
Stack:classicopenais(withplugin)
CurrentDC:test1.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
2Resourcesconfigured
Online:[test1.localtest2.local]
Fulllistofresources:
Master/SlaveSet:ms_mysqldrbd[mysqldrbd]
Masters:[test2.local]
Slaves:[test1.local]

12.10.5、定义文件系统资源

[root@test1~]#crm
crm(live)#configure
crm(live)configure#primitivemystoreocf:heartbeat:Filesystemparamsdevice=/dev/drbd0directory=/mydrbdfstype=ext3opstarttimeout=60opstoptimeout=60注:这里的语句表示,primitivemystore:定义一个资源mystore,使用heartbeat的文件系统,params参数定义:drbd的设备名,挂在目录,文件系统类型,和启动停止超时时间
crm(live)configure#verify

12.10.6、定义排列约束以确保Filesystem和主节点在一起。

crm(live)configure#colocationmystore_withms_mysqldrbdinf:mystorems_mysqldrbd:Master
crm(live)configure#verify

12.10.7、定义Order约束,以确保主从资源必须要先成为主节点以后才能挂在文件系统

crm(live)configure#ordermystore_after_ms_mysqldrbdmandatory:ms_mysqldrbd:promotemystore:start注:这里的语句表示,mystore_after_ms_mysqldrbdmandatory:,mystore在ms_mysqldrbd之后启动,mandatory(代表强制的),先启动ms_mysqldrbd,promote(角色切换成功后),在启动mystore:start
crm(live)configure#verify
crm(live)configure#commit
crm(live)configure#cd
crm(live)#status#查看状态,可以看到文件系统已经自动挂在到主节点test1.local上了。
Lastupdated:ThuFeb2515:29:392016
Lastchange:ThuFeb2515:29:362016
Stack:classicopenais(withplugin)
CurrentDC:test1.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
3Resourcesconfigured
Online:[test1.localtest2.local]
Fulllistofresources:
Master/SlaveSet:ms_mysqldrbd[mysqldrbd]
Masters:[test1.local]
Slaves:[test2.local]
mystore(ocf::heartbeat:Filesystem):Startedtest1.local

[root@test1~]#ls/mydrbd/#已经有之前创建的文件了。
inittablost+found

12.10.8、切换主从节点,验证文件系统是否会自动挂载

[root@test1~]#crmnodestandbytest1.local
[root@test1~]#crmnodeonlinetest1.local
[root@test1~]#crmstatus
Lastupdated:ThuFeb2515:32:392016
Lastchange:ThuFeb2515:32:362016
Stack:classicopenais(withplugin)
CurrentDC:test1.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
3Resourcesconfigured
Online:[test1.localtest2.local]
Fulllistofresources:
Master/SlaveSet:ms_mysqldrbd[mysqldrbd]
Masters:[test2.local]
Slaves:[test1.local]
mystore(ocf::heartbeat:Filesystem):Startedtest2.local
[root@test2~]#ls/mydrbd/
inittablost+found

12.9.12、配置mysql

12.9.12.1、关闭mysqld的开机自启动

[root@test1~]#chkconfigmysqldoff
[root@test2~]#chkconfigmysqldoff#一定要记住,只要是高可用集群中的资源的服务,一定不能开机自启动

12.9.12.2、配置主节点1的mysql服务(mysql安装就不写了,直接进入mysql的配置)

[root@test1mysql]#mkdir/mydrbd/data
[root@test1mysql]#chown-Rmysql.mysql/mydrbd/data/
[root@test1mysql]#./scripts/mysql_install_db--user=mysql--datadir=/mydrbd/data/--basedir=/usr/local/mysql/
InstallingMySQLsystemtables...
16022516:07:12[Note]/usr/local/mysql//bin/mysqld(mysqld5.5.44)startingasprocess18694...
OK
Fillinghelptables...
16022516:07:18[Note]/usr/local/mysql//bin/mysqld(mysqld5.5.44)startingasprocess18701...
OK
Tostartmysqldatboottimeyouhavetocopy
support-files/mysql.servertotherightplaceforyoursystem
PLEASEREMEMBERTOSETAPASSWORDFORTHEMySQLrootUSER!
Todoso,starttheserver,thenissuethefollowingcommands:
/usr/local/mysql//bin/mysqladmin-urootpassword'new-password'
/usr/local/mysql//bin/mysqladmin-uroot-htest1.localpassword'new-password'
Alternativelyyoucanrun:
/usr/local/mysql//bin/mysql_secure_installation
whichwillalsogiveyoutheoptionofremovingthetest
databasesandanonymoususercreatedbydefault.Thisis
stronglyrecommendedforproductionservers.
Seethemanualformoreinstructions.
YoucanstarttheMySQLdaemonwith:
cd/usr/local/mysql/;/usr/local/mysql//bin/mysqld_safe&
YoucantesttheMySQLdaemonwithmysql-test-run.pl
cd/usr/local/mysql//mysql-test;perlmysql-test-run.pl
Pleasereportanyproblemsat


[root@test1mysql]#servicemysqldstart
StartingMySQL.....[OK]


[root@test1mysql]#mysql-uroot
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis2
Serverversion:5.5.44Sourcedistribution
Copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.

mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|mysql|
|performance_schema|
|test|
+--------------------+
4rowsinset(0.06sec)
mysql>createdatabasedrbd_mysql;
QueryOK,1rowaffected(0.00sec)


mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|drbd_mysql|
|mysql|
|performance_schema|
|test|
+--------------------+
5rowsinset(0.00sec)
mysql>

12.9.12.3、配置从节点的mysql

注:因为刚才已经在test1.local上的共享存储上初始化了mysql的data目录,在test2.local上就不用重复初始化了。

[root@test1mysql]#crmstatus
Lastupdated:ThuFeb2516:14:142016
Lastchange:ThuFeb2515:35:162016
Stack:classicopenais(withplugin)
CurrentDC:test1.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
3Resourcesconfigured
Online:[test1.localtest2.local]
Fulllistofresources:
Master/SlaveSet:ms_mysqldrbd[mysqldrbd]
Masters:[test1.local]
Slaves:[test2.local]
mystore(ocf::heartbeat:Filesystem):Startedtest1.local

1、先让test2.local成为master才能继续操作

[root@test1mysql]#crmnodestandbytest1.local
[root@test1mysql]#crmnodeonlinetest1.local
[root@test1mysql]#crmstatus
Lastupdated:ThuFeb2516:14:462016
Lastchange:ThuFeb2516:14:302016
Stack:classicopenais(withplugin)
CurrentDC:test1.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
3Resourcesconfigured
Nodetest1.local:standby
Online:[test2.local]
Fulllistofresources:
Master/SlaveSet:ms_mysqldrbd[mysqldrbd]
Masters:[test2.local]
Stopped:[test1.local]
mystore(ocf::heartbeat:Filesystem):Startedtest2.local#要确保让test2.local成为master节点

2、test2.local的mysql配置

[root@test2~]#vim/etc/my.cnf
添加:
datadir=/mydrbd/data


[root@test2~]#servicemysqldstart
StartingMySQL.[OK]

[root@test2~]#mysql-uroot
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis1
Serverversion:5.5.44Sourcedistribution
Copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.

mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|drbd_mysql|
|mysql|
|performance_schema|
|test|
+--------------------+
5rowsinset(0.10sec)
mysql>

12.9.13、定义mysql资源

1、停止mysql

[root@test2~]#servicemysqldstop
ShuttingdownMySQL.[OK]

2、定义mysql资源

[root@test1mysql]#crmconfigure
crm(live)configure#primitivemysqldlsb:mysqld
crm(live)configure#verify

3、定义mysql和主节点约束

crm(live)configure#colocationmysqld_with_mystoreinf:mysqldmystore#注:因为mystore一定和主节点在一起,那么我们就定义mysql和mystore的约束。
crm(live)configure#verify

4、定义mysql和mystore启动次序约束

crm(live)configure#ordermysqld_after_mystoremandatory:mystoremysqld#一定要弄清楚启动的先后次序,mysql是在mystore之后启动的。
crm(live)configure#verify
crm(live)configure#commit
crm(live)configure#cd
crm(live)#status
Lastupdated:ThuFeb2516:44:292016
Lastchange:ThuFeb2516:42:162016
Stack:classicopenais(withplugin)
CurrentDC:test1.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
4Resourcesconfigured
Online:[test1.localtest2.local]
Fulllistofresources:
Master/SlaveSet:ms_mysqldrbd[mysqldrbd]
Masters:[test2.local]
Slaves:[test1.local]
mystore(ocf::heartbeat:Filesystem):Startedtest2.local
mysqld(lsb:mysqld):Startedtest2.local注:现在主节点在test2.local上。

5、验证test2.local上的mysql登录是否正常和角色切换后是否正常

[root@test2~]#mysql-uroot
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis2
Serverversion:5.5.44Sourcedistribution
Copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.

mysql>showdatabases;#test2.local上已经启动好了mysql并自动挂在了drbd资源
+--------------------+
|Database|
+--------------------+
|information_schema|
|drbd_mysql|
|mysql|
|performance_schema|
|test|
+--------------------+
5rowsinset(0.07sec)

mysql>createdatabasemydb;#创建一个数据库,然后切换到test1.local节点上,看是否正常
QueryOK,1rowaffected(0.00sec)
mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|drbd_mysql|
|mydb|
|mysql|
|performance_schema|
|test|
+--------------------+
6rowsinset(0.00sec)
mysql>exit


[root@test1mysql]#crmnodestandbytest2.local#将test2.local主节点standby,让test1.local自动成为master
[root@test1mysql]#crmnodeonlinetest2.local
[root@test1mysql]#crmstatus

Lastupdated:ThuFeb2516:53:242016
Lastchange:ThuFeb2516:53:192016
Stack:classicopenais(withplugin)
CurrentDC:test1.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
4Resourcesconfigured
Online:[test1.localtest2.local]
Fulllistofresources:
Master/SlaveSet:ms_mysqldrbd[mysqldrbd]
Masters:[test1.local]
Slaves:[test2.local]
mystore(ocf::heartbeat:Filesystem):Startedtest1.local#test1.local已经成为master
mysqld(lsb:mysqld):Startedtest1.local

[root@test1mysql]#mysql-uroot#在test1.local上登录mysql
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis1
Serverversion:5.5.44Sourcedistribution
Copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.

mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|drbd_mysql|
|mydb|#已经有刚才在test2.local上创建的mydb数据库。目前,一切正常
|mysql|
|performance_schema|
|test|
+--------------------+
6rowsinset(0.15sec)
mysql>

12.9.14、定义VIP资源及约束关系

crm(live)configure#primitivemyipocf:heartbeat:IPaddrparamsip=192.168.10.3nic=eth0cidr_netmask=24#这里出了一个错误,浪费半天时间,是因为子网掩码写的255.255.255.0,应该写成24,忘记命令的时候,要多使用table和help。
crm(live)configure#verify
crm(live)configure#colocationmyip_with_ms_mysqldrbdinf:ms_mysqldrbd:Mastermyip#定义vip和ms_mysqldrbd的约束关系
crm(live)configure#verify
crm(live)configure#commit
crm(live)configure#cd
crm(live)#status#查看状态
Lastupdated:FriFeb2610:05:162016
Lastchange:FriFeb2610:05:122016
Stack:classicopenais(withplugin)
CurrentDC:test1.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
5Resourcesconfigured
Online:[test1.localtest2.local]
Fulllistofresources:
Master/SlaveSet:ms_mysqldrbd[mysqldrbd]
Masters:[test1.local]
Slaves:[test2.local]
mystore(ocf::heartbeat:Filesystem):Startedtest1.local
mysqld(lsb:mysqld):Startedtest1.local
myip(ocf::heartbeat:IPaddr):Startedtest1.local#vip已经启动在test1.local节点上了。

12.9.15、测试连接VIP

[root@test1~]#ipaddr#查看test1.local上是否绑定VIP
1:lo:mtu65536qdiscnoqueuestateUNKNOWN
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
2:eth0:mtu1500qdiscpfifo_faststateUPqlen1000
link/ether00:0c:29:34:7d:9fbrdff:ff:ff:ff:ff:ff
inet192.168.10.55/24brd192.168.10.255scopeglobaleth0
inet192.168.10.3/24brd192.168.10.255scopeglobalsecondaryeth0#VIP已经绑定到test1.local上的eth0接口上
inet6fe80::20c:29ff:fe34:7d9f/64scopelink
valid_lftforeverpreferred_lftforever


[root@test-3~]#mysql-uroot-h192.168.10.3-p#使用VIP连接mysql,这里要给连接的客户端授权,不然不能登录
Enterpassword:
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis6
Serverversion:5.5.44Sourcedistribution
Copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>showdatabases;#下面有我们刚才创建的两个库
+--------------------+
|Database|
+--------------------+
|information_schema|
|drbd_mysql|
|mydb|
|mysql|
|performance_schema|
|test|
+--------------------+
6rowsinset(0.08sec)

模拟test1.local故障:

[root@test1mysql]#crmnodestandbytest1.local#将test1.local降级为slave,看vip是否会自动切换
[root@test1mysql]#crmnodeonlinetest1.local
[root@test1mysql]#crmstatus
Lastupdated:FriFeb2610:20:382016
Lastchange:FriFeb2610:20:352016
Stack:classicopenais(withplugin)
CurrentDC:test1.local-partitionwithquorum
Version:1.1.11-97629de
2Nodesconfigured,2expectedvotes
5Resourcesconfigured
Online:[test1.localtest2.local]
Fulllistofresources:
Master/SlaveSet:ms_mysqldrbd[mysqldrbd]
Masters:[test2.local]
Slaves:[test1.local]
mystore(ocf::heartbeat:Filesystem):Startedtest2.local
mysqld(lsb:mysqld):Startedtest2.local
myip(ocf::heartbeat:IPaddr):Startedtest2.local#vip已经在test2.local节点上了。

查看test2的IP信息:

[root@test2~]#ipaddr#在test2.local上查看VIP绑定信息
1:lo:mtu65536qdiscnoqueuestateUNKNOWN
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
2:eth0:mtu1500qdiscpfifo_faststateUPqlen1000
link/ether00:0c:29:fd:7f:e5brdff:ff:ff:ff:ff:ff
inet192.168.10.56/24brd192.168.10.255scopeglobaleth0
inet192.168.10.3/24brd192.168.10.255scopeglobalsecondaryeth0#VIP已经在test2.local上的eth0接口上。
inet6fe80::20c:29ff:fefd:7fe5/64scopelink
valid_lftforeverpreferred_lftforever

测试连接MySQL:

[root@test-3~]#mysql-uroot-h192.168.10.3-p#连接mysql
Enterpassword:
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis1
Serverversion:5.5.44Sourcedistribution
Copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>showdatabases;#一切正常。
+--------------------+
|Database|
+--------------------+
|information_schema|
|drbd_mysql|
|mydb|
|mysql|
|performance_schema|
|test|
+--------------------+
6rowsinset(0.06sec)

到此,corosync+drbd+mysql已经配置完毕,文档不够详细,没有corosync、heartbeat、drbd、mysql结合的原理讲解,没有发生脑裂后的处理办法和预防方法。以后有时间在加上吧。

以上就是corosync+drbd+mysql实现的高可用_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!