MySQL Cluster

ตุลาคม 19th, 2007 by catidcdeveloper

MySQL Cluster

การทำระบบ MySQL Cluster ครั้งนี้จะใช้เครื่องทั้งหมด จำนวน 4 เครื่องดังนี้
เครื่องที่1 : เป็น storage node (sqlnode1:192.168.0.1)
เครื่องที่2 : เป็น storage node (sqlnode2:192.168.0.2)
เครื่องที่3 : เป็น API node (sqlload:192.168.0.3)
เครื่องที่4 : เป็น MySQL cluster management server (sqlmang:192.168.0.4)

mysql_cluster_1.jpg


Storage node หรือ Data node เป็นส่วนที่เป็น Cluster ของข้อมูล ทำหน้าที่เก็บข้อมูล โดยเครื่องเหล่านี้จะ Replicate ทั้งด้านเวลาและจำนวนของแต่ละ Fragment สำหรับชื่อ process ของ Node นี้มีชื่อว่า ndbd

API node หรือ SQL node จะทำหน้าที่เป็นตัวกลางในการเรียกใช้งานข้อมูลจาก storage node โดย node นี้ connect โดยใช้ Deamon ที่ชื่อว่า mysqld -ndbcluster หรือจะใช้ mysqld ร่วมกับ ndbcluster ก็ได้ เพียงแต่ต้องเข้าไป config ใน file my.cnf (การทดสอบระบบครั้งนี้ เลือกใช้วิธีนี้ในการทดสอบ)

MySQL cluster management server หรือ Management node (MGM node) จะทำหน้าที่เป็นตัวตรวจสอบการคงอยู่ของ storage node (สถานะของ cluster) และทำหน้าที่จัดการ node อื่น ๆ เช่น การจัดการ Config, Start&Stop node อื่น ๆ , ทำการ backup&restore ข้อมูล ซึ่งแน่นอนว่า node นี้ต้อง run เป็นตัวแรกแน่นอน และ process ที่อยู่ node นี้มีชื่อว่า ndb_mgmd

 การติดตั้ง  MySQL Cluster Management Server

     เริ่มต้น install mysql version 5.0.38-Ubuntu โดยจะมีการ install cluster management server (ndb_mgmd) และ cluster management client (ndb_mgm) ให้โดยอัตโนมัติดังนี้

sqlmang:192.168.0.4

sudo apt-get install mysql-server

สร้าง cluster configuration file ซึ่งอยู่ที่ /var/lib/mysql-cluster ซึ่งโดยปกติระหว่างการ install mysql-server จะสร้าง directory mysql-cluster ให้โดยอัตโนมัติจึงอาจไม่จำเป็นต้องใช้ คำสั่ง mkdir /var/lib/mysql-cluster นี้

cd /var/lib/mysql-cluster
pico config.ini

1.JPG

หรือถ้าต้องการจะให้ cluster management server start เองโดยอัตโนมัติเมื่อมีการ boot ระบบ เราสามารถสร้าง script เพื่อจัดการ ดังนี้

echo ‘ndb_mgmd -f /var/lib/mysql-cluster/config.ini’ > /etc/init.d/ndb_mgmd
chmod 755 /etc/init.d/ndb_mgmd
update-rc.d ndb_mgmd defaults

การติดตั้ง MySQL Cluster Nodes (Storage Nodes)

sqlnode1:192.168.0.1/sqlnode2:192.168.0.2

groupadd mysql
useradd -g mysql mysql

ทำการ install mysql version 5.0.38-Ubuntu

sudo apt-get install mysql-server

แก้ไข MySQL configuration file ทั้งสองเครื่อง ที่ /etc/mysql

pico /etc/mysql/my.cnf

2.JPG

จากนั้นทำการ start service MySQL ทั้ง 2 เครื่อง ดังนี่

mkdir /var/lib/mysql-cluster
cd /var/lib/mysql-cluster
/etc/init.d/mysql stop
ndbd –initial
/etc/init.d/mysql start

หมายเหตุ  

     การใช้คำสั่ง ndbd –initial จะใช้ในกรณีที่ start service MySQL เป็นครั้งแรกหรือในกรณีที่ file config.ini ที่ /var/lib/mysql-cluster บน MySQL cluster management server มีการแก้ไข ส่วนในกรณีอื่น ๆ ใช้คำสั่ง ndbd

     ทั้งนี้จะใช้คำสั่ง ndbd, ndbd –initial ได้ก็ต่อเมื่อ service MySQL stop อยู่

     โดยปกติเมื่อทำการ install mysql เสร็จ : service MySQL จะ start โดยอัตโนมัติ ซึ่งเราสามารถตรวจสอบสถานะของ service MySQL ได้ว่าขณะนี้เป็นอย่างไร โดยใช้คำสั่ง   /etc/init.d/mysql status

      นอกจากนี้ในกรณีที่ service MySQL start อยู่ เรามีการสร้างหรือแก้ไข file my.cnf เราต้อง restart service MySQL ทุกครั้ง โดยใช้คำสั่ง /etc/init.d/mysql restart

     แนะนำว่าในครั้งแรกที่ติดตั้ง mysql เสร็จ ควรทำการเปลี่ยนแปลง password ของ user root โดยใช้คำสั่ง mysqladmin -u root password newrootpassword

ในกรณีที่เราต้องการให้ start cluster node (storage node) เองโดยอัตโนมัติเมื่อมีการ boot เครื่อง เราสามารถสร้าง script เพื่อจัดการดังนี้

echo ‘ndbd’ > /etc/init.d/ndbd
chmod 755 /etc/init.d/ndbd
update-rc.d ndbd defaults

การติดตั้ง MySQL API Nodes 

sqlload:192.168.0.3

เริ่มต้น install MySQL

sudo apt-get install mysql-server

จากนั้นแก้ไข MySQL configuration file ที่ /etc/mysql

pico /etc/mysql/my.cnf

3.JPG

ทำการ start service MySQL

/etc/init.d/mysql start //หรือใช้ /etc/init.d/mysql restart ในกรณี service MySQL start อยู่

ทำการแก้ไข user ที่ table user ภายในฐานข้อมูล mysql ดังนี้

mysql -u root -p
USE mysql;
SELECT host, user, password FROM user;

ให้ตรวจสอบว่ามี user ที่ชื่อเดียวกันกับเครื่องหรือมี user ที่ชื่อเดียวกับ IP address ของเครื่องหรือไม่ ในกรณีที่มีให้ทำการ update user นั้นเป็น % แทนดังนี้

UPDATE user SET host=’%’ WHERE host=’sqlload’;

ทั้งนี้เพื่อให้เครื่องอื่น ๆ ในวงเดียวกันสามารถ connect database ที่ API node นี้ได้

การทดสอบ MySQL Cluster

ในการทำ MySQL Cluster ครั้งนี้ ได้กำหนดขั้นตอนในการทดสอบดังนี้

ขั้นตอนที่ 1 : ทดสอบว่า MySQL cluster management server (sqlmang:192.168.0.4) สามารถ query สถานะของ cluster ได้หรือไม่
ขั้นตอนที่ 2 : ทดสอบการ replicate ข้อมูลของ storage node ทั้งสองเครื่อง (sqlnode1:192.168.0.1 และ sqlnode2:192.168.0.2)
ขั้นตอนที่ 3 : ทดสอบการเรียกใช้งานข้อมูลจากฐานข้อมูลผ่านทาง web application (PHP)

ขั้นตอนที่ 1 : จะทดสอบโดยการ kill process ndbd ของ storage node nodeใดนnodeหนึ่ง (ในที่นี้เลือก sqlnode1:192.168.0.1)

กรณีที่ storage node ทั้งสองเครื่องทำงานปกติ

sqlmang:192.168.0.4

ใช้คำสั่ง ndb_mgm เพื่อ run cluster management client (ndb_mgm) สำหรับตรวจสอบว่ามี cluster node (storage node) ใด connect อยู่บ้าง

ndb_mgm

จะแสดงผล

– NDB Cluster — Management Client –
ndb_mgm>

ใช้คำสั่ง show สำหรับการแสดงผล

41.JPG

ใช้คำสั่ง quit เพื่อออกจาก ndb_mgm client console

ndb_mgm> quit;

กรณีที่ storage node nodeใดnodeหนึ่ง (ในที่นี้เลือก sqlnode1:192.168.0.1) process ndbd ไม่ทำงาน

sqlnode1:192.168.0.1

ทำการ kill process ndbd โดยใช้คำสั่ง

killall ndbd

ข้อสังเกต ขณะที่ run คำสั่ง killall ndbd ที่ sqlnode1:192.168.0.1 ที่หน้า ndb_mgm client console ของ MySQL cluster management server (sqlmang:192.168.0.4) จะแสดง

ndb_mgm>Node 2: Node shutdown completed. Initiated by signal 15.

ตรวจสอบว่า process ndbd ยัง run อยู่หรือไม่โดยใช้คำสั่ง

ps aux | grep ndbd | grep -iv grep

ในกรณีที่ยังมี process ndbd run อยู่ให้ใช้คำสั่ง killall ndbd อีกจนกระทั่งไม่มี process ndbd ทำงานอยู่

จากนั้นให้ทำการตรวจสอบสถานะของ cluster บน MySQL cluster management server (sqlmang:192.168.0.4)

sqlmang:192.168.0.4

51.JPG 

สังเกตว่า sqlnode2:192.168.0.2 จะทำหน้าที่เป็น Master แทน sqlnode1:192.168.0.1

ถ้าต้องการให้ sqlnode1: 192.168.0.1 กลับมาทำงานได้ตามปกติ ให้ใช้คำสั่ง

sqlnode1:192.168.0.1

ndbd

จากนั้นตรวจสอบสถานะการทำงานของ cluster บน MySQL cluster management server (sqlmang:192.168.0.4)

sqlmang:192.168.0.4

6.JPG

สังเกตว่า sqlnode2:192.168.0.2 จะยังคงทำหน้าที่เป็น Master อยู่ถึงแม้ sqlnode1:192.168.0.1 จะทำงานได้ตามปกติแล้ว

ขั้นตอนที่ 2 : ทดสอบโดยจะสลับกันเพิ่มข้อมูลที่ storage node nodeใดnodeหนึ่ง ซึ่งผลที่ได้ข้อมูลภายในฐานข้อมูลที่ storage node ทั้งสองต้องเหมือนกัน

ข้อกำหนด การสร้างตาราง (table) ภายในฐานข้อมูลใด ๆ ที่ใช้สำหรับ cluster จำเป็นต้องกำหนดให้ table เหล่านั้นใช้ ENGINE=NDBCLUSTER ถ้าคุณใช้ ENGINE ประเภทอื่น จะไม่สามารถทำ cluster ได้

เริ่มต้นสร้างฐานข้อมูลและ table สำหรับการทดสอบที่ sqlnode1:192.168.0.1

sqlnode1:192.168.0.1

mysql -u root -p
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE test_table (i INT) ENGINE=NDBCLUSTER;
INSERT INTO test_table () VALUES (1);
INSERT INTO test_table () VALUES (2);
SELECT * FROM test_table;
quit;

ผลลัพธ์ที่ได้จากการ select ข้อมูลมีดังนี้

7.JPG

สำหรับที่ sqlnode2:192.168.0.2 เราสร้างเพียงฐานข้อมูล โดยให้ชื่อฐานข้อมูลที่สร้างนี้ตรงกับชื่อฐานข้อมูลที่เครื่อง sqlnode1:192.168.0.1 ซึ่ง table และข้อมูลต่าง ๆ ภายใน table จะถูก replicate เองโดยอัตโนมัติ

sqlnode2:192.168.0.2

mysql -u root -p
CREATE DATABASE test_db;
USE test_db;
SELECT * FROM test_table;

ผลลัพธ์ที่ได้จากการ select ข้อมูลมีดังนี้

8.JPG

จากการทดสอบนี้แสดงให้เห็นว่าข้อมูลได้ถูก replicate จาก sqlnode1:192.168.0.1 มาที่ sqlnode2:192.168.0.2

ลองเปลี่ยนให้มีการ replicate ข้อมูลจาก sqlnode2:192.168.0.2 ไปที่ sqlnode1:192.168.0.1 บ้าง

sqlnode2:192.168.0.2

INSERT INTO test_table () VALUES (3);
quit;

sqlnode1:192.168.0.1

mysql -u root -p
USE test_db;
SELECT * FROM test_table;

ผลลัพธ์ที่ได้จากการ select ข้อมูลมีดังนี้

9.JPG

แสดงให้เห็นว่าข้อมูลที่ storage node ทั้งสองเหมือนกันตลอด

ขั้นตอนที่ 3 : โดยในการทดสอบนี้ได้จำลองเครื่องของผู้เขียนเองเป็น web server โดยลงโปรแกรม AppServ

เริ่มต้นทำการ replicate ข้อมูลจาก storage node ไป API node (sqlload:192.168.0.3) ดังนี้

sqlload:192.168.0.3

mysql -u root -p
CREATE DATABASE test_db;
USE test_db;
SELECT * FROM test_table;

ผลลัพธ์ที่ได้จากการ select ข้อมูลมีดังนี้

9.JPG

จากนั้นนำ file loginmysql.php (file ทดสอบ) ที่ใช้สำหรับ connect database test_db (ฐานข้อมูลที่เราสร้างทดสอบก่อนหน้าที่) มาวางที่ path www

หมายเหตุ สิ่งสำคัญในการ connect database เพื่อการเข้าถึงข้อมูลใน NDB table เราจำเป็นต้องให้ application ของเรา connect database ผ่านทาง API node (ซึ่งในที่นี้คือ sqlnode:192.168.0.3) เท่านั้น ไม่สามารถเข้าถึงผ่านทาง MySQL cluster management server และ storage node ได้

ตัวอย่าง file loginmysql.php

<html>
<head><title>TEST CONNECT MySQL DATABASE</title></head>
<body bgcolor=”#FFFFFF”>
<?

$DBhost=”192.168.0.3″   //IP address ของ API Node
$DBuser=”root”;
$DBpass=”";
$DBName=”test_db”;
$table=”test_table”;

mysql_connect($DBhost,$DBuser,$DBPass) or die(”Unable to connect to database”);
@mysql_select_db(”$DBName”) or die(”Unable to select database $DBName”);
print “<CENTER><P>This Username and Password are Correct, You can connect MySQL Database.</P></CENTER>”;
$sqlquery = “SELECT * FROM $table”;
$result = mysql_query($sqlquery);
$number = mysql_numrows($result);

$i=0;

if ($number < 1) {
print “<CENTER>***************************************</CENTER><br>”;
}
else {
while($row = mysql_fetch_array($result)) {
echo “<CENTER>”. $row[’i'] .”</CENTER>”;
echo “<br />”;
}
}

?>
</body>
</html>

จากนั้นเรียกผ่าน web browser โดย http://localhost/loginmysql.php

10.JPG 

ถ้าผลลัพธ์ที่ปรากฎไมเป็นไปตามนี้ หรือมี error เกิดขึ้นให้กลับไปตรวจสอบค่าที่กำหนดให้ตัวแปร $DBhost, $DBuser, $DBpass, $DBName, $table

การ Restart MySQL Cluster

การ restart MySQL cluster จะใช้ในกรณีที่ใกรเปลี่ยนแปลงแก้ไข file /var/lib/mysql-cluster/config.ini บน MySQL cluster management server (sqlmang:192.168.0.4) หรือด้วยเหตุผลอื่น ๆ

sqlmang:192.168.0.4

เข้าสู่ ndb_mgm client console

ndb_mgm
– NDB Cluster — Management Client –
ndb_mgm>

ใช้คำสั่ง shutdown สำหรับ shutdown NDB cluster node (storage node) และ shutdown NDB cluster management server

 11.JPG

ใช้คำสั่ง quit เพื่อออกจาก ndb_mgm client console

ndb_mgm> quit;

ให้ start cluster management server บน MySQL cluster management server (sqlmang:192.168.0.4) โดยใช้คำสั่ง

ndb_mgmd -f /var/lib/mysql-cluster/config.ini

ต่อจากนั้นที่ storage node ทั้งสองเครื่อง ให้ start process ndbd ดังนี้

sqlnode1:192.168.0.1/sqlnode2:192.168.0.2

ndbd

แต่ในกรณีที่มีการเปลี่ยนแปลงแก้ไข file /var/lib/mysql-cluster/config.ini บน MySQL cluster management server (sqlmang:192.168.0.4) ให้ใช้คำสั่ง ndbd –initial แทน

จากนั้นกลับมาตรวจสอบสถานะของ cluster ดังนี้

sqlmang:192.168.0.4

 12.JPG

ในขั้นตอนนี้ให้รอสักระยะหนึ่ง จนกว่าจะปรากฎข้อความ 

ndb_mgm> Node 2: Started (version 5.0.38)
Node 3: Started (version 5.0.38)

เมื่อใช้คำสั่ง show เพื่อตรวจสอบสถานะของ cluster อีกครั้ง

13.JPG

Reference :

[1]http://www.dnsthailand.net/index.php?option=com_content&task=view&id=105&Itemid=59&limit=1&limitstart=1
[2]http://little-cow.siamscript.com/?q=node/14
[3]http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-multi-load-data-queries.html
[4]http://www.howtoforge.com/loadbalanced_mysql_cluster_debian 


Hello world!

ตุลาคม 19th, 2007 by catidcdeveloper

Welcome to Thaipki.com. This is your first post. Edit or delete it, then start blogging!