Transfer from http://biancheng.dnbcw.info/mysql/381020.html
Keepalived+mysql Automatic Switching
Network structure:
VIP 192.168.88.200
Mysq11 192.168.88.130 Main
MYSQL2 192.168.88.131 Preparation
One MySQL Master master sync
Two mounting keepalived
Three keepalived primary and standby configuration files
Quad MySQL status detection script/root/keepalived_check_mysql.sh
Five firewall settings
Six-Start keepalived
Seven View VRRP communication record
Eight View virtual IP
Nine Test
One MySQL Master Master sync (slightly)
Description: The database testa and Testb are on MYSQL1 and MYSQL2 respectively, TestDB is obtained through primary master replication, facilitating subsequent testing
Two mounting keepalived
1. Package http://www.keepalived.org
After installing keepalived, download keepalived-1.2.2.tar.gz from the official website
# tar XVF keepalived-1.2.2.tar.gz
# CD keepalived-1.2.2
#./configure
# Make && make install
2. View Keepalived Location
[[email protected] keepalived-1.2.2]# Find/-name "keepalived"
/root/keepalived-1.2.2/keepalived
/root/keepalived-1.2.2/keepalived/etc/keepalived
/root/keepalived-1.2.2/bin/keepalived
/usr/local/sbin/keepalived
/usr/local/etc/rc.d/init.d/keepalived
/usr/local/etc/keepalived
/usr/local/etc/sysconfig/keepalived
3. Copying files
# cp/usr/local/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
# cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/
# mkdir/etc/keepalived
# cp/usr/local/etc/keepalived/keepalived.conf/etc/keepalived/
# cp/usr/local/sbin/keepalived/usr/sbin/
Three keepalived primary and standby configuration files
The main 備置 file has the state nopreempt the priority reference in the different place in the detail function
192.168.88.130 master Configuration
Vim/etc/keepalived/keepalived.conf
! Configuration File for Keepalived
Global_defs {
Notification_email {
[Email protected]
}
Notification_email_from [email protected]
Smtp_connect_timeout 3
Smtp_server 127.0.0.1
router_id mysql-ha
}
Vrrp_script Check_run {
Script "/root/keepalived_check_mysql.sh"
Interval 5
}
Vrrp_sync_group VG1 {
Group {
Vi_1
}
}
Vrrp_instance Vi_1 {
State MASTER
Interface eth0
VIRTUAL_ROUTER_ID 88
Priority 100
Advert_int 1
Nopreempt
Authentication {
Auth_type PASS
Auth_pass skyai1.cublog.cn
}
Track_script {
Check_run
}
virtual_ipaddress {
192.168.88.200
}
}
Copy code 192.168.88.131 Alternate Configuration file
Vim/etc/keepalived/keepalived.conf
! Configuration File for Keepalived
Global_defs {
Notification_email {
[Email protected]
}
Notification_email_from [email protected]
Smtp_connect_timeout 3
Smtp_server 127.0.0.1
router_id mysql-ha
}
Vrrp_script Check_run {
Script "/root/keepalived_check_mysql.sh"
Interval 5
}
Vrrp_sync_group VG1 {
Group {
Vi_1
}
}
Vrrp_instance Vi_1 {
State BACKUP
Interface eth0
VIRTUAL_ROUTER_ID 88
Priority 99
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass skyai1.cublog.cn
}
Track_script {
Check_run
}
virtual_ipaddress {
192.168.88.200
}
}
Quad MySQL status detection script/root/keepalived_check_mysql.sh
# vim/root/keepalived_check_mysql.sh
#!/bin/bash
Mysql=/usr/local/webserver/mysql/bin/mysql
Mysql_host=localhost
Mysql_user=root
Mysql_password=mysql
Check_time=3
#mysql is working mysql_ok are 1, MySQL down MYSQL_OK is 0
Mysql_ok=1
function Check_mysql_helth () {
$MYSQL-H $MYSQL _host-u $MYSQL _user-p${mysql_password}-e "show status;" >/dev/null 2>&1
If [$ = 0]; then
Mysql_ok=1
Else
Mysql_ok=0
Fi
return $MYSQL _OK
}
While [$CHECK _time-ne 0]
Do
Let "check_time-= 1"
Check_mysql_helth
if [$MYSQL _OK = 1]; Then
Check_time=0
Exit 0
Fi
If [$MYSQL _ok-eq 0] && [$CHECK _time-eq 0]
Then
/etc/init.d/keepalived stop
Exit 1
Fi
Sleep 1
Done
Give Execute permission
# chmod +x/root/keepalived_check_mysql.sh
Five firewall settings
VRRP protocol uses 224.0.0.18 address multicast
Iptables-i rh-firewall-1-input-d 224.0.0.18-j ACCEPT
Six-Start keepalived
# service Keepalived Start
Seven View VRRP communication record
# tcpdump VRRP
Tcpdump:verbose output suppressed, use-v OR-VV for full protocol decode
Listening on eth0, Link-type EN10MB (Ethernet), capture size bytes
11:51:45.632044 IP 192.168.88.130 > Vrrp.mcast.net:VRRPv2, advertisement, Vrid, Prio, authtype simple, intvl 1s , length 20
Eight View virtual IP
# IP A
1:lo: <LOOPBACK,UP,LOWER_UP> MTU 16436 Qdisc noqueue
Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00
inet 127.0.0.1/8 Scope host Lo
INET6:: 1/128 Scope Host
Valid_lft Forever Preferred_lft Forever
2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast Qlen 1000
Link/ether 00:0c:29:bf:c5:77 BRD FF:FF:FF:FF:FF:FF
inet 192.168.88.130/24 BRD 192.168.88.255 Scope Global eth0
inet 192.168.88.200/32 Scope Global eth0
Inet6 FE80::20C:29FF:FEBF:C577/64 Scope link
Valid_lft Forever Preferred_lft Forever
3:sit0: <NOARP> MTU 1480 Qdisc NoOp
Link/sit 0.0.0.0 BRD 0.0.0.0
Nine Test
# mysql-uroot-h192.168.88.200-p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| Information_schema |
| MySQL |
| Test |
| TestA |
| TestDB |
+--------------------+
5 rows in Set (0.00 sec)
Shut down the primary database service, test
# Service Mysqld Stop
# mysql-uuser-h "192.168.88.200"-P
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| Information_schema |
| MySQL |
| Test |
| Testb |
| TestDB |
+--------------------+
5 rows in Set (0.02 sec)
Here, the test is done.
0120Keeplived enables automatic switching of MySQL services