keepalived mysql Failure auto-switch script
The MySQL schema is master-slave (master-slave), master failure is automatically switched to slave. Of course, can also be set to double master, but here is a disadvantage: when the main pressure is very large, from the delay is very large, such as backward 2000 seconds, at this time the main hung, from the takeover (VIP drift to from), the user just published the article, at this time because of synchronization delay large, has not copied over, so the user has , when the original master repaired, since the IO and the SQL thread is still open, and will continue to synchronize the data has not been synchronized just now, it is possible to change the user's newly published article, resulting in user data loss.
In view of this situation, I still use the Master-slave (master-slave) architecture.
KeepAlive installation is very simple, no longer verbose here. Mainly look at the configuration files and scripts:
# 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 <STRING>|<QUOTED-STRING> # The script executed after the state changes to master Notify_backup <STRING>|< quoted-string> # Status changed to backup script executed after Notify_fault <STRING>|<QUOTED-STRING> # Scripts executed after the state changes to fault notify_stop <STRING>|<QUOTED-STRING> # VRRP after a stop after execution of the script notify <STRING>|< Quoted-string> # (1) scripts executed after any state change
The following explains the usage of these 4 scripts:
mysql_check.sh (Health Check script, when found MySQL connection is not on, will put the keepalive process off, and switch. )
# 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 (the script executed after the state changes to master) (first determine if synchronous replication is complete, if not, wait 1 minutes, and then skip and stop the synchronous copy process, whether or not it finishes.) (Second, change the permissions and passwords for the business account admin that the front-end program connects to, and record the logs and POS points that are currently switched.) )
# 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 $} ')
relay_master_log_file=$ (mysql-e "show slave status\g" | grep-w relay_master_log_file | awk-f ":" ' {print $} ')
read_master_log_pos=$ (mysql-e "show slave status\g" | grep-w read_master_log_pos | awk-f ":" ' {print $} ')
exec_master_log_pos=$ (mysql-e "show slave status\g" | grep-w exec_master_log_pos | awk-f ":" ' {print $} ')
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 (the script executed after the state changes to backup)
# more Backup.sh
#!/bin/bash
. /root/.bash_profile
MYSQL-E "GRANT all privileges on * * to ' admin ' @ ' percent ' identified by ' 1q2w3e4r '; flush privileges;"
stop.sh (script executed after keepalived stop) (first change the admin password) (second, set the parameters to ensure that no data is lost) (Finally, see if there is a write operation, regardless of whether or not the execution is complete, 1 minutes after the exit. )
# more Stop.sh
#!/bin/bash
. /root/.bash_profile
MYSQL-E "GRANT all privileges on * * to ' admin ' @ ' percent ' 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 $} ')
m_position1=$ (mysql-e "show Master status\g" | awk-f ': '/position/{print $} ')
Sleep 1
m_file2=$ (mysql-e "show Master status\g" | awk-f ': '/file/{print $} ')
m_position2=$ (mysql-e "show Master status\g" | awk-f ': '/position/{print $} ')
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
mysql+keepalived Master-slave switch script to go