Keepalived+MySQL双主配置实践

整理了近期在项目上做的一些技术研究,希望与大家共同探讨交流。

一:环境介绍

master1:10.124.151.20 
master2:10.124.151.22 
VIP:10.124.151.23 

OS 版本:CentOS 6.5 
mysql 版本:5.7.9
keepalived 版本:1.2.20

二:安装 mysql 和 keepalived

1.安装 mysql

配置文件:(两台服务器的 server_id 必须不同)
注意以下参数是简化过后的:

[mysqld] datadir=/usr/local/mysql/data socket=/tmp/mysql.sock user=mysql
symbolic-links=0
server_id=3
gtid_mode=ON enforce_gtid_consistency=ON master_info_repository=TABLE relay_log_info_repository=TABLE binlog_checksum=NONE log_slave_updates=ON log_bin=binlog binlog_format=ROW
#relay-log = /tmp/mysql-relay #relay-log-index = /tmp/relay-index
=2# =1#1
[mysqld_safe] log-error=/var/log/mysqld.log pid-file=/tmp/mysqld.pid

重启 mysql 服务器

2.搭建主主

1)创建复制用户(两台服务器都要创建)

grant replication slave,replication client on *.* to 'repl'@'10.124.151.%' identified by '123123';
flush privileges;
auto_increment_increment
auto_increment_offset

这两个参数必须要有(两台机器一个1一个2,避免出现主键冲突)

2)将 master1 设置为 master2 的主服务器

如果之前搭建过 replication,执行:

stop slave;
reset slave;

如果没有:
因为设置了 gtid,所以:

change master to MASTER_HOST='10.124.151.20', MASTER_USER='repl', MASTER_PASSWORD='123123', MASTER_PORT=3306,
master_auto_position=1;

开启复制

start slave;

检查复制状态

show slave status;

都为 yes,则复制成功。

3)将 master2 设置为 master1 的主服务器

如果之前搭建过 replication,执行:

stop slave;
reset slave;

如果没有:
因为设置了 gtid,所以:

change master to MASTER_HOST='10.124.151.22', MASTER_USER='repl', MASTER_PASSWORD='123123', MASTER_PORT=3306,
master_auto_position=1;

开启复制

start slave;

检查复制状态

show slave status;

都为 yes,则复制成功。
如果现在任何一台 MySQL 上更新数据都会同步到另一台 MySQL,则 MySQL 双主搭建完成。

3.keepalived 安装配置

1)安装关联包

在编译安装 Keepalived 之前,必须先安装内核开发包 kernel-devel以及 openssl-devel、popt-devel 等支持库

yum install -y kernel-devel openssl-devel popt-devel

也可以用 rpm:

rpm -ivh kernel-devel openssl-devel popt-devel

2)安装软件

使用指定的 linux 内核位置对 keepalived 进行配置,并将安装路径指定为根目录,这样就无需额外创建链接文件了,配置完成后,
依次执行 make、make install 进行安装。

tar –xf keepalived-1.2.20.tar.gz
cd keepalived-1.2.20
./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/ && make && make install
chkconfig --add keepalived chkconfig keepalived on

3)修改配置文件

vim /etc/keepalived/keepalived.conf
master1:
! Configuration File for keepalived global_defs {
router_id mysql-1 }
vrrp_script chk_mysql {
script "/etc/keepalived/bin/mysql.sh" interval 1
fall 1
rise 1
}
vrrp_instance VI_1 { state BACKUP
interface bond0 virtual_router_id 51 priority 100 advert_int 1 nopreempt authentication {
auth_type PASS
auth_pass 1111 }
track_script { chk_mysql
}
virtual_ipaddress {
10.124.151.23 }
}
master2:
! Configuration File for keepalived global_defs {
router_id mysql-2 }

vrrp_script chk_mysql {
script "/etc/keepalived/bin/mysql.sh" interval 1
fall 1
rise 1
}
vrrp_instance VI_1 { state BACKUP
interface bond0 virtual_router_id 51 priority 50 advert_int 1 authentication {
auth_type PASS
auth_pass 1111 }
track_script { chk_mysql
}
virtual_ipaddress {
10.124.151.23 }
}

4)启动keepalived

service keepalived start

5)添加脚本

master1 和 master2 上都添加检测脚本,作用是当 mysql 停止工作时自动关闭本机的 keepalived,从而实现将故障机器踢出(因每台机器上 keepalived 只添加了本机 为 real server).
当 mysqld 正常启动起来后,要手动启动 keepalived 服务。

mkdir /etc/keepalived/bin
cat /etc/keepalived/bin/mysql.sh
chmod +x /etc/keepalived/bin/mysql.sh

4.测试

1)将 master1 的 mysql 服务宕掉:

mysqladmin -u -p -S shutdown

2)查看 master1 和 master2 的 IP:

ip add show dev bond0

3)如果虚拟 IP 切换成功,则 keepalived 成功。

点赞