MySQL中mmm實現高可用羣集

  • 介紹

    MMM(Master-Master replication manager for MySQL)是一套支持雙主故障切換和雙主日常管理的腳本程序。MMM使用Perl語言開發,主要用來監控和管理MySQL Master-Master(雙主)複製,可以說是mysql主主複製管理器。雖然叫做雙主複製,但是業務上同一時刻只允許對一個主進行寫入,另一臺備選主上提供部分讀服務,以加速在主主切換時刻備選主的預熱,可以說MMM這套腳本程序一方面實現了故障切換的功能,另一方面其內部附加的工具腳本也可以實現多個slave的負載均衡。

  • 優點

    高可用性,擴展性好,出現故障自動切換,對於主主同步,在同一時間只提供一臺數據庫寫操作,保證的數據的一致性。

    • 缺點

    Monitor節點是單點,可以結合Keepalived實現高可用。

  • 實驗要求

    兩臺主服務器master

    master1:192.168.177.128

    master2:192.168.177.135

    兩臺從服務器slave

    slave1:192.168.177.132

    slave2:192.168.177.133

    監控服務器monitor

    monitor:192.168.177.134

配置ALI雲源,然後安裝epel-release源(四臺主從)

# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# yum -y install epel-release

# yum clean all && yum makecache

搭建本地YUM源(四臺主從)

# yum -y install mariadb-server mariadb

# systemctl stop firewalld.service 
# setenforce 0

# systemctl start mariadb.service

修改m1主配置文件

# vim /etc/my.cnf //
刪掉原來的[mysqld]9行,添加如下內容:
[mysqld]    
log_error=/var/lib/mysql/mysql.err
log=/var/lib/mysql/mysql_log.log
log_slow_queries=/var/lib/mysql_slow_queris.log
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
==注意==server_id要不同
# systemctl restart mariadb.service

MySQL中mmm實現高可用羣集
MySQL中mmm實現高可用羣集
MySQL中mmm實現高可用羣集
MySQL中mmm實現高可用羣集

配置主主複製-兩臺主服務器相互複製

# mysql>
# show master status; //記錄日誌文件名稱和位置值,在兩臺主上查看
在m1上爲m2授予從的權限,在m2上也要爲m1授予從的權限
# grant replication slave on *.* to 'replication'@'192.168.177.%' identified by '123456'; //m1上
# grant replication slave on *.* to 'replication'@'192.168.177.%' identified by '123456';//m2上
#change master to master_host='192.168.177.135',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;//m1上
# change master to master_host='192.168.177.128',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;//m2上
# start slave;
# show slave status\G;

MySQL中mmm實現高可用羣集

在兩臺從上做-注意日誌文件和位置參數的改變(都指向m1)

# change master to master_host='192.168.177.128',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
# start slave; //開啓同步
# show slave status\G;

MySQL中mmm實現高可用羣集

安裝MMM(四臺主從)

# yum -y install mysql-mmm*
安裝結束後,對mmm進行配置
# cd /etc/mysql-mmm/
#vim mmm_common.conf //所有主機上都要配置,直接複製多份
<host default>
    cluster_interface       ens33
    ……
    replication_user        replication
    replication_password    123456
    agent_user              mmm_agent
    agent_password          123456
 <host db1>
    ip      192.168.177.128
    mode    master
    peer    db2
</host>

<host db2>
    ip      192.168.177.135
    mode    master
    peer    db1
</host>

<host db3>
    ip      192.168.177.132
    mode    slave
</host>
<host db4>
    ip      192.168.177.133
    mode    slave
</host>
<role writer>
    hosts   db1, db2
    ips     192.168.177.200    #主服務器虛擬IP
    mode    exclusive
</role>
<role reader>
    hosts   db3, db4
    ips     192.168.177.20,192.168.177.30   #從服務器虛擬IP
    mode    balanced
</role>
在主1上面複製:
# scp mmm_common.conf [email protected]:/etc/mysql-mmm/
# scp mmm_common.conf [email protected]:/etc/mysql-mmm/
# scp mmm_common.conf [email protected]:/etc/mysql-mmm/

MySQL中mmm實現高可用羣集
MySQL中mmm實現高可用羣集
MySQL中mmm實現高可用羣集

在monitor服務器上配置

# systemctl stop firewalld.service
# setenforce 0
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# yum -y install epel-release
# yum clean all && yum makecache
# yum -y install mysql-mmm*
# cd /etc/mysql-mmm
# vim mmm_mon.conf
<host default>
    ping_ips            192.168.177.132,192.168.177.128,192.168.177.133,192.168.177.135 //四臺地址
    monitor_user        mmm_monitor
    monitor_password    123456
    auto_set_online     10
</host>
在m1上:# scp mmm_common.conf [email protected]:/etc/mysql-mmm/

在所有數據庫上爲mmm_agent授權-四臺主從

# mysql>
# grant super, replication client, process on *.* to 'mmm_agent'@'192.168.177.%' identified by '123456';

在所有數據庫上爲mmm_moniter授權-四臺主從

# grant replication client on *.* to 'mmm_monitor'@'192.168.177.%' identified by '123456';
# flush privileges;

修改所有數據庫的mmm_agent.conf-四臺主從

# vim /etc/mysql-mmm/mmm_agent.conf
this db1 //根據規劃進行逐一調整
this db2
this db3
this db4

在所有數據庫服務器上啓動mysql-mmm-agent-四臺主從

# systemctl start mysql-mmm-agent.service
# systemctl enable mysql-mmm-agent.service   #開機自啓動

在monitor服務器上配置

# systemctl start mysql-mmm-monitor.service 
# mmm_control show
# mmm_control checks all
# mmm_control move_role writer db2    //指定db2綁定虛擬IP

MySQL中mmm實現高可用羣集
MySQL中mmm實現高可用羣集
MySQL中mmm實現高可用羣集

故障測試

停止m1 確認 虛擬地址 200 是否移動到 m2 上。注意:主不會搶佔
# systemctl stop mariadb.service //m1上
# mmm_control show
   db1(192.168.177.128) master/HARD_OFFLINE. Roles:
  db2(192.168.177.135) master/ONLINE. Roles: writer(192.168.177.200)

MySQL中mmm實現高可用羣集
MySQL中mmm實現高可用羣集

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章