Use KeepAlived to build a MySQL high-availability environment

Source: Internet
Author: User
Use KeepAlived to build a high-availability MySQL environment. First, set up MySQL Master-Slave replication to enable binlog on the Master, create a replication account, and then enter the command cha on the Slave

Use KeepAlived to build a high-availability MySQL environment. First, set up MySQL Master-Slave replication to enable binlog on the Master, create a replication account, and then enter the command cha on the Slave

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

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.