溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

linux上mysql MM(雙主)及keepalived搭建

發(fā)布時(shí)間:2020-08-13 21:11:58 來(lái)源:ITPUB博客 閱讀:161 作者:sqysl 欄目:MySQL數(shù)據(jù)庫(kù)

一、主備機(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ù)制賬號(hào)
  grant replication slave,replication client on *.* to 'repl'@'10.1.1.15' identified by 'repl';
  
4.創(chuàng)建master2復(fù)制賬號(hào)
  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.啟動(dòng)slave
  master1:
  start slave;
  master2:
  start slave;

三、keepalived配置
1.編輯master1的keepalived配置文件
  #vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
    #配置告警通知郵箱,可以配置多個(gè)
   notification_email {
    root@localhost
   }
   #配置郵件發(fā)送目標(biāo)地址
   notification_email_from mysql@xiaomi.com
   #配置smtp服務(wù)器地址,其必須存在
   smtp_server 10.1.1.11
   #配置連接smtp服務(wù)器的超時(shí)時(shí)間
   smtp_connect_timeout 30
   #設(shè)置運(yùn)行Keepalived實(shí)例的標(biāo)識(shí),其將顯示于郵件標(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ī),此處兩個(gè)都設(shè)置為BACKUP
    state BACKUP 
    #配置keepalived監(jiān)測(cè)的網(wǎng)絡(luò)接口
    interface eth0
    #虛擬路由標(biāo)識(shí),其為一個(gè)(1-255)的數(shù)字,一個(gè)VRRP實(shí)例中主機(jī)的該ID必須相同
    virtual_router_id 66
    #服務(wù)器優(yōu)先級(jí),數(shù)字越大優(yōu)先級(jí)越高,一個(gè)實(shí)例中主服務(wù)器優(yōu)先級(jí)要高于備服務(wù)器
    priority 50  
    #配置主備服務(wù)器間同步檢查的時(shí)間間隔(秒)
    advert_int 1
    #配置服務(wù)器搶占模式,這里配置為非搶占模式(只需對(duì)master1配置即可)
    nopreempt
    #配置驗(yàn)證類(lèi)型和密碼
    authentication {
        #兩種驗(yàn)證類(lèi)型{PASS|HA}
        auth_type PASS
        #指定驗(yàn)證密碼,一個(gè)實(shí)例中的主備服務(wù)器密碼要一樣
        auth_pass centos
    }
    track_script {
    #指定執(zhí)行監(jiān)控的服務(wù)
        chk_mysql
    }
    #配置虛擬IP,可指定有多個(gè),每個(gè)占一行
    virtual_ipaddress {
    10.1.1.16
    }
}

2.編輯master1心跳檢測(cè)腳本:
#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 {
   #配置告警通知郵箱,可以配置多個(gè)
   notification_email {
    root@localhost
   }
   #配置郵件發(fā)送目標(biāo)地址
   notification_email_from mysql@xiaomi.com
   #配置smtp服務(wù)器地址,其必須存在
   smtp_server 10.1.1.11
   #配置連接smtp服務(wù)器的超時(shí)時(shí)間
   smtp_connect_timeout 30
   #設(shè)置運(yùn)行Keepalived實(shí)例的標(biāo)識(shí),其將顯示于郵件標(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ī),此處兩個(gè)都設(shè)置為BACKUP
    state BACKUP
    #配置keepalived監(jiān)測(cè)的網(wǎng)絡(luò)接口
    interface eth0
    #虛擬路由標(biāo)識(shí),其為一個(gè)(1-255)的數(shù)字,一個(gè)VRRP實(shí)例中主機(jī)的該ID必須相同
    virtual_router_id 66
    #服務(wù)器優(yōu)先級(jí),數(shù)字越大優(yōu)先級(jí)越高,一個(gè)實(shí)例中主服務(wù)器優(yōu)先級(jí)要高于備服務(wù)器
    priority 49
    #配置主備服務(wù)器間同步檢查的時(shí)間間隔(秒)
    advert_int 1
    #配置服務(wù)器搶占模式,這里配置為非搶占模式(只需對(duì)master1配置即可)
    #nopreempt
    #配置驗(yàn)證類(lèi)型和密碼
    authentication {
        #兩種驗(yàn)證類(lèi)型{PASS|HA}
        auth_type PASS
        #指定驗(yàn)證密碼,一個(gè)實(shí)例中的主備服務(wù)器密碼要一樣
        auth_pass centos
    }
    track_script {
    #指定執(zhí)行監(jiān)控的服務(wù)
        chk_mysql  
    }
    #配置虛擬IP,可指定有多個(gè),每個(gè)占一行
    virtual_ipaddress {
    10.1.1.16
    }
}

4.編輯master2檢測(cè)腳本
# 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漂移檢測(cè)
1)master1和master2上同時(shí)開(kāi)啟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;
 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI