当前位置:网站首页>Keeping alive to realize MySQL automatic failover
Keeping alive to realize MySQL automatic failover
2022-07-26 09:48:00 【Color the sky】
Environmental Science :
MySQL Master 192.168.135.139
MySQL Slave 192.168.135.141
VIP 192.168.135.188
First configure master-slave
https://blog.csdn.net/u010533511/article/details/88063523
master and slave All installed keepalived
Download the package to be installed and send it to the server :http://www.keepalived.org/download.html
My test catalog is :/www/package/keepalived-2.0.13.tar.gz
#tar -zxvf keepalived-2.0.13.tar.gz
#cd keepalived-2.0.13
#./configure
# make && make install
#cp /www/package/keepalived-2.0.13/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/
#cp /www/package/keepalived-2.0.13/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#mkdir /etc/keepalived
#cp /www/package/keepalived-2.0.13/keepalived/etc/keepalived/keepalived.conf etc/keepalived/
#cp /usr/local/sbin/keepalived /usr/sbin/
#chkconfig --add keepalived
#chkconfig --level 345 keepalived on
Some dependent packages may be required during installation , use yum Direct installation
Master slave configuration file modification
master Configuration file for
#vim /etc/keepalived/keepalived.conf
global_defs {
router_id MySQL-HA # function Keepalived A sign of the machine
}
vrrp_script check_run {
script "/home/mysql/mysql_check.sh" # Configure the business process monitoring script
interval 60 # Set the time interval for script execution , second
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface ens33 # To change to the value of the current server
virtual_router_id 51
priority 100 # The weight
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1234
}
track_script {
check_run
}
notify_master /home/mysql/master.sh
notify_stop /home/mysql/stop.sh
virtual_ipaddress {
192.168.135.188
}
}
slave Of keepalived The configuration file :
#vim /etc/keepalived/keepalived.conf
global_defs {
router_id MySQL-HA # function Keepalived A sign of the machine
}
vrrp_script check_run {
script "/home/mysql/mysql_check.sh" # Configure the business process monitoring script
interval 60 # Set the time interval for script execution , second
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface ens33 # To change to the value of the current server
virtual_router_id 51
priority 90 # The weight
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1234
}
track_script {
check_run
}
notify_master /home/mysql/master.sh
notify_stop /home/mysql/stop.sh
virtual_ipaddress {
192.168.135.188
}
}
master And slave Of keepalived There are only... In the configuration file priority Set different ,master by 100,slave by 90, Everything else is the same .
/home/mysql/mysql_check.sh Documents are used to detect MySQL Whether the service is normal , When it is found that the connection is not connected mysql, Automatic handle keepalived Process kill , Give Way VIP Drift .
vim /home/mysql/mysql_check.sh
#!/bin/bash
. /home/mysql/.bashrc
count=1
while true
do
mysql -uroot -S /data/mysql.sock -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
home/mysql/master.sh The function of is to change the state to master Scripts to be executed later . First, determine if there is a delay in replication , If there's a delay , etc. 1 Minutes later , With or without delay , And stop copying , And record binlog and pos spot .
vim /home/mysql/master.sh
#!/bin/bash
. /home/mysql/.bashrc
Master_Log_File=$(mysql -uroot -S /data/mysql.sock -e "show slave status\G" | grep -w Master_Log_File | awk -F": " '{print $2}')
Relay_Master_Log_File=$(mysql -uroot -S /data/mysql.sock -e "show slave status\G" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}')
Read_Master_Log_Pos=$(mysql -uroot -S /data/mysql.sock -e "show slave status\G" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}')
Exec_Master_Log_Pos=$(mysql -uroot -S /data/mysql.sock -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 -uroot -S /data/mysql.sock -e "stop slave;"
mysql -uroot -S /data/mysql.sock -e "reset slave all;"
mysql -uroot -S /data/mysql.sock -e "reset master;"
mysql -uroot -S /data/mysql.sock -e "show master status;" > /tmp/master_status_$(date "+%y%m%d-%H%M").txt
home/mysql/stop.sh Express Keepalived Script to be executed after stopping . Check whether there are any copy and write operations , Finally, whether the execution is completed or not, exit . The contents of the document are as follows .
vim /home/mysql/stop.sh
#!/bin/bash
. /home/mysql/.bashrc
M_File1=$(mysql -uroot -S /data/mysql.sock -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position1=$(mysql -uroot -S /data/mysql.sock -e "show master status\G" | awk -F': ' '/Position/{print $2}')
sleep 1
M_File2=$(mysql -uroot -S /data/mysql.sock -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position2=$(mysql -uroot -S /data/mysql.sock -e "show master status\G" | awk -F': ' '/Position/{print $2}')
i=1
while true
do
if [ $M_File1 = $M_File1 ] && [ $M_Position1 -eq $M_Position2 ]
then
echo "ok"
break
else
sleep 1
if [ $i -gt 60 ]
then
break
fi
continue
let i++
fi
done
Respectively in master Shanghe slave Start the keepalived process .
/etc/init.d/keepalived start ( Turn on )
/etc/init.d/keepalived stop ( Turn on )
Check the process
see vip
Client side usage VIP Connect to database , Create a test library , insert data .
When master Of mysql After hanging up
Will automatically switch to slave Then start it manually master after
Restart keepalived( Restart both servers ) Then the master and slave will synchronize automatically It can be configured as a dual main structure , Ensure data integrity
keepalived.conf Configuration file parameter description
group: Set VRRP Instance name , There is only one example VI_1.
vrrp_instance To configure VRRP example .VRRP The instance indicates that VRRP agreement . This example shows VRRP Some characteristics of , For example, master and slave 、VRID wait . You can open an instance on each network card .VRRP Examples mainly define vrrp_sync_group The drift of each group inside IP etc. .
state: Specify the initial state of the instance . After both routes are started , There will be competition soon , high priority Will run for Master, So here state That doesn't mean this one has always been Backup.
interface: Instance bound network card .
virtual_router_id:VRID Mark , The value is 0..255, The default... Is used here 51.
priority: The high priority campaign is Master,Master Above Backup At least 50. here MySQL The two priorities of master and slave libraries are set to 100 and 90, So when Keepalived After starting ,MySQL The main library will be selected Master.
advert_int: Check interval , This is set as the default 1 second .
nopreempt: Set to no preemption , Note that this configuration can only be set in state by BACKUP On a host of . When MASTER When something goes wrong ,BACKUP Will run for the new MASTER, So when the previous MASTER After being online again , Is to continue to be MASTER Or become BACKUP Well ? No preemption is set by default , So what happened before MASTER When you get up, you will continue to seize and become MASTER. Such frequent switching is intolerable for business , We hope MASTER Get up and become BACKUP, So set no preemption . Again because nopreempt Configuration can only be used in state by BACKUP On a host of , therefore MASTER Of state It must also be set to BACKUP, in other words 192.168.135.139 and 192.168.135.141 All of them will state Set to BACKUP. Through two BACKUP The above settings are different priority, Let them come together and seize , high priority Of 192.168.135.139 Become the original MASTER.
authentication: Set the authentication type and authentication password .
auth_type: Authentication type , Support PASS、AH Two kinds of , Usually use PASS type .
auth_pass: Plaintext authentication password . same VRRP Example of MASTER And BACKUP Use the same password to communicate properly .
track_script: Set the tracking script , Here for check_run, That is to call vrrp_script Script defined in .
notify_master: Specify when switching to MASTER Script executed on .
notify_stop:VRRP Stop scripts that are executed later .
virtual_ipaddress: Specify the drift address (VIP), That is, switch to MASTER when , these IP Or added , Switch to BACKUP when , these IP Will be deleted . Therefore, no virtual address can be bound to each server , And put them in virtual_ipaddress Inside , You can have more .Keepalived Will be used automatically ip addr Binding .
Reference resources :https://blog.csdn.net/wzy0623/article/details/80916567
边栏推荐
- V-permission add permission
- The diagram of user login verification process is well written!
- Flutter Event 派发
- 2019 ICPC Asia Yinchuan Regional(水题题解)
- EOJ 2020 1月月赛 E数的变换
- (二)面扫描仪与机械臂的手眼标定(眼在手外:九点标定)
- Mo team learning summary (II)
- 电机转速模糊pid控制
- 2022年中科磐云——服务器内部信息获取 解析flag
- Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
猜你喜欢
电机转速模糊pid控制
The diagram of user login verification process is well written!
Node memory overflow and V8 garbage collection mechanism
Development to testing: a six-year road to automation starting from 0
Fiddler packet capturing tool for mobile packet capturing
matlab中的AR模型短时预测交通流
高斯消元求解矩阵的逆(gauss)
Qt随手笔记(三)在vs中使用QtCharts画折线图
AR model in MATLAB for short-term traffic flow prediction
在Blazor 中自定义权限验证
随机推荐
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
SSG框架Gatsby访问数据库,并显示到页面上
云原生(三十六) | Kubernetes篇之Harbor入门和安装
(二)面扫描仪与机械臂的手眼标定(眼在手外:九点标定)
高斯消元的应用
E. Two Small Strings
Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
Node memory overflow and V8 garbage collection mechanism
[MySQL] understand the important architecture of MySQL (I)
A new paradigm of distributed deep learning programming: Global tensor
Qt随手笔记(三)在vs中使用QtCharts画折线图
服务器、客户端双认证(2)
RMQ学习笔记
【荧光字效果】
[untitled]
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
Simple pedestrian recognition code to 88% accuracy Zheng Zhedong preparation
新增市场竞争激烈,中国移动被迫推出限制性超低价5G套餐
高斯消元求解异或线性方程组
Search module use case writing