MySQL-MMM的讀寫分離及高可用

用途
 mmm是基於信息探測方式進行mysql主從複製架構的監測與故障轉移
 mmm可以做到負載均衡,100%的數據可用性
 mmm所涉及的檢查項   服務器可達性,服務可達性,複製線程可控性

 如圖: 當 master1在宕機時, mmm可以將之前分攤的流量進行轉移,甚至於將從服務器提升爲主

延續雙主模型.
wKioL1NT5nvQXXy2AADZ8aMp5QU075.jpg
mmm-agent端狀態一覽
   online 節點可用
   replication_delay 複製延遲或無法進行(檢查req_backlog文件)
   replication_fail 複製失敗
   awating_recovery 等待恢復
   hard_offline  主機離線
   admin_offline  主控端離線
   unknown  未知錯誤

規劃如下
172.16.43.200  主控機器負責監測與資源的管理
172.16.43.1  vip(172.16.43.11)  master1 主主複製第一臺(可讀寫)
172.16.43.2  vip(172.16.43.12)  master2 主主複製第二臺(只讀)
172.16.43.3  vip(172.16.43.13)  slave   主從方式複製 1 , 2 的信息(只讀)


實驗過程如下

主控安裝安裝: ansible, mysql客戶端, mysql-mmm
集羣節點安裝如下: mariadb, mysql-agent


i)  主機互信,ansible部署的關鍵

yum -y install ansible-1.5.4-1.el6.noarch.rpm
vim /etc/ansible/hosts
# 主控節點完成
[masterserver]
master1.king.com
master2.king.com
.
[slaveserver]
slave.king.com
# 主控節點完成互信多個被管理節點不重複了
ssh-keygen -t rsa
ssh-copy-id -i .ssh/id_rsa.pub [email protected]
.
# 主控節點完成安裝mysql-mmm*
yum -y install mysql mysql-mmm*
.
# 實現mmm配置文件有兩種
# 1. 主控端   mmm_common, mmm_mon
# 2. 集羣端   mmm_common, mmm_agent
# 主控端配置common   /etc/mysql-mmm/mmm_common.conf
active_master_role  writer
.
<host default>
cluster_interface       eth0
pid_path                /var/run/mysql-mmm/mmm_agentd.pid
bin_path                /usr/sbin
replication_user        repl
replication_password    repl
agent_user              mmm_agent
agent_password          agent_password
</host>
.
<host master1>
ip 172.16.43.1
mode master
peer master2
</host>
.
<host master2>
ip 172.16.43.2
mode master
peer master1
</host>
.
<host slave>
ip 172.16.43.3
mode slave
</host>
.
# 誰能寫  vip是多少(參照本文配置)
<role writer>
hosts master1, master2
ips 172.16.43.11
mode exclusive
</role>
.
# 誰能讀  vip是多少(參照本文配置)
<role reader>
hosts master1, master2
ips 172.16.43.11, 172.16.43.12, 172.16.43.13
mode balanced
</role>
.
# 主控節點完成
vim /etc/mysql-mmm/mmm_mon.conf
# 需要改 ping_ips, 把你想監控的集羣加入進入就可以了
# 172.16.0.1 是網關ip
ping_ips 172.16.0.1, 172.16.43.1, 172.16.43.2, 172.16.43.3
.
<host default>
    monitor_user        mmm_monitor
    monitor_password    monitor_password
</host>

ii) 安裝 mariadb, mysql-agent安裝與配置

# 編寫 install.yaml, 使用ansible-playbook執行按成安裝配置
- hosts: masterserver:slaveserver
  remote_user: root
  tasks:
    - name: mariadb install
      copy: src=/root/mariadb-10.0.10-linux-x86_64.tar.gz dest=/tmp
    - name: conf service
      command: tar xf /tmp/mariadb-10.0.10-linux-x86_64.tar.gz -C /usr/local
    ###############################################################
    # 見諒下面的 name: x , 寫了半天才知道不能在一個name中包含多個command,所以..  #
    ###############################################################
    - name: 1
      command: ln -sv /usr/local/mariadb-10.0.10-linux-x86_64 /usr/local/mysql
    - name: 2
      command: cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
    - name: 3
      command: chmod +x /etc/rc.d/init.d/mysqld
    - name: 4
      command: mkdir /mydata/data -p
    - name: 5
      command: mkdir /mydata/binlogs -p
    - name: 6
      command: mkdir /mydata/relaylogs -p
    - name: 7
      command: useradd -r -s /sbin/nologin mysql -U
    - name: 8
      command: chown mysql.mysql /mydata -R
    - name: 9
      command: chown mysql.mysql /usr/local/mysql -R
    - name: 9-1
      # 將本地配置好的安裝文件移植到集羣節點, 解壓的配置文件可能不符合要求
      copy: src=/usr/local/mysql/scripts/mysql_install_db dest=/usr/local/mysql/scripts/
    - name: 10
      command: /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mydata/data
    - name: mariadb conf
      # 此處配置文件 /etc/my.cnf 配置好
      # log-bin=/mydata/binlogs/mysql-bin
      # relay-log=/mydata/relaylogs/relay-bin
      # datadir=/mydata/data
      copy: src=/etc/my.cnf dest=/etc/my.cnf
      notify:
        - restart mariadb
    - name: install mysql-mmm-agent
      yum: name=mysql-mmm-agent state=present
    - name: conf mysql-mmm-agent
      copy: src=/etc/mysql-mmm/mmm_common.conf dest=/etc/mysql-mmm
    - name: modify mmm_agent.conf
      command: sed -i 's@^\(this[[:space:]]\).*@\1` item `@' /etc/mysql-mmm/mmm_agent.conf
      with_items:
        - master1
        - master2
        - slave
    - name: 11
      command: sed -i 's@^\(ENABLED=\).*@\11@' /etc/default/mysql-mmm-agent
  handlers:
    - name: restart mariadb
      service: name=mysqld state=restarted

wKioL1NT2rTwIXu9AAZvuvT0Nhc295.jpg

不熟yaml的童鞋可以 yaml.org


iii) 實現雙主複製與主從複製

1. 雙主配置 (master1.king.com , master2.king.com)
vim /etc/my.cnf
service-id  = 1 | 2 (不可相同分別設置)
log-slave-updates = 1
*** 自動增長列的配置要隔開
auto-increment-offset = 1
auto-increment-increment = 2
2. 均授權複製賬號給對方, 進入命令行
mysql> grant replication client, replication slave on *.* to repl@'172.16.%.%' identified by 'repl';
mysql> flush privileges;
3. 均授權監控賬號
mysql> grant replication client on *.* to 'mmm_monitor'@'172.16.%.%' identified by 'monitor_password';
mysql> grant super, replication client, process on *.* to 'mmm_agent'@'172.16.%.%' identified by 'agent_password';
4. 均連接對方服務器
mysql> change master to master_host='172.16.43.1',master_user='repl', master_password='repl', master_log_file='mysql-bin.000001', master_log_pos=312;
5. 均啓動複製線程
mysql> start salve;
.
.
# 主從複製 (slave.king.com)
1、改server-id 配置文件中
vim /etc/my.cnf
server-id       = 3
2, 授權監控賬號,進入命令行 mysql
mysql> grant replication client on *.* to 'mmm_monitor'@'172.16.%.%' identified by 'monitor_password';
mysql> grant super, replication client, process on *.* to 'mmm_agent'@'172.16.%.%' identified by 'agent_password';
3、連接主服務器
mysql> change master to master_host='172.16.43.1',master_user='repl', master_password='repl', master_log_file='mysql-bin.000001', master_log_pos=312;
4、啓動複製線程
mysql> start slave;

wKiom1NT51WDp8IUAAgb-V35r4o354.jpg

wKioL1NT5y-gIkEjAASUFCwnjhU795.jpg


iv) 啓動測試

# mmm的停止與啓動
/etc/init.d/mysql-mmm-agent stop | start
/etc/init.d/mysql-mmm-monitor stop | start
.
# 離線或上線一個節點
mmm_control set_offline master1
mmm_control set_online master1
.
# 查看節點狀態
mmm_control show

測試1

如圖: 讓 master2 離線,讀寫均不受影響 (抱歉此圖沒有截圖下來,,但最後結果圖中有所顯示  : )

wKioL1NUCxyQkh7HAAI8QGCjORw386.jpg


測試2:

再來一次測試,將master1主節點下線,我們觀察一下情況, 在圖中我們可以看到讀寫資源已經遷移到了master2節點

wKiom1NUCy7BPSkvAAJlwRj-N_Q679.jpg

wKioL1NUCWfjNxr6AAkMPtrbMMM864.jpg

總結

經過測試可以看出MMM在mysql的高可用表現非常良好,無論是隻剩一主還是一主一從,數據的同步確實比較及時準確

生產環境還需觀察...


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