一:环境介绍
master1:10.124.151.20 安装mysql和keepalived
master2:10.124.151.22 安装mysql和keepalived
VIP:10.124.151.23
mysql版本:5.7.9
OS版本:CentOS 6.5
keepalived版本:1.2.20
二:安装过程
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
auto_increment_increment = 2 #这两个参数必须要有
auto_increment_offset = 1 #(两台机器一个1 一个2,避免出现主键冲突)
[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;
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
两种方法:
1.
./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/ && make && make install
chkconfig --add keepalived
chkconfig keepalived on
2../configure prefix=/usr/local/keepalived
则:
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
或:
mkdir /etc/keepalived;
ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
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
[root@zhufu keepalived-1.2.20]# 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成功。