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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了corosync+pacemaker+mysql+drbd 实现mysql的高可用相关的知识,希望对你有一定的参考价值。

# 接上版 链接:http://xiong51.blog.51cto.com/5239058/1929054


########### node1 mysql安装开始 ##################


[[email protected] ~]# pcs cluster stop --all


[[email protected] ~]# systemctl  start drbd

[[email protected] ~]# systemctl  start drbd


# 将节点1设为主节点挂载磁盘配置mariadb

[[email protected] ~]# drbdadm primary mystore

[[email protected] ~]# drbd-overview 

 0:mystore/0  Connected Primary/Secondary UpToDate/UpToDate



[[email protected] ~]# groupadd -r -g 3000 mysql

[[email protected] ~]# useradd -r -g 3000 -u 3001 mysql


# 挂载drbd磁盘并创建目录

[[email protected] ~]# mount /dev/drbd0 /mydata/

[[email protected] ~]# mkdir /mydata/mysql


# 设置目录权限为mysql组,mysql用户

[[email protected] ~]# chown mysql.mysql /mydata/mysql/ -R


# 解压至/usr/local 并软链成mysql目录

[[email protected] ~]# tar xf mariadb-5.5.54-linux-x86_64.tar.gz -C /usr/local/

[[email protected] ~]# cd !$

[[email protected] local]# chown mysql.mysql mariadb-5.5.54-linux-x86_64/ -R

[[email protected] local]# ln -sv mariadb-5.5.54-linux-x86_64/ mysql

[[email protected] local]# cd mysql/

[[email protected] mysql]# cp support-files/my-large.cnf /etc/my.cnf



# 添加文件属性,不检查名称,目录   添加如下几行

[[email protected] mysql]#  vim /etc/my.cnf

innodb_file_per_table = 1

skip_name_resolve = 1

datadir = /mydata/mysql

basedir = /usr/local/mysql


# 初始化数据库

[[email protected] mysql]# ./scripts/mysql_install_db --user=mysql --group=mysql --basedir=/usr/local/mysql/ --datadir=/mydata/mysql/


# 复制启动脚本

[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[[email protected] mysql]# chmod +x  /etc/init.d/mysqld 


# 安装完成 直接 

[[email protected] mysql]# service mysqld start

Starting MySQL.170524 17:12:04 mysqld_safe Logging to ‘/mydata/mysql/node1.err‘.

170524 17:12:04 mysqld_safe Starting mysqld daemon with databases from /mydata/mysql

.. SUCCESS! 


# 设置环境变量

[[email protected] bin]# vim /etc/profile.d/mysql.sh

export PATH=/usr/local/mysql/bin:$PATH

[[email protected] bin]# source !$


# 能直接登陆说明安装成功

[[email protected] bin]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 2

Server version: 5.5.54-MariaDB MariaDB Server


[[email protected] bin]# service mysqld stop

Shutting down MySQL...... SUCCESS! 


# 卸载磁盘 注意上面那步停止mysql一定要做

[[email protected] mysql]# umount /mydata/


# 将node1设置为从节点

[[email protected] bin]# drbdadm secondary mystore

[[email protected] bin]# drbd-overview

 0:mystore/0  Connected Secondary/Secondary UpToDate/UpToDate 


# 复制配置至另一主机中

[[email protected] mysql]# scp /etc/my.cnf node2:/etc/


################## node1 mysql安装完成 ###################


################## node2 mysql安装开始 ###################


# 此处为节点2新建mysql用户

[[email protected] ~]# groupadd -r -g 3000 mysql

[[email protected] ~]# useradd -r -g 3000 -u 3001 mysql



[[email protected] ~]# drbdadm primary mystore

[[email protected] ~]# drbd-overview 

 0:mystore/0  Connected Primary/Secondary UpToDate/UpToDate


# 挂载drbd磁盘并创建目录

[[email protected] ~]# mount /dev/drbd0 /mydata/


# 解压至/usr/local 并软链成mysql目录

[[email protected] ~]# tar xf mariadb-5.5.54-linux-x86_64.tar.gz -C /usr/local/

[[email protected] ~]# cd !$

[[email protected] local]# chown mysql.mysql mariadb-5.5.54-linux-x86_64/ -R

[[email protected] local]# ln -sv mariadb-5.5.54-linux-x86_64/ mysql

[[email protected] local]# cd mysql/


#无需初始化,配置启动文件

[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[[email protected] mysql]# chmod +x  /etc/init.d/mysqld 


# 安装完成 直接 

[[email protected] mysql]# service mysqld start

Starting MySQL.170524 17:12:04 mysqld_safe Logging to ‘/mydata/mysql/node2.err‘.

170524 17:12:04 mysqld_safe Starting mysqld daemon with databases from /mydata/mysql

.. SUCCESS! 


# 设置环境变量

[[email protected] bin]# vim /etc/profile.d/mysql.sh

export PATH=/usr/local/mysql/bin:$PATH

[[email protected] bin]# source !$


# 能直接登陆说明安装成功


[[email protected] bin]# service mysqld stop

Shutting down MySQL...... SUCCESS! 


# 卸载磁盘 注意上面那步停止mysql一定要做

[[email protected] mysql]# umount /mydata/


# 将node1设置为从节点

[[email protected] bin]# drbdadm secondary mystore

[[email protected] bin]# drbd-overview

 0:mystore/0  Connected Secondary/Secondary UpToDate/UpToDate 


#################### node2 mysql安装完成 #####################


# 配置高可用 必须停止drbd

[[email protected] ~]# systemctl  stop drbd

[[email protected] ~]# systemctl  stop drbd


[[email protected] ~]# pcs cluster start --all


# 定义一个虚拟IP地址

crm(live)configure# primitive mysqlip ocf:heartbeat:IPaddr2 params ip=200


# 定义mysql启动服务

crm(live)configure# primitive mysqlserver lsb:mysqld op monitor timeout=30s  interval=20s



# Mandatory: 也可定义成inf 


# 定义mysqlip必须跟mysqlserver一起

crm(live)configure# colocation mysqlip_with_mysqlserver inf: mysqlip mysqlserver


# 定义顺序约束 主从启动后跟挂载,(排序约束中也有一条,定义的是mount必须跟drbd的主节点在一起)

crm(live)configure# order mount_after_ms_mydatas_master Mandatory: ms_mydatas:promote mount


# 顺序约束大概如下意思     drbd(启动主节点时直接挂载) --> mysqlip -->  mount --> mysqlserver 

crm(live)configure# order mysqlip_after_mount Mandatory: mysqlip mount

crm(live)configure# order mysqlserver_after_mysqlip Mandatory: mysqlip mysqlserver


###################查看所有定义资源的信息####################

crm(live)# configure

crm(live)configure# show

node 1: node1

node 2: node2

primitive mount Filesystem \

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

op start timeout=60s interval=0 \

op stop timeout=60s interval=0

primitive mydatas ocf:linbit:drbd \

params drbd_resource=mystore \

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

op monitor role=Slave interval=20s timeout=20s \

op start timeout=240s interval=0 \

op stop timeout=100s interval=0

primitive mysqlip IPaddr \

params ip=192.168.8.200 broadcast=192.168.8.255

primitive mysqlserver lsb:mysqld \

op monitor timeout=30s interval=20s

ms ms_mydatas mydatas \

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

order mount_after_ms_mydatas_master inf: ms_mydatas:promote mount

colocation mount_with_ms_mydatas_master inf: mount ms_mydatas:Master

order mysqlip_after_mount Mandatory: mysqlip mount

colocation mysqlip_with_mysqlserver inf: mysqlip mysqlserver

order mysqlserver_after_mysqlip Mandatory: mysqlip mysqlserver

##################################################################


##############################状态查看##############################

crm(live)# show

ERROR: show: No such command

crm(live)# status

Last updated: Fri May 26 11:51:42 2017

Last change: Fri May 26 11:43:38 2017 by root via cibadmin on node1

Stack: corosync

Current DC: node1 (version 1.1.13-10.el7-44eb2dd) - partition with quorum

2 nodes and 5 resources configured


Online: [ node1 node2 ]


Full list of resources:


 Master/Slave Set: ms_mydatas [mydatas]

     Masters: [ node1 ]

     Slaves: [ node2 ]

 mount (ocf::heartbeat:Filesystem): Started node1

 mysqlip (ocf::heartbeat:IPaddr): Started node1

 mysqlserver (lsb:mysqld): Started node1


####################################################################


# 切换节点,查看状态是否能够转移

crm(live)# node 

crm(live)node# standby node1



# 错误提示

 Master/Slave Set: ms_mydatas [mydatas]

     Masters: [ node2 ]

     Stopped: [ node1 ]

 mount (ocf::heartbeat:Filesystem): Stopped

 mysqlip (ocf::heartbeat:IPaddr): Stopped

 mysqlserver (lsb:mysqld): Stopped


Failed Actions:

* mysqlserver_start_0 on node2 ‘unknown error‘ (1): call=216, status=complete, exitreason=‘none‘,

    last-rc-change=‘Fri May 26 11:52:53 2017‘, queued=0ms, exec=1060ms

* mount_start_0 on node2 ‘unknown error‘ (1): call=215, status=complete, exitreason=‘Couldn‘t mount filesystem /dev/drbd0 on /mydata‘,

    last-rc-change=‘Fri May 26 11:52:53 2017‘, queued=0ms, exec=349ms

# 解决方案:当出现无法挂载之类的情况首先先清空一下状态,可能是前一个节点信息还没有传递到另一个节点

crm(live)node# clearstate node2

Do you really want to drop state for node node2 (y/n)? y

Waiting for 1 replies from the CRMd. OK


############清空之后再查看#############

crm(live)# status

Last updated: Fri May 26 11:54:28 2017

Last change: Fri May 26 11:52:47 2017 by root via crm_attribute on node1

Stack: corosync

Current DC: node1 (version 1.1.13-10.el7-44eb2dd) - partition with quorum

2 nodes and 5 resources configured


Node node1: standby

Online: [ node2 ]


Full list of resources:


 Master/Slave Set: ms_mydatas [mydatas]

     Masters: [ node2 ]

     Stopped: [ node1 ]

 mount (ocf::heartbeat:Filesystem): Started node2

 mysqlip (ocf::heartbeat:IPaddr): Started node2

 mysqlserver (lsb:mysqld): Started node2

############成功切换至另一节点


# 避免node1恢复抢回,抢回可能也会导致服务无法正常使用,设置黏性

crm(live)configure# property default-resource-stickiness=100


# 然后将node1设置为online状态

crm(live)node# online


# 再次查看服务, node2依旧正常运行

一些常见错误的解决链接:http://blog.csdn.net/t1anyuan/article/details/52143789

                                         http://litaotao.blog.51cto.com/6224470/1303307

以上是关于corosync+pacemaker+mysql+drbd 实现mysql的高可用的主要内容,如果未能解决你的问题,请参考以下文章

drbd+corosync+pacemaker构建高可用MySQL集群

corosync+pacemaker+drbd 实现mysql的高可用性

how-to create a high-availability mysql setup with corosync pacemaker and drbd on ubuntu

corosync+pacemaker+crm实现drbd高可用

pacemaker+corosync实现zabbix高可用集群

corosync+pacemaker+crmsh