Project Environment:
VIP 192.168.122.100
MYSQL1 192.168.122.10
MYSQL2 192.168.122.20
First, MySQL master sync (do not use shared storage, data saving local storage)
1. MYSQL1 192.168.122.10:
[Email protected] ~]# VIM/ETC/MY.CNF
Log-bin=mysql-bin
Server-id = 4
[[Email protected] ~]# service mysqld start
[[email protected] ~]# MySQL
mysql> Reset Master;
mysql> grant replication Slave, replication Client on *. *
-To [e-mail protected] '% ' identified by ' 123 ';
mysql> flush Privileges;
Mysql> Change Master to
-Master_host= '192.168.122.20‘,
-master_user= ' admin ',
-master_password= ' 123 ',
-master_log_file= ' mysql-bin.000001 ',//based on actual personal modifications
master_log_pos=0; According to individual
Query OK, 0 rows affected (0.02 sec)
Mysql>Show Slave Status\g
2. Mysql2192.168.122.20:
[Email protected] ~]# VIM/ETC/MY.CNF
Log-bin=mysql-bin
Server-id = 251
[[Email protected] ~]# service mysqld start
[[email protected] ~]# MySQL
mysql> Reset Master;
mysql> grant replication Slave, replication Client on *. *
-To [e-mail protected] '% ' identified by ' 123 ';
mysql> flush Privileges;
Mysql> Change Master to
-Master_host= '192.168.122.10‘,
-master_user= ' admin ',
-master_password= ' 123 ',
Master_log_file= ' mysql-bin.000001 ',
master_log_pos=0;
Query OK, 0 rows affected (0.02 sec)
Mysql>Show Slave Status\g
Second, installation keepalived
[Email protected] ~]# yum-y install ipvsadm kernel-headers kernel-devel openssl-devel popt-devel
Method One:
Source Installation keepalived
[Email protected] ~]# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
[Email protected] ~]# tar zxvf keepalived-1.2.2.tar.gz
[Email protected] ~]# CD keepalived-1.2.2
[[email protected] ~]#./configure --prefix=/ //install to the root to prevent the directory from being too deep
[[email protected] ~]# make
[[email protected] ~]# make install
Method Two:
[Email protected] ~]# yum-y install keepalived
Three, keepalived main preparation configuration file
192.168.122.10 master Configuration
[Email protected] ~]# vim/etc/keepalived/keepalived.conf
=====================================================================
! Configuration File for Keepalived
Global_defs {
router_idMySQL1
}
Vrrp_scriptCheck_run{
Script "/root/keepalived_check_mysql.sh"
Interval 5
}
Vrrp_instance Vi_1 {
StateMASTER
Interface eth0
VIRTUAL_ROUTER_ID 88
Priority -
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass Root
}
Track_script {
Check_run
}
virtual_ipaddress {
192.168.122.100
}
}
192.168.122.20 Slave configuration
[Email protected] ~]# vim/etc/keepalived/keepalived.conf
=====================================================================
! Configuration File for Keepalived
Global_defs {
router_idMYSQL2
}
Vrrp_scriptCheck_run{
Script "/root/keepalived_check_mysql.sh"
Interval 5
}
Vrrp_instance Vi_1 {
StateBACKUP
Interface eth0
VIRTUAL_ROUTER_ID 88
Priority50
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass Root
}
Track_script {
Check_run
}
virtual_ipaddress {
192.168.122.100
}
}
1. Note the space
2. Log to see if the script is executed
[Email protected] ~]# tail-f/var/log/messages
June 15:20:19 Xen1 keepalived_vrrp[6341]: Using linkwatch kernel netlink reflector ...
June 15:20:19 Xen1 keepalived_vrrp[6341]: VRRP sockpool: [IfIndex (2), Proto (FDS), FD (11,12)]
June 15:20:19 Xen1 keepalived_vrrp[6341]: vrrp_script (Check_run) succeeded
Iv. MySQL status detection script/root/bin/keepalived_check_mysql.sh
[Email protected] ~]# vim/root/keepalived_check_mysql.sh
#!/bin/bash
Mysql=/usr/local/mysql/bin/mysql
Mysql_host=localhost
Mysql_user=root//username connected to MySQL
Mysql_password=root//password to connect to MySQL
Check_time=3
#mysql is working mysql_ok are 1, MySQL down MYSQL_OK is 0
Mysql_ok=1
Check_mysql_helth () {
$MYSQL-H $MYSQL _host-u $MYSQL _user-p${mysql_password}-e "show status;" &>/dev/null
If [$?-eq 0]; then
Mysql_ok=1
Else
Mysql_ok=0
Fi
return $MYSQL _OK
}
While [$CHECK _time-ne 0]
Do
Check_mysql_helth
If [$MYSQL _ok-eq 1]; Then
Exit 0
Fi
Let check_time--
Sleep 1
Done
/etc/init.d/keepalived stop
Exit 1
[Email protected] ~]# chmod 755/root/keepalived_check_mysql.sh
Both sides are started keepalived
[[email protected] ~]#/etc/init.d/keepalived start
[[email protected] ~]#/etc/init.d/keepalived start
[Email protected] ~]# chkconfig--add keepalived
[Email protected] ~]# chkconfig keepalived on
V. Testing and diagnosis
Note The IP address used for communication between keepalived using VRRP multicast is 224.0.0.18
Keepalived+mysql for High Availability