中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Mysql + Keepalived 實現雙主互備高可用詳細配置

發布時間:2020-07-17 17:56:34 來源:網絡 閱讀:568 作者:wx5d78c87dd0584 欄目:編程語言

Mysql+Keepalived雙主互備架構搭架

Mysql + Keepalived 實現雙主互備高可用詳細配置
配置前進行校時操作

#安裝ntpdate工具
yum install ntpdate -y
#使用ntpdate校時(后面的是ntp服務器)
ntpdate pool.ntp.org

一、配置mysql雙主備

0.測試mysql安裝

 yum install mariadb mariadb-server -y
systemctl restart mariadb
systemctl enable mariadb
mysql_secure_installation

1.DB1修改配置文件(需重啟)


vim /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.%

2.DB2修改配置文件(需重啟)


vim /etc/my.cnf
#在[mysqld]添加
server-id=168
#開啟mysql日志功能
log-bin=mysql-bin
#定義日志命名格式 
relay-log=mysql-relay-bin

3.DB1,DB2分別創建復制帳號

mysql -u root -p
#創建用戶slave_up允許從192.168.1網段登錄
create user 'slave_cp'@'192.168.1.%' identified by 'pass';
grant replication slave on *.* to 'slave_cp'@'192.168.1.%';
exit

4.DB1獲取二進制日志信息

mysql -u root -p
#對數據庫進行只讀鎖定(防止查看二進制日志同時有人對數據庫修改操作)
flush tables with read lock;
#查詢主機二進制文件信息
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 494 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
#解除只讀鎖定
unlock tables;
exit

5.同步數據庫


#DB1
#備份主數據庫,并上傳給從數據庫
mysqldump -uroot -p123456 --all-databases --lock-all-tables >/tmp/dbdump.sql
scp /tmp/dbdump.sql 192.168.1.168:/tmp/
#DB2
#從服務器導入主服務器數據庫
mysql -uroot -p </tmp/dbdump.sql

6.在DB2上將DB1設置為主服務器


 #開啟mysql防火墻端口
firewall-cmd --add-port=3306/tcp --permanent
firewall-cmd --reload
 change master to
master_host='192.168.1.168' ,
master_user='slave_cp', 
master_password='pass',
master_log_file='mysql-bin.000001', 
master_log_pos=494;
#啟動slave
start slave;
#查看是否正常工作
show slave status\G
exit

7.在DB2獲取二進制日志信息

 mysql -u root -p
#對數據庫進行只讀鎖定(防止查看二進制日志同時有人對數據庫修改操作)
flush tables with read lock;
#查詢主機二進制文件信息
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 513721| | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
#解除只讀鎖定
unlock tables;
exit

8.在DB1將DB2設置為主服務器

 change master to
master_host='192.168.1.168' ,
master_user='slave_cp', 
master_password='pass',
master_log_file='mysql-bin.000001', 
master_log_pos=513721;
#啟動slave
start slave;
#查看是否正常工作
show slave status\G
exit

二、Keepalived配置(/etc/keepalived/keepalived.conf)(yum install keepalived -y)(注意:關閉selinux策略 setenforce 0)

1.keepalived配置文件配置

! Configuration File for keepalived
global_defs {
 #設置報警通知郵件地址,可以設置多個
 notification_email { 
 msun1996@163.com
 } 
 #設置郵件的發送地址 
 notification_email_from keepalived@msun.com 
 #設置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/mysqlcheck/check_slave.sh"
 interval 2
 weight 2
}
#定義VRRP實例,實例名自定義
vrrp_instance mysql_msun {
 #指定Keepalived的角色,MASTER主機 BACKUP備份
 state BACKUP #此處兩個都設置為BACKUP
 #指定HA監測的接口 
 interface eno16777736 
 #虛擬路由標識,這個標識是一個數字(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.1.160
 } 
}

2.創建監控腳本(/etc/keepalived/mysqlcheck/check_slave.sh)

#!/bin/bash
#This scripts is check for Mysql Slave status

Mysqlbin=/usr/bin/mysql
user=root
pw='123456'
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

3.防火墻策略


#運行VPPR組播(keepalived)
firewall-cmd --permanent --add-rich='rule family="ipv4" destination address="224.0.0.18" protocol value="vrrp" accept'
firewall-cmd --reload

4.測試

1.正常啟動DB1、DB2信息

[root@DB1 ~]# ip a
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
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:43:30:13 brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.166/24 brd 192.168.1.255 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet 192.168.1.160/32 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet6 fe80::20c:29ff:fe43:3013/64 scope link 
 valid_lft forever preferred_lft forever
[root@DB2 ~]# ip a
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
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:da:7d:9d brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.168/24 brd 192.168.1.255 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet6 fe80::20c:29ff:feda:7d9d/64 scope link 
 valid_lft forever preferred_lft forever

2.關閉DB1數據庫,觀察IP

[root@DB1 ~]# systemctl stop mariadb
[root@DB1 ~]# ip a
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
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:43:30:13 brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.166/24 brd 192.168.1.255 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet6 2001:da8:4002:3301:20c:29ff:fe43:3013/64 scope global noprefixroute dynamic 
 valid_lft 2591817sec preferred_lft 604617sec
 inet6 fe80::20c:29ff:fe43:3013/64 scope link 
 valid_lft forever preferred_lft forever
[root@DB2 ~]# ip a
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
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:da:7d:9d brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.168/24 brd 192.168.1.255 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet 192.168.1.160/32 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet6 2001:da8:4002:3301:20c:29ff:feda:7d9d/64 scope global noprefixroute dynamic 
 valid_lft 2591669sec preferred_lft 604469sec
 inet6 fe80::20c:29ff:feda:7d9d/64 scope link 
 valid_lft forever preferred_lft forever

3.重啟主機mariadb,觀察IP

[root@DB1 ~]# ip a #DB1不切換成主機(因為設置為不搶占模式)
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
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:43:30:13 brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.166/24 brd 192.168.1.255 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet6 2001:da8:4002:3301:20c:29ff:fe43:3013/64 scope global noprefixroute dynamic 
 valid_lft 2591605sec preferred_lft 604405sec
 inet6 fe80::20c:29ff:fe43:3013/64 scope link 
 valid_lft forever preferred_lft forever
[root@DB2 ~]# ip a
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
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:da:7d:9d brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.168/24 brd 192.168.1.255 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet 192.168.1.160/32 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet6 2001:da8:4002:3301:20c:29ff:feda:7d9d/64 scope global noprefixroute dynamic 
 valid_lft 2591533sec preferred_lft 604333sec
 inet6 fe80::20c:29ff:feda:7d9d/64 scope link 
 valid_lft forever preferred_lft forever

4.再次關閉DB2的maraiadb,問題出現了,預想當DB2宕機,IP未能正常漂移!!!

[root@DB2 ~]# systemctl stop mariadb 
[root@DB2 ~]# ip a 
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
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:da:7d:9d brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.168/24 brd 192.168.1.255 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet 192.168.1.160/32 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet6 2001:da8:4002:3301:20c:29ff:feda:7d9d/64 scope global noprefixroute dynamic 
 valid_lft 2591326sec preferred_lft 604126sec
 inet6 fe80::20c:29ff:feda:7d9d/64 scope link 
 valid_lft forever preferred_lft forever
[root@DB1 ~]# ip a
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
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:43:30:13 brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.166/24 brd 192.168.1.255 scope global eno16777736
 valid_lft forever preferred_lft forever
 inet6 2001:da8:4002:3301:20c:29ff:fe43:3013/64 scope global noprefixroute dynamic 
 valid_lft 2591241sec preferred_lft 604041sec
 inet6 fe80::20c:29ff:fe43:3013/64 scope link 
 valid_lft forever preferred_lft forever

關于這個問題的思考:

先說說本案例中采用的是Keepalived+mysql的組合,Keepalived用了雙備模式,采用腳本檢測應用是否正常來決定keepalived的權重,從而讓正常的數據庫能得到VIP而正常工作,考慮到了如果DB1宕掉(僅mysql服務)(DB1的權重會低于DB2),DB2接管服務,而DB1再次正常時(權重又會高于DB2),為了避免數據庫來回切換,而在DB1的keepalived配置中設置了不搶占模式來避免此問題。但是,導致了一個新的問題,就算是DB2的數據庫不正常工作,(權重更低于DB2),但因為DB1不搶占模式,DB2仍然會占有VIP,(除非是Keepalived服務宕掉)。

關于這個問題的思考解決方案

1.keepalived取消使用不搶占模式,從而使主機切換正常,但這樣也就使DB1的數據庫服務宕掉再正常啟動時,會搶占DB2的VIP,使數據庫進行不必要的切換。

2.修改檢測腳本,keepalived使用檢測腳本正常時返回0,不正常時,直接讓keepalived服務本身宕掉。這樣情況下,DB1服務mysql服務再次正常(并啟動keepalived),不會搶占DB2的VIP;當DB2mysql服務不正常工作時,(會自動停止keepalived),從而使DB1正常得到VIP。

 #!/bin/bash
#This scripts is check for Mysql Slave status

Mysqlbin=/usr/bin/mysql
user=root
pw='123456'
port=3306
host=127.0.0.1
#最大延時
sbm=120
#Check for $Mysqlbin
if [ ! -f $Mysqlbin ];then
 echo 'Mysqlbin not found,check the variable Mysqlbin'
 pkill keepalived
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
 pkill keepalived
fi
#Check if the thread run 
if [[ "$IOThread" == "No" || "$SQLThread" == "No" ]];then
 pkill keepalived
 elif [[ $SBM -ge $sbm ]];then
 pkill keepalived
 else
 exit 0
fi
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

晋宁县| 博爱县| 应城市| 调兵山市| 昆山市| 洛阳市| 比如县| 崇左市| 舞钢市| 如皋市| 惠水县| 化州市| 安新县| 田林县| 徐州市| 长寿区| 荆州市| 阜康市| 南平市| 连州市| 安徽省| 江都市| 嘉兴市| 瓮安县| 阳高县| 大安市| 通海县| 毕节市| 荔浦县| 河南省| 城口县| 昭通市| 新野县| 桦甸市| 丽江市| 进贤县| 鄂托克前旗| 喀喇| 文山县| 建宁县| 社会|