Using keepalived to build a MySQL high-availability environment

Source: Internet
Author: User



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

    1. Change Master to
    2. Master_host= ' 192.168.1.70 ',
    3. master_port=3306,
    4. Master_user= ' xx ',
    5. 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.

    1. #!/bin/bash
    2. 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

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.