IV 11 MySQL+corosync+drbd

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

操作:

在《drbd》基础上进行操作

1)将drbd配置为corosync高可用的资源,能实现主从角色的自动切换

注意:作为高可用的资源,必须要接受CRM管理,不能开机自启动

node2-side

 [root@node2~]# drbd-overview

 0:mydrbd  Connected Primary/Secondary UpToDate/UpToDate C r----- /mydata ext3 2.0G 36M 1.9G 2%

[root@node2 ~]# umount /mydata(注意,切换角色前要先卸载FS

[root@node2 ~]# drbdadm secondary mydrbd

[root@node2 ~]# drbd-overview

 0:mydrbd  Connected Secondary/Secondary UpToDate/UpToDate C r-----

[root@node2 ~]# service drbd stop

[root@node2 ~]# ssh node1 'service drbd stop'

[root@node2 ~]# chkconfig drbd off

[root@node2 ~]# chkconfig --list drbd

drbd              0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@node2 ~]# ssh node1 'chkconfig drbd off'

[root@node2 ~]# ssh node1 'chkconfig --list drbd'

drbd              0:off 1:off 2:off 3:off 4:off 5:off 6:off

 

准备一台机器作为跳板机(node3上分别要配置与node1node2的双机互信,node1node2准备好yum源)

node3-side

[root@node3 ~]# ll /root/corosync | awk'{print $9}'

cluster-glue-1.0.6-1.6.el5.i386.rpm

cluster-glue-libs-1.0.6-1.6.el5.i386.rpm

corosync-1.2.7-1.1.el5.i386.rpm

corosynclib-1.2.7-1.1.el5.i386.rpm

heartbeat-3.0.3-2.3.el5.i386.rpm

heartbeat-libs-3.0.3-2.3.el5.i386.rpm

libesmtp-1.0.4-5.el5.i386.rpm

pacemaker-1.0.12-1.el5.centos.i386.rpm

pacemaker-libs-1.0.12-1.el5.centos.i386.rpm

resource-agents-1.0.3-2.6.el5.i386.rpm

[root@node3 ~]# for I in {1..2};do ssh node$I 'mkdir /root/corosync';scp /root/corosync/* node$I:/root/corosync/;ssh node$I 'yum -y --nogpgcheck localinstall /root/corosync/*.rpm';done

 

node1-side

 [root@node1~]# cp /etc/corosync/corosync.conf.example /etc/corosync/corosync.conf

[root@node1 ~]# vim /etc/corosync/corosync.conf

compatibility: whitetank

totem {

       version: 2

        secauth: on

        threads: 2

       interface {

                ringnumber: 0

                bindnetaddr: 192.168.41.0

                mcastaddr: 226.94.1.8

                mcastport: 5405

       }

}

logging {

       fileline: off

       to_stderr: no

        to_logfile: yes

        to_syslog: no

        logfile: /var/log/cluster/corosync.log

       debug: off

       timestamp: on

       logger_subsys {

                subsys: AMF

                debug: off

       }

}

 

amf {

       mode: disabled

}

service {

        ver: 0

        name: pacemaker

}

aisexec {

       user: root

       group: root

}

[root@node1 ~]# mkdir /var/log/cluster

[root@node1 ~]# ssh node2 'mkdir/var/log/cluster'

[root@node1 ~]# corosync-keygen

[root@node1 ~]# scp -p /etc/corosync/{authkey,corosync.conf} node2:/etc/corosync

[root@node1 ~]# service corosync start

Starting Corosync Cluster Engine (corosync):               [  OK  ]

[root@node1 ~]# ssh node2 'service corosync start'

Starting Corosync Cluster Engine(corosync): [  OK  ]

 

[root@node1 ~]# vim .bashrc

alias grep='grep --color=auto'(添加此行)

[root@node1 ~]# . .bashrc

[root@node1 ~]# grep -e "CorosyncCluster Engine" -e "configuration file"/var/log/cluster/corosync.log

[root@node1 ~]# grep TOTEM !$

[root@node1 ~]# grep ERROR: !$(有关于STONITH报错信息,下文会在crm交互模式下配置)

[root@node1 ~]# grep pcmk_startup !$

 

[root@node1 ~]# crm configure

INFO: building help index

crm(live)configure# (在此模式下按两下<TAB>键会有提示)

crm(live)configure# verify(是STONITH的报错)

crm(live)configure# property stonith-enabled=false(在property后按两下<TAB>会有提示,同样在等号处按两下<TAB>也有提示)

crm(live)configure# verify

crm(live)configure# property no-quorum-policy=ignore(无法定票数的策略)

crm(live)configure# rsc_defaults resource-stickiness=100(资源粘性)

crm(live)configure# verify(无报错信息就能提交了)

crm(live)configure# commit

 

注意:drbd配置为高可用资源,有两项(primitive主资源;master/slave类型clone类资源)

 

crm(live)configure# cd

crm(live)# ra

crm(live)ra# providers drbd

heartbeat linbit

crm(live)ra# meta drbd

crm(live)ra# meta ocf:linbit:drbd(查看parameters有哪些,还有operations’s defaults(advisory minimum),注意不能小于建议的最小值)

crm(live)ra# meta ocf:heartbeat:drbd

 

crm(live)ra# up

crm(live)# configure

crm(live)configure# help primitive

crm(live)configure# primitive mysqldrbd ocf:heartbeat:drbd params drbd_resource=mydrbd op start timeout=240 op stop timeout=100 op monitor role=Master interval=10 timeout=20 op monitor role=Slave  interval=20 timeout=20

crm(live)configure# verify

crm(live)configure# help msThe `ms`command creates a master/slave resource type

crm(live)configure# ms ms_mysqldrbd mysqldrbd meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true

crm(live)configure# verify

crm(live)configure# edit(如果有问题可通过edit编辑,删除或修改某行,保存后重新定义等)

crm(live)configure# show

crm(live)configure# commit

crm(live)configure# bye

bye

[root@node1 ~]# crm status

……

Online: [ node1.magedu.com node2.magedu.com]

Master/Slave Set: ms_mysqldrbd

    Masters: [ node1.magedu.com ]

    Slaves: [ node2.magedu.com ]

[root@node1 ~]# crm node standby

[root@node1 ~]# crm status

……

Online: [ node2.magedu.com ]

Master/Slave Set: ms_mysqldrbd

    Masters: [ node2.magedu.com ]

    Stopped: [ mysqldrbd:0 ]

[root@node1 ~]# drbd-overview

 0:mydrbd  Unconfigured . . . .

[root@node1 ~]# ssh node2 'drbd-overview'

 0:mydrbd  WFConnection Primary/Unknown UpToDate/DUnknown C r-----

[root@node1 ~]# crm node online

[root@node1 ~]# drbd-overview

 0:mydrbd  Connected Secondary/Primary UpToDate/UpToDate C r-----

 

注:master-maxhow many copies of the resource can be promoted to masterstatus.defaults to 1)最多多少个主资源,不是双主模型,要么主从要么从从,所以最多1个主资源

master-node-maxhow manycopies of the resource can be promoted to master status on a single node.defaultsto 1)最多1个主节点

clone-maxhow many copies ofthe resource to start.defaults to the number of nodes in the cluster)主和从都是克隆资源,所以最多两个

clone-node-maxhow manycopies of the resource can be started on a single node.default 1)在某一个节点上最多启动多少个克隆资源

notifywhen stopping orstarting a copy of the clone,tell all the other copies beforehand and when theaction was successful.allowed values:false,ture


注:crm(live)configure# show xml(在查看xml配置文件时,注意rsc和with-rsc)

rscthe colocationsource.if the constraint cannot be satisfied,the cluster may decide not toallow the resource to run at all

with-rscthe colocationtarget.the cluster will decide where to put this resource first and then decidewhere to put the resource in the rsc field

以上只定义了drbd作为高可用资源(两个node主从角色的转换),要想使用还要挂载

 

2)将自动挂载FS这个功能添加为高可用资源

[root@node1 ~]# crm configure

crm(live)configure# primitive mystore ocf:heartbeat:Filesystem params device=/dev/drbd0 directory=/mydata fstype=ext3 op start timeout=60 op stop timeout=60

crm(live)configure# verify

crm(live)configure# colocation mystore_with_ms_mysqldrbd inf: mystore ms_mysqldrbd:Master

crm(live)configure# order mystore_after_ms_mysqldrbd mandatory: ms_mysqldrbd:promote mystore:start

crm(live)configure# verify(如果出问题,清理两个node状态crm(live)node#clearstate node1.magedu.comcrm(live)node# clearstate node2.magedu.com;清理资源状态crm(live)resource#cleanup mystorecrm(live)resource# cleanup ms_mysqldrbd;然后两个node重启服务)

crm(live)configure# show

……

crm(live)configure# commit

[root@node1 ~]# crm status

……

2 Nodes configured, 2 expected votes

2 Resources configured.

============

Online: [ node1.magedu.com node2.magedu.com]

Master/Slave Set: ms_mysqldrbd

    Masters: [ node1.magedu.com ]

    Slaves: [ node2.magedu.com ]

 mystore (ocf::heartbeat:Filesystem):   Started node1.magedu.com

[root@node1 ~]# crm node standby

[root@node1 ~]# crm node online

[root@node1 ~]# crm status

……

Online: [ node1.magedu.com node2.magedu.com]

Master/Slave Set: ms_mysqldrbd

    Masters: [ node2.magedu.com ]

    Slaves: [ node1.magedu.com ]

 mystore (ocf::heartbeat:Filesystem):   Started node2.magedu.com

 

 

3)将mysql服务加入高可用资源

以下几步两个node都要执行(创建用户和组,一定要注意两个节点的UID要相同、GID要相同,因为访问的是一个共享存储;解压;创建软链接;改安装目录下的属主属组;复制配置文件和启动脚本)

[root@node2 ~]# groupadd -g 3306 mysql

[root@node2 ~]# useradd -u 3306 -g mysql -s/sbin/nologin -M mysql

[root@node2 ~]# tar xf mysql-5.5.45-linux2.6-i686.tar.gz -C /usr/local/

[root@node2 ~]# cd /usr/local/

[root@node2 local]# ln -sv mysql-5.5.45-linux2.6-i686/ mysql

create symbolic link `mysql' to`mysql-5.5.45-linux2.6-i686/'

[root@node2 ~]# cd mysql

[root@node2 mysql]# chown -R root.mysql ./

[root@node2 mysql]# cp support-files/my-large.cnf /etc/my.cnf

[root@node2 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

[root@node2 mysql]# vim /etc/my.cnf

[mysqld]

datadir = /mydata/data

innodb_file_per_table = 1

 

node2-side(注意在当前master节点上初始化,由#crm  status得知当前主节点是node2):

[root@node2 ~]# mkdir /mydata/data

[root@node2 ~]# chown -R mysql.mysql /mydata/data

[root@node2 ~]# ll /mydata

total 32

drwxr-xr-x 2 mysql mysql  4096 Nov 6 21:09 data

-rw-r--r-- 1 root  root    74 Nov  6 17:11 issue

drwx------ 2 root  root 16384 Nov  6 17:10 lost+found

[root@node2 ~]# cd /usr/local/mysql

[root@node2 mysql]#scripts/mysql_install_db --user=mysql --datadir=/mydata/data

[root@node2 mysql]# service mysqld start

Starting MySQL....                                         [  OK  ]

[root@node2 mysql]# bin/mysql

……

mysql> CREATE DATABASE mydb;

Query OK, 1 row affected (0.02 sec)

mysql> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mydb               |

……

mysql> \q

Bye

[root@node2 mysql]# service mysqld stop

Shutting down MySQL.                                       [  OK  ]

[root@node2 mysql]# chkconfig mysqld off

[root@node2 mysql]# chkconfig --list mysqld

mysqld            0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@node2 ~]# crm node standby(将主node切换为node1

[root@node2 ~]# crm node online

[root@node2 ~]# crm status

……

Online: [ node1.magedu.com node2.magedu.com]

Master/Slave Set: ms_mysqldrbd

    Masters: [ node1.magedu.com ]

    Slaves: [ node2.magedu.com ]

 mystore (ocf::heartbeat:Filesystem):   Started node1.magedu.com

 

node1-side

[root@node1 mysql]# service mysqld start

[root@node1 mysql]# bin/mysql

mysql> SHOW DATABASES;(查看有无node2创建的库mydb

[root@node1 mysql]# service mysqld stop

[root@node1 mysql]# chkconfig mysqld off

[root@node1 mysql]# chkconfig --list mysqld

mysqld            0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@node1 mysql]# crm configure

crm(live)configure# primitive mysqld lsb:mysqld

crm(live)configure# verify

crm(live)configure# colocation mysqld_with_mystore inf: mysqld mystore

crm(live)configure# verify

crm(live)configure# order mysqld_after_mystore mandatory: mystore mysqld

crm(live)configure# verify

crm(live)configure# show xml(注意这段信息<rsc_order first="mystore" id="mysqld_after_mystore" score="INFINITY" then="mysqld"/>

crm(live)configure# commit

crm(live)configure# exit

[root@node1 mysql]# crm status

……

2 Nodes configured, 2 expected votes

3 Resources configured.

============

Online: [ node1.magedu.com node2.magedu.com]

Master/Slave Set: ms_mysqldrbd

    Masters: [ node1.magedu.com ]

    Slaves: [ node2.magedu.com ]

 mystore (ocf::heartbeat:Filesystem):   Started node1.magedu.com

 mysqld   (lsb:mysqld):    Started node1.magedu.com

[root@node1 mysql]# bin/mysql

mysql> DROP DATABASES mydb;

mysql> SHOW DATABASES;

mysql> CREATE DATABASE hellodrbd;

mysql> \q

[root@node1 ~]# crm node standby

[root@node1 ~]# crm node online

 

4)将VIP加入高可用资源

node2-side

[root@node2 mysql]# crm status

[root@node2 mysql]# bin/mysql

mysql> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| hellodrbd          |

mysql> GRANT  ALL  ON  *.* TO  'root'@'%’ IDENTIFIED BY  'redhat';

mysql> FLUSH PRIVILEGES;

mysql> \q

Bye

[root@node2 mysql]# crm configure

crm(live)configure# primitive myip ocf:heartbeat:IPaddr params ip=192.168.41.222 nic=eth0 cidr_netmask=24

crm(live)configure# verify

crm(live)configure# colocation myip_with_ms_mysqldrbd inf: myip ms_mysqldrbd:Master

crm(live)configure# verify(若有问题,清理节点,清理各资源,重启服务)

crm(live)configure# commit

crm(live)configure# show

node node1.magedu.com \

         attributes standby="off"

node node2.magedu.com \

         attributes standby="off"

primitive myip ocf:heartbeat:IPaddr \

         params ip="192.168.41.222" nic="eth0" cidr_netmask="24"

primitive mysqld lsb:mysqld

primitive mysqldrbd ocf:heartbeat:drbd \

         params drbd_resource="mydrbd" \

         op start interval="0" timeout="240" \

         op stop interval="0" timeout="100" \

         op monitor interval="10" role="Master" timeout="20"\

         op monitor interval="20" role="Slave" timeout="20"

primitive mystore ocf:heartbeat:Filesystem\

         params device="/dev/drbd0" directory="/mydata"fstype="ext3" \

         op start interval="0" timeout="60" \

         op stop interval="0" timeout="60"

ms ms_mysqldrbd mysqldrbd \

         meta master-max="1" master-node-max="1" clone-max="2"clone-node-max="1" notify="true"

colocation myip_with_ms_mysqldrbd inf: myipms_mysqldrbd:Master

colocation mysqld_with_mystore inf: mysqldmystore

colocation mystore_with_ms_mysqldrbd inf:mystore ms_mysqldrbd:Master

order mysqld_after_mystore inf: mystoremysqld

order mystore_after_ms_mysqldrbd inf:ms_mysqldrbd:promote mystore:start

property $id="cib-bootstrap-options"\

         dc-version="1.0.12-unknown"\

         cluster-infrastructure="openais"\

         expected-quorum-votes="2"\

         stonith-enabled="false"\

         no-quorum-policy="ignore"\

         last-lrm-refresh="1446895492"

rsc_defaults $id="rsc-options" \

         resource-stickiness="100"

crm(live)configure# exit

bye

[root@node2 mysql]# crm status

……

2 Nodes configured, 2 expected votes

4 Resources configured.

============

Online: [ node1.magedu.com node2.magedu.com]

Master/Slave Set: ms_mysqldrbd

    Masters: [ node2.magedu.com ]

    Slaves: [ node1.magedu.com ]

 mystore (ocf::heartbeat:Filesystem):   Started node2.magedu.com

 mysqld   (lsb:mysqld):    Started node2.magedu.com

 myip       (ocf::heartbeat:IPaddr):  Started node2.magedu.com

 

node3-side

[root@node3 ~]# mysql -uroot -p-h192.168.41.222(查看库hellodrbd

Enter password:

……

mysql> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| hellodrbd          |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.05 sec)

 

node2-side

[root@node2 mysql]# crm node standby

[root@node2 mysql]# crm node online

[root@node2 mysql]# crm status

……

2 Nodes configured, 2 expected votes

4 Resources configured.

============

Online: [ node1.magedu.com node2.magedu.com]

Master/Slave Set: ms_mysqldrbd

    Masters: [ node1.magedu.com ]

    Slaves: [ node2.magedu.com ]

 mystore (ocf::heartbeat:Filesystem):   Started node1.magedu.com

 myip       (ocf::heartbeat:IPaddr):  Started node1.magedu.com

 

node3-side

[root@node3 ~]# mysql -u root -p -h192.168.41.222(节点间切换后,对外依然可以访问到同样的库)

……

mysql> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| hellodrbd          |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.03 sec)

 

 

本文转自 chaijowin 51CTO博客,原文链接:http://blog.51cto.com/jowin/1720102,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
Oracle 关系型数据库 MySQL
|
存储 MySQL 关系型数据库
|
存储 监控 关系型数据库
|
关系型数据库 MySQL 网络安全