High Availability of MySQL: Keepalived + Mysql

Source: Internet
Author: User
Environment Description: OS: CentOS6.5 _ X64MASTER: 192.168.0.202BACKUP: 192.168.0.203VIP: 192.168.0.2041 # Yuminstallmysql-servermy

Environment Description: OS: CentOS6.5 _ X64MASTER: 192.168.0.202BACKUP: 192.168.0.203VIP: 192.168.0.2041 # Yuminstallmysql-servermy

Environment Description:

OS: CentOS6.5 _ X64

MASTER: 192.168.0.202

BACKUP: 192.168.0.203

VIP: 192.168.0.204

1. Configure two Mysql master-master Synchronization

[Root @ master ~] # Yum install mysql-server mysql-y [root @ master ~] # Service mysqld start [root @ master ~] # Mysqladmin-u root password 123.com[ root @ master ~] # Vi/etc/my. cnf # enable binary log and Set id [mysqld] log-bin = mysql-binserver-id = 1 # backup this setting 2 [root @ master ~] # Service mysqld restart

# First view the log bin log and pos Value Location

The master configuration is as follows:

[Root @ master ~] # Mysql-u root-p123.commysql> grant replication slave on *. * TO 'replicase' @ '192. 168.0.% 'identified BY 'replicase'; mysql> flush privileges; mysql> change master to-> master_host = '2017. 168.0.203 ',-> master_user = 'replicase',-> master_password = 'replicase',-> master_log_file = 'mysql-bin.000002',-> master_log_pos = 106; # The value of the peer status is mysql> start slave; # start synchronization.

The backup configuration is as follows:

[Root @ backup ~] # Mysql-u root-p123.commysql> grant replication slave on *. * TO 'replicase' @ '192. 168.0.% 'identified BY 'replicase'; mysql> flush privileges; mysql> change master to-> master_host = '2017. 168.0.202 ',-> master_user = 'replicase',-> master_password = 'replicase',-> master_log_file = 'mysql-bin.000002',-> master_log_pos = 106; mysql> start slave;

2. Configure keepalived for Hot Backup

[Root @ backup ~] # Yum install-y pcre-devel openssl-devel popt-devel # install the dependency package

[Root @ master ~] # Wget [root @ master ~] # Tar zxvf keepalived-1.2.7.tar.gz [root @ master ~] # Cd keepalived-1.2.7 [root @ master ~] #./Configure -- prefix =/usr/local/keepalivedmake & make install

# Configure keepalived as a system service

[Root @ master ~] # Cp/usr/local/keepalived/etc/rc. d/init. d/keepalived/etc/init. d/[root @ master ~] # Cp/usr/local/keepalived/etc/sysconfig/[root @ master ~] # Mkdir/etc/keepalived/[root @ master ~] # Cp/usr/local/keepalived/etc/keepalived. conf/etc/keepalived/[root @ master ~] # Cp/usr/local/keepalived/sbin/keepalived/usr/sbin/[root @ master ~] # Vi/etc/keepalived. conf! Configuration File detail {icationication_email {test@sina.com} notification_email_fromadmin@test.comsmtp _ server detail _ connect_timeout 30router_id MYSQL_MASTER # backup server settings MYSQL_BACKUP} vrrp_instance VI_1 {state BACKUP # Both sets BACKUP interface eth0 virtual_router_id 51 # Master backup is the same priority 100 # priority, backup is set to 90 advert_int 1 nopreempt # it does not take the initiative to seize resources, only in the master priority settings, backup is not set to authentication {Auth_type PASS auth_pass 1111} virtual_ipaddress {192.168.0.204} virtual_server 192.168.0.204 3306 {delay_loop 2 lb_algo rr # LVS algorithm lb_kind DR # LVS mode persistence_timeout 50 # the connection from the same IP address is allocated same Real Server protocol TCP real_server 192.168.0.202 3306 {# detect local mysql, backup also needs to write to check local mysq weight 3 notify_down/usr/local/keepalived/mysql. sh # execute this script when the mysq server is down, and kill keepalived to implement switching TCP_CHECK {connect_timeout 3 # connection timeout Nb_get_retry 3 # Number of Retries delay_before_retry 3 # Retry Interval} [root @ master ~] # Vi/usr/local/keepalived/mysql. sh #! /Bin/bashpkill keepalived [root @ master ~] # Chmod + x/usr/local/keepalived/mysql. sh [root @ master ~] #/Etc/init. d/keepalived start


# Authorize two Mysql servers to allow remote root logon:

Mysql> grant all on *. * to 'root' @ '2014. 192.% 'identified by '2014. com ';

Mysql> flush privileges;

3. Test High Availability

1. Use the Mysql client to connect through VIP to check whether the connection is successful.

2. Stop the mysql service on the master node and check whether the service can be switched normally. You can run the ip addr command to check which server the VIP is on.

3. Check the/var/log/messges log to see the Master/Slave switchover process.

4. After the master server recovers from a fault, whether to actively seize resources and become an active server.


This article is from the "Penguin" blog. Please keep this source

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.