利用keepalived构建高可用的MySQL
前言:公司大牛让用keepalived构建高可用的MySQL,在网上看到一篇很完整的文章,于是乎照着前辈的足迹,加上自己补充,生成此篇blog
环境拓扑如下:
MySQL-VIP:192.168.1.150
MySQL-master1:192.168.1.151
MySQL-master2:192.168.1.152
OS版本:redhat 5.4
MySQL版本:5.0.77
Keepalived版本:1.1.20
一、MySQL master-master的安装及配置
1、安装mysql
#yum install mysql-server -y \\为了节省时间,这里两台server都直接yum装,希望你配好了yum源。
2、修改配置文件
On server1:
#vim /etc/my.cnf
[mysqld]
server-id = 10
log-bin = mysql-bin
replicate-do-db = ccledb
auto-increment-increment = 2
auto-increment-offset = 1
On server2 :
#vim /etc/my.cnf
[mysqld]
server-id = 20
log-bin = mysql-bin
replicate-do-db = ccledb
auto-increment-increment = 2
auto-increment-offset = 2
3、建立授权用户
On server1:
mysql> grant replication client,replication slave on *.* to cclo1@’192.168.1.%’ identified by ‘123456’;
On server2:
mysql> grant replication client,replication slave on *.* to cclo2@’192.168.1.%’ identified by ‘123456’;
4、指定主服务器
On server1
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000003 | 374 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> change master to master_host='192.168.1.152',master_user='cclo2',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=374;
Query OK, 0 rows affected (0.05 sec)
On server2
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000003 | 374 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> change master to master_host='192.168.1.151',master_user='cclo1',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=374;
Query OK, 0 rows affected (0.05 sec)
5、开启主主服务
mysql> start slave; \\两台server分别执行
MySQL> show slave status\G \\查看主主状态
Slave_IO_Running: Yes
Slave_SQL_Running: Yes \\如果此2项都为yes,master-master配置即成功
如上述均正确配置,现在在任何一台MySQL上更新数据都会同步到另一台MySQL。
二、keepalived安装及配置
1、
192.168.1.151服务器上keepalived安装及配置
安装keepalived
#tar zxvf keepalived-1.1.20.tar.gz
#cd keepalived-1.1.20
#./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686
\\此处一定确保你的linux中有/usr/src/kernels/2.6.18-164.el5-i686这个目录,如果没有请安装kernel- devel包,建议最好是下载和你的linux系统内核版本相同的devel包安装(一般安装镜像有),此处直接#yum install -y kernel-devel
#make && make install
配置keepalived
此处需新建一个配置文件,默认情况下keepalived启动时会去/etc/keepalived目录下找配置文件
#mkdir /etc/keepalived
#vim /etc/keepalived/keepalived.conf \\配置文件内容如下:
! Configuration File for keepalived
global_defs {
notification_email {
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1 {
state BACKUP #两台配置此处均是BACKUP
interface eth0
virtual_router_id 51
priority 100 #优先级,另一台改为90
advert_int 1
nopreempt #不主动抢占资源,只在优先级高的机器上设置即可,优先级低的机器不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.150
}
}
virtual_server 192.168.1.150 3306 {
delay_loop 2 #每个2秒检查一次real_server状态
lb_algo wrr #LVS算法
lb_kind DR #LVS模式
persistence_timeout 60 #会话保持时间
protocol TCP
real_server 192.168.1.151 3306 {
weight 3
notify_down /usr/local/my/my.sh #检测到服务down后执行的脚本
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
编写检测服务down后所要执行的脚本:
#vim /usr/local/my/my.sh
#!/bin/sh
pkill keepalived
#chmod +x /usr/local/my/my.sh
注:此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项来检查real_server的服务状态,当发现real_server服务故障时,便触发此脚本;我们可以看到,脚本就一个命令,通过pkill keepalived强制杀死keepalived进程,从而实现了MySQL故障自动转移。另外,我们不用担心两个MySQL会同时提供数据更新操作,因为每台MySQL上的keepalived的配置里面只有本机MySQL的IP+VIP,而不是两台MySQL的IP+VIP
启动keepalived:
#/usr/local/keepalived/sbin/keepalived –D
#ps -aux | grep keepalived
测试
找一台局域网PC,然后去ping MySQL的VIP,这时候MySQL的VIP是可以ping的通的
停止MySQL服务,#ps -aux | grep keepalived 看keepalived健康检查程序是否会触发我们编写的脚本
2、
192.168.1.152上keepalived安装及配置
安装keepalived
#tar zxvf keepalived-1.1.20.tar.gz
#cd keepalived-1.1.20
#./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686 \\保证--with-kernel-dir=指定的目录的存在
#make && make install
配置keepalived
这台配置和上面基本一样,但有三个地方不同:优先级为90、无抢占设置、real_server为本机IP
#mkdir /etc/keepalived
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.150
}
}
virtual_server 192.168.1.150 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.1.152 3306 {
weight 3
notify_down /usr/local/my/my.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
编写检测服务down后所要执行的脚本:
#vi /usr/local/my/my.sh
#!/bin/sh
pkill keepalived
#chmod +x /usr/local/my/my.sh
启动keepalived :
#/usr/local/keepalived/sbin/keepalived –D
#ps -aux | grep keepalived
测试:
停止MySQL服务,看keepalived健康检查程序是否会触发我们编写的脚本
三、测试
两台MySQL服务器都要授权允许从远程登录
MySQL> grant all privileges on *.* to cclo@'192.168.1.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MySQL> flush privileges;
Query OK, 0 rows affected (0.00 sec)
keepalived故障转移测试:
在windows客户端一直去ping VIP,然后关闭192.168.1.151上的keepalived,正常情况下VIP就会切换到192.168.1.202上面去
开启192.168.1.151上的keepalived,关闭192.168.1.152上的keepalived,看是否能自动切换,正常情况下VIP又会属于192.168.1.151
注:keepalived切换速度还是非常块的,整个切换过程只需1-3秒
MySQL故障转移测试:
在192.168.1.151上关闭MySQL服务,看VIP是否会切换到192.168.1.152上
开启192.168.1.151上的MySQL和keepalived,然后关闭192.168.1.152上的MySQL,看VIP是否会切换到192.168.1.151上
如果都没问题,到此整个配置即已完成。