1. 程式人生 > >Mysql5.7.22+Keepalived雙主互備高可用集群

Mysql5.7.22+Keepalived雙主互備高可用集群

msu ria 自己的 進行 pos 執行 elif 通知 file

DB1:192.168.254.128
DB2:192.168.254.129
配置前進行校時操作
#安裝ntpdate工具
yum install ntpdate -y
#使用ntpdate校時(後面的是ntp服務器)
ntpdate pool.ntp.org

配置mysql雙主備

安裝數據庫鏈接(在主頁數據庫裏面可以看到)
http://blog.51cto.com/10158955/1926574

DB1修改配置文件(需重啟)
vi /etc/my.cnf
#在[mysqld]添加
server-id=166
#開啟mysql日誌功能
log-bin=mysql-bin
#定義日誌命名格式
relay-log=mysql-relay-bin

#以下table復制過濾
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=performance_schema.%

DB2修改配置文件(需重啟)
vi /etc/my.cnf
#在[mysqld]添加
server-id=168
#開啟mysql日誌功能
log-bin=mysql-bin
#定義日誌命名格式
relay-log=mysql-relay-bin

DB1,DB2分別創建復制帳號
mysql -u root -p
#創建用戶slave_up允許從192.168.254網段登錄

create user ‘slave_cp‘@‘192.168.254.%‘ identified by ‘pass‘;
grant replication slave on . to ‘slave_cp‘@‘192.168.254.%‘;
exit
DB1,DB2分別獲取二進制日誌信息
mysql -u root -p
#對數據庫進行只讀鎖定(防止查看二進制日誌同時有人對數據庫修改操作)
flush tables with read lock;
#查詢主機二進制文件信息
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#解除只讀鎖定
unlock tables;

數據庫裏面的數據一定要相同!!!如果不同就要先做同步數據!

在DB1和DB2上分別設置對方為主服務器!

change master to
master_host=‘192.168.254.128‘ ,
master_user=‘slave_cp‘,
master_password=‘pass‘,
master_log_file=‘mysql-bin.000001‘,
master_log_pos=154;

change master to
master_host=‘192.168.254.129‘ ,
master_user=‘slave_cp‘,
master_password=‘pass‘,
master_log_file=‘mysql-bin.000001‘,
master_log_pos=154;

#啟動slave
start slave;
#分別查看DB1,DB2是否正常工作
DB1:192.168.254.128服務器
show slave status\G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.254.129
Master_User: slave_cp
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

DB2:192.168.254.129服務器
mysql> show slave status\G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.254.128
Master_User: slave_cp
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000011
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

檢驗雙主互備
①通過分別在兩臺服務器上使用show slave status\G,查詢主庫信息以及IO進程、SQL進程工作狀態。若兩臺服務器的查詢結果都為Slave_IO_Running: Yes,Slave_SQL_Running: Yes;則表示當前雙主互備狀態正常。
②在Mysql248數據庫上建庫建表,檢查Mysql249上是否同步正常;然後在Mysql249上建庫建表,檢查Mysql248上是否同步正常。
成功之後退出MySQL
exit

配置keepalived實現MySQL雙主高可用
128服務器上配置
! Configuration File for keepalived
global_defs {
#設置報警通知郵件地址,可以設置多個
notification_email { [email protected]
br/>[email protected]
#設置郵件的發送地址
notification_email_from [email protected]
#設置smtp server的地址,該地址必須是存在的
smtp_server 127.0.0.1
#設置連接smtp server的超時時間
smtp_connect_timeout 30
#運行Keepalived服務器的標識,發郵件時顯示在郵件標題中的信息
router_id mysql_msun
}

檢測haproxy腳本

vrrp_script chk_mysql {
script "/etc/keepalived/check_slave.sh"
interval 2
weight 2
}
#定義VRRP實例,實例名自定義
vrrp_instance mysql_msun {
#指定Keepalived的角色,MASTER主機 BACKUP備份
state BACKUP #此處兩個都設置為BACKUP
#指定HA監測的接口
interface ens32
#虛擬路由標識,這個標識是一個數字(1-255),在一個VRRP實例中主備服務器ID必須一樣
virtual_router_id 68
#優先級,數字越大優先級越高,在一個實例中主服務器優先級要高於備服務器
priority 100 #從服務器99
#設置主備之間同步檢查的時間間隔單位秒
advert_int 1
#設置不搶占模式(DB1設置即可)
nopreempt
#設置驗證類型和密碼
authentication {
#驗證類型有兩種{PASS|HA}
auth_type PASS
#設置驗證密碼,在一個實例中主備密碼保持一樣
auth_pass 1689
}
track_script {
chk_mysql # 執行監控的服務
}
#定義虛擬IP地址,可以有多個,每行一個
virtual_ipaddress {
192.168.254.160
}
}

129服務器上配置
! Configuration File for keepalived
global_defs {
#設置報警通知郵件地址,可以設置多個
notification_email {[email protected]
br/>[email protected]
#設置郵件的發送地址
notification_email_from [email protected]
#設置smtp server的地址,該地址必須是存在的
smtp_server 127.0.0.1
#設置連接smtp server的超時時間
smtp_connect_timeout 30
#運行Keepalived服務器的標識,發郵件時顯示在郵件標題中的信息
router_id mysql_msun
}

檢測haproxy腳本

vrrp_script chk_mysql {
script "/etc/keepalived/check_slave.sh"
interval 2
weight 2
}
#定義VRRP實例,實例名自定義
vrrp_instance mysql_msun {
#指定Keepalived的角色,MASTER主機 BACKUP備份
state BACKUP #此處兩個都設置為BACKUP
#指定HA監測的接口
interface ens32
#虛擬路由標識,這個標識是一個數字(1-255),在一個VRRP實例中主備服務器ID必須一樣
virtual_router_id 68
#優先級,數字越大優先級越高,在一個實例中主服務器優先級要高於備服務器
priority 99 #從服務器99
#設置主備之間同步檢查的時間間隔單位秒
advert_int 1
#設置不搶占模式(DB1設置即可)
#nopreempt
#設置驗證類型和密碼
authentication {
#驗證類型有兩種{PASS|HA}
auth_type PASS
#設置驗證密碼,在一個實例中主備密碼保持一樣
auth_pass 1689
}
track_script {
chk_mysql # 執行監控的服務
}
#定義虛擬IP地址,可以有多個,每行一個
virtual_ipaddress {
192.168.254.160
}
}

創建監控腳本
分別在兩臺服務器上面創建腳本,
chmod +x /etc/keepalived/check_slave.sh
要根據自己的情況修改

/etc/keepalived/check_slave.sh
#!/bin/bash
#This scripts is check for Mysql Slave status

Mysqlbin=/usr/bin/mysql
user=root
pw=‘a123456‘
port=3306
host=127.0.0.1
#最大延時
sbm=120

#Check for $Mysqlbin
if [ ! -f $Mysqlbin ];then
echo ‘Mysqlbin not found,check the variable Mysqlbin‘
exit 99
fi

#Get Mysql Slave Status
IOThread=$Mysqlbin -h $host -P $port -u$user -p$pw -e ‘show slave status\G‘ 2>/dev/null|grep ‘Slave_IO_Running:‘|awk ‘{print $NF}‘
SQLThread=$Mysqlbin -h $host -P $port -u$user -p$pw -e ‘show slave status\G‘ 2>/dev/null|grep ‘Slave_SQL_Running:‘|awk ‘{print $NF}‘
SBM=$Mysqlbin -h $host -P $port -u$user -p$pw -e ‘show slave status\G‘ 2>/dev/null|grep ‘Seconds_Behind_Master:‘|awk ‘{print $NF}‘

#Check if the mysql run
if [[ -z "$IOThread" ]];then
exit 1
fi

#Check if the thread run
if [[ "$IOThread" == "No" || "$SQLThread" == "No" ]];then
exit 1
elif [[ $SBM -ge $sbm ]];then
exit 1
else
exit 0
fi

Mysql5.7.22+Keepalived雙主互備高可用集群