时间:2021-07-01 10:21:17 帮助过:32人阅读
公司大牛让用keepalived构建高可用的MySQL,在网上看到一篇很完整的文章,于是乎照着前辈的足迹,加上自己补充。如上述均正确配置
前言:公司大牛让用keepalived构建高可用的MySQL,在网上看到一篇很完整的文章,于是乎照着前辈的足迹,加上自己补充。
环境拓扑如下:
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 {
cclo@cer.cn
}
notification_email_from cclo@cer.cn
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健康检查程序是否会触发我们编写的脚本