MySQL Cluster Installation Guide

2012/01/10更新: Startup Scripts在最底下

照規矩, 這是一個 Run Down, 有問題請看文件.

MySQL Cluster是一個HA架構(High Availability)的MySQL服務, 當單一MySQL不夠用, 換成 Master-Slave Replication 也不夠用, 換到MySQL Proxy 時也出現了單一Master的寫入瓶頸時, MySQL Cluster 是不用變更目前程式架構下, 最後一個Solution.

MySQL Cluster有三種身份: Management Node, Data Node, SQL Node三種, Management Node是Cluster的judgement role, 負責控制各個Node之間的狀態, 在某個Node出現問題時自動切換, 或是重要的Node出狀況時會停掉服務避免資料遺失; Data Node 是實際存資料的Node, 原始結構依然是類似Master-Slave的Replication關係; SQL Node是提供標準mysql protocol 的前端frontend, 拿來提供服務用的.

如果只是為了提供很容易橫向擴充的結構, 那在資料保全的前提下, 基本消費是四台: Management Node一台, Data Node兩台, SQL Node一台. 如果是為了達到完整的HA, 也就是避免任何一種OPF(One Point Failure), 那需要的基本消費就五台:Management Node一台, Data Node兩台, SQL Node兩台, 配合OS本身做的NIC bond, 可以做到軟硬體各層的OPF-Free HA. 配合L4 Switch或是Haproxy一類的L4 Base LBS, 跟TCP Healthy Check, 可以做到無限量橫向擴充. 大致上的架構如下圖.

MySQL Cluster

以下是簡單的Run Down, 範例是使用CentOS 6.0 x64, 共需要五台, 以下是範例用的IP配置:

  • Management Node (ndb_mgmd): 192.168.0.1 [mgmt]
  • Data Node (ndbd Master): 192.168.0.2 [master]
  • Data Node (ndbd Slave): 192.168.0.3 [slave]
  • SQL Node (mysqld A): 192.168.0.4 [front-a]
  • SQL Node (mysqld B): 192.168.0.5 [front-b]

以下是五台共通的部份.

  1. 安裝CentOS 6.0, 只裝Minimum的Base即可.
  2. 安裝完成後, 關閉 ip6tables, iptables, postfix

    # /sbin/service ip6tables off
    # /sbin/service iptables off
    # /sbin/service postfix off

    同時編輯 /etc/selinux/config 把 selinux關掉, 把enforcing 改成 disabled

    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    # enforcing – SELinux security policy is enforced.
    # permissive – SELinux prints warnings instead of enforcing.
    # disabled – No SELinux policy is loaded.
    SELINUX=disabled
    # SELINUXTYPE= can take one of these two values:
    # targeted – Targeted processes are protected,
    # mls – Multi Level Security protection.
    SELINUXTYPE=targeted

    關掉iptables跟selinux的原因是避免安裝與設定時碰到麻煩事, 請記得在裝好之後, 確認一切沒問題了, 重新打開並且設定好 firewall ruleset

  3. 把 postfix 跟 mysql-libs 移除, 因為mysql-libs跟MySQL Cluster衝突, 而postfix又得dependence mysql-libs.

    # yum -y erase postfix mysql-libs

    接著做一次 yum update 升級一下, 然後reboot讓剛剛修改selinux的部份跟關掉service的部份生效

    # yum -y update
    # reboot

接下來是各台不同的部份. 先從Mgmt Node開始

  1. 去抓取以下兩個檔案, 請依照你裝的CentOS 版本抓取適合的 x86_64 或 i386 版本.
    • MySQL-Cluster-gpl-management-7.2.0_devmilestone-1.rhel5 [x86_64] 或 [i386]
    • MySQL-Cluster-gpl-tools-7.2.0_devmilestone-1.rhel5 [x86_64] 或 [i386]

    抓好之後, 用以下指令安裝

    # rpm -Uhv MySQL-Cluster-gpl-management-7.2.0_devmilestone-1.rhel5.x86_64.rpm
    # rpm -Uhv MySQL-Cluster-gpl-tools-7.2.0_devmilestone-1.rhel5.x86_64.rpm

然後是 Data Node

  1. 抓取以下檔案
    MySQL-Cluster-gpl-storage-7.2.0_devmilestone-1.rhel5 [x86_64] 或 [i386]
    然後安裝

    # rpm -Uhv MySQL-Cluster-7.2/MySQL-Cluster-gpl-storage-7.2.0_devmilestone-1.rhel5.x86_64.rpm

最後是 SQL Node

  1. 抓取以下檔案
    • MySQL-Cluster-gpl-server-7.2.0_devmilestone-1.rhel5 [x86_64] 或 [i386]
    • MySQL-Cluster-gpl-client-7.2.0_devmilestone-1.rhel5 [x86_64] 或 [i386]

    然後安裝

    # rpm -Uhv MySQL-Cluster-gpl-server-7.2.0_devmilestone-1.rhel5.x86_64.rpm
    # rpm -Uhv MySQL-Cluster-gpl-client-7.2.0_devmilestone-1.rhel5.x86_64.rpm

裝完之後, 開始設定與啟動.

  1. 先到 Management Node 上建立設定檔目錄跟設定檔

    # mkdir -p /var/lib/mysql-cluster
    # vi /var/lib/mysql-cluster/config.ini

    其中內容如下

    [ndbd default]
    NoOfReplicas=2
    DataMemory=80M
    IndexMemory=18M
    [tcp default]
    portnumber=2202
    [ndb_mgmd]
    hostname=192.168.0.1
    datadir=/var/lib/mysql-cluster
    [ndbd]
    hostname=192.168.0.2
    datadir=/var/lib/mysql-cluster
    [ndbd]
    hostname=192.168.0.3
    datadir=/var/lib/mysql-cluster
    [mysqld]
    hostname=192.168.0.4
    [mysqld]
    hostname=192.168.0.5
  2. 接著到Data Node跟SQL Node上建立 /etc/my.cnf

    # vi /etc/my.cnf

    內容如下

    [mysqld]
    ndbcluster
    ndb-connectstring=192.168.0.1

    [ndbd]
    connect-string=192.168.0.1

    [ndb_mgm]
    connect-string=192.168.0.1

    [ndb_mgmd]
    config-file=/var/lib/mysql-cluster/config.ini

  3. 都設定好之後, 先到 Management Node上啟動ndb_mgmd

    # ndb_mgmd -f /var/lib/mysql-cluster/config.ini –initial
    MySQL Cluster Management Server mysql-5.1.51 ndb-7.2.0-beta
    2011-07-25 23:20:02 [MgmtSrvr] INFO — The default config directory ‘/usr/mysql-cluster’ does not exist. Trying to create it…
    2011-07-25 23:20:02 [MgmtSrvr] INFO — Sucessfully created config directory
  4. 接著到Data Node啟動ndbd (兩台都要)

    # ndbd
    2011-07-25 23:21:13 [ndbd] INFO — Angel connected to ‘192.168.0.2:1186’
    2011-07-25 23:21:13 [ndbd] INFO — Angel allocated nodeid: 3
  5. 最後啟動SQL Node (一樣兩台都要)

    # /sbin/service mysql start
    Starting MySQL. SUCCESS!
  6. 回到 Management Node 上, 用以下指令檢查狀況

    # ndb_mgm -e show
    Connected to Management Server at: localhost:1186
    Cluster Configuration
    ———————
    [ndbd(NDB)] 2 node(s)
    id=2 @192.168.0.2 (mysql-5.1.51 ndb-7.2.0, Nodegroup: 0, Master)
    id=3 @192.168.0.3 (mysql-5.1.51 ndb-7.2.0, Nodegroup: 0)

    [ndb_mgmd(MGM)] 1 node(s)
    id=1 @192.168.0.1 (mysql-5.1.51 ndb-7.2.0)

    [mysqld(API)] 2 node(s)
    id=4 @192.168.0.4 (mysql-5.1.51 ndb-7.2.0)
    id=5 @192.168.0.5 (mysql-5.1.51 ndb-7.2.0)

    這樣就表示成功了.

  7. 接著就可以到SQL Node上用MySQL Client Command Line去連線並且開始執行SQL指令

    # mysql -u root
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.1.51-ndb-7.2.0-devmilestone-cluster-gpl MySQL Cluster Server (GPL)

    Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
    This software comes with ABSOLUTELY NO WARRANTY. This is free software,
    and you are welcome to modify and redistribute it under the GPL v2 license

    Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

    mysql>

    看到上頭版本是 5.1.51-ndb-7.2.0-devmilestone-cluster-gpl MySQL Cluster Server (GPL)就大功告成啦, 可以開始試著執行指令並看看有沒有自動被同步到另外一台SQL Node上.

  8. 要關閉MySQL Cluster時請反過來執行, 先到兩台SQL Node上關掉mysqld

    # /sbin/service mysql stop

    再到Management Node關閉

    # ndb_mgm -e shutdown

    這個指令會同時關掉 Data Node 的 ndbd 及Management Node上的ndb_mgmd

以上就是簡單的Run Down, 如果要架設前端的Layer 4 Switch, 選用TCP Ping當作Healthy Check Algorithm即可.
大功告成, 有問題請洽Google大神.

2012/01/10更新
ndb_mgmd 的 startup scripts內容如下

#!/bin/sh
# Source function library.
. /etc/rc.d/init.d/functions

start()
{
echo -n $”Starting ndb_mgmd: ”
daemon /usr/sbin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini

touch /var/lock/subsys/ndb_mgmd
echo
}

stop()
{
echo -n $”Shutting down ndb_mgmd: ”
killproc ndb_mgmd

rm -f /var/lock/subsys/ndb_mgmd
echo
}

[ -f /usr/sbin/ndb_mgmd ] || exit 0

# See how we were called.
case “$1″ in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
condrestart)
[ -e /var/lock/subsys/ndb_mgmd ] && (stop; start)
;;
*)
echo $”Usage: $0 {start|stop|restart|reload|condrestart}”
exit 1
esac

exit 0

新建一個 /etc/init.d/ndb_mgmd之後

# chmod a+X /etc/init.d/ndb_mgmd
# /sbin/chkconfig –add ndb_mgmd
# /sbin/chkconfig ndb_mgmd on
# /sbin/service ndb_mgmd start

ndbd (Data Store Node) 的Startup Scripts如下

#!/bin/sh
# Source function library.
. /etc/rc.d/init.d/functions

start()
{
echo -n $”Starting ndbd: ”
daemon /usr/sbin/ndbd

touch /var/lock/subsys/ndbd
echo
}

stop()
{
echo -n $”Shutting down ndbd: ”
killproc ndbd

rm -f /var/lock/subsys/ndbd
echo
}

[ -f /usr/sbin/ndbd ] || exit 0

# See how we were called.
case “$1″ in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
condrestart)
[ -e /var/lock/subsys/ndbd ] && (stop; start)
;;
*)
echo $”Usage: $0 {start|stop|restart|reload|condrestart}”
exit 1
esac

exit 0

一樣新建在 /etc/init.d/ndbd

# chmod a+x /etc/init.d/ndbd
# /sbin/chkconfig –add ndbd
# /sbin/chkconfig ndbd on
# /sbin/service ndbd start

  • Post a comment

    Threaded commenting powered by interconnect/it code.