由於 MySQL Cluster 完全使用 RAM 來作資料(包含索引)的抄寫,所以 data-node 依照資料庫的使用量需有相應足夠的 RAM,其兩者關係可以參考官方的算法。
這個 Cluster 除了可以做到 redundancy,還能透過多個 data node 做到 load balance。
Update by 2012-02-21:新版 MySQL Cluster 7.2.4
node | ip address | package | process |
Management & SQL node | 10.10.10.131 | MySQL-server-community-5.0.67-0.rhel5 MySQL-client-community-5.0.67-0.rhel5 MySQL-clustertools-community-5.0.67-0.rhel5 MySQL-clustermanagement-community-5.0.67-0.rhel5 | ndb_mgmd & ndb_mgm(for Mgm node) mysqld(for SQL node) |
data node #1 | 10.10.10.132 | MySQL-clusterstorage-community-5.0.67-0.rhel5 | ndbd |
data node #2 | 10.10.10.133 | MySQL-clusterstorage-community-5.0.67-0.rhel5 | ndbd |
架構示意圖如下,圖片引用自官方網站的資料,圖示內的 IP 請以上述表格所列的為主,而且本篇實作將 Management node 和 SQL node 裝在同一台,與下圖有些不同。
各個不同元件的連接關係,請參考下圖:
到官方下載需要的套件檔,請參考上方表格的內容。
MySQL-5.0.67 MySQL-6.3.20
rpm -ivh MySQL-client-community-5.0.67-0.rhel5.i386.rpm rpm -ivh MySQL-shared-compat-5.0.67-0.rhel5.i386.rpm rpm -ivh MySQL-devel-community-5.0.67-0.rhel5.i386.rpm rpm -ivh MySQL-server-community-5.0.67-0.rhel5.i386.rpm
以上套件是 SQL node 會用到的
rpm -ivh perl-HTML-Template-2.9-1.el5.rf.noarch.rpm rpm -ivh MySQL-clustermanagement-community-5.0.67-0.rhel5.i386.rpm rpm -ivh MySQL-clustertools-community-5.0.67-0.rhel5.i386.rpm
以上套件是 Management node 會用到,其中 perl-HTML-Template 不在官方網站的下載連結,請自行用 Google 搜尋。
新增設定檔:
/etc/my.cnf (SQL node 會用到)
# Options for mysqld process: [MYSQLD] ndbcluster # run NDB engine ndb-connectstring=10.10.10.131 # location of MGM node # Options for ndbd process: [MYSQL_CLUSTER] ndb-connectstring=10.10.10.131 # location of MGM node
/var/lib/mysql-cluster/config.ini (Management node 會用到)
# Options affecting ndbd processes on all data nodes: [NDBD DEFAULT] NoOfReplicas=2 # Number of replicas DataMemory=256M # How much memory to allocate for data storage IndexMemory=18M # How much memory to allocate for index storage # For DataMemory and IndexMemory, we have used the # default values. Since the "world" database takes up # only about 500KB, this should be more than enough for # this example Cluster setup. # TCP/IP options: [TCP DEFAULT] portnumber=2202 # This the default; however, you can use any # port that is free for all the hosts in cluster # Note: It is recommended beginning with MySQL 5.0 that # you do not specify the portnumber at all and simply allow # the default value to be used instead # Management process options: [NDB_MGMD] hostname=10.10.10.131 # Hostname or IP address of MGM node datadir=/var/lib/mysql-cluster # Directory for MGM node logfiles # Options for data node "A": [NDBD] # (one [NDBD] section per data node) hostname=10.10.10.132 # Hostname or IP address datadir=/mysql-data # Directory for this data node's datafiles # Options for data node "B": [NDBD] hostname=10.10.10.133 # Hostname or IP address datadir=/mysql-data # Directory for this data node's datafiles # SQL node options: [MYSQLD] hostname=10.10.10.131 # Hostname or IP address # (additional mysqld connections can be # specified for this node for various # purposes such as running ndb_restore)
到官方下載需要的套件檔,請參考上方表格的內容。
MySQL-5.0.67 MySQL-6.3.20
rpm -ivh MySQL-clusterstorage-community-5.0.67-0.rhel5.i386.rpm
新增設定檔
/etc/my.cnf
# Options for mysqld process: [MYSQLD] ndbcluster # run NDB engine ndb-connectstring=10.10.10.131 # location of MGM node # Options for ndbd process: [MYSQL_CLUSTER] ndb-connectstring=10.10.10.131 # location of MGM node
先啟動 Managemnet console,登入 Management & SQL node,執行
nbd_mgmd -f /var/lib/mysql-cluster/config.ini
在 data node #1 及 #2,分別執行
ndbd --initial
注意,第一次執行時才需要加上 --initial,爾後啟動時,就不能在加此參數,除非是要重新作初始化。
在 Management & SQL node 啟動 MySQL
service mysql stop
service mysql start
在 Management node 可以檢查 Cluster 狀態,執行
ndb_mgm
>show
Connected to Management Server at: 10.10.10.131:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @10.10.10.132 (Version: 5.0.67, Nodegroup: 0)
id=3 @10.10.10.133 (Version: 5.0.67, Nodegroup: 0, Master)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @10.10.10.131 (Version: 5.0.67)
[mysqld(API)] 1 node(s)
id=4 @10.10.10.131 (Version: 5.0.67)
>all status
Node 2: started (Version 5.0.67)
Node 3: started (Version 5.0.67)
My Tips:
啟動順序:mgm node -> data-node -> SQL node
啟動指令:
線上維護管理方式(Rolling Restart) *DBA必讀*
官方手冊:http://dev.mysql.com/doc/refman/5.0/...g-restart.html
使用時機:
各 node 的線上維護方式:
Cluster 關機
要關閉整個 Cluster,可以在 Management node,執行
ndb_mgm -e shutdown
所有的 data node(ndbd) 程序會自動停止,且 Cluster 機制不再運行。
My Tips:
官方手冊:http://dev.mysql.com/doc/refman/5.0/...a-queries.html
資料庫要設為 Cluster 有些重點必須先知道:
在 SQL node 新增一筆測試的資料庫 mydb。
CREATE SCHEMA mydb;
新增一個 city 的 table 的匯入檔 city_table.sql
DROP TABLE IF EXISTS `City`;
CREATE TABLE `City` (
`ID` int(11) NOT NULL auto_increment,
`Name` char(35) NOT NULL default '',
`CountryCode` char(3) NOT NULL default '',
`District` char(20) NOT NULL default '',
`Population` int(11) NOT NULL default '0',
PRIMARY KEY (`ID`)
) ENGINE=NDBCLUSTER DEFAULT CHARSET=latin1;
INSERT INTO `City` VALUES (1,'Kabul','AFG','Kabol',1780000);
INSERT INTO `City` VALUES (2,'Qandahar','AFG','Qandahar',237500);
INSERT INTO `City` VALUES (3,'Herat','AFG','Herat',186800);
將此表匯入資料庫 mydb
mysql -u root -p mydb < city_table.sql
Cluster 狀態:ndb_mgm -> show
[root@mgm_sql-node ~]# ndb_mgm -- NDB Cluster -- Management Client -- ndb_mgm> show Connected to Management Server at: 10.10.10.131:1186 Cluster Configuration --------------------- [ndbd(NDB)] 2 node(s) id=2 @10.10.10.132 (Version: 5.0.67, Nodegroup: 0, Master) id=3 @10.10.10.133 (Version: 5.0.67, Nodegroup: 0) [ndb_mgmd(MGM)] 1 node(s) id=1 @10.10.10.131 (Version: 5.0.67) [mysqld(API)] 2 node(s) id=4 @10.10.10.131 (Version: 5.0.67) id=5 @10.10.10.135 (Version: 5.0.67)
狀況模擬:
date node#1 - down
data node#2 - up
mgm node - up
SQL node#1 - 存取正常
SQL node#2 - 存取正常
date node#1 - down
data node#2 - up
mgm node - down
SQL node#1 - 存取正常
SQL node#2 - 存取正常
date node#1 - down
data node#2 - up
mgm node - down
SQL node#1 - down
SQL node#2 - 存取正常
文件 | 大小 | 日期 | 附件上傳者 | |||
---|---|---|---|---|---|---|
cluster-components-1.png Cluster componets | 58.1 KB | 14:35, 16 Feb 2009 | alang | 動作 | ||
multi-comp-1.png MySQL Cluster Structure | 41.96 KB | 13:53, 15 Feb 2009 | alang | 動作 | ||
mysql-cluster-final.PNG MySQL Cluster Demo | 90.68 KB | 18:57, 16 Feb 2009 | alang | 動作 |
Images 3 | ||
---|---|---|
Cluster componetscluster-components-1.png | MySQL Cluster Structuremulti-comp-1.png | MySQL Cluster Demomysql-cluster-final.PNG |