search
HomeDatabaseMysql TutorialKeepalive之MySQL故障自动切换脚本
Keepalive之MySQL故障自动切换脚本Jun 07, 2016 pm 05:58 PM
keepalivemysqlswitchFaultScriptautomatic

MySQL架构为master-slave(主从),master故障自动切换到slave上。当然也可以设置为双master,但这里有个弊端:就是当主的压力很大时,从上延时很大,比如落后2000秒,此时主挂了,从接管(VIP漂移到从),用户刚才发表的文章,此时因为同步延时大,还没复制

MySQL架构为master-slave(主从),master故障自动切换到slave上。当然也可以设置为双master,但这里有个弊端:就是当主的压力很大时,从上延时很大,比如落后2000秒,此时主挂了,从接管(VIP漂移到从),用户刚才发表的文章,此时因为同步延时大,还没复制过来,于是用户又发表了一篇文章,当原来的master修好后,因从的IO和SQL线程还在开启状态,还会继续同步刚才没有同步复制完的数据,这时有可能把用户新发表的文章更改掉,造成用户数据丢失。

考虑到这种情况,我这里还是用的master-slave(主从)架构。

keepalive安装很简单,这里不再啰嗦。主要看下配置文件和脚本:

# more /etc/keepalived/keepalived.conf 
global_defs {
   router_id KeepAlive_Mysql


vrrp_script check_run {
script "/root/sh/mysql_check.sh"
interval 300
}

vrrp_sync_group VG1 {
group {
VI_1
}
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0  
    virtual_router_id 51
    priority 100  
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    check_run
    }

    notify_master /root/sh/master.sh
    notify_backup /root/sh/backup.sh
    notify_stop /root/sh/stop.sh

    virtual_ipaddress {
        192.168.8.150
    }
}

notify_master |    # 状态改变为MASTER后执行的脚本
notify_backup |    # 状态改变为BACKUP后执行的脚本
notify_fault |    # 状态改变为FAULT后执行的脚本
notify_stop |    # VRRP停止后后执行的脚本
notify |        # (1)任意状态改变后执行的脚本
下面解释下这4个脚本的用法:

mysql_check.sh(健康检查脚本,当发现mysql连接不上,会把keepalive进程关闭,并切换。)
# more mysql_check.sh 
#!/bin/bash

. /root/.bash_profile

count=1

while true
do

mysql -e "show status;" > /dev/null 2>&1
i=$?
ps aux | grep mysqld | grep -v grep > /dev/null 2>&1
j=$?
if [ $i = 0 ] && [ $j = 0 ]
then
   exit 0
else
   if [ $i = 1 ] && [ $j = 0 ]
   then
       exit 0
   else
        if [ $count -gt 5 ]
        then
              break
        fi
   let count++
   continue
   fi
fi

done

/etc/init.d/keepalived stop
master.sh(状态改变为MASTER后执行的脚本)  (首先判断同步复制是否执行完毕,如果未执行完毕,等1分钟后,不论是否执行完毕,都跳过,并停止同步复制进程。) (其次,更改前端程序连接的业务账号admin的权限和密码,并记录当前切换以后的日志和POS点。)
# more master.sh
#!/bin/bash

. /root/.bash_profile

Master_Log_File=$(mysql -e "show slave status\G" | grep -w Master_Log_File | awk -F": " '{print $2}')
Relay_Master_Log_File=$(mysql -e "show slave status\G" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}')
Read_Master_Log_Pos=$(mysql -e "show slave status\G" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}')
Exec_Master_Log_Pos=$(mysql -e "show slave status\G" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}')

i=1

while true
do

if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ]
then
   echo "ok"
   break
else
   sleep 1

   if [ $i -gt 60 ]
   then
      break
   fi
   continue
   let i++
fi
done

mysql -e "stop slave;"
mysql -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY 'admin';flush privileges;"
mysql -e "show master status;" > /tmp/master_status_$(date "+%y%m%d-%H%M").txt
backup.sh(状态改变为BACKUP后执行的脚本)
# more backup.sh 
#!/bin/bash

. /root/.bash_profile

mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '1q2w3e4r';flush privileges;"
stop.sh(keepalived停止后后执行的脚本) (首先把admin密码更改掉) (其次,设置参数,保证不丢失数据) (最后,查看是否还有写操作,不论是否执行完毕,1分钟后都退出。)
# more stop.sh 
#!/bin/bash

. /root/.bash_profile

mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '1q2w3e4r';flush privileges;"
mysql -e "set global innodb_support_xa=1;"
mysql -e "set global sync_binlog=1;"
mysql -e "set global innodb_flush_log_at_trx_commit=1;"

M_File1=$(mysql -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position1=$(mysql -e "show master status\G" | awk -F': ' '/Position/{print $2}')
sleep 1
M_File2=$(mysql -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position2=$(mysql -e "show master status\G" | awk -F': ' '/Position/{print $2}')

i=1

while true
do

if [ $M_File1 = $M_File2 ] && [ $M_Position1 -eq $M_Position2 ]
then
   echo "ok"
   break
else
   sleep 1

   if [ $i -gt 60 ]
   then
      break
   fi
   continue
   let i++
fi
done
有兴趣的朋友可以测试下,有问题及时交流。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

nginx upstream如何开启keepalivenginx upstream如何开启keepaliveMay 14, 2023 pm 07:04 PM

nginxupstream开启keepaliveupstreamtomcat{serverops-coffee.cn:8080;keepalive1024;}server{location/{proxy_http_version1.1;proxy_set_headerConnection"";proxy_passhttp://tomcat;}}nginx在项目中大多数情况下会作为反向代理使用,例如nginx后接tomcat,nginx后接php等,这时我们开启nginx和后端

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

vue3 keepalive线上问题怎么解决vue3 keepalive线上问题怎么解决May 19, 2023 am 08:04 AM

1、keepalive功能keepalive是vue3中的一个全局组件keepalive本身不会渲染出来,也不会出现在dom节点当中,但是它会被渲染为vnode,通过vnode可以跟踪到keepalive中的cache和keys,当然也是在开发环境才可以,build打包以后没有暴露到vnode中(这个还要再确认一下)keepalive最重要的功能就是缓存组件keepalive通过LRU缓存淘汰策略来更新组件缓存,可以更有效的利用内存,防止内存溢出,源代码中的最大缓存数max为10,也就是10个组

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

Nginx中HTTP的keepalive怎么配置Nginx中HTTP的keepalive怎么配置May 12, 2023 am 11:28 AM

httpkeepalive在http早期,每个http请求都要求打开一个tpcsocket连接,并且使用一次之后就断开这个tcp连接。使用keep-alive可以改善这种状态,即在一次tcp连接中可以持续发送多份数据而不会断开连接。通过使用keep-alive机制,可以减少tcp连接建立次数,也意味着可以减少time_wait状态连接,以此提高性能和提高httpd服务器的吞吐率(更少的tcp连接意味着更少的系统内核调用,socket的accept()和close()调用)。但是,keep-ali

nginx keepalive如何使用nginx keepalive如何使用May 19, 2023 pm 02:13 PM

默认http1.1协议的请求头是默认开启keepalive,如图:那什么是keepalive?作用是什么?keepalive是在tcp中一个可以检测死连接的机制,作用是保持socket长连接不被断开,属于tcp层的功能,并不属于应用层。tcp层怎么做到保持长连接的呢?先看keepalive的用法:有三个参数,开放给应用层使用sk->keepalive_probes:探测次数,重试次数sk->keepalive_time探测的心跳间隔,tcp链接在多少秒之后没有数据报文传输启动探测报文

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),