Description: Requires 3 nodes, master node (202.207.178.6), slave node (202.207.178.7), test node (202.207.178.8), virtual address: 202.207.178.4. Configure the MySQL database master-slave replication, and use keepalived to achieve high availability, can implement the primary database down, from the database available!
MySQL master-slave copy see my blog http://10927734.blog.51cto.com/10917734/1868323
Premise:
Because it is a highly available service, you first configure some of the basic conditions for a highly available service implementation
1) The node name must match the execution result of the Uname-n command
Node1:
# hostname Node1
# vim/etc/sysconfig/network
Hostname=node1
Node2:
# hostname Node2
# vim/etc/sysconfig/network
Hostname=node2
2) Communication between nodes must be trusted via SSH
[[email protected] ~]# ssh-keygen-t rsa-f ~/.ssh/id_rsa-p '
[Email protected] ~]# ssh-copy-id-i ssh/id_rsa.pub [email protected]
[[email protected] ~]# ssh-keygen-t rsa-f ~/.ssh/id_rsa-p '
[Email protected] ~]# ssh-copy-id-i ssh/id_rsa.pub [email protected]
3) Time between nodes in the cluster must be synchronized
Synchronizing time with NTP server
Ntpdate IP (host address with NTP service configured)
4) Configure local resolution:
[Email protected] ~]# vim/etc/hosts
202.207.178.6 Node1
202.207.178.7 Node2
[Email protected] ~]# scp/etc/hosts node2:/etc/
First, install keepalived, achieve high availability
On the master node:
1, installation keepalived
# Tar XF keepalived-1.2.1.tar.gz
# CD keepalived-1.2.1
#./configure--with-kernel-dir=/usr/src/kernels/2.6.32-642.11.1.el6.i686/
# Make && make install
Attention:
You may need to install this package:
# yum Install Popt-devel
/usr/src/kernels/found to be empty, need to install kernel source code
# yum Install Kernel-devel
2, Configuration keepalived
1) Set a variable
# dir=/usr/local/
2) Execute the following statement to place the keepalived configuration file in/etc in the relevant directory
# CP $DIR/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
# CP $DIR/etc/sysconfig/keepalived/etc/sysconfig/&& mkdir-p
/etc/keepalived
# CP $DIR/sbin/keepalived/usr/sbin/
3) Provide configuration file for keepalived
# cd/etc/keepalived/
# Vim keepalived.conf (add the following content)
! Configuration File for Keepalived
Global_defs {
Notification_email {
[Email protected]
}
Notification_email_from [email protected]
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
router_id Lvs_devel
}
# VIP1
Vrrp_instance Vi_1 {
State MASTER
Interface eth0
Lvs_sync_daemon_inteface eth0
VIRTUAL_ROUTER_ID 151
Priority 100
Advert_int 5
#非抢占 nopreempt
Authentication {
Auth_type PASS
Auth_pass 2222
}
virtual_ipaddress {
202.207.178.4
}
}
Virtual_server 202.207.178.4 3306 {
Delay_loop 6
Lb_algo WRR
Lb_kind DR
Persistence_timeout 60
Protocol TCP
Real_server 202.207.178.6 3306 {
Weight 100
notify_down/data/sh/mysql.sh
Tcp_check {
Connect_timeout 10
Nb_get_retry 3
Delay_before_retry 3
Connect_port 3306
}
}
}
4) Start the service
#/etc/init.d/keepalived Restart
# Ps-ef | grep keepalived
At this point the Discovery service has started!
# TAIL-FN 100/var/log/messages
At this time to roll the screen, is missing a module, the following load this module
# modprobe Ip_vs
#/etc/init.d/keepalived Restart
# TAIL-FN 100/var/log/messages
Everything's fine now.
On the slave node:
Configure the same master node, just modify the following items in the configuration file:
State BACKUP
Priority 90
Real_server 202.207.178.7 3306
Second, configure the relevant operation, and test
1, on the master-slave node to add the following two files and authorization, to achieve on one side of the database down to stop keepalived, master-slave role to achieve the switch
# vim/data/sh/mysql.sh
/etc/init.d/keepalived stop
# chmod +x/data/sh/nginx.sh
2, the database authorization on the master and slave nodes respectively
Mysql>grant all on fsy.* to [e-mail protected] ' 202.207.178.% ' identified by ' 123456 ';
3. You can start testing now
1) Restart a virtual machine to connect to the database with this virtual machine
# mysql-uroot-h202.207.178.4-p
2) Stop the MySQL database at the master node, and you will find that the database of the test node is still connected properly!
So far, MySQL high availability has been achieved using keepalived!
Welcome to criticize and correct!
This article is from the "10917734" blog, please be sure to keep this source http://10927734.blog.51cto.com/10917734/1875658
Keepalived for MySQL high availability