MySQL dual master replication, that is, mutual master-slave (only one master provides write operations), you can implement the database server hot standby, but a master down after the failure to achieve dynamic switching. And keepalived through the virtual IP, realize the dual-master external unified interface and automatic check, failure switching mechanism. In combination, a high-availability scenario for MySQL databases can be implemented.
Environment Description:
OS: CentOS6.5_x64
master:192.168.196.133
backup:192.168.196.134
vip:192.168.196.135 Operating system time consistent change:
# date-s "20170707 01:51:42"
# Hwclock--SYSTOHC
1 According to the previous article, set up a dual master standby machine
2 Installing keepalived
[email protected] ~]# Yum install-y pcre-devel openssl-devel popt-devel #安装依赖包
[Email protected] ~]# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
[Email protected] ~]# tar zxvf keepalived-1.2.7.tar.gz
[Email protected] ~]# CD keepalived-1.2.7
[Email protected] ~]#./configure--prefix=/usr/local/keepalived
[[email protected] ~]#make && make install
3 Configuring keepalived as a system service
[Email protected] ~]# cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
[Email protected] ~]# cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
[Email protected] ~]# mkdir/etc/keepalived/
[Email protected] ~]# cp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/
[Email protected] ~]# cp/usr/local/keepalived/sbin/keepalived/usr/sbin/
4 Modifying a configuration file[email protected] ~]# vi/etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.loc failover@firewall.loc Sysadmin@firewall.loc} notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 Smtp_co Nnect_timeout router_id Mysql_ha #标识, dual master same}vrrp_instance vi_1 {state BACKUP #两台都设置BACKUP int Erface eth1 virtual_router_id #主备相同 priority #优先级, backup set up Advert_int 1 nopreempt #不主动抢占资源, only in the master this priority setting, backup does not set authentication {auth_type PASS auth_pass 1111} virtual_ipaddress {192.168.196.135}}virtual_server 192.168.196.135 3306 {delay_loop 2 #lb_algo RR #LVS算法, we shut down #lb_kind NAT #LVS模式, if not, the standby server cannot connect to the main MySQL nat_mask 255.255.255.0 persistence_t by VIP Imeout protocol TCP real_server 192.168.196.133 3306 {#检测本地mysql, backup also to write detect local MySQL weight 3 Notify_down/usr/locaL/keepalived/mysql.sh #当mysq服down时, execute this script, kill keepalived implementation switch Tcp_check {connect_timeout 3 #连接超 Nb_get_retry 3 #重试次数 delay_before_retry 3 #重试间隔时间}}}[email protected] ~]# vi/usr/local/keepalived/mysql.sh
#!/bin/bash Pkill keepalived[email protected] ~]# chmod +x/usr/local/keepalived/mysql.sh
[[email protected] ~]#/etc/init.d/keepalived start
#backup服务器只修改priority为90, Nopreempt not set, real_server set local IP.
5 authorize two MySQL servers to allow root login to log on to other servers!
Mysql>grant all privileges on * * to ' root ' @ '% ' identified by ' root '; Mysql>flush privileges
6. Test High Availability
1, through the MySQL client through the VIP connection, see if the connection is successful.
2, stop the master this MySQL service, whether it can switch to the past, you can use the IP addr command to see the VIP on which server.
3, can see the/var/log/messges log, see the main standby switching process
4, master server failure recovery, whether proactively seize resources, become Active server.
MySQL High Availability Keepalived+mysql (dual Master Hot Spares)