赞
踩
Mysql是一个市占率相当高的关系型数据库,做技术开发基本都会涉及到。那么,保证Mysql的高可用、数据安全性是一个相当重要的问题。这里介绍较为常用的一种基于keepalived的MySQl HA
MySQL的HA离不开其主从复制的技术。主从复制是指一台服务器充当主数据库服务器(master),另一台或多台服务器充当从数据库服务器(slave),从服务器(slave)自动向主服务器(master)同步数据。实现MySQL的HA,需使两台服务器互为主从关系。
Keepalived是基于VRRP(Virtual Router Redundancy Protocol,虚拟路由器冗余协议)协议的一款高可用软件。Keepailived有一台主服务器(master)和多台备份服务器(backup),在主服务器和备份服务器上面部署相同的服务配置,使用一个虚拟IP地址对外提供服务,当主服务器出现故障时,虚拟IP地址会自动漂移到备份服务器。
hadoop102 | hadoop103 | hadoop104 |
---|---|---|
MySQL(master) | MySQL(slave) |
[mysqld]
#开启binlog
log_bin = mysql-bin
#binlog日志类型
binlog_format = row
#MySQL服务器唯一id
server_id = 1
[zhouchen@hadoop103 ~]$ sudo service mysql restart
mysql>show master status;
[mysqld]
#MySQL服务器唯一id
server_id = 2
#开启slave中继日志
relay_log=mysql-relay
[zhouchen@hadoop104 ~]$ sudo service mysql restart
执行以下命令
mysql>
CHANGE MASTER TO
MASTER_HOST='hadoop203',
MASTER_USER='root',
MASTER_PASSWORD='zhou59420',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=120;
mysql> start slave;
mysql>show slave status \G;
hadoop102 | hadoop103 | hadoop104 |
---|---|---|
MySQL(master,slave) | MySQL(slave,master) |
[mysqld]
#开启binlog
log_bin = mysql-bin
#binlog日志类型
binlog_format = row
#MySQL服务器唯一id
server_id = 2
#开启slave中继日志
relay_log=mysql-relay
[zhouchen@hadoop104 ~]$ sudo service mysql restart
mysql>show master status;
[mysqld]
#MySQL服务器唯一id
server_id = 1
#开启binlog
log_bin = mysql-bin
#binlog日志类型
binlog_format = row
#开启slave中继日志
relay_log=mysql-relay
[zhouchen@hadoop103 ~]$ sudo service mysql restart
mysql>
CHANGE MASTER TO
MASTER_HOST='hadoop204',
MASTER_USER='root',
MASTER_PASSWORD='zhou59420',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=120;
mysql> start slave;
mysql>show slave status \G;
须在hadoop103,hadoop104两台节点上部署Keepalived
sudo yum install -y keepalived
! Configuration File for keepalived global_defs { router_id MySQL-ha } vrrp_instance VI_1 { state master #初始状态 interface eth0 #网卡 virtual_router_id 51 #虚拟路由id priority 100 #优先级 advert_int 1 #Keepalived心跳间隔 nopreempt #只在高优先级配置,原master恢复之后不重新上位 authentication { auth_type PASS #认证相关 auth_pass 1111 } virtual_ipaddress { 192.168.1.100 #虚拟ip } } #声明虚拟服务器 virtual_server 192.168.1.100 3306 { delay_loop 6 persistence_timeout 30 protocol TCP #声明真实服务器 real_server 192.168.139.134 3306 { notify_down /var/lib/mysql/killkeepalived.sh #真实服务故障后调用脚本 TCP_CHECK { connect_timeout 3 #超时时间 nb_get_retry 1 #重试次数 delay_before_retry 1 #重试时间间隔 } } }
#! /bin/bash
sudo service keepalived stop
sudo chmod +x /var/lib/mysql/killkeepalived.sh
sudo service keepalived start
sudo chkconfig keepalived on
sudo vim /etc/init.d/mysql
sudo vim /etc/init.d/keepalived
a. 修改/etc/init.d/mysql
sudo vim /etc/init.d/mysql
b.重新设置mysql开机自启
sudo chkconfig --del mysql
sudo chkconfig --add mysql
sudo chkconfig mysql on
c.修改/etc/init.d/keepalived
sudo vim /etc/init.d/keepalived
d.重新设置keepalived开机自启
sudo chkconfig --del keepalived
sudo chkconfig --add keepalived
sudo chkconfig keepalivedon
sudo yum install -y keepalived
! Configuration File for keepalived global_defs { router_id MySQL-ha } vrrp_instance VI_1 { state master #初始状态 interface eth0 #网卡 virtual_router_id 51 #虚拟路由id priority 100 #优先级 advert_int 1 #Keepalived心跳间隔 authentication { auth_type PASS #认证相关 auth_pass 1111 } virtual_ipaddress { 192.168.1.100 #虚拟ip } } #声明虚拟服务器 virtual_server 192.168.1.100 3306 { delay_loop 6 persistence_timeout 30 protocol TCP #声明真实服务器 real_server 192.168.1.104 3306 { notify_down /var/lib/mysql/killkeepalived.sh #真实服务故障后调用脚本 TCP_CHECK { connect_timeout 3 #超时时间 nb_get_retry 1 #重试次数 delay_before_retry 1 #重试时间间隔 } } }
#! /bin/bash
sudo service keepalived stop
sudo chmod +x /var/lib/mysql/killkeepalived.sh
sudo service keepalived start
sudo chkconfig keepalived on
sudo vim /etc/init.d/mysql
sudo vim /etc/init.d/keepalived
a.修改/etc/init.d/mysql
sudo vim /etc/init.d/mysql
b.重新设置mysql开机自启
sudo chkconfig --del mysql
sudo chkconfig --add mysql
sudo chkconfig mysql on
c.修改/etc/init.d/keepalived
sudo vim /etc/init.d/keepalived
d.重新设置keepalived开机自启
sudo chkconfig --del keepalived
sudo chkconfig --add keepalived
sudo chkconfig keepalivedon
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。