1. 程式人生 > 其它 >Mysql主主複製高可用解決方案

Mysql主主複製高可用解決方案

技術標籤:linuxjava資料庫mysql大資料

前言

最近做一個專案,專案考慮了一些風險,其中就有mysql宕機的風險,mysql是申請了兩臺伺服器。於是打算搞個主主複製,用keepalived進行漂移實現高可用。

方案可行性

首先主主複製可以保證兩臺機器資料同步,keeplalived通過虛擬ip,保證我們的mysql可以快速切換。方案可行。

搭建步驟

首先我們準備好兩臺已經搭建好的mysql伺服器。
ip分別為15.1.1.46,15.1.1.50

修改配置檔案

在第一臺mysql伺服器上的my.cf檔案中,我們增加如下內容。

server-id=1
log-bin=mysql-bin
binlog-ignore-db=mysql                (可以不加)
binlog-ignore-db=information_schema  (可以不加)
binlog-do-db=mydatabase            (可以不加)
binlog_format=STATEMENT
auto-increment-increment = 2
auto-increment-offset = 1

增加後的整體情況如下:

[mysqld]

server-id=1
log-bin=mysql-bin
binlog_format=STATEMENT
auto-increment-increment = 2
auto-increment-offset = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

basedir = /home/mysql-5.7.25
datadir = /home/mysql-5.7.25/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=utf8

log-error = /home/mysql-5.7.25/data/mysqld.log
pid-file = /home/mysql-5.7.25/data/mysqld.pid

第一臺機器配置完畢。
第二臺機器進行如下配置:

server-id=2
log-bin=mysql-bin
binlog_format=STATEMENT
auto-increment-increment = 2
auto-increment-offset = 2

和第一臺基本一樣,不過是increment不同,防止自增衝突。
整體配置檔案如下:

[mysqld]

server-id=2
log-bin=mysql-bin
binlog_format=STATEMENT
auto-increment-increment = 2
auto-increment-offset = 2

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

basedir = /home/mysql-5.7.25
datadir = /home/mysql-5.7.25/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=utf8

log-error = /home/mysql-5.7.25/data/mysqld.log
pid-file = /home/mysql-5.7.25/data/mysqld.pid

兩臺機器全部重啟

service mysql restart

第二臺機器複製第一臺,第一臺作為主機

第一臺機器執行如下:

GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123456';

展示當前bin-log位置

show master status;


記住上方的file 和postion
切換到第二臺機器執行如下
change master to master_host='15.1.1.46',master_user='root',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=154;

開啟複製

start slave;

展示從機複製情況

show slave status\G;


兩個都是yes證明覆製成功。

第一臺機器複製第二臺,第二臺作為主機

第二臺機器執行如下:

GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123456';

展示master的位置

show master status;

切換到第一臺機器
執行如下命令

change master to master_host='15.1.1.50',master_user='root',master_password='123456',master_log_file='mysql-bin.000007',master_log_pos=154;

然後開啟複製
start slave;
展示從機的複製情況
show slave status\G;
如果下圖中兩個都是yes則證明覆製成功。

使用keepalived進行漂移保證高可用

(1)安裝yum包
yum install -y gcc openssl-devel popt-devel
(2)上傳keepalived並解壓
tar -zxvf keepalived-1.2.15.tar.gz
(3)解壓keepalived並進行編譯
cd keepalived-1.2.15
./configure --prefix=/usr/local/keepalived
make && make install
(4)進行配置檔案的初始化複製工作
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
(5)開機自啟
chkconfig --add keepalived
chkconfig keepalived on
以上步驟第二臺機器也如此執行
(6)進入主機 /etc/keepalived。修改裡面的 keepalived.conf檔案

! Configuration File for keepalived

global_defs {
   router_id MYSQL-HA
}

vrrp_script check_mysql {
    script "/home/mysql/mysql.sh"        
    interval 1                                      
    weight 2                                        
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
	track_script {
        check_mysql        
    }
    virtual_ipaddress {
        15.1.1.99
    }
}

(7)編寫監測的自殺指令碼
當mysql掛掉之後,keepalived監控到mysql掛掉,keepalived才能進行漂移。

#!/bin/bash
ps -ef | grep mysqld| grep -v grep &> /dev/null
if [ $? -eq 0 ]
then
	echo "mysql服務正常執行!!!"
else
	service keepalived stop
	echo "mysql服務已經停止!請及時解決!!!"
fi

(8)編寫從機的keepalived的指令碼
上面的步驟裡面的keepalived的conf按照下面的改下。其他不用動。
state MASTER 改為BACKUP
priority 100 改為90
(9)將keepalived的開啟

service keepalived start

總結

至此mysql高可用已經搭建完畢。