MySQL雙主-高可用
資源規劃
主機名 os 版本 MySQL 版本 主機 IP MySQL VIP
db01.lyk.com centos 6.4 mysql-5.6.21-linux-glibc2.5-x86_64 172.31.30.12 172.31.30.222
db02.lyk.com centos 6.4 mysql-5.6.21-linux-glibc2.5-x86_64 172.31.30.11
3.修改MySQL配置文件
修改DB01的配置文件:
vi /etc/my.cnf
#在[mysqld]添加如下內容#
server-id = 100
log-bin = /usr/local/mysql/data/ttpai-bin
binlog_format = MIXED #非必需
relay-log = /usr/local/mysql/data/ttpai-relay-bin
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
replicate-wild-ignore-table = mysql.%
replicate-wild-ignore-table = test.%
replicate-wild-ignore-table = information_schema.%
replicate-wild-ignore-table = performance_schema.%
修改DB02的配置文件:
vi /etc/my.cnf
#在[mysqld]添加如下內容#
server-id = 110
log-bin = /usr/local/mysql/data/ttpai-bin
binlog_format = MIXED #非必需
relay-log = /usr/local/mysql/data/ttpai-relay-bin
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
replicate-wild-ignore-table = mysql.%
replicate-wild-ignore-table = test.%
replicate-wild-ignore-table = information_schema.%
replicate-wild-ignore-table = performance_schema.%
4.手動同步數據庫
如果DB01上已經有MySQL數據,那麼執行主主互備之前,需要將DB01和DB02上的兩個MySQL的數據保持同步,首先在DB01上備份MySQL數據,執行如下SQL:
mysql> FLUSH TABLES WITH READ LOCK;
在不退出終端的情況下(推出鎖失效),再開啟一個session,直接打包MySQL的數據文件或者mysqldump工具導出:
cd /usr/local/mysql/
tar zcvf data.tar.gz data/
將data.tar.gz 傳輸到DB02,依次重啟DB01和DB02。
其實,可以在不執行READ LOCK語句,直接使用mysqldump語句備份,最起碼個人測試是數據不會丟失或者說出現同步異常。使用如下命令:
mysqldump --default-character-set=gbk --opt --triggers -R -E --hex-blob --single-transaction --master-data=2 ttpai > ttpai.sql
其中–master-data=2可以鎖定binlog的文件名及坐標。
5.創建授權復制用戶
DB01執行:
mysql> grant REPLICATION SLAVE ON *.* TO lyk@'172.31.30.11' IDENTIFIED BY 'lyk123';
DB02執行:
mysql> grant REPLICATION SLAVE ON *.* TO lyk@'172.31.30.12' IDENTIFIED BY 'lyk123';
其中關於坐標的值,可以執行如下SQL得到:
mysql> show master status;
+------------------+-----------+--------------+--------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+-----------+--------------+--------------------------------------------------+-------------------+
| ttpai-bin.000093 | 502389306 | | mysql,test,information_schema,performance_schema | |
+------------------+-----------+--------------+--------------------------------------------------+-------------------+
如果使用mysqldump 可以使用如下命令:
head -n50 ttpai.sql
6.啟動互為主從的模式
在DB02上執行:
mysql>CHANGE MASTER TO
MASTER_LOG_FILE='ttpai-bin.000050',
MASTER_LOG_POS=754861035,
MASTER_HOST='172.31.30.12',
MASTER_USER='lyk',
MASTER_PASSWORD='lyk123';
mysql>start slave;
在DB01上執行:
mysql>CHANGE MASTER TO
MASTER_LOG_FILE='ttpai-bin.000050',
MASTER_LOG_POS=754861035,
MASTER_HOST='172.31.30.12',
MASTER_USER='lyk',
MASTER_PASSWORD='lyk123';
mysql>start slave;
驗證同步結果:
mysql>show slave statusG;
自此Mysql互為主從已完畢。
7.安裝lvs+keepalived
yum instal keepalived ipvsadm
8.編輯keepalived配置文件
DB01的配置文件:
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_Mysql
}
vrrp_instance HA_1 {
state BACKUP
interface bond0
virtual_router_id 80
priority 100
advert_int 2
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.31.30.222
}
}
virtual_server 172.31.30.222 3306 {
delay_loop 2
lb_algo wlc
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
persistence_timeout 60
real_server 172.31.30.12 3306 {
weight 1
notify_down /etc/keepalived/mysqlcheck/mysql.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 2
delay_before_retry 1
}
}
}
其中需要注意的是:
state BACKUP #DB01和DB02均配置為BACKUP狀態
nopreempt #為不強占模式,DB02(slave)不用設置
notify_down #定義監測realserver失敗下一步的動作
priority #DB02要比DB01低
interface bond0 #沒什麼特別,卻是我的整個搭建最耗時的痛點,稍後解釋
創建notify_down的腳本(DB01和DB02都需創建):
mkdir -p /etc/keepalived/mysqlcheck/
cd /etc/keepalived/mysqlcheck/
vi mysql.sh
#!/bin/sh
pkill keepalived
chmod u+x mysql.sh
其實就是干掉keepalived進程,釋放VIP。
DB02的配置文件:
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_Mysql
}
vrrp_instance HA_1 {
state BACKUP
interface bond0
virtual_router_id 80
priority 90
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.31.30.222
}
}
virtual_server 172.31.30.222 3306 {
delay_loop 2
lb_algo wlc
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
persistence_timeout 60
real_server 172.31.30.11 3306 {
weight 1
notify_down /etc/keepalived/mysqlcheck/mysql.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 2
delay_before_retry 1
}
}
}
DB01和DB02的啟動keepalived:
/etc/init.d/keepalived start
9.測試
測試主從同步,通過VIP連接:
mysql -h172.31.30.222 -utest -ptest ttpai
mysql> show vaiables like 'server_id'; #可以確認那台為master狀態
mysql> create table .... #到slave狀態的數據庫看都是否正常
.....
測試keepalived實現MySQL故障轉移:
可以在DB01執行
/etc/init.d/mysqld stop
遠程一直執行
show vaiables like 'server_id';
發現會卡1~3s,server_id變為110,即master變為DB02。
即使重啟DB01,VIP也不會轉移,因為采用的是不搶占模式,直到DB02的服務掛,才會轉移VIP。
10:總結
來說下interface bond0的事兒:
一切配置都很順,但是在測試的時候,發現VIP連接數據庫,執行SQL時卡時不卡,在windows下ping VIP或者VIP所在的DB的主機IP,丟包;有沒有蛋碎的感覺。開始一頭霧水,後來仔細又看了一邊lvs DR模式的原理,原來都和MAC地址相關聯,而自己的環境網卡做了bond0(mode=0),卻在交換機上沒做端口聚合,可以認為網卡MAC發生紊亂。
解決:
配置交換機(H3C)
#創建端口聚合組(不同型號,命令不同,叫法不一樣)
interface Bridge-Aggregation2
description bonding
port access vlan 30
interface Bridge-Aggregation3
description bonding
port access vlan 30
#將做bonding的網卡加入對應的聚合組
interface GigabitEthernet1/0/3
port access vlan 30
port link-aggregation group 3
......
一切恢復正常,發生這個事件,不知是自己太低端,還是大家都沒遇到,搜資料真沒收到,呵呵。
注意:
LVS DR原理
bonding 模式的注意點