首页 > 代码库 > corosync+pacemaker+mysql+drbd 实现mysql的高可用

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

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


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


[root@node1 ~]# pcs cluster stop --all


[root@node1 ~]# systemctl  start drbd

[root@node2 ~]# systemctl  start drbd


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

[root@node1 ~]# drbdadm primary mystore

[root@node1 ~]# drbd-overview 

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



[root@node1 ~]# groupadd -r -g 3000 mysql

[root@node1 ~]# useradd -r -g 3000 -u 3001 mysql


# 挂载drbd磁盘并创建目录

[root@node1 ~]# mount /dev/drbd0 /mydata/

[root@node1 ~]# mkdir /mydata/mysql


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

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


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

[root@node1 ~]# tar xf mariadb-5.5.54-linux-x86_64.tar.gz -C /usr/local/

[root@node1 ~]# cd !$

[root@node1 local]# chown mysql.mysql mariadb-5.5.54-linux-x86_64/ -R

[root@node1 local]# ln -sv mariadb-5.5.54-linux-x86_64/ mysql

[root@node1 local]# cd mysql/

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



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

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

innodb_file_per_table = 1

skip_name_resolve = 1

datadir = /mydata/mysql

basedir = /usr/local/mysql


# 初始化数据库

[root@node1 mysql]# ./scripts/mysql_install_db --user=mysql --group=mysql --basedir=/usr/local/mysql/ --datadir=/mydata/mysql/


# 复制启动脚本

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

[root@node1 mysql]# chmod +x  /etc/init.d/mysqld 


# 安装完成 直接 

[root@node1 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! 


# 设置环境变量

[root@node1 bin]# vim /etc/profile.d/mysql.sh

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

[root@node1 bin]# source !$


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

[root@node1 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


[root@node1 bin]# service mysqld stop

Shutting down MySQL...... SUCCESS! 


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

[root@node1 mysql]# umount /mydata/


# 将node1设置为从节点

[root@node1 bin]# drbdadm secondary mystore

[root@node1 bin]# drbd-overview

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


# 复制配置至另一主机中

[root@node1 mysql]# scp /etc/my.cnf node2:/etc/


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


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


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

[root@node2 ~]# groupadd -r -g 3000 mysql

[root@node2 ~]# useradd -r -g 3000 -u 3001 mysql



[root@node2 ~]# drbdadm primary mystore

[root@node2 ~]# drbd-overview 

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


# 挂载drbd磁盘并创建目录

[root@node2 ~]# mount /dev/drbd0 /mydata/


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

[root@node2 ~]# tar xf mariadb-5.5.54-linux-x86_64.tar.gz -C /usr/local/

[root@node2 ~]# cd !$

[root@node2 local]# chown mysql.mysql mariadb-5.5.54-linux-x86_64/ -R

[root@node2 local]# ln -sv mariadb-5.5.54-linux-x86_64/ mysql

[root@node2 local]# cd mysql/


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

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

[root@node2 mysql]# chmod +x  /etc/init.d/mysqld 


# 安装完成 直接 

[root@node2 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! 


# 设置环境变量

[root@node2 bin]# vim /etc/profile.d/mysql.sh

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

[root@node2 bin]# source !$


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


[root@node1 bin]# service mysqld stop

Shutting down MySQL...... SUCCESS! 


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

[root@node1 mysql]# umount /mydata/


# 将node1设置为从节点

[root@node1 bin]# drbdadm secondary mystore

[root@node1 bin]# drbd-overview

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


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


# 配置高可用 必须停止drbd

[root@node1 ~]# systemctl  stop drbd

[root@node2 ~]# systemctl  stop drbd


[root@node1 ~]# 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的高可用