您好,登錄后才能下訂單哦!
理論介紹:
我們通常說的雙機熱備是指兩臺機器都在運行,但并不是兩臺機器都同時在提供服務。當提供服務的一臺出現故障的時候,另外一臺會馬上自動接管并且提供服務,而且切換的時間非常短。MySQL雙主復制,即互為Master-Slave(只有一個Master提供寫操作),可以實現數據庫服務器的熱備,但是一個Master宕機后不能實現動態切換。使用Keepalived,可以通過虛擬IP,實現雙主對外的統一接口以及自動檢查、失敗切換機制,從而實現MySQL數據庫的高可用方案。
環境說明:
版本 version 5.6.25-log
主1庫ip: 10.219.24.25
主2庫ip:10.219.24.22
vip: 10.219.24.30
os 版本: centos 6.7
已安裝熱備軟件:xtrabackup
防火墻已關
補充:
主從復制原理: http://blog.csdn.NET/zhang123456456/article/details/72972701
MySQL 5.6安裝 :http://blog.csdn.net/zhang123456456/article/details/53608554
xtrabackup 安裝: http://blog.csdn.Net/zhang123456456/article/details/72836184
全程:先搭建一主一從,然后反過來搭建,就是雙主復制,最后配置 Mysql+Keepalived 故障轉移的高可用環境
一、 先一主一從搭建 ,再反向搭建,配置雙主復制
1、 主庫參數調整
-- 停止主庫mysql
[root@mysql02 ~]# service mysql stop
[root@mysql02 ~]# netstat -nltp|grep mysql|grep 3606
-- 主庫創建relay log目錄
[root@mysql02 full]# mkdir -p /data/mysql/relaylog/
[root@mysql02 full]# chown -R mysql:mysql /data/mysql/relaylog
-- 調整 my.cnf 參數
[root@mysql02 ~]# cat /etc/my.cnf
[client]
password = Oracle
port = 3306
socket = /data/mysql/mysql.sock
[mysqld]
server-id=25
port = 3306
socket = /data/mysql/mysql.sock
character_set_server = utf8
character_set_client = utf8
collation-server=utf8_general_ci
lower_case_table_names = 1
max_connections = 1000
datadir = /data/mysql
log_bin = /data/mysql/binarylog/binlog
log_bin_index = /data/mysql/binarylog/binlog
relay-log = /data/mysql/relaylog/relay
relay-log-index = /data/mysql/relaylog/relay
relay_log_purge = on
binlog_format = mixed
innodb_data_file_path = ibdata1:12M:autoextend
auto_increment_increment = 10
auto_increment_offset = 1
[mysql]
default-character-set = utf8
說明:
a、主庫必須配置的參數
server-id (主從的server-id必須不同)、log_bin、binlog_format、relay-log、relay-log-index、relay_log_purge
auto-increment-offset、auto-increment-increment
b、log-slave-updates 意思是,中繼日志執行之后,這些變化是否需要計入自己的binarylog。 當你的B服務器需要作為另外一個服務器的主服務器的時候需要打開。 就是雙主互相備份,或者多主循環備份。 我們這里需要, 所以打開。
c、auto-increment-offset、auto-increment-increment 兩個參數用于在 雙主(多主循環)互相備份。 因為每臺數據庫服務器都可能在同一個表中插入數據,如果表有一個自動增長的主鍵,那么就會在多服務器上出現主鍵沖突。 解決這個問題的辦法就是讓每個數據庫的自增主鍵不連續。 上圖說是, 我假設需要將來可能需要10臺服務器做備份, 所以auto-increment-increment 設為10. 而 auto-increment-offset=1 表示這臺服務器的序號。 從1開始, 不超過auto-increment-increment。這樣做之后, 我在這臺服務器上插入的第一個id就是 1, 第二行的id就是 11了, 而不是2.(同理,在第二臺服務器上插入的第一個id就是2, 第二行就是12, 這個后面再介紹) 這樣就不會出現主鍵沖突了。 后面我們會演示這個id的效果。
-- 啟動主庫
[root@mysql02 ~]# mysqld_safe --defaults-file=/etc/my.cnf &
2、 從庫參數調整
-- 停止從庫mysql
[root@mysql01 ~]# service mysql stop
[root@mysql01 ~]# netstat -nltp|grep mysql|grep 3606
-- 調整 my.cnf 參數
[root@mysql01 ~]# cat /etc/my.cnf
[client]
password = oracle
port = 3306
socket = /data/mysql/mysql.sock
[mysqld]
server-id=22
port = 3306
socket = /data/mysql/mysql.sock
character_set_server = utf8
character_set_client = utf8
collation-server=utf8_general_ci
lower_case_table_names = 1
max_connections = 1000
datadir = /data/mysql
log_bin = /data/mysql/binarylog/binlog
log_bin_index = /data/mysql/binarylog/binlog
relay-log = /data/mysql/relaylog/relay
relay-log-index = /data/mysql/relaylog/relay
relay_log_purge = on
auto_increment_increment = 10
auto_increment_offset = 2
[mysql]
default-character-set = utf8
說明:從庫必須配置的參數
server-id、log_bin、relay-log、relay_log_purge、auto-increment-offset、auto-increment-increment、
3、 主庫備份
-- 主庫備份目錄
[root@mysql02 full]# pwd
/xtrabackup/full
-- 主庫 innobackupex 備份
[root@mysql02 ~]# innobackupex --user=root --password=oracle --port=3606 /xtrabackup/full/
170610 17:50:23 Backup created in directory '/xtrabackup/full/2017-06-10_17-50-19/'
MySQL binlog position: filename 'binlog.000010', position '120'
....
170610 17:50:23 completed OK!
-- 查看備份 binlog編號 與 截止 position
[root@mysql02 2017-06-10_17-50-19]# cat xtrabackup_binlog_info
binlog.000010 120
4、 從庫創建與主庫相同的備份目錄
[root@mysql01 ~]# mkdir -p /xtrabackup/full
[root@mysql01 ~]# chown -R mysql:mysql /xtrabackup/full/
5、 主庫將備份 scp 到從庫
[root@mysql02 full]# pwd
/xtrabackup/full
[root@mysql02 full]# scp -r 2017-06-10_17-50-19 10.219.24.22:/xtrabackup/full
6、 從庫查看scp過來的備份
[root@mysql01 ~]# cd /xtrabackup/full/2017-06-10_17-50-19/
[root@mysql01 2017-06-10_17-50-19]# ll
total 12320
-rw-r-----. 1 root root 419 Jun 10 18:01 backup-my.cnf
-rw-r-----. 1 root root 12582912 Jun 10 18:01 ibdata1
drwxr-x---. 2 root root 4096 Jun 10 18:01 mysql
drwxr-x---. 2 root root 4096 Jun 10 18:01 performance_schema
drwxr-x---. 2 root root 4096 Jun 10 18:01 test
-rw-r-----. 1 root root 18 Jun 10 18:01 xtrabackup_binlog_info
-rw-r-----. 1 root root 113 Jun 10 18:01 xtrabackup_checkpoints
-rw-r-----. 1 root root 482 Jun 10 18:01 xtrabackup_info
-rw-r-----. 1 root root 2560 Jun 10 18:01 xtrabackup_logfile
7、 主庫創建同步用戶
mysql> GRANT replication slave ON *.* TO 'slave25'@'%' IDENTIFIED BY 'oracle';
Query OK, 0 rows affected (0.05 sec)
8、 從庫恢復主庫數據
-- 從庫將原有datadir文件夾重命名到新位置,并創建原文件夾
[root@mysql01 ~]# mv /data/mysql /data/mysqlbak
[root@mysql01 ~]# mkdir -p /data/mysql
-- innobackupex apply-log
[root@mysql01 ~]# innobackupex --apply-log --user=oracle \
--password=oracle --port=3606 /xtrabackup/full/2017-06-10_17-50-19/
-- innobackupex copy 恢復的文件到原來的數據位置
[root@mysql01 mysql]# innobackupex --defaults-file=/etc/my.cnf --user=root \
--copy-back /xtrabackup/full/2017-06-10_17-50-19/
170610 18:25:11 completed OK!
-- 創建binlog目錄與 relaylog 目錄并賦權
[root@mysql01 ~]# mkdir -p /data/mysql/binarylog
[root@mysql01 ~]# mkdir -p /data/mysql/relaylog/
[root@mysql01 mysql]# chown -R mysql:mysql /data/mysql
9、 從庫配置與檢測
-- 從庫啟動
[root@mysql01 mysql]# mysqld_safe --defaults-file=/etc/my.cnf &
-- 從庫指定與主庫同步的基本信息
mysql>
change master to
master_host='10.219.24.25',
master_port=3306,
master_user='slave25',
master_password='oracle',
master_log_file='binlog.000010',
master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
參數解釋:
MASTER_HOST : 設置要連接的主服務器的ip地址
MASTER_USER : 設置要連接的主服務器的用戶名
MASTER_PASSWORD : 設置要連接的主服務器的密碼
MASTER_LOG_FILE : 設置要連接的主服務器的bin日志的日志名稱
MASTER_LOG_POS : 設置要連接的主服務器的bin日志的記錄位置
-- 啟動slave 狀態(開始監聽msater的變化)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
-- 查看slave的狀態.
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.219.24.25 #主庫 IP
Master_User: slave25 # 主庫復制的用戶
Master_Port: 3306 # 主庫 mysqld
Connect_Retry: 60
Master_Log_File: binlog.000010 #io_thread 讀取主庫 master_log_file
Read_Master_Log_Pos: 717 # io_thread 讀取主庫 master_log_pos
Relay_Log_File: relay.000002
Relay_Log_Pos: 877
Relay_Master_Log_File: binlog.000010 #sql_thread 執行主庫的 master_log_file
Slave_IO_Running: Yes # 關鍵的,io_thread 是否 running
Slave_SQL_Running: Yes # 關鍵的,sql_thread 是否 running
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 717 #sql_thread 執行主庫的 master_log_pos
Relay_Log_Space: 1040
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0 # 從庫 的延遲
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 25
Master_UUID: 29d68531-4cf9-11e7-8e1f-000c297c4100
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified
10、 主從同步檢查
-- 主庫
mysql> create database repl;
Query OK, 1 row affected (0.00 sec)
mysql> use repl
Database changed
mysql> create table repl (id int);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into repl values(1);
Query OK, 1 row affected (0.00 sec)
-- 從庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| binarylog |
| mysql |
| performance_schema |
| relaylog |
| repl |
| test |
+--------------------+
7 rows in set (0.00 sec)
mysql> use repl
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from repl;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec) >一主一從同步成功!
##################################################
#至此A到B的復制已經配置完成,下面配置從B到A的復制。#
##################################################
聲明> 下面操作中 新主庫即為原從庫(10.219.24.22) 新從庫為原主庫(10.219.24.25)
11、 新主庫創建同步用戶
mysql> GRANT replication slave ON *.* TO 'slave22'@'%' IDENTIFIED BY 'oracle';
Query OK, 0 rows affected (0.00 sec)
12、 新主庫查看 binlog 文件號與 position 點
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000004 | 313 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
13、 新從庫指定與主庫同步的基本信息
mysql>
change master to
master_host='10.219.24.22',
master_port=3306,
master_user='slave22',
master_password='oracle',
master_log_file='binlog.000004',
master_log_pos=313;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
14、新從庫打開 slave 復制功能
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
15、 新從庫檢測同步復制狀態
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.219.24.22
Master_User: slave22
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000004
Read_Master_Log_Pos: 313
Relay_Log_File: relay.000002
Relay_Log_Pos: 280
Relay_Master_Log_File: binlog.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 313
Relay_Log_Space: 443
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 22
Master_UUID: 70023652-4dc7-11e7-9360-000c2944297a
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified
-- 新從庫測試數據同步狀態
mysql> create database mm_repl;
Query OK, 1 row affected (0.00 sec)
mysql> use mm_repl;
Database changed
mysql> create table mm_repl(id int auto_increment,name varchar(10), primary key(id));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into mm_repl(name) values("andy"),("taoYe");
Query OK, 1 row affected (0.00 sec)
mysql> select * from mm_repl;
+----+-------+
| id | name |
+----+-------+
| 1 | andy |
| 11 | taoYe |
+----+-------+
2 rows in set (0.00 sec)
-- 新主庫測試數據同步狀態
mysql> select * from mm_repl;
+----+-------+
| id | name |
+----+-------+
| 1 | andy |
| 11 | taoYe |
+----+-------+
2 rows in set (0.00 sec)
mysql> insert into mm_repl(name) values("andy"),("taoYe");
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from mm_repl;
+----+-------+
| id | name |
+----+-------+
| 1 | andy |
| 11 | taoYe |
| 12 | andy |
| 22 | taoYe |
+----+-------+
4 rows in set (0.00 sec)
-- 新從庫檢查同步復制
mysql> select * from mm_repl;
+----+-------+
| id | name |
+----+-------+
| 1 | andy |
| 11 | taoYe |
| 12 | andy |
| 22 | taoYe |
+----+-------+
4 rows in set (0.00 sec) >主主同步測試成功
二、 配置Mysql+Keepalived故障轉移的高可用環境
安裝keepalived ( master1 和 master2 兩臺機器上都安裝)
二、1.1 安裝 LVS
前提:已經提前配置好本地 Yum 源 配置過程可參考> http://blog.csdn.NET/zhang123456456/article/details/56690945
1、 檢查一下系統內核
安裝之前,首先檢查一下系統內核是否支持LVS的IPVS模塊;自Linux 2.6開始,系統內核完全內置了LVS的各個模塊。
[root@mysql02 ~]# modprobe -l|grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
....
kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko
看到如上輸出信息,則表明系統內核默認支持IPVS模塊。
2、安裝依賴包
由于是源碼包安裝,所以編譯時需要打上如下依賴包。
# yum -y install kernel-headers glibc-headers glibc-devel libgomp gcc libstdc++-devel gcc-c++
3、 yum安裝 ipvsadm-1.26-4
[root@mysql02 ~]# yum -y install kernel-devel ipvsadm
===================================================================================
Package Arch Version Repository Size
===================================================================================
Installing:
ipvsadm x86_64 1.26-4.el6 c6-media 42 k
Transaction Summary
===================================================================================
Install 1 Package(s)
Total download size: 42 k
Installed size: 78 k
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : ipvsadm-1.26-4.el6.x86_64 1/1
Verifying : ipvsadm-1.26-4.el6.x86_64 1/1
Installed:
ipvsadm.x86_64 0:1.26-4.el6
Complete!
4、 做個 ln
[root@mysql02 ~]# ln -sv /usr/src/kernels/2.6.32-573.el6.x86_64/ /usr/src/linux <--請自行修改 kernels 版本號
5、 查看 ipvsadm 版本信息
[root@mysql02 ~]# ipvsadm -version
ipvsadm v1.26 2008/5/15 (compiled with popt and IPVS v1.2.1) #> 能看到幫助信息,則說明IPVS安裝成功。
二、1.2 安裝 keepalived-1.2.24
1、官網下載地址:
[root@mysql02 ~]# wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz
2、解壓安裝包:
[root@mysql02 ~]# ll keepalived-1.2.24.tar.gz
-rw-r--r--. 1 root root 601873 Jun 16 23:49 keepalived-1.2.24.tar.gz
[root@mysql02 ~]# tar -zxvf keepalived-1.2.24.tar.gz
3、進入安裝目錄:
[root@mysql02 ~]# cd keepalived-1.2.24
4、安裝keepalived:
[root@mysql03 keepalived-1.2.24]# ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-573.el6.x86_64/
說明:--with-kernel-dri :指定使用內核源碼中的頭文件,即include目錄,該參數很重要,只有使用LVS時才用此參數。
[root@mysql03 keepalived-1.2.24]# make && make install
5、復制腳本和文件:
5.1 拷貝執行文件
[root@mysql02 keepalived-1.2.24]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
5.2 將init.d文件拷貝到etc下,加入開機啟動項
[root@mysql02 keepalived-1.2.24]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
5.3 將keepalived文件拷貝到etc下,加入網卡配置
[root@mysql02 keepalived-1.2.24]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
5.4 創建keepalived文件夾
[root@mysql02 keepalived-1.2.24]#mkdir /etc/keepalived
5.5 將keepalived配置文件拷貝到etc下
[root@mysql02 keepalived-1.2.24]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
5.6 復制啟動腳本
[root@mysql02 keepalived-1.2.24]# cp /usr/local/keepalived/sbin/keepalived /etc/keepalived/
6、加入開機啟動項
[root@mysql02 keepalived-1.2.24]# chkconfig --add keepalived #添加時必須保證/etc/init.d/keepalived存在
7、 啟動
[root@mysql02 keepalived-1.2.24]# chmod 755 /etc/init.d/keepalived
[root@mysql03 keepalived-1.2.24]# service keepalived start
Starting keepalived: [ OK ]
8、 關閉
[root@mysql02 keepalived-1.2.24]# service keepalived stop
Stopping keepalived: [ OK ] > OK,成功!
_________________________________________________________________________________________________________
9、 綁定虛擬IP(VIP),只在master1機器上綁定。## 說明,這一步隨便做不做,不影響正常使用,作為了解。
說明:
綁定虛擬IP(VIP),這個只在master1機器上綁定即可。如下配置好keepalived后,當master1機器出現故障時,VIP資源就會被master2機器自動接管!(這一步其實可以不用這么直接在外部使用ifconfig綁定,VIP就直接在 Keepalived 的配置文件里配置就好,使用命令 ip addr 就能看出 vip;只有LVS+Keepalived 四層負載均衡的高可用環境中, vip是要在外面單獨設置的(即ifconfig eth0:0 ....的方式創建vip),通過 ifconfig 可以查看出來 vip。)
[root@mysql02 keepalived-1.2.24]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:7C:41:00
inet addr:10.219.24.25 Bcast:10.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::20c:29ff:fe7c:4100/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12156 errors:0 dropped:0 overruns:0 frame:0
TX packets:2700 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1389340 (1.3 MiB) TX bytes:901117 (879.9 KiB)
[root@mysql02 keepalived-1.2.24]# ifconfig eth0:0 10.219.24.30 broadcast 10.255.255.255 netmask 255.0.0.0 up
[root@mysql02 keepalived-1.2.24]# route add -host 10.219.24.30 dev eth0:0
[root@mysql02 keepalived-1.2.24]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:7C:41:00
inet addr:10.219.24.25 Bcast:10.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::20c:29ff:fe7c:4100/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12397 errors:0 dropped:0 overruns:0 frame:0
TX packets:2748 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1405326 (1.3 MiB) TX bytes:906805 (885.5 KiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:7C:41:00
inet addr:10.219.24.30 Bcast:10.255.255.255 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
補充:
刪除虛擬ip命令:ip addr del 10.219.24.30 dev eth0 以及 route del -host 10.219.24.30 dev eth0:0
_____________________________________________________________________________________________________________________
9、 master1 機器上的keepalived.conf配置。(下面配置中沒有使用lvs的負載均衡功能,所以不需要配置虛擬服務器virtual server)
[root@mysql02 keepalived-1.2.24]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
-- 清空默認內容
[root@mysql02 keepalived-1.2.24]# > /etc/keepalived/keepalived.conf
-- 采用如下配置
[root@mysql02 keepalived-1.2.24]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id MYSQL-MASTER-HA
}
vrrp_script chk_mysql_port {
script "/data/chk_mysql.sh" # mysql 健康檢查腳本
interval 2 #腳本執行間隔,每2s檢測一次
}
vrrp_instance VI_2 {
state BACKUP
interface eth0 #指定虛擬ip的網卡接口
virtual_router_id 54 #路由器標識,MASTER和BACKUP必須是一致的
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.219.24.30 # vip
}
track_script {
chk_mysql_port
}
}
10、 master2機器上的keepalived配置。
master2機器上的keepalived.conf文件只修改priority為90、nopreempt不設置、real_server設置本地IP。
[root@mysql01 keepalived-1.2.24]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@mysql01 keepalived-1.2.24]# >/etc/keepalived/keepalived.conf
[root@mysql01 keepalived-1.2.24]# vim /etc/keepalived/keepalived.conf
[root@mysql01 keepalived-1.2.24]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id MYSQL-MASTER-HA
}
vrrp_script chk_mysql_port {
script "/data/chk_mysql.sh"
interval 2
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 54
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.219.24.30
}
track_script {
chk_mysql_port
}
}
11、 編寫切換腳本。 (master1 與 master2 都操作)
KeepAlived做心跳檢測,如果Master的MySQL服務掛了(3306端口掛了),那么它就會選擇自殺。Slave的KeepAlived通過心跳檢測發現這個情況,就會將VIP的請求接管。
-- 檢查nc能否正常使用
[root@mysql02 keepalived-1.2.24]# nc
-bash: nc: command not found
-- 未安裝nc包 ,yum安裝
[root@mysql02 keepalived-1.2.24]# yum -y install nc
[root@mysql02 keepalived-1.2.24]# vim /data/chk_mysql.sh
[root@mysql02 ~]# cat /data/chk_mysql.sh
#!/bin/bash
# check mysql server status
# mysql端口
PORTS="3306"
function check_ports {
for port in $PORTS;do
nc -z 127.0.0.1 $port | grep -q succeeded
[ "${PIPESTATUS[1]}" -eq 0 ] && mark=${mark}1
done
# 如果mark值為空說明端口都不通。
# 如果mark等于1,說明有端口是通的。
echo $mark
}
ret1=$(check_ports)
# 如果mysql端口不通,會嘗試重啟一次mysql
if [ "$ret1" != 1 ];then
service mysql stop
mysqld_safe --defaults-file=/etc/my.cnf &
q! #無用命令,只是為了跳出上面的命令
sleep 1
ret2=$(check_ports)
# 如果還是有端口不通,表示mysql服務不正常,則停掉keepalived,使VIP發生切換
[ "$ret2" != 1 ] && /etc/init.d/keepalived stop
fi
12、 腳本賦權
[root@mysql02 keepalived-1.2.24]# chmod 755 /data/chk_mysql.sh
13、 master1 與 master2 開啟 keepalived 進程
-- master1
[root@mysql02 keepalived-1.2.24]# service keepalived start
-- master2
[root@mysql01 keepalived-1.2.24]# service keepalived start
14、 測試
14.1 查看 vip 情況 ,發現 vip 只在 master1 上, tcpdump 抓包,發現只有 master1 10.219.24.25 多播包 這也符合 VRRP 工作機制
-- master1
[root@mysql02 keepalived-1.2.24]# ip a l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:7c:41:00 brd ff:ff:ff:ff:ff:ff
inet 10.219.24.25/8 brd 10.255.255.255 scope global eth0
inet 10.219.24.30/32 scope global eth0
inet6 fe80::20c:29ff:fe7c:4100/64 scope link
valid_lft forever preferred_lft forever
[root@mysql02 keepalived-1.2.24]# tcpdump -n 'host 224.0.0.18'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
00:58:35.883861 IP 10.219.24.25 > 224.0.0.18: VRRPv2, Advertisement, vrid 54, prio 100, authtype simple, intvl 1s, length 20
00:58:36.885543 IP 10.219.24.25 > 224.0.0.18: VRRPv2, Advertisement, vrid 54, prio 100, authtype simple, intvl 1s, length 20
-- master2
[root@mysql01 keepalived-1.2.24]# ip a l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:44:29:7a brd ff:ff:ff:ff:ff:ff
inet 10.219.24.22/8 brd 10.255.255.255 scope global eth0
inet6 fe80::20c:29ff:fe44:297a/64 scope link
valid_lft forever preferred_lft forever
14.2 master1 中 keepalived 關閉 , 發現 vip 漂到了 master2 上
-- master1
[root@mysql02 keepalived-1.2.24]# service keepalived stop
Stopping keepalived: [ OK ]
-- master2
[root@mysql01 keepalived-1.2.24]# ip a l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:44:29:7a brd ff:ff:ff:ff:ff:ff
inet 10.219.24.22/8 brd 10.255.255.255 scope global eth0
inet 10.219.24.30/32 scope global eth0
inet6 fe80::20c:29ff:fe44:297a/64 scope link
valid_lft forever preferred_lft forever
14.3 再將之前關閉的 keepalived 開啟,發現 vip 未漂回來, 正好符合主備為 非搶占式。
[root@mysql02 keepalived-1.2.24]# service keepalived start
Starting keepalived: [ OK ]
[root@mysql02 keepalived-1.2.24]# ip a l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:7c:41:00 brd ff:ff:ff:ff:ff:ff
inet 10.219.24.25/8 brd 10.255.255.255 scope global eth0
inet6 fe80::20c:29ff:fe7c:4100/64 scope link
valid_lft forever preferred_lft forever
14.4 將 master1 上的 mysql 關閉, 發現 master1 上的 keepalived 進程也被腳本 kill 了, 順利的完成了 vip 漂回 master2 上
-- master1
[root@mysql02 keepalived-1.2.24]# ip a l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:7c:41:00 brd ff:ff:ff:ff:ff:ff
inet 10.219.24.25/8 brd 10.255.255.255 scope global eth0
inet6 fe80::20c:29ff:fe7c:4100/64 scope link
valid_lft forever preferred_lft forever
-- master2
[root@mysql01 keepalived-1.2.24]# ip a l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:44:29:7a brd ff:ff:ff:ff:ff:ff
inet 10.219.24.22/8 brd 10.255.255.255 scope global eth0
inet 10.219.24.30/32 scope global eth0
inet6 fe80::20c:29ff:fe44:297a/64 scope link
valid_lft forever preferred_lft forever
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。