您好,登錄后才能下訂單哦!
一、主備機IP及VIP規劃:
master1 10.1.1.14 VIP 10.1.1.16
master2 10.1.1.15 VIP 10.1.1.16
二、mysql MM配置
1.修改master1的my.cnf
# vi /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql/
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/mysqld.log
port = 3306
socket=/usr/local/mysql/mysql.sock
pid-file=/usr/local/mysql/mysql.pid
expire-logs-days=10
#binlog-do-db=db1
#binlog-ignore-db=db2
server-id = 1
log-bin = binlog
relay_log = relay-bin
log_slave_updates =1
auto_increment_increment=2
auto_increment_offset=1
2.修改master2的my.cnf
# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql/
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/mysqld.log
port = 3306
socket=/usr/local/mysql/mysql.sock
pid-file=/usr/local/mysql/mysql.pid
expire-logs-days=10
#binlog-do-db=db1
#binlog-ignore-db=db2
server-id=2
relay_log=relay-bin
log_bin =binlog
log_slave_updates =1
auto_increment_increment=2
auto_increment_offset=2
3.創建master1復制賬號
grant replication slave,replication client on *.* to 'repl'@'10.1.1.15' identified by 'repl';
4.創建master2復制賬號
grant replication slave,replication client on *.* to 'repl'@'10.1.1.14' identified by 'repl';
5.為master1配置master
show master status;
change master to
master_host='10.1.1.15',master_user='repl',master_password='repl',master_log_file='binlog.000005',master_log_pos=154;
6.為master2配置master
show master status;
change master to
master_host='10.1.1.14',master_user='repl',master_password='repl',master_log_file='binlog.000001',master_log_pos=154;
7.啟動slave
master1:
start slave;
master2:
start slave;
三、keepalived配置
1.編輯master1的keepalived配置文件
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
#配置告警通知郵箱,可以配置多個
notification_email {
root@localhost
}
#配置郵件發送目標地址
notification_email_from mysql@xiaomi.com
#配置smtp服務器地址,其必須存在
smtp_server 10.1.1.11
#配置連接smtp服務器的超時時間
smtp_connect_timeout 30
#設置運行Keepalived實例的標識,其將顯示于郵件標題中
router_id mysql_ha
}
#監控腳本
vrrp_script chk_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 2
weight 2
}
#配置VRRP實例,實例命名任意
vrrp_instance mysql-ha {
#配置Keepalived角色,MASTER為主機 BACKUP為備機,此處兩個都設置為BACKUP
state BACKUP
#配置keepalived監測的網絡接口
interface eth0
#虛擬路由標識,其為一個(1-255)的數字,一個VRRP實例中主機的該ID必須相同
virtual_router_id 66
#服務器優先級,數字越大優先級越高,一個實例中主服務器優先級要高于備服務器
priority 50
#配置主備服務器間同步檢查的時間間隔(秒)
advert_int 1
#配置服務器搶占模式,這里配置為非搶占模式(只需對master1配置即可)
nopreempt
#配置驗證類型和密碼
authentication {
#兩種驗證類型{PASS|HA}
auth_type PASS
#指定驗證密碼,一個實例中的主備服務器密碼要一樣
auth_pass centos
}
track_script {
#指定執行監控的服務
chk_mysql
}
#配置虛擬IP,可指定有多個,每個占一行
virtual_ipaddress {
10.1.1.16
}
}
2.編輯master1心跳檢測腳本:
#vi /etc/keepalived/check_mysql.sh
#!/bin/bash
#This scripts is check for Mysql Slave status
counter=$(netstat -na|grep "LISTEN"|grep "3311"|wc -l)
if [ "${counter}" -eq 0 ]; then
service keepalived stop
killall keepalived
fi
ping 10.1.1.14 -w1 -c1 &>/dev/null
if [ $? -ne 0 ]
then
systemctl stop keepalived
killall keepalived
fi
3.編輯master2的keepalived配置文件
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
#配置告警通知郵箱,可以配置多個
notification_email {
root@localhost
}
#配置郵件發送目標地址
notification_email_from mysql@xiaomi.com
#配置smtp服務器地址,其必須存在
smtp_server 10.1.1.11
#配置連接smtp服務器的超時時間
smtp_connect_timeout 30
#設置運行Keepalived實例的標識,其將顯示于郵件標題中
router_id mysql_ha
}
# 監控監本
vrrp_script chk_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 2
weight 2
}
#配置VRRP實例,實例命名任意
vrrp_instance mysql-ha {
#配置Keepalived角色,MASTER為主機 BACKUP為備機,此處兩個都設置為BACKUP
state BACKUP
#配置keepalived監測的網絡接口
interface eth0
#虛擬路由標識,其為一個(1-255)的數字,一個VRRP實例中主機的該ID必須相同
virtual_router_id 66
#服務器優先級,數字越大優先級越高,一個實例中主服務器優先級要高于備服務器
priority 49
#配置主備服務器間同步檢查的時間間隔(秒)
advert_int 1
#配置服務器搶占模式,這里配置為非搶占模式(只需對master1配置即可)
#nopreempt
#配置驗證類型和密碼
authentication {
#兩種驗證類型{PASS|HA}
auth_type PASS
#指定驗證密碼,一個實例中的主備服務器密碼要一樣
auth_pass centos
}
track_script {
#指定執行監控的服務
chk_mysql
}
#配置虛擬IP,可指定有多個,每個占一行
virtual_ipaddress {
10.1.1.16
}
}
4.編輯master2檢測腳本
# vim /etc/keepalived/check_mysql.sh
#!/bin/bash
#This scripts is check for Mysql Slave status
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
killall keepalived
fi
ping 10.1.1.15 -w1 -c1 &>/dev/null
if [ $? -ne 0 ]
then
service keepalived stop
killall keepalived
fi
5.vip漂移檢測
1)master1和master2上同時開啟keepalived和mysql
#service keepalived start
#service mysqld start
2)查看master1上ip地址
ip addr
3)登錄10.1.1.16上的mysql
mysql -uusername -ppassword -h20.1.1.16 -P3311
4)停掉master1上的mysql服務
service mysqld stop
5)觀察master1和master2上的ip地址
ip addr
6)繼續在3)中的session中運行mysql命令,看看發生了什么
mysql> use information_schema;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。