SQL: January 2014 Archives

5 Hosts MySQL Cluster

user-pic
Vote 0 Votes

1 Management Server (MGM) 192.168.1.1
2 SQL Nodes (mysqld API) VIP 192.168.1.2, SQL1 IP 192.168.1.3, SQL2 IP 192.168.1.4
2 Data Nodes (NDB) NDB1 IP 192.168.1.5, NDB2 IP 192.168.1.6

共同作業, 所有主機都需裝 MySQL-Cluster-server 及 MySQL-Cluster-client (非必要, 但建議安裝)
wget http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-Cluster-7.3/MySQL-Cluster-client-gpl-7.3.3-1.el6.x86_64.rpm
wget http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-Cluster-7.3/MySQL-Cluster-server-gpl-7.3.3-1.el6.x86_64.rpm
rpm -ivh MySQL*.rpm

MGM 設定
mkdir /var/lib/mysql-cluster
cd /var/lib/mysql-cluster
vi config.ini (參考 Initial Configuration of MySQL Cluster)

[ndbd default]
NoOfReplicas=2
DataMemory=1G
IndexMemory=1G

[ndb_mgmd]
hostname=192.168.1.1
datadir=/var/lib/mysql-cluster

[ndbd]
hostname=192.168.1.5
datadir=/usr/local/mysql/data

[ndbd]
hostname=192.168.1.6
datadir=/usr/local/mysql/data

[mysqld]
hostname=192.168.1.3

[mysqld]
hostname=192.168.1.4

設定初始化, config.ini 有異動時執行
ndb_mgmd -f /var/lib/mysql-cluster/config.ini --initial

啟動 MGM
ndb_mgmd -f /var/lib/mysql-cluster/config.ini

加到 /etc/rc.local 下次開機啟動
echo ndb_mgmd -f /var/lib/mysql-cluster/config.ini >> /etc/rc.local

Data Nodes 及 SQL Nodes 設定
Data Nodes 及 SQL Nodes 的設定均相同, 設定 MGM 的 IP
vi /etc/my.cnf

[mysqld]
ndbcluster

[mysql_cluster]
ndb-connectstring=192.168.1.1

Data Notes 要 mkdir -p /usr/local/mysql/data

啟動 Data Nodes
ndbd

加到 /etc/rc.local 下次開機啟動
echo ndbd >> /etc/rc.local

啟動 SQL Nodes
service mysql start (與一般 MySQL 啟動一樣)

在 MGM 查看狀態
# ndb_mgm -e show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.1.3 (mysql-5.6.14 ndb-7.3.3, Nodegroup: 0, *)
id=3 @192.168.1.4 (mysql-5.6.14 ndb-7.3.3, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.1 (mysql-5.6.14 ndb-7.3.3)

[mysqld(API)] 2 node(s)
id=4 @192.168.1.5 (mysql-5.6.14 ndb-7.3.3)
id=5 @192.168.1.6 (mysql-5.6.14 ndb-7.3.3)

以上是 MySQL cluster 的部份, 但是這樣有一個問題, 若程式平常是連第1台 SQL Node,
若第一台掛掉了, 就要改程式指向第2台 SQL Node,
若在兩台 SQL Node 跑 keepalived, 起一個 VIP 192.168.1.2, 程式是連 VIP 即可解決此問題
設定可參考 Linux下keepalived+mysql實現高可用

如此任何一台主機掛點都不會影響服務, MGM 是仲裁角色, 避免 Split Brain 狀況,
MGM 雖然只有一台, 但是它掛點並不會影響服務.

任何一台主機都可以關機維護, 若要全部關機重開, 啟動順序是 MGM, Data Nodes (NDB), SQL Nodes (API)

把現有資料轉存到 Data Node
ALTER TABLE tbl_name ENGINE=NDBCLUSTER;

以 ENGINE=NDBCLUSTER 建立 Table 資料才會存到 Data Node
CREATE TABLE tbl_name (col_name column_definitions) ENGINE=NDBCLUSTER;

Symptom:
Drop table shows "Unknown table"
Create table only create tblname.idb, but not tblname.frm, select the tblname shows doesn't exist

Fix procedure:
1. Stop MySQL
  service mysql stop
2. Remove ib_logfile0 and ib_logfile1
  cd /var/lib/mysql
  rm ib_logfile0 ib_logfile1
3. Remove tblname files
  cd /var/lib/mysql/dbname
  rm tblname*
4. Start MySQL
  service mysql start

About this Archive

This page is an archive of entries in the SQL category from January 2014.

SQL: November 2013 is the previous archive.

SQL: December 2014 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Monthly Archives