一、主備機(jī)IP及VIP規(guī)劃:
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.創(chuàng)建master1復(fù)制賬號
grant replication slave,replication client on *.* to 'repl'@'10.1.1.15' identified by 'repl';
4.創(chuàng)建master2復(fù)制賬號
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
}
#配置郵件發(fā)送目標(biāo)地址
notification_email_from mysql@xiaomi.com
#配置smtp服務(wù)器地址,其必須存在
smtp_server 10.1.1.11
#配置連接smtp服務(wù)器的超時時間
smtp_connect_timeout 30
#設(shè)置運(yùn)行Keepalived實(shí)例的標(biāo)識,其將顯示于郵件標(biāo)題中
router_id mysql_ha
}
#監(jiān)控腳本
vrrp_script chk_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 2
weight 2
}
#配置VRRP實(shí)例,實(shí)例命名任意
vrrp_instance mysql-ha {
#配置Keepalived角色,MASTER為主機(jī) BACKUP為備機(jī),此處兩個都設(shè)置為BACKUP
state BACKUP
#配置keepalived監(jiān)測的網(wǎng)絡(luò)接口
interface eth0
#虛擬路由標(biāo)識,其為一個(1-255)的數(shù)字,一個VRRP實(shí)例中主機(jī)的該ID必須相同
virtual_router_id 66
#服務(wù)器優(yōu)先級,數(shù)字越大優(yōu)先級越高,一個實(shí)例中主服務(wù)器優(yōu)先級要高于備服務(wù)器
priority 50
#配置主備服務(wù)器間同步檢查的時間間隔(秒)
advert_int 1
#配置服務(wù)器搶占模式,這里配置為非搶占模式(只需對master1配置即可)
nopreempt
#配置驗(yàn)證類型和密碼
authentication {
#兩種驗(yàn)證類型{PASS|HA}
auth_type PASS
#指定驗(yàn)證密碼,一個實(shí)例中的主備服務(wù)器密碼要一樣
auth_pass centos
}
track_script {
#指定執(zhí)行監(jiān)控的服務(wù)
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
}
#配置郵件發(fā)送目標(biāo)地址
notification_email_from mysql@xiaomi.com
#配置smtp服務(wù)器地址,其必須存在
smtp_server 10.1.1.11
#配置連接smtp服務(wù)器的超時時間
smtp_connect_timeout 30
#設(shè)置運(yùn)行Keepalived實(shí)例的標(biāo)識,其將顯示于郵件標(biāo)題中
router_id mysql_ha
}
# 監(jiān)控監(jiān)本
vrrp_script chk_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 2
weight 2
}
#配置VRRP實(shí)例,實(shí)例命名任意
vrrp_instance mysql-ha {
#配置Keepalived角色,MASTER為主機(jī) BACKUP為備機(jī),此處兩個都設(shè)置為BACKUP
state BACKUP
#配置keepalived監(jiān)測的網(wǎng)絡(luò)接口
interface eth0
#虛擬路由標(biāo)識,其為一個(1-255)的數(shù)字,一個VRRP實(shí)例中主機(jī)的該ID必須相同
virtual_router_id 66
#服務(wù)器優(yōu)先級,數(shù)字越大優(yōu)先級越高,一個實(shí)例中主服務(wù)器優(yōu)先級要高于備服務(wù)器
priority 49
#配置主備服務(wù)器間同步檢查的時間間隔(秒)
advert_int 1
#配置服務(wù)器搶占模式,這里配置為非搶占模式(只需對master1配置即可)
#nopreempt
#配置驗(yàn)證類型和密碼
authentication {
#兩種驗(yàn)證類型{PASS|HA}
auth_type PASS
#指定驗(yàn)證密碼,一個實(shí)例中的主備服務(wù)器密碼要一樣
auth_pass centos
}
track_script {
#指定執(zhí)行監(jiān)控的服務(wù)
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服務(wù)
service mysqld stop
5)觀察master1和master2上的ip地址
ip addr
6)繼續(xù)在3)中的session中運(yùn)行mysql命令,看看發(fā)生了什么
mysql> use information_schema;
名稱欄目:linux上mysqlMM(雙主)及keepalived搭建-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://www.rwnh.cn/article30/cchhpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、網(wǎng)站制作、軟件開發(fā)、面包屑導(dǎo)航、網(wǎng)站導(dǎo)航、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容