1. 程式人生 > >MySQL高可用之雙主複製模式

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
雙主模式之雙主半同步複製模式
存在的風險: