当前位置: 首页 > 工具软件 > heartBeat 0.1 > 使用案例 >

drbd实现mysql地热备_Mysql+DRBD+Heartbeat 实现mysql高可用的双机热备(mysql+heartbeat篇)...

左丘子平
2023-12-01

*************************************部署MYSQL*******************************************

yum -y install  gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* make cmake bison*

useradd mysql -d /usr/local/mysql -s /sbin/nologin

tar -zxvf mysql-5.1.72.tar.gz

cd mysql-5.1.72.tar.gz

./configure --prefix=/usr/local/mysql/ --with-charset=utf8

make && make install

chown -R mysql.mysql /usr/local/mysql

cd /usr/local/mysql/scripts

./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql

copy配置文件到/etc目录

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

copy启动脚本到资源目录

cp support-files/mysql.server /etc/rc.d/init.d/mysqld

添加mysql服务

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

mysqladmin –u root password 123456        //设置roo密码

10、启动mysql

[root@node1 mysql-5.1.72]#service mysqld start

#配置库文件搜索路径

#echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf

#ldconfig

#添加/usr/local/mysql/bin到环境变量PATH中

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

cp -R /data/mysql

chown -R mysql.mysql /data/mysql

##编辑Mysql的配置文件/etc/my.cnf

[root@node1 ~]# sed -n "/^[^#]/p" /etc/my.cnf

[mysqld]

server-id=1                     ##保证全局唯一

log-bin=mysql-bin##开启记录二进制日志的功能

relay-log=mysql-relay-bin

datadir=/data/mysql/

socket=/data/mysql/mysql.sock

user=mysql

symbolic-links=0

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

将mysql启动交给heartbeat来管理,在heartbeat资源脚本目录中建立mysql的启动脚本的软链接

ln -s /etc/init.d/mysqld /etc/ha.d/resource.d/mysqld

#####重启mysql服务#######

service mysqld restart

##############配置mysql的主从复制####################

在master上增加一个用于复制的帐号

mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.0.0.2' IDENTIFIED BY 'repl123456';

备份master上的数据,首先执行如下语句:

mysql>FLUSH TABLES WITH READ LOCK;

mysql>reset master;

不要退出终端,否则这个锁就失效了,在不退出终端的情况下,再开启一个终端直接打包压缩数据文件或使用mysqldump工具来导出数据。

cd /data/mysql

tar zcvf mysql.tar.gz /data/mysql

scp mysql.tar.gz  root@10.0.0.2:/root/

数据传输完成后,再刚刚执行FLUSHTABLES WITH READ LOCK 命令的master 终端上执行如下命令:

mysql> UNLOCK TABLES;

查看master的状态:

mysql>show master status\G   #以下面列出的配置,进行设置slave的MASTER_LOG_FILE值,和MASTER_LOG_POS值。

设置slave主机

开启master与slave的同步,在slave上执行下列语句:

mysql>CHANGE MASTER TO MASTER_HOST='10.0.0.1',

->MASTER_USER='repl',

->MASTER_PASSWORD='repl123456',

->MASTER_LOG_FILE='mysql-bin.000001',

->MASTER_LOG_POS=106;

完成后,开启slave

mysql>slave start;

查看slave状态

mysql>show slave status\G

当Slave_IO_Running 和Slave_SQL_Running 都为yes时,表示配置成功。

****************************需要注意的几个问题********************************

1)如果在my.cnf中定义了log-bin、relay-log参数,那么要保证这些定义与主机名无关,

因为如果这两类的log文件名与主机名有关,切换过程会导致slave主机不能继续同步,可以如下设置:

log-bin=mysql-bin

relay-log=mysql-relay-bin

保证两台主机上两个文件的名字一样

2)保证mysql能通过 start\stop\status 三个参数,默认heartbeat采用的是LSB风格,

返回值包含OK或者running则表示资源正常,返回值包含stopped或NO则表示资源不正常,其他资源脚本也类似。

3)不要设置mysql在机器重启时自动启动,mysqld作为heartbeat的一项资源,heartbeat会统一管理。

4)保证每台mysql的my.cnf配置文件 除了server-id以外的其他配置项一模一样。

************************************Heartbeat部署****************************************

两台主机的心跳网卡是eth1

node1的心跳IP是:10.0.0.1

node2的心跳IP是:10.0.0.2

安装heartbeat:

1、首先创建用户和组

groupadd -g 2000 haclient

useradd -u 2000 -g haclient hacluster

装heartbeat所需的软件包:

yum -y  install  pkgconfig glib2-devel  e2fsprogs  libxml2*  PyXML

再编译安装libnet

tar -zxvf libnet-1.1.2.1.tar.gz -C ./

./configure

make && make install

安装heartbeat

tar -zxvf heartbeat-2.1.2.tar.gz -C ./

./ConfigureMe configure

make&& make install

配置heartbeat

需要的配置文件有三个:ha.cf、haresources、authkeys。

这三个配置文件需要在/etc/ha.d 目录下面,但是默认是没有这三个文件的,所以你要:

copy 存放路径/heartbeat-2.1.2/doc/ha.cf         /etc/ha.d/

copy 存放路径/ heartbeat-2.1.2/doc/haresources  /etc/ha.d/

copy 存放路径/ heartbeat-2.1.2/doc/authkeys     /etc/ha.d/

修改authkeys的权限为600

chmod 600 /etc/ha.d/authkeys

配置ha.cf

[root@node2 ~]# sed -n "/^[^#]/p" /etc/ha.d/ha.cf

logfile/var/log/ha-log     #指定heartbeat日志的位置

logfacilitylocal0

keepalive 1                 #每秒检测心跳

deadtime 15#备用节点15s内没有检测到主机心跳,确认对方故障

warntime 5#警告5次

initdead 30#守护进程启动30s后,启动服务资源

ucast eth1 10.0.0.1#另一台主机的eth1和eth0的ip地址,通过两个不同的网络来检测心跳可用性

ucast eth0 10.2.16.254

auto_failback off           #当主节点切换到备节点之后,主节点恢复正常后,不进行切回操作,因为切换一次mysql-master的成本很高

respawn hacluster /usr/lib64/heartbeat/ipfail

respawn hacluster /usr/lib64/heartbeat/dopd #开启dopd功能

apiauth ipfail gid=haclient uid=hacluster

apiauth dopd gid=haclient uid=hacluster

node node1 定义节点的主机名

node node2 定义节点的主机名

配置资源文件haresources

/etc/ha.d/haresources

node1  drbddisk::r0 Filesystem::/dev/drbd0::/data mysqld IPaddr::10.2.16.250/24/eth0

node1 是主节点的主机名

drbddisk是管理DRBD的脚本,默认heartbeat没有,需要拷贝此脚本到 /etc/ha.d/resource.d/drbddisk (文末有此脚本,直接复制粘贴即可)

r0是drbd的启动资源, “drbddisk::r0"可以切换drbd的主机为primary节点或secondary节点,只有状态为primary的节点才能挂载drbd分区

drbddisk脚本相当于执行drbdadm primary r0 或 drbdadm secondary r0操作,把drbd的资源角色变更并且挂载

mysqld 表示启动本机的mysql服务

Ipaddr::10.2.16.250/24/eth0 表示虚拟IP为250实现IP漂移

配置authkeys认证文件

/etc/ha.d/authkeys

auth 1

1 sha1 HA_cluster  #使用sha1验证,密码为:HA_cluster

#将drbddisk脚本加执行权限:

chmod+x /etc/ha.d/resource.d/drbddisk

将主节点设置好后,直接拷贝 ha.cf haresources authkeys 以及 drbddisk文件到 备节点

scp ha.cf haresources authkeys  resources.d/drbddisk  root@10.0.0.2:/root/

在备节点上移动至ha.d目录后,修改ha.cf的ucast值为主节点IP即可,其他不用变

确认mysql服务关闭后,在两台主机上,同时开启heartbeat服务

service heartbeat start

通过查看 /var/log/ha-log 来观察heartbeat的启动过程

##############################drbddisk脚本#####################################

#!/bin/bash

#

# This script is inteded to be used as resource script by heartbeat

#

# Copright 2003-2008 LINBIT Information Technologies

# Philipp Reisner, Lars Ellenberg

#

###

DEFAULTFILE="/etc/default/drbd"

DRBDADM="/sbin/drbdadm"

if [ -f $DEFAULTFILE ]; then

. $DEFAULTFILE

fi

if [ "$#" -eq 2 ]; then

RES="$1"

CMD="$2"

else

RES="all"

CMD="$1"

fi

## EXIT CODES

# since this is a "legacy heartbeat R1 resource agent" script,

# exit codes actually do not matter that much as long as we conform to

# http://wiki.linux-ha.org/HeartbeatResourceAgent

# but it does not hurt to conform to lsb init-script exit codes,

# where we can.

# http://refspecs.linux-foundation.org/LSB_3.1.0/

#LSB-Core-generic/LSB-Core-generic/iniscrptact.html

####

drbd_set_role_from_proc_drbd()

{

local out

if ! test -e /proc/drbd; then

ROLE="Unconfigured"

return

fi

dev=$( $DRBDADM sh-dev $RES )

minor=${dev#/dev/drbd}

if [[ $minor = *[!0-9]* ]] ; then

# sh-minor is only supported since drbd 8.3.1

minor=$( $DRBDADM sh-minor $RES )

fi

if [[ -z $minor ]] || [[ $minor = *[!0-9]* ]] ; then

ROLE=Unknown

return

fi

if out=$(sed -ne "/^ *$minor: cs:/ { s/:/ /g; p; q; }" /proc/drbd); then

set -- $out

ROLE=${5%/**}

: ${ROLE:=Unconfigured} # if it does not show up

else

ROLE=Unknown

fi

}

case "$CMD" in

start)

# try several times, in case heartbeat deadtime

# was smaller than drbd ping time

try=6

while true; do

$DRBDADM primary $RES && break

let "--try" || exit 1 # LSB generic error

sleep 1

done

;;

stop)

# heartbeat (haresources mode) will retry failed stop

# for a number of times in addition to this internal retry.

try=3

while true; do

$DRBDADM secondary $RES && break

# We used to lie here, and pretend success for anything != 11,

# to avoid the reboot on failed stop recovery for "simple

# config errors" and such. But that is incorrect.

# Don't lie to your cluster manager.

# And don't do config errors...

let --try || exit 1 # LSB generic error

sleep 1

done

;;

status)

if [ "$RES" = "all" ]; then

echo "A resource name is required for status inquiries."

exit 10

fi

ST=$( $DRBDADM role $RES )

ROLE=${ST%/**}

case $ROLE in

Primary|Secondary|Unconfigured)

# expected

;;

*)

# unexpected. whatever...

# If we are unsure about the state of a resource, we need to

# report it as possibly running, so heartbeat can, after failed

# stop, do a recovery by reboot.

# drbdsetup may fail for obscure reasons, e.g. if /var/lock/ is

# suddenly readonly. So we retry by parsing /proc/drbd.

drbd_set_role_from_proc_drbd

esac

case $ROLE in

Primary)

echo "running (Primary)"

exit 0 # LSB status "service is OK"

;;

Secondary|Unconfigured)

echo "stopped ($ROLE)"

exit 3 # LSB status "service is not running"

;;

*)

# NOTE the "running" in below message.

# this is a "heartbeat" resource script,

# the exit code is _ignored_.

echo "cannot determine status, may be running ($ROLE)"

exit 4 # LSB status "service status is unknown"

;;

esac

;;

*)

echo "Usage: drbddisk [resource] {start|stop|status}"

exit 1

;;

esac

exit 0

 类似资料: