MySQL日常运维之 MySQL双主复制+keepalived+haproxy配置(负载均衡)

Posted 数据与人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL日常运维之 MySQL双主复制+keepalived+haproxy配置(负载均衡)相关的知识,希望对你有一定的参考价值。

MySQL日常运维之 MySQL双主复制+keepalived+haproxy配置(负载均衡)


实验系统:CentOS 6.5_x86_64

实验前提:防火墙和selinux都关闭

实验软件:keepalived-1.2.13  haproxy-1.8.13  mysql—5.7.21

主1 ip:192.168.226.134

主2 ip:192.168.226.135

vip 192.168.226.150


一、安装mysql


获取mysql安装包:

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

安装mysql过程省略


二、配置主主复制

命令:service iptables stop 检查:service iptables status关闭 vi /etc/selinux/config SELINUX=disabled

两台服务器上都要执行:

创建复制用户

grant replication slave on *.* to 'repl'@'%' identified by '123';

进入从服务器mysql命令:# mysql -uroot -p关闭slave命令:stop slave;

开始配置:

输入下面代码即可:

CHANGE MASTER TO MASTER_HOST='192.168.226.135', MASTER_USER='repl', MASTER_PASSWORD='123', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=681;


先在从服务器配置完成,启动从服务器:

命令:start slave;

反向再配置一次


三、安装haproxy(两台节点都要安装)

tar -zxvxf haproxy-1.8.13.tar.gz cd haproxy-1.8.13make TARGET=linux2628 //根据自己主机设定make install


提供启动脚本


vi /etc/init.d/haproxy
#!/bin/sh## haproxy## chkconfig: - 85 15# description: HAProxy is a free, very fast and reliable solution \# offering high availability, load balancing, and \# proxying for TCP and HTTP-based applications# processname: haproxy# config: /etc/haproxy/haproxy.cfg# pidfile: /var/run/haproxy.pid
# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0
exec="/usr/local/sbin/haproxy"prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
cfgfile=/etc/haproxy/haproxy.cfgpidfile=/var/run/haproxy.pidlockfile=/var/lock/subsys/haproxy
check() {$exec -c -V -f $cfgfile $OPTIONS}
start() {$exec -c -q -f $cfgfile $OPTIONSif [ $? -ne 0 ]; thenecho "Errors in configuration file, check with $prog check."return 1fi
echo -n $"Starting $prog: "# start it up here, usually something like "daemon $exec"daemon $exec -D -f $cfgfile -p $pidfile $OPTIONSretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}
stop() {echo -n $"Stopping $prog: "# stop it here, often "killproc $prog"killproc $progretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval}
restart() {$exec -c -q -f $cfgfile $OPTIONSif [ $? -ne 0 ]; thenecho "Errors in configuration file, check with $prog check."return 1fistopstart}
reload() {$exec -c -q -f $cfgfile $OPTIONSif [ $? -ne 0 ]; thenecho "Errors in configuration file, check with $prog check."return 1fiecho -n $"Reloading $prog: "$exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)retval=$?echoreturn $retval}
force_reload() {restart}
fdr_status() {status $prog}
case "$1" instart|stop|restart|reload)$1;;force-reload)force_reload;;check)check;;status)fdr_status;;condrestart|try-restart)[ ! -f $lockfile ] || restart;;*)echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"exit 2esac


chkconfig --add haproxychkconfig haproxy onchmod +x /etc/init.d/haproxy


提供配置文件

mkdir /etc/haproxymkdir /var/lib/haproxyuseradd -r haproxy


vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 4000user haproxygroup haproxydaemon
stats socket /var/lib/haproxy/stats
defaultsmode tcp //haproxy运行模式log globaloption dontlognulloption redispatchretries 3timeout http-request 10stimeout queue 1mtimeout connect 10stimeout client 1mtimeout server 1mtimeout http-keep-alive 10stimeout check 10smaxconn 600 //最大连接数
listen stats //配置haproxy状态页mode httpbind :6677 //找一个比较特殊的端口stats enablestats hide-version //隐藏haproxy版本号stats uri /haproxyadmin?stats //一会用于打开状态页的uristats realm Haproxy\ Statistics //输入账户密码时的提示文字stats auth admin:admin //用户名:密码stats admin if TRUE //开启状态页的管理功能
frontend main bind *:80 default_backend mysql //后端服务器组名
backend mysqlbalance leastconn //使用最少连接方式调度server m1 192.168.226.134:80 check port 80 maxconn 300server m2 192.168.226.135:80 check port 80 maxconn 300


启动日志


vi /etc/rsyslog.conf# Provides UDP syslog reception //去掉下面两行注释,开启UDP监听$ModLoad imudp$UDPServerRun 514
local2.* /var/log/haproxy.log //添加此行
service rsyslog restart


启动测试haproxy:

service haproxy startnetstat -tnlp


四、安装keepalived (两台服务器都要执行)


yum install -y keepalivedchkconfig --add keepalivedchkconfig keepalived on

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bakvi /etc/keepalived/keepalived.conf
粘贴如下内容
! Configuration File for keepalivedglobal_defs {router_id Mysql_ha}
vrrp_script chk_mysql {script "/etc/keepalived/check_mysql.sh"interval 2weight 5}
vrrp_script chk_haproxy {script "/etc/keepalived/chk.sh"interval 2weight 5}
vrrp_instance mysql-instance {state MASTER #另一台为BACKUPinterface enp4s0 #与网卡名称对应virtual_router_id 11 #每一个IP唯一,另一台绑定相同IP要与整个ID一致priority 10 #另一台为9advert_int 1authentication {auth_type PASSauth_pass password321}track_script {chk_mysql}virtual_ipaddress {192.168.226.150/24}}
vrrp_instance mysql-ha {state MASTERinterface enp4s0virtual_router_id 13priority 10advert_int 1# nopreemptauthentication {auth_type PASSauth_pass password321}track_script {chk_haproxy}virtual_ipaddress {192.168.226.14/24}notify_backup "/etc/init.d/haproxy restart"notify_fault "/etc/init.d/haproxy stop"}



在两台机器上创建chk.sh文件:

vi /etc/keepalived/chk.sh
#!/bin/bash#if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then/etc/init.d/keepalived stopfi
chmod +x /etc/keepalived/chk.sh
vi /etc/keepalived/check_mysql.sh#!/bin/bashMYSQL_PORT=:3306alive=$(netstat -apn |grep $MYSQL_PORT|grep LISTEN)if [ -z "$alive" ] thenexit 1elseexit 0fi


五、在两台机器上测试

在浏览器打开http://192.168.226.150:6677/haproxyadmin?stats,打开haproxy状态页:

 



以上是关于MySQL日常运维之 MySQL双主复制+keepalived+haproxy配置(负载均衡)的主要内容,如果未能解决你的问题,请参考以下文章

MySQL数据库运维之主从复制搭建

Linux运维之Shell编程------监控MySQL错误码及主从复制同步异常

MySQL数据库运维之主从复制延迟问题排查

MySQL数据库运维之主从复制延迟问题排查

Linux系统运维之MYSQL数据库集群部署(主从复制)

MySQL - MMM 实现 MySQL 高可用