当前位置:网站首页>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
边栏推荐
- 阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
- Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res
- MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
- JS continuous assignment operation
- Fuzzy PID control of motor speed
- A new paradigm of distributed deep learning programming: Global tensor
- Source code analysis of object wait notify notifyAll
- MySQL的逻辑架构
- JS table auto cycle scrolling, mouse move in pause
- IIS网站配置
猜你喜欢

Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)

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

Gauss elimination solves the inverse of matrix (Gauss)

Does volatile rely on the MESI protocol to solve the visibility problem? (top)

Fuzzy PID control of motor speed
![[datawhale] [machine learning] Diabetes genetic risk detection challenge](/img/98/7981af7948feb73168e5200b3dfac9.png)
[datawhale] [machine learning] Diabetes genetic risk detection challenge

Node 内存溢出及V8垃圾回收机制
![[MySQL] understand the important architecture of MySQL (I)](/img/89/5fb595b0112fac987626857b76f9a4.png)
[MySQL] understand the important architecture of MySQL (I)

The diagram of user login verification process is well written!

挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破
随机推荐
MySQL的逻辑架构
Logical architecture of MySQL
Source code analysis of object wait notify notifyAll
一种分布式深度学习编程新范式:Global Tensor
服务器、客户端双认证(2)
2022 zhongkepan cloud - server internal information acquisition and analysis flag
Gauss elimination solves the inverse of matrix (Gauss)
Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
在同一conda环境下先装Pytroch后装TensorFlow
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
Node memory overflow and V8 garbage collection mechanism
Solve NPM -v sudden failure and no response
Network flow learning notes
(二)面扫描仪与机械臂的手眼标定(眼在手外:九点标定)
C managed and unmanaged
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
(一)面扫描仪与机械臂的手眼标定(眼在手上)
QT随手笔记(六)——更新界面、截图、文件对话框
Mo team learning notes (I)
在Blazor 中自定义权限验证