mysql ha安装配置

  • mysql ha安装配置已关闭评论
  • 115,099 views
  • A+
所属分类:存储应用

Keepalived的安装与配置

  • 下载

官网下载地址:http://www.keepalived.org/download.html,这里选择最新的1.3.9

#wget http://www.keepalived.org/software/keepalived-1.3.9.tar.gz

安装系统依赖程序

yum -y update kernel kernel-headers #升级内核

reboot #重新启动电脑

yum -y install gcc make kernel-devel yum -y install curl libnl3-devel iptables-devel libnfnetlink-devel ipset-devel openssl-devel net-snmp-devel

  • 安装

#tar -zxvf keepalived-1.3.9.tar.gz

#cd keepalived-1.3.9

#./configure --prefix=/usr/local/keepalived  --with-kernel-dir=/usr/src/kernels/`uname -r`

#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

#mkdir /etc/keepalived

#ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/ touch /etc/init.d/keepalived

#chmod +x /etc/init.d/keepalived

#vi /etc/init.d/keepalived#如下:

#!/bin/sh
#
# keepalived High Availability monitor built upon LVS and VRRP
#
# chkconfig: - 86 14
# description: Robust keepalive facility to the Linux Virtual Server project \
# with multilayer TCP/IP stack checks.

### BEGIN INIT INFO
# Provides: keepalived
# Required-Start: $local_fs $network $named $syslog
# Required-Stop: $local_fs $network $named $syslog
# Should-Start: smtpdaemon httpd
# Should-Stop: smtpdaemon httpd
# Default-Start: 
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: High Availability monitor built upon LVS and VRRP
# Description: Robust keepalive facility to the Linux Virtual Server
# project with multilayer TCP/IP stack checks.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

exec="/usr/sbin/keepalived"
prog="keepalived"
config="/etc/keepalived/keepalived.conf"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/keepalived

start() {
 [ -x $exec ] || exit 5
 [ -e $config ] || exit 6
 echo -n $"Starting $prog: "
 daemon $exec $KEEPALIVED_OPTIONS
 retval=$?
 echo
 [ $retval -eq 0 ] && touch $lockfile
 return $retval
}

stop() {
 echo -n $"Stopping $prog: "
 killproc $prog
 retval=$?
 echo
 [ $retval -eq 0 ] && rm -f $lockfile
 return $retval
}

restart() {
 stop
 start
}

reload() {
 echo -n $"Reloading $prog: "
 killproc $prog -1
 retval=$?
 echo
 return $retval
}

force_reload() {
 restart
}

rh_status() {
 status $prog
}

rh_status_q() {
 rh_status &>/dev/null
}



case "$1" in
 start)
 rh_status_q && exit 0
 $1
 ;;
 stop)
 rh_status_q || exit 0
 $1
 ;;
 restart)
 $1
 ;;
 reload)
 rh_status_q || exit 7
 $1
 ;;
 force-reload)
 force_reload
 ;;
 status)
 rh_status
 ;;
 condrestart|try-restart)
 rh_status_q || exit 0
 restart
 ;;
 *)
 echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
 exit 2
esac
exit $

#chkconfig --add keepalived

#chkconfig keepalived on

修改配置文件

vi /etc/keepalived/keepalived.conf

  • 主配置
! Configuration File for keepalived
 global_defs {
 router_id mysql_ha_5
 }
 vrrp_instance VI_1 {
 state MASTER -- 主为MASTER 备份为BACKUP(必须大写!)
 interface eth0 virtual_router_id 100 -- 每一个vrrp_instance都需要不一样的id
 priority 100 -- 优先级 MASTER 要比 BACKUP高
 advert_int 1
 authentication {
 auth_type PASS
 auth_pass 1111 -- 认证密码 MASTER和BACKUP要一致
 }
 virtual_ipaddress {
 192.168.30.9
 }
 }
 virtual_server 192.168.30.9 3306 {
 delay_loop 6
 lb_algo wrr
 lb_kind DR
 nat_mask 255.255.255.0
 persistence_timeout 50
 protocol TCP
 real_server 192.168.30.5 3306 {
 weight 1
 notify_down /usr/local/bin/keepalived.sh
 TCP_CHECK {
 connect_timeout 3
 nb_get_retry 3
 delay_before_retry 3
 connect_port 3306
 }
 }
 }
  • 备配置
! Configuration File for keepalived 
global_defs { 
router_id mysql_ha_6
 } 
 vrrp_instance VI_1 { 
 state BACKUP -- 主为MASTER 备份为BACKUP(必须大写!)
 interface eth0
 virtual_router_id 100 -- 每一个vrrp_instance都需要不一样的id
 priority 80 -- 优先级 MASTER 要比 BACKUP高
 advert_int 1
 authentication {
 auth_type PASS
 auth_pass 1111 -- 认证密码 MASTER和BACKUP要一致
 } 
 virtual_ipaddress { 
 192.168.30.9
 } 
 } 
 virtual_server 192.168.30.9 3306 {
 delay_loop 6
 lb_algo wrr
 lb_kind DR
 nat_mask 255.255.255.0
 persistence_timeout 50
 protocol TCP
 real_server 192.168.30.6 3306 {
 weight 1
 notify_down  /usr/local/bin/keepalived.sh
 TCP_CHECK { 
 connect_timeout 3 
 nb_get_retry 3 
 delay_before_retry 3
 connect_port 3306
 }
 }
 }

创建mysql端口异常执行脚本
#vi /usr/local/bin/keepalived.sh

#!/bin/sh 

service keepalived stop

#chmod +x /usr/local/bin/keepalived.sh

修改keepalived日志文件存放位置

#vi /etc/sysconfig/keepalived(修改最后的KEEPALIVED_OPTIONS,修改后内容如下):

KEEPALIVED_OPTIONS="-D -d -S 0"

修改系统日志配置文件

#vi /etc/syslog.conf(在最后加入如下内容)

local0.* /var/log/keepalived.log

重启的syslog服务

#service syslog restart

启动keepalived

#service keepalived start

查看日志

#more /var/log/keepalived.log

mysql主从同步就不用说了,直接用命令

 

 

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar