当前位置:网站首页>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 onSome 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
donehome/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").txthome/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
doneRespectively 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
边栏推荐
- uni-app学习总结
- A new paradigm of distributed deep learning programming: Global tensor
- QT handy notes (II) edit control and float, qstring conversion
- Search module use case writing
- 2022年中科磐云——服务器内部信息获取 解析flag
- 【荧光字效果】
- (一)面扫描仪与机械臂的手眼标定(眼在手上)
- POJ 1012 Joseph
- Node memory overflow and V8 garbage collection mechanism
- JS one line code to obtain the maximum and minimum values of the array
猜你喜欢

Xiaobai makes a wave of deep copy and shallow copy

开发转测试:从0开始的6年自动化之路...

Node memory overflow and V8 garbage collection mechanism

QT handy notes (III) use qtcharts to draw a line chart in VS

Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market

图解用户登录验证流程,写得太好了!
![[datawhale] [machine learning] Diabetes genetic risk detection challenge](/img/98/7981af7948feb73168e5200b3dfac9.png)
[datawhale] [machine learning] Diabetes genetic risk detection challenge

Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice

【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛

Server and client dual authentication (2)
随机推荐
mfc随手笔记
Node 内存溢出及V8垃圾回收机制
图解用户登录验证流程,写得太好了!
Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
RMQ学习笔记
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
SSG framework Gatsby accesses the database and displays it on the page
ie7设置overflow属性失效解决方法
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
Spolicy request case
V-permission add permission
Use of OpenCV class
【荧光字效果】
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
CSV data file settings of JMeter configuration components
【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)
Smart gourmet C language
Why does new public chain Aptos meet market expectations?
matlab中的AR模型短时预测交通流