mysql双主+keepalive实现双机热备
配置mysql互为主从:
1、环境配置:
A:192.168.137.200 mysql账户root,密码wang123,两台相同
B:192.168.137.201
安装mysql5.8确保可用,关闭防火墙、selinux
2、修改两台主机的配置文件:(修改内容类似)
vi /etc/my.cnf
[mysqld]
basedir = /opt/mysql
datadir = /opt/mysql/data
port = 3306
server_id = 1 此id不可相同,另一台写2即可
auto_increment_offset = 1 #自增长ID,另一台主机该参数为2
auto_increment_increment = 2 #自增长ID的步长,有两台就设为2,保证不冲突
socket = /opt/mysql/data/mysql.sock
log-bin = mysql-bin
max_binlog_size=1024M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
重启mysql服务
3、mysql互相授权并配置:
A:192.168.137.200 给B主机创建授权用户,并查看本地数据库master的状态
grant replication slave on *.* to 'root'@'192.168.137.201' identified by 'wang123'; 给B主机root用户授权
flush privileges; 刷新一下授权
show master status; 查看本地数据库master的状态,重点记录File和Position的值
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 421 | | | |
+------------------+----------+--------------+------------------+-------------------+
B:192.168.137.201 和A主机做相同的操作
grant replication slave on *.* to 'root'@'192.168.137.200' identified by 'wang123';
flush privileges;
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 421 | | | |
+------------------+----------+--------------+------------------+-------------------+
A:192.168.137.200 设置B主机为本机的master,并启动slave模式,查看slave状态
change master to master_host='192.168.137.201',master_port=3306,master_user='root',master_password='wang123',master_log_file='mysql-bin.000001',master_log_pos=421; 设置自己的master信息
start slave; 开启slave
show slave status\G; 查看本机slave状态,主要查看以下内容
Slave_IO_Running: Yes IO线程,为Yes
Slave_SQL_Running: Yes SQL线程,为Yes
B:192.168.137.201 设置参见A主机
change master to master_host='192.168.137.201',master_port=3306,master_user='root',master_password='wang123',master_log_file='mysql-bin.000001',master_log_pos=421;
start slave;
show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
至此A主机和B主机的互为主从已配置完毕
两台主机安装keepalive实现高可用
1、安装keepalive
mkdir /opt/keepalive 创建安装目录
./configure --prefix=/opt/keepalive 进入安装目录,执行
make &&make install
2、前期配置
cp /opt/keepalive/sbin/keepalived /usr/bin/
mkdir /etc/keepalived/
cp /opt/keepalive/etc/keepalived/keepalived.conf /etc/keepalived/
cp /opt/keepalive/etc/sysconfig/keepalived /etc/sysconfig/
cp 源码包位置/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
修改配置文件:(根据本次试验情况,配置文件如下)
A主机:
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { #全局配置部分
notification_email { #有关邮件发送,本次不做设置
}
smtp_server 192.168.200.1
smtp_connect_timeout 30
}
vrrp_script check_mysql { #监测脚本部分
script "/opt/keepalive/scripts/check_mysql.sh" #所执行脚本的位置,或直接写命令
interval 5 #执行脚本的频率,单位秒
weight -20 #当脚本返回错误值时改变权重的值(正数为加,负数为减)
}
vrrp_instance mysql_check { #项目实例
state BACKUP #初始状态,其实会根据两台主机的权重值来决定谁为master
#nopreempt #非抢占模式,处于非抢占模式,除非keepalived挂掉,否则VIP不漂移
interface eth0 #实例绑定的网卡,vip在该网卡上绑定
virtual_router_id 90 #设置VRID,相同的id为一个组,决定多播的MAC地址最后的数字
priority 100 #本节点的初始优先级,高的那个为master
advert_int 1 #检查时间间隔,默认为1秒
authentication { #设置认证
auth_type PASS #认证方式,这里设置的是密码认证
auth_pass 1111 #认证的密码
}
virtual_ipaddress { #VIP地址
192.168.137.250
}
track_script { #需要运行的监测脚本
check_mysql
}
#notify_master "脚本路径" #当角色变为master时执行的脚本
#notify_backup "脚本路径" #当角色变为backup时执行的脚本
}
vi /opt/keepalive/scripts/check_mysql.sh #编写监测脚本
#!/bin/bash
netstat -anptu|grep mysql
a=`echo $?`
echo $a
if [ $a == 0 ];then
exit 0
else
exit 1
fi
B主机:
priority 90 配置文件和A主机大致相同,将优先级的初始值设成90即可
3、开启keepalive服务,测试vip是否正常生成,关闭mysql或keepalived看vip是否转移
ip addr ## 查看是否生成vip
4、若出现脑裂情况(两台主机均有VIP生成),需首先排查防火墙,若防火墙开启状态需考虑是否允许vrrp协议通信,可添加条目
-A INPUT -p vrrp -j ACCEPT