溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL高可用集群的介紹以及用法

發(fā)布時間:2021-09-28 14:04:49 來源:億速云 閱讀:135 作者:柒染 欄目:MySQL數(shù)據(jù)庫

MySQL高可用集群的介紹以及用法,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

1 MMM概述

1.1 關(guān)于mysql-mmm

MySQL主主復制管理器

MySQL主主復制配置的監(jiān)控,故障轉(zhuǎn)移和管理的一套腳本套件;

能對居于標準的主從配置的任意數(shù)量的從服務器進行讀負載均衡和實現(xiàn)數(shù)據(jù)備份;

通過MMM方案能實現(xiàn)服務器的故障轉(zhuǎn)移,從而實現(xiàn)mysql的高可用。

1.2 高可用集群介紹:

主備模式 當主不能提供服務的時候備用主機接替它提供服務,這個過程對于客戶端是透明的。

1.3 案例 前提環(huán)境:主從從結(jié)構(gòu)

5臺服務器,2臺數(shù)據(jù)服務器臺互為主主,另2臺數(shù)據(jù)服務器為主中的一臺的從,1臺監(jiān)控服務器。

master51:192.168.4.51

master52:192.168.4.52

slave53:192.168.4.53

slave54:192.168.4.54

write-vip:192.168.4.100

read-vip:192.168.4.101/102

1.4 主主模式配置

(1)用戶授權(quán)   

在master51上配置

mysql> grant  replication  slave on *.*  to  用戶名@192.168.4.52  identified by "密碼";

同樣的操作在master52上給192.168.4.51授權(quán)

(2)啟用binlog日志

master51和master52除了server_id不一致其它保持一致

vim  /etc/my.cnf

server_id=51

log-bin

binlog-format="mixed"

(3)重啟數(shù)據(jù)庫服務器

systemctl restart mysqld

(4)查看正在使用binlog日志文件。

mysql> show  master  status;

從服務器配置

master51為主數(shù)據(jù)庫,master52為從數(shù)據(jù)庫時:

(1)在52上測試主庫授權(quán)的用戶是否有效

#mysql  -h292.168.4.51  -u用戶名  -p密碼

mysql> show grants;

(2)在52本機數(shù)據(jù)管理員登錄主庫信息

mysql> change master  to

    -> master_host="192.168.4.51",

    -> master_user="用戶名",

    -> master_password="密碼",

    -> master_log_file=“主的文件名",     #show master status可以看到 

    -> master_log_pos=數(shù)字; #查看主的Position(偏移位置)

(5)開啟從服務

mysql> start slave;

mysql> show slave status\G;

            Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

必須保證Slave_IO,Slave_SQL的狀態(tài)為yes。

(6) master52為主數(shù)據(jù)庫,master51為從數(shù)據(jù)庫時,在master51上操作同上。

2 Mysql-MMM構(gòu)架

2.1 服務器角色

管理節(jié)點:mmm-monitor(服務進程)  (55服務器)

#負責所有的監(jiān)控工作的監(jiān)控守護進程,決定故障節(jié)點的移除或恢復;

數(shù)據(jù)庫節(jié)點:mmm-agent(服務進程)  (即51---54的服務器)

#運行在Mysql服務器上的代理守護進程,提供簡單遠程服務集,提供給監(jiān)控節(jié)點;

2.2 核心軟件包及作用

Net-ARP:分配虛擬Ip地址

mysql-mmm 構(gòu)架的核心進程

2.3 配置

(1)tar -xf mysql-mmm-2.2.1.tar.gz(所有主機上安裝)

查看說明文件,執(zhí)行make install

配置文件路徑:/etc/mysql-mmm

(2)修改數(shù)據(jù)節(jié)點主機mmm_agentd服務的配置文件

vim /etc/mysql-mmm/mmm_agent.conf #指定服務器在集群中的名稱,不能重復

this 主機名  #配置聲明自己的主機名

(3)修改管理節(jié)點主機mmm_monitor服務的配置文件

vim /etc/mysql-mmm/mmm_mon.conf

include mmm_common.conf  #加載公共配置文件

<monitor>

        ip                     192.168.4.55  #管理節(jié)點主機(即監(jiān)控主機的IP地址)

        pid_path        /var/run/mmm_mond.pid

        bin_path        /usr/lib/mysql-mmm/

        status_path   /var/lib/misc/mmm_mond.status

        ping_ips         192.168.4.51, 192.168.4.52, 192.168.4.53, 192.168.4.54   #設(shè)置被監(jiān)控的數(shù)據(jù)庫

</monitor>

<host default>

        monitor_user              monitor   #監(jiān)控數(shù)據(jù)庫的mysql用戶

        monitor_password    123456

</host>

debug 0  #啟動服務不顯示啟動信息(1顯示)

(4)修改公共配置文件 

vim /etc/mysql-mmm/mmm_common.conf  #修改之后發(fā)送給其它服務器

active_master_role      writer

<host default>

        cluster_interface          eth0

        pid_path                       /var/run/mmm_agentd.pid

        bin_path                       /usr/lib/mysql-mmm/

 replication_user          ser51  

             #設(shè)置主從同步的用戶,要和主數(shù)據(jù)庫給本機授權(quán)的用戶一致,

             #可在/var/lib/mysql/master.info 中查看

      replication_password    123456

      agent_user                agent      #mmm_agent控制用戶

      agent_password      123456

</host>

<host master51>  #設(shè)置第一個主服務器

        ip                  192.168.4.51 #主服務器IP

        mode           master

        peer             master52 #指定另一臺主服務器

</host>

<host master52>   #設(shè)置第二個主服務器

        ip                  192.168.4.52

        mode           master

        peer             master51

</host>

<host slave53>

        ip                   192.168.4.53

        mode            slave

</host>

<host slave54>  

        ip                   192.168.4.54

        mode            slave

</host>

<role writer>      #設(shè)置寫服務器工作模式

        hosts            master51,  master52 #寫的主服務器

        ips                192.168.4.100              #設(shè)置VIP地址

        mode           exclusive                       #獨占模式,只允許51/52一個使用

</role>

 <role reader>

        hosts            slave53,  slave54

        ips                192.168.4.101, 192.168.4.102    #reader-vip

        mode           balanced   #均衡模式

</role>

(5)根據(jù)配置文件設(shè)置,在數(shù)據(jù)節(jié)點主機添加對應的授權(quán)用戶

管理節(jié)點監(jiān)控數(shù)據(jù)庫用戶授權(quán)

mysql> grant replication client on *.* to monitor@'%' identified by "123456";

數(shù)據(jù)庫節(jié)點控制數(shù)據(jù)庫用戶授權(quán)

mysql> grant replication client,process,super on *.* to agent@'%' identified by "123456";

2.4 安裝依賴關(guān)系

所有服務器上安裝:

書寫一個腳本:

vim install.sh

#!/bin/bash

yum -y install perl-*

rpm -q gcc gcc-c++

if [ $? -ne 0 ];then

yum -y install gcc gcc-c++

fi

tar -xf Algorithm-Diff-1.1902.tar.gz

cd Algorithm-Diff-1.1902

perl  Makefile.PL

make

make install

cd ..

rpm -ivh perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm

tar -zxf Proc-Daemon-0.03.tar.gz

cd Proc-Daemon-0.03

perl Makefile.PL

make

make install

cd ..

gunzip Net-ARP-1.0.8.tgz

tar -xf Net-ARP-1.0.8.tar

cd Net-ARP-1.0.8/

perl Makefile.PL

make

make install

2.5啟動服務

(1)啟動數(shù)據(jù)節(jié)點主機mmm_agentd服務

[root@ser51 ~]# /etc/init.d/mysql-mmm-agent start (stop,status)

[root@ser55 ~]# netstat -anptu |grep 9989

(2)啟動管理節(jié)點主機mmm_monitor服務

[root@ser55 ~]# /etc/init.d/mysql-mmm-monitor start

[root@ser55 ~]# netstat -anptu |grep 9988

(3)在管理節(jié)點主機上,查看監(jiān)控信息

[root@ser55 ~]# mmm_control show

defined(@array) is deprecated at /usr/share/perl5/vendor_perl/Log/Log4perl/Config.pm line 863.

(Maybe you should just omit the defined()?)

  master51(192.168.4.51) master/AWAITING_RECOVERY. Roles

  master52(192.168.4.52) master/AWAITING_RECOVERY. Roles

  slave53(192.168.4.54) slave/AWAITING_RECOVERY. Roles

  slave54(192.168.4.53) slave/AWAITING_RECOVERY. Roles

[root@ser55 ~]# mmm_control set_online master51(agent配置文件里設(shè)置的名稱;set_offine:不在線狀態(tài))

defined(@array) is deprecated at /usr/share/perl5/vendor_perl/Log/Log4perl/Config.pm line 863.

(Maybe you should just omit the defined()?)

  master51(192.168.4.51) master/ONLINE. Roles: writer(192.168.4.100)

  master52(192.168.4.52) master/ONLINE. Roles:

  slave53(192.168.4.54) slave/ONLINE. Roles: reader(192.168.4.102)

  slave54(192.168.4.53) slave/ONLINE. Roles: reader(192.168.4.101)

此時ping 192.168.4.100/101/102 都可以ping通(保證該網(wǎng)段這幾個ip沒有被占用)

(4)數(shù)據(jù)節(jié)點主機查看 VIP 地址?

ip  addr  show   |  grep  192.168.4.

2.6 測試

mysql> grant all on *.* to  用戶名@'%' identified by "123456";  #授權(quán)客戶端連接數(shù)據(jù)庫用戶

mysql  -h292.168.4.100  -u用戶名  -p123456

無論關(guān)掉master51的還是master52的數(shù)據(jù)庫服務,此時VIP會被其中一個占用,用戶連接同樣的VIP一樣能夠連接數(shù)據(jù)庫,且在數(shù)據(jù)庫進行操作后,掛掉主的數(shù)據(jù)庫服務器重啟啟用后,數(shù)據(jù)會自動同步。此時VIP不會被搶占。

停掉agent服務,當其中一個主數(shù)據(jù)庫停掉,監(jiān)控不能夠自動切換VIP。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

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

AI