MySQL 8.0 Keepalived+雙主架構——手把手搭建

如需轉載請註明出處https://my.oschina.net/feistel/blog/2996572mysql

 

我醜 話說在前頭:sql

192.168.1.200 主masterB, VMUbuntu18 MySQL8.0
192.168.1.123 主masterC, VMCentOS7  MySQL8.0
vip:192.168.1.199shell

B shell表明Ubuntu終端
C shell表明CentOS終端
shell表明兩系統都須要操做數據庫

----------------------------------------------------------------------------------------------------------------vim

1bash

修改B的server-id爲2,兩個庫的server-id不能相同
在B etc/mysqld.cnf裏的[mysqld]添加:
# Server Id.
server-id=2
網絡

#開啓GTID
gtid-mode=on
enforce_gtid_consistency=on
#開啓binlog寫二進制日誌文件:
log_bin=on
異步

保存ide

每次修改配置文件須要重啓服務以便載入
B shell>service mysql restartoop

B mysql>show variables like "%id";
B mysql>show variables like "%slave";
查看相應值是否修改爲功,不然說明配置文件不對
說明:若是etc/mysqld.cnf裏新加的參數不對或打錯什麼的,重啓數據庫服務會出錯

關閉AB防火牆,Windows控制面板,Ubuntu爲:
B shell>service iptables stop

相應地,C也作相同操做,可是與CentOS略有區別,兩Linux不一樣系別,Ubuntu是基於Debian,CentOS是基於Red Hat

在C etc/my.cnf裏的[mysqld]添加:
# Server Id.
server-id=3

#開啓GTID
gtid-mode=on
enforce_gtid_consistency=on
#開啓binlog寫二進制日誌文件:
log_bin=on

保存

重啓服務以便載入
C shell>service mysqld restart
注意是「mysqld」,已瞎,我什麼都看不到,萬惡的CentOS

關閉防火牆,CentOS爲:
C shell>systemctl stop firewalld

能夠參考:MySQL主從複製——異步方式(binlog+position、GTID)
這裏採用GTID方式

----------------------------------------------------------------------------------------------------------------

2

建立主從複製帳號並受權:
B mysql>create user 'lgl'@'%' identified by '123456';
B mysql>grant replication slave on *.* to 'lgl'@'%';
B mysql>flush privileges;

C mysql>create user 'lgl'@'%' identified by '123456';
C mysql>grant replication slave on *.* to 'lgl'@'%';
C mysql>flush privileges;

----------------------------------------------------------------------------------------------------------------

3

使用下面,保證雙主庫裏的數據庫、表和數據一致
必須參考:MySQL數據庫備份

----------------------------------------------------------------------------------------------------------------

4

兩兩互從,即互主

B mysql>
CHANGE MASTER TO
MASTER_HOST='192.168.1.123',
MASTER_USER='lgl',
MASTER_PASSWORD='123456',
master_auto_position=1;

B mysql>start slave;
B mysql>show slave status\G;

C mysql>
CHANGE MASTER TO
MASTER_HOST='192.168.1.200',
MASTER_USER='lgl',
MASTER_PASSWORD='123456',
master_auto_position=1;

C mysql>start slave;
C mysql>show slave status\G;

----------------------------------------------------------------------------------------------------------------

5

安裝keepalived

B shell>apt-get install libssl-dev 
B shell>apt-get install openssl 
B shell>apt-get install libpopt-dev 
B shell>apt_get install keepalived

C shell>yum -y install keepalived

會看到安裝完畢

----------------------------------------------------------------------------------------------------------------

6

兩系統終端都新建shell腳本
shell>vim /etc/keepalived/checkmysql.sh

#!/bin/bash
mysqlstr=mysql
host=192.168.1.200
port=3306
user=lgl
password=123456
mysql_status=1

$mysqlstr -h $host -P $port -u $user -p$password -e "show status;" > /dev/null
if [ $? -eq 0 ] ;then
    echo "mysql_status=1"
    exit 0
else
    /etc/init.d/keepalived stop
fi

shell腳本參考:Linux經常使用命令、Vim、Shell、計劃任務

上述腳本意思是:當數據庫服務(B mysql)正常運行時,回顯mysql_status=1,不然中止keepalived

shell>chmod +x /etc/keepalived/checkmysql.sh
shell>/etc/keepalived/checkmysql.sh
mysql_status=1

----------------------------------------------------------------------------------------------------------------

7

B shell>ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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:
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:7c:0f:de brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.200/24 brd 192.168.1.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.1.199/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::2cf0:bf6d:e273:350d/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

顯示出網卡,以及網卡對應的信息,一般「1」表示迴環網絡
這裏記錄下本機ip所在的網卡,Unbuntu的爲ens33

C shell>ip addr
記下CentOS的爲eno16777736

part8有用

----------------------------------------------------------------------------------------------------------------

8

B shell>vim /etc/keepalived/keepalived.conf

vrrp_svript vs_mysql_100 {
  script "/etc/keepalived/checkmysql.sh"  ##執行shell腳本
  interval 10                ##間隔10s
}

vrrp_instance VI_100 {      ##集羣名
    state BACKUP            ##MASTER
    nopreempt                 ##非搶佔式
    interface ens33            ##part7中所尋找的網卡
    virtual_router_id 100   ##vrid,主備必須一致
    priority 100            ##優先級
    advert_int 5            ##主備同步間隔時間

    authentication {        
        auth_type PASS
        auth_pass 1111      ##主備密碼一致
    }

    track_script {
      vs_mysql_100          ##執行監控服務
    }

    virtual_ipaddress {
        192.168.1.199
    }
}

C shell>vim /etc/keepalived/keepalived.conf

vrrp_script vs_mysql_101 {
  script "/etc/keepalived/checkmysql.sh"  ##執行shell腳本
  interval 10                ##間隔10s
}

vrrp_instance VI_101 {      ##集羣名
    state BACKUP            ##MASTER
    nopreempt                 ##非搶佔式
    interface eno16777736    ##part7中所尋找的網卡
    virtual_router_id 100   ##vrid,主備必須一致
    priority 90             ##優先級,低於BMySQL的
    advert_int 5            ##主備同步間隔時間

    authentication {        
        auth_type PASS
        auth_pass 1111      ##主備密碼一致
    }

    track_script {
      vs_mysql_101          ##執行監控服務
    }

    virtual_ipaddress {
        192.168.1.199
    }
}

----------------------------------------------------------------------------------------------------------------

9

啓動keepalived進程,兩繫系統區別很大!
B shell>/etc/init.d/keepalived start
C shell>service keepalived start

B shell>ip addr |grep 192.168

表示成功綁定了虛擬地址,此時用A mysql客戶端鏈接192.168.1.199。

 

若是不存在虛擬地址,說明綁定失敗,跳回part7,看看網卡正誤。

查看日誌,必定要查!若是不存在文件,那確定是不想給你看,怕你玩壞,跳part10(一)先解決
B shell>cat /var/log/messages

----------------------------------------------------------------------------------------------------------------

10

疑難雜症

1、

若是
B shell>cat /var/log/messages
出現
cat: /var/log/messages: 沒有那個文件或目錄


sudo vim /etc/rsyslog.d/50-default.conf

#*.=debug;\
#        auth,authpriv.none;\
#        news.none;mail.none     -/var/log/debug
#*.=info;*.=notice;*.=warn;\
#        auth,authpriv.none;\
#        cron,daemon.none;\
#       mail,news.none          -/var/log/messages

這幾行註釋去掉
B shell>systemctl restart rsyslog

OK

----------------------------------------------------------------------------------------------------------------

至此,共勉,如需轉載請註明出處https://my.oschina.net/feistel/blog/2996572

相關文章
相關標籤/搜索