Use KeepAlived to build a MySQL high-availability environment

Source: Internet
Author: User
Tags haproxy

Use KeepAlived to build a MySQL high-availability environment

Use KeepAlived to build a high-availability MySQL environment.
First, set up master-slave replication for MySQL.
Enable binlog on the Master node and create a replication account,
Then enter the command in Slave.
 

Change master
Master_host = '1970. 168.1.70 ',
Master_port = 3306,
Master_user = 'XX ',
Master_password = 'xx ';
Start slave to enable replication.
 
Compile and install KeepAlived
Go to keepalived-1.2.12 directory
Then use
./Configure
Make & make install
 
Then, edit the KeepAlived configuration file on the Master server.
Vim/etc/keepalived. conf
 

! Configuration File for keepalived


Global_defs {
 
Router_id HA_MySQL
}


Vrrp_instance VI_1 {
State BACKUP
Interface eth0
Virtual_router_id 51
Priority100
Advert_int 1
Nopreempt
Authentication {
Auth_type PASS
Auth_pass 1111
}
Virtual_ipaddress {
192.168.1.199
}
}

 
Virtual_server 192.168.1.199 3306 {
Delay_loop 2
Lb_algo wrr
Lb_kind DR
Persistence_timeout 60
Protocol TCP
Real_server 192.168.1.70 3306 {
Weight 3
Notify_down/root/shutdown. sh
TCP_CHECK {
Connect_timeout 10
Nb_get_retry 3
Delay_before_retry 3
Connect_port 3306
}
}
}
Then edit the Slave configuration file.
Vim/etc/keepalived. conf
 
! Configuration File for keepalived
Global_defs {
Router_id HA_MySQL
}

 
Vrrp_instance VI_1 {
State BACKUP
Interface eth0
Virtual_router_id 51
Priority 90
Advert_int 1
Nopreempt
Authentication {
Auth_type PASS
Auth_pass 1111
}
Virtual_ipaddress {
192.168.1.199
}
}

 
Virtual_server 192.168.1.199 3306 {
Delay_loop 2
Lb_algo wrr
Lb_kind DR
Persistence_timeout 60
Protocol TCP
Real_server 192.168.1.80 3306 {
Weight 3
Notify_down/root/shutdown. sh
TCP_CHECK {
Connect_timeout 10
Nb_get_retry 3
Delay_before_retry 3
Connect_port 3306
}
}
}
Where
Priority indicates priority
Virtual_ipaddress: virtual IP address (VIP)
Delay_loop checks the real_server status every 2 seconds.
Notify_down indicates the script executed after the service is down.
Connect_timeout connection timeout
Nb_get_retry reconnection times
Delay_before_retry reconnection Interval
Connect_port health check Port
 
Shutdown. sh you can consider adding the mail alarm function.
 

#! /Bin/bash
Pkill keepalived
 
Start MySQL and KeepAlived services on two servers
Service mysql start
Service keepalived start
 
The server_id of the Master is 1.
The server_id of Slave is 2.
 
Then connect to the VIP MySQL, and you can see that it has been connected to the Master server (server_id is 1)

If you kill the Master's MySQL, KeepAlived will be automatically transferred to the Slave
 
Run
Killall mysqld
 
View server_id again,
After a transient disconnection, the VIP is connected again, and the server_id has changed to 2, indicating that the VIP has been directed to the Slave
 

The nopreempt parameter indicates whether to direct the VIP to the Master after the Master recovers to normal.
In this case, the switchover is triggered again.
 
The KeepAlived of the two servers has a heartbeat check,
If the MySQL Service of the Master node is down (Port 3306 is down), the Master node will choose to commit suicide.
The KeepAlived of Slave detects this situation through heartbeat detection and takes over VIP requests.
 

KeepAlived has many other parameters that do not understand what it means.
The production environment switch script. After the Slave is upgraded to the Master, wait until all the relay logs are applied. Otherwise, data may be lost.

Haproxy + Keepalived + Apache configuration notes in CentOS 6.3

Haproxy + KeepAlived WEB Cluster on CentOS 6

Keepalived + Haproxy configure high-availability Load Balancing

Haproxy + Keepalived build high-availability Load Balancing

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.