MySQL高可用之双主复制模式
Posted elontian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL高可用之双主复制模式相关的知识,希望对你有一定的参考价值。
mysql双主模式高可用实现
生产案例:
VIP:10.105.98.211
MASTER
HOSTNAME IPADDR PORT
my-prod01.oracle.com 192.168.10.97 3306
my-prod02.oracle.com 192.168.10.5 3306
SLAVE
HOSTNAME IPADDR PORT
my-em01.oracle.com 10.100.10.10.65 3306
两个主库之间复制模式:半同步复制 主库从库之间复制模式:异步复制
keepalived配置:
[[email protected]]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id MySQL-MDS-HA
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
smtp_alert
virtual_router_id 46
priority 100
advert_int 1
# preempt
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 192.168.10.97
unicast_peer {
192.168.10.5
}
virtual_ipaddress {
10.105.98.211/16 #VIP
}
}
virtual_server 10.105.98.211 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.10.97 3306 {
weight 3
notify_down "/etc/keepalived/shutdown_keepalived.sh"
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
[[email protected] ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id MySQL-MDS-HA
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
smtp_alert
virtual_router_id 46
priority 80
advert_int 1
# preempt
unicast_src_ip 192.168.10.5
unicast_peer {
192.168.10.97
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.105.98.211/16 #VIP
}
}
virtual_server 10.105.98.211 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.10.5 3306 {
weight 3
notify_down "/etc/keepalived/shutdown_keepalived.sh"
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
[[email protected] ~]#cat /etc/keepalived/shutdown_keepalived.sh
systemctl stop keepalived
半同步超时时间1秒
[email protected](none) 05:55:09>show variables like ‘%semi%‘;
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 1000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+------------------------------------+-------+
6 rows in set (0.00 sec)
[email protected](none) 05:55:16> show global status like ‘%semi%‘;
+--------------------------------------------+-------------+
| Variable_name | Value |
+--------------------------------------------+-------------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 1309 |
| Rpl_semi_sync_master_net_wait_time | 99361626284 |
| Rpl_semi_sync_master_net_waits | 75893320 |
| Rpl_semi_sync_master_no_times | 6 |
| Rpl_semi_sync_master_no_tx | 620 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 1420 |
| Rpl_semi_sync_master_tx_wait_time | 86802817655 |
| Rpl_semi_sync_master_tx_waits | 61111551 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 237 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 61234280 |
| Rpl_semi_sync_slave_status | ON |
+--------------------------------------------+-------------+
15 rows in set (0.00 sec)
优缺点:
双主模式之双主异步复制模式
存在的风险:
假定主库1的keepalived优先级高,在主库1自增字段到120,但是因为延迟,主库2只同步到100,这时候主库1挂了,vip漂移到主库2上,
因为主库2的自增字段只到了100,所以id可以从101-120被使用,此时主库1重新启动之后,主库1上101-120的数据就因为id冲突,
不能同步到主库2。
1:keepalived 配置state BACKUP模式,避免角色竞争;配置优先级priority
2:该模式需要配置auto_increment_increment auto_increment_offset
auto_increment_offset表示自增长字段从哪个数开始,取值范围是1 .. 65535
auto_increment_increment表示自增长字段每次递增的量,其默认值是1,取值范围是1 .. 65535
在主主同步配置时,需要将两台服务器的auto_increment_increment增长量都配置为2,而要把auto_increment_offset分别配置为1和2.
这样才可以避免两台服务器同时做更新时自增长字段的值之间发生冲突。
https://m.aliyun.com/jiaocheng/1121889.html
双主模式之双主半同步复制模式
存在的风险:
以上是关于MySQL高可用之双主复制模式的主要内容,如果未能解决你的问题,请参考以下文章