mmm實現mysql高可用

來源:互聯網
上載者:User

標籤:mysql mmm

一 mysql主從搭建


一 設定hosts解析
[[email protected] conf]# vi /etc/hosts  (三台伺服器相同都添加下面三項)
192.168.31.2 M1
192.168.31.3 M2
192.168.31.4 slave1

二 設定各台伺服器的名字分別與hosts檔案中的對應


三 建立相應的帳號
 GRANT REPLICATION SLAVE , REPLICATION CLIENT ON * . * TO ‘mmm_slave‘@‘192.168.31.%‘ IDENTIFIED BY ‘123456‘; (用於主從和主主複製 M1,M2 上都添加這個使用者)
 flush privileges;
 
四 M1 M2 配置成主主模式
M1 設定檔
[mysqld]
innodb_file_per_table=1
log-bin=/data/mysql/mysql-bin.log
binlog_format           = ROW
server-id=1
binlog-do-db=mmm_test
replicate-do-db=mmm_test

M2設定檔
[mysqld]
innodb_file_per_table=1
log-bin=/data/mysql/mysql-bin.log
binlog_format           = ROW
server-id=2
binlog-do-db=mmm_test
replicate-do-db=mmm_test



在M2上執行
change master to master_host=‘192.168.31.2‘,master_user=‘mmm_slave‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000017‘,master_log_pos=326;
start slave;

在M1 上執行 (主主模型)
change master to master_host=‘192.168.31.3‘,master_user=‘mmm_slave‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=312;
start slave;

五 配置M1和slave1 主從
slave1 設定檔
[mysqld]
server-id =3
innodb_file_per_table=1
log-bin=/data/mysql/mysql-bin.log
binlog_format           = ROW
replicate-do-db=mmm_test

執行
change master to master_host=‘192.168.31.2‘,master_user=‘mmm_slave‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000017‘,master_log_pos=326;
start slave;



六 安裝MySQL-MMM (三台伺服器執行結果相同)
[[email protected] software]# wget http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
[[email protected] software]# rpm -ivh epel-release-5-4.noarch.rpm
[[email protected] software]# yum -y install mysql-mmm*
[[email protected] software]# rpm -qa | grep mysql-mmm
mysql-mmm-2.2.1-2.el6.noarch
mysql-mmm-agent-2.2.1-2.el6.noarch
mysql-mmm-tools-2.2.1-2.el6.noarch
mysql-mmm-monitor-2.2.1-2.el6.noarch

七 在三個節點上授權

GRANT PROCESS,SUPER,REPLICATION CLIENT ON * . * TO ‘mmm_agent‘@‘192.168.31.%‘ IDENTIFIED BY ‘123456‘; (代理帳號,是MMM代理用來變成唯讀模式和同步master等的)
GRANT REPLICATION CLIENT ON * . * TO ‘mmm_monitor‘@‘192.168.31.%‘ IDENTIFIED BY ‘123456‘; (監聽帳號,是MMM監控伺服器用來對MySQL伺服器做健全狀態檢查的)
 flush privileges;

八 配置 MMM 監控、代理服務
    1 三台伺服器上修改mmm_common.conf (三台伺服器上執行步驟相同)
    [[email protected] conf]# cd /etc/mysql-mmm/
    [[email protected] mysql-mmm]# cp mmm_common.conf mmm_common.conf.bak
    [[email protected] mysql-mmm]# vi mmm_common.conf
    active_master_role      writer

    <host default>
        cluster_interface       eth1  # 這裡視情況而定 我的三台伺服器這裡不相同
        pid_path                /var/run/mysql-mmm/mmm_agentd.pid
        bin_path                /usr/libexec/mysql-mmm/
        replication_user        mmm_slave # 前面建立的複製帳號
        replication_password    123456    # 複製帳號的密碼
        agent_user              mmm_agent # 前面建立的代理帳號
        agent_password          123456     # 代理帳號的密碼
    </host>

    <host M1>
        ip      192.168.31.2
        mode    master
        peer    M1
    </host>

    <host M2>
        ip      192.168.31.3
        mode    master
        peer    M2
    </host>

    <host slave1>
        ip      192.168.31.4
        mode    slave
    </host>

    <role writer>
        hosts   M1, M2    # 能夠作為writer的伺服器
        ips     192.168.31.10 # writer節點的虛擬ip, 應用的寫請求將直接連接到這個ip
        mode    exclusive # 排他模式
    </role>

    <role reader>
        hosts   M1,M2,slave1 # 作為reader的伺服器
        ips     192.168.31.20 # reader節點虛擬ip,應用的讀請求將直接連接到這個ip
        mode    balanced # 平衡模式
    </role>

    2 修改M1的mmm_agent.conf
    [[email protected] mysql-mmm]# vi mmm_agent.conf
    include mmm_common.conf

    # The ‘this‘ variable refers to this server.  Proper operation requires
    # that ‘this‘ server (db1 by default), as well as all other servers, have the
    # proper IP addresses set in mmm_common.conf.
    this M1
    
    3 修改M2的mmm_agent.conf
    [[email protected] mysql-mmm]# vi mmm_agent.conf
    include mmm_common.conf

    # The ‘this‘ variable refers to this server.  Proper operation requires
    # that ‘this‘ server (db1 by default), as well as all other servers, have the
    # proper IP addresses set in mmm_common.conf.
    this M2

    4 修改slave1的mmm_agent.conf
    [[email protected] mysql-mmm]# vi mmm_agent.conf
    include mmm_common.conf

    # The ‘this‘ variable refers to this server.  Proper operation requires
    # that ‘this‘ server (db1 by default), as well as all other servers, have the
    # proper IP addresses set in mmm_common.conf.
    this slave1
    
    5 修改 mon 上的mmm_mon.conf (這裡就是M1 共用一台伺服器)
    [[email protected] mysql-mmm]# vi mmm_mon.conf
    include mmm_common.conf

    <monitor>
        ip                  ip                  192.168.31.2
        pid_path            /var/run/mysql-mmm/mmm_mond.pid
        bin_path            /usr/libexec/mysql-mmm
        status_path         /var/lib/mysql-mmm/mmm_mond.status
        ping_ips            192.168.31.2,192.168.31.3,192.168.31.4
        auto_set_online     10

        # The kill_host_bin does not exist by default, though the monitor will
        # throw a warning about it missing.  See the section 5.10 "Kill Host
        # Functionality" in the PDF documentation.
        #
        # kill_host_bin     /usr/libexec/mysql-mmm/monitor/kill_host
        #
    </monitor>

    <host default>
        monitor_user        mmm_monitor
        monitor_password    123456
    </host>

    debug 0


    

九 啟動各個相關服務
    1 三台伺服器都啟動mysql-mmm-agent服務
    [[email protected] mysql-mmm]# /etc/init.d/mysql-mmm-agent start
    [[email protected] mysql-mmm]# /etc/init.d/mysql-mmm-agent start
    [[email protected] mysql-mmm]# /etc/init.d/mysql-mmm-agent start
    
    2 在M1 上啟動mysql-mon服務
    [[email protected] mysql-mmm]# /etc/init.d/mysql-mmm-monitor  start

    3 查看mysql-mon 監控機上的MMM狀態資訊
    [[email protected] mysql-mmm]# mmm_control show
    出現Can‘t connect to monitor daemon
    則yum -y install perl-Time-HiRes*
    如果還是出現 Can‘t connect to monitor daemon 則要檢查主從是否有問題
    
    [[email protected] ~]# mmm_control show # 再次執行 會出現下面這幾行
  M1(192.168.31.2) master/ONLINE. Roles: writer(192.168.31.10)
  M2(192.168.31.3) master/ONLINE. Roles: reader(192.168.31.20)
  slave1(192.168.31.4) slave/ONLINE. Roles:
 
  [[email protected] ~]# mmm_control checks all  # 出現下面這些
M1      ping         [last change: 2015/09/24 00:39:19]  OK
M1      mysql        [last change: 2015/09/24 00:39:19]  OK
M1      rep_threads  [last change: 2015/09/24 00:39:19]  ERROR: Replication is broken
......
M2      rep_backlog  [last change: 2015/09/24 00:39:19]  OK: Backlog is null


十 查看叢集日誌
[[email protected] ~]# tail -f /var/log/mysql-mmm/mmm_agentd.log
2015/09/23 18:38:11 FATAL Child exited with exitcode 98, restarting after 10 second sleep
2015/09/23 18:38:21 FATAL Listener: Can‘t create socket!
.......
2015/09/24 00:39:13  INFO Added:   writer(192.168.31.10)


[[email protected] ~]# tail -f /var/log/mysql-mmm/mmm_mond.log
2015/09/24 00:35:10  WARN Checker ‘mysql‘ is dead!
......
2015/09/24 00:35:16  INFO Child exited normally (with exitcode 0), shutting down

[[email protected] mysql-mmm]# tail -f /var/log/mysql-mmm/mmm_agentd.log
2015/09/23 02:53:38  INFO END
.......
2015/09/24 00:38:55  INFO Child exited normally (with exitcode 0), shutting down
2015/09/24 00:38:55  INFO Changing active master to ‘M1‘

十一 測試
我們使用在M1 ip a 可以發現 192.168.31.10 在M1上
類比M1宕機
使用 service mysql stop

在查看  ip a   發現vip 在M2上了。









    
    


    




 
 

mmm實現mysql高可用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.