Keepalived + MySQL双主配置方案
Keepalived + MySQL双主配置方案
- 系统环境以及软件版本
主机名 |
操作系统版本 |
Keepalived版本 |
Mysql版本 |
IP地址 |
Master1 |
REDHAT6.5 |
2.0.0 |
5.6.19 |
172.16.1.1(内网ip) |
10.10.10.1(数据传输ip) |
||||
Master2 |
REDHAT6.5 |
2.0.0 |
5.6.19 |
172.16.1.2(内网ip) |
10.10.10.2(数据传输ip) |
- MySQL配置
MySQL主主复制结构区别于主从复制结构。在主主复制结构中,两台服务器的任何一台上面的数据库存发生了改变都会同步到另一台服务器上,这样两台服务器互为主从,并且都能向外提供服务。
2.1 在master1 上的配置
basedir = /usr/local/mysql5.6.19
datadir = /usr/local/mysql5.6.19/data
socket = /tmp/mysql.sock
pid-file = /usr/local/mysql5.6.19/data/mysql.pid
bind-address = 0.0.0.0
log_bin = /usr/local/mysql5.6.19/mysql-bin.log #开启二进制日志
server-id = 1 #任意自然数n,只要保证两台MySQL主机不重复即可
#replicate-do-db = test #要同步的数据库,默认所有库 (这里注释掉,同步所有库)
binlog-ignore-db = mysql #忽略mysql库
binlog-ignore-db = information_schema #忽略information_schema库
auto-increment-increment = 2 #步进值,有n台主MySQL就填n
auto-increment-offset = 1 #起始值。第n台主MySQL,此时为第1主MySQL
2.2 在 master2 上的配置
basedir = /usr/local/mysql5.6.19
datadir = /usr/local/mysql5.6.19/data
socket = /tmp/mysql.sock
pid-file = /usr/local/mysql5.6.19/data/mysql.pid
bind-address = 0.0.0.0
log_bin = /usr/local/mysql5.6.19/mysql-bin.log #开启二进制日志
server-id = 2 #任意自然数n,只要保证两台MySQL主机不重复即可
#replicate-do-db = test #要同步的数据库,默认所有库
binlog-ignore-db = mysql #忽略mysql库
binlog-ignore-db = information_schema #忽略information_schema库
auto-increment-increment = 2 #步进值,有n台主MySQL就填n
auto-increment-offset = 2 #起始值。第n台主MySQL,此时为第1主MySQL
注:二都只有server-id不同和 auto-increment- offset不同
auto-increment-offset是用来设定数据库中自动增长的起点的,回为这两能服务器都设定了一次自动增长值2,所以它们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突
replicate-do-db 指定同步的数据库,我们只在两台服务器间同步test数据库
另:auto-increment-increment的值应设为整个结构中服务器的总数,本案例用到两台服务器,所以值设为2
2.3 相互授权用户(在A服务器授权一个允许B访问的用户,反之亦然)
在服务器master1(172.16.1.1)上
mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'10.10.10.2' IDENTIFIED BY PASSWORD '123456';
mysql> flush privileges;
在服务器master2(172.16.1.2)上
mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'10.10.10.1' IDENTIFIED BY PASSWORD '123456';
mysql> flush privileges;
2.4 互告bin-log信息
在服务器master1
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000006 | 106 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
在服务器master2
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000008 | 192 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
在服务器master1上执行
mysql> change master to master_host='10.10.10.2',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=192;
在服务器master2上执行
mysql> change master to master_host='10.10.10.1',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=106;
2.5 在两服务器都执行以下命令
mysql> start slave;
2.6查看状态
show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
标红这两个必须是Yes
- Keepalived 配置
Master1配置信息
! Configuration File for keepalived
global_defs {
notification_email {
test@163.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
Vrrp_script check_mysql {
Script “/usr/local/keepalived-2.0.0/etc/keepalived/check_mysql.sh”
Interval 10
Weight 5
}
vrrp_instance VI_1 {
state BACKUP
interface eth0 #虚拟ip工作在哪个接口
virtual_router_id 51
priority 100 #优先级
advert_int 1
Nopreempt #不抢占资源
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.100/24 dev eth0 label eth0:0
}
Track_script {
Check_mysql #调用上面的函数
}
Notify_master “/usr/local/keepalived-2.0.0/etc/keepalived/notify.sh master 192.168.67.153”
Notify_backup “/usr/local/keepalived-2.0.0/etc/keepalived/notify.sh backup 192.168.67.153”
Notify_fault “/usr/local/keepalived-2.0.0/etc/keepalived/notify.sh fault 192.168.67.153”
}
virtual_server 172.16.1.100 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.10.10.1 3306 {
weight 1
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306
retry 3
}
}
}
Master2 配置
! Configuration File for keepalived
global_defs {
notification_email {
zhanglei_py@163.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.100/24 dev eth0 label eth0:0
}
}
virtual_server 172.16.1.100 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.10.10.2 3306 {
weight 1
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306
retry 3
}
}
}
备注:监测脚本
Notify.sh
#!/bin/bash
#
contact="zhanglei_py@163.com"
Usage(){
echo "Usage: `basename $0`{master|backup|fault} VIP"
}
Notify(){
subject="`hostname` state changed to $1"
mailbody="`date "+%F %T"`: `hostname` state change to $1,$VIP floating."
echo $mailbody |mail -s "$subject" $contact
echo $mailbody >>/var/log/keepalived.log
}
[ $# -lt 2 ] && Usage && exit
VIP=$2
case $1 in
master)
Notify master
;;
backup)
Notify backup
;;
fault)
Notify fault
;;
*)
Usage
exit 1
;;
Esac
Check_mysql.sh
#!/bin/bash
check_mysql_status(){
status=`netstat -tnlp|grep 3306`
if [ $? != 0 ]
then
echo "down" >>/var/log/keepalived.log
Service keepalived stop
fi
}
main(){
check_mysql_status
}
main