Use keepalived to build a high-availability environment for MySQL.
First build MySQL master-slave replication
On master, open Binlog, create a copy account,
Then enter the command in the slave
- Change Master to
- Master_host= ' 192.168.1.70 ',
- master_port=3306,
- Master_user= ' xx ',
- master_password= ' xx ';
Then use start slave to turn on replication.
Then compile and install keepalived
Enter the keepalived-1.2.12 directory
Then use
./configure
Make && make install
Then edit the keepalived configuration file on the master server
Vim/etc/keepalived/keepalived.conf
global_defs {router_id ha_mysql}vrrp_instance vi_1 {State BACKUP interface eth0 virtual_router_id Wuyi Priority -Advert_int1nopreempt Authentication {auth_type PASS auth_pass1111} virtual_ipaddress {192.168.1.199}}virtual_server192.168.1.199 3306{Delay_loop2Lb_algo wrr lb_kind DR persistence_timeout -protocol TCP Real_server192.168.1.70 3306{weight3Notify_down/root/shutdown.SHTcp_check {connect_timeoutTenNb_get_retry3Delay_before_retry3Connect_port3306 } }}
Then edit the slave configuration file
Vim/etc/keepalived/keepalived.conf
global_defs {router_id ha_mysql}vrrp_instance vi_1 {State BACKUP interface eth0 virtual_router_id
Wuyi Priority -Advert_int1nopreempt Authentication {auth_type PASS auth_pass1111} virtual_ipaddress {192.168.1.199}}virtual_server192.168.1.199 3306{Delay_loop2Lb_algo wrr lb_kind DR persistence_timeout -protocol TCP Real_server192.168.1.80 3306{weight3Notify_down/root/shutdown.SHTcp_check {connect_timeoutTenNb_get_retry3Delay_before_retry3Connect_port3306 } }}
which
Priority indicates precedence
Virtual_ipaddress Virtual IP address (VIP)
Delay_loop Check real_server status every 2 seconds
Notify_down script executed after service down is detected
Connect_timeout Connection Timeout time
Nb_get_retry number of re-connect
Delay_before_retry re-connect interval time
Connect_port Health Check Port
Shutdown.sh can consider the ability to add email alerts.
- #!/bin/bash
- Pkill keepalived
Start MySQL and keepalived services on two servers
Service MySQL Start
Service keepalived Start
Master's server_id is 1
Slave's server_id is 2.
Then connect to the VIP MySQL, you can see already connected to the master server (server_id 1)
If Kill Master's mysql,keepalived is automatically transferred to slave
Execute on master server
Killall mysqld
Then check server_id again,
After a brief loss of connectivity, the VIP,SERVER_ID has changed to 2, indicating that the VIP has pointed to the slave
The Nopreempt parameter indicates whether the VIP continues to point to master after the master is restored to normal
This will trigger the switchover again.
The keepalived of the two servers will have heartbeat detection,
If master's MySQL service hangs (3306 port hangs), then he will choose to commit suicide.
Slave's keepalived detects the condition through heartbeat detection and takes over the VIP request.
Keepalived still have a lot of parameters, not knowing what it means.
Production environment switch script, after slave promotion to master, should wait for all the relay log application complete, otherwise may lose the data
Using keepalived to build a MySQL high-availability environment