บทความนี้จะอธิบายวิธีการคอนฟิก Replication ของ MySQL เพื่อทำการ replicate ข้อมูลในฐานข้อมูล (database) จากเครื่องหลัก (Master) ไปยังเครื่องสำรอง (Slave) ได้
หลังจากที่คอนฟิก Replication ถูกต้องเรียบร้อย เมื่อมีการเปลี่ยนแปลงข้อมูลในฐานข้อมูลบนเครื่องหลัก (Master) ไม่ว่าจะเป็นการ INSERT, UPDATE, DELETE หรือ แก้ไข table การเปลี่ยนแปลงนี้จะถูกส่งต่อ (replicate) ไปยังเครื่องสำรอง (Slave) โดยอัตโนมัติ
การคอนฟิก Replication เป็นการป้องกันการเสียหาย ถ้าเครื่องหลักมีปัญหา ข้อมูลที่ถูกเก็บไว้ในเครื่องสำรอง ก็ยังสามารถใช้งานได้
จากบทความ ติดตั้งและคอนฟิก DRBD เราได้คอนฟิกเป็นแบบ Single-primary mode คือ ณ เวลาใดเวลาหนึ่ง มีเครื่องเดียวเท่านั้นที่สามารถ อ่าน เขียน ข้อมูลได้
ดังนั้นขณะที่เครื่องที่ 1 (centos54-a) ทำหน้าที่เป็น primary ของดิสก์ drbd0 อยู่ เครื่องที่ 2 (centos54-b) จะไม่สามารถ mount ดิสก์ drbd0 นี้ขึ้นมาใช้งานได้
ในบทความนี้จะแสดงการทดสอบใช้คำสั่งเพื่อเปลี่ยนโหมด Primary, Secondary ระหว่างเครื่องทั้งสอง
ตรวจสอบสถานะบนเครื่อง centos54-a
[root@centos54-a ~]# service drbd status drbd driver loaded OK; device status: version: 8.3.2 (api:88/proto:86-90) GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by mockbuild@v20z-x86-64.home.local, 2009-08-29 14:07:55 m:res cs ro ds p mounted fstype 0:r0 Connected Primary/Secondary UpToDate/UpToDate C /export ext3
[root@centos54-a ~]# df -h /export Filesystem Size Used Avail Use% Mounted on /dev/drbd0 950M 18M 885M 2% /export
ทดสอบ mount ดิสก์บนเครื่อง centos54-b
[root@centos54-b ~]# service drbd status drbd driver loaded OK; device status: version: 8.3.2 (api:88/proto:86-90) GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by mockbuild@v20z-x86-64.home.local, 2009-08-29 14:07:55 m:res cs ro ds p mounted fstype 0:r0 Connected Secondary/Primary UpToDate/UpToDate C
[root@centos54-b ~]# mount /dev/drbd0 /export mount: block device /dev/drbd0 is write-protected, mounting read-only mount: Wrong medium type
ในกรณีที่ต้องการเปลี่ยนให้เครื่องที่ 2 (centos54-b) ทำหน้าที่เป็น primary เช่นในกรณีที่ต้องการซ่อมบำรุงเครื่องที่ 1 สามารถทำได้ดังนี้
บนเครื่องที่ 1 ปิดเซอร์วิส DRBD
[root@centos54-a ~]# service drbd stop Stopping all DRBD resources: . [root@centos54-a ~]# service drbd status drbd not loaded
ตรวจสอบสถานะบนเครื่องที่ 2 หลังจากปิดเซอร์วิสบนเครื่องที่ 1
[root@centos54-b ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by mockbuild@v20z-x86-64.home.local, 2009-08-29 14:07:55
m:res cs ro ds p mounted fstype
0:r0 WFConnection Secondary/Unknown UpToDate/DUnknown C
แม้เครื่อง Primary ปิดไปแล้ว บนเครื่องที่ 2 ที่ยังอยู่ในโหมด Secondary ก็ยัง mount ดิสก์ ไม่ได้
[root@centos54-b ~]# mount /dev/drbd0 /export mount: block device /dev/drbd0 is write-protected, mounting read-only mount: Wrong medium type
ต้องเปลี่ยนสถานะบนเครื่องที่ 2 ให้เป็น primary ด้วยคำสั่ง drbdadm primary
[root@centos54-b ~]# drbdadm primary all
ตรวจสอบสถานะบนเครื่องที่ 2
[root@centos54-b ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by mockbuild@v20z-x86-64.home.local, 2009-08-29 14:07:55
m:res cs ro ds p mounted fstype
0:r0 WFConnection Primary/Unknown UpToDate/DUnknown C
เมื่ออยู่ในโหมด primary แล้ว ก็สามารถ mount ดิสก์ขึ้นมาใช้งานได้
[root@centos54-b ~]# mount /dev/drbd0 /export [root@centos54-b ~]# df -h /export Filesystem Size Used Avail Use% Mounted on /dev/drbd0 950M 18M 885M 2% /export
ทดลองสร้างไฟล์ในดิสก์ drbd0
[root@centos54-b ~]# cd /export/
[root@centos54-b export]# ls
lost+found
[root@centos54-b export]# echo "hello world from node 2" > test-file-on-node-2.txt
[root@centos54-b export]# ls -l
total 20
drwx------ 2 root root 16384 Feb 6 15:46 lost+found
-rw-r--r-- 1 root root 24 Feb 6 17:17 test-file-on-node-2.txt
รันเซอร์วิส DRBD บนเครื่องที่ 1 ขึ้นมาอีกครั้ง ตอนนี้เครื่องที่ 1 จะทำหน้าที่เป็น Secondary และจะทำการ replicate ข้อมูลมาจากเครื่องที่ 2 (Primary) โดยอัตโนมัติ
[root@centos54-a ~]# service drbd start Starting DRBD resources: [ d(r0) s(r0) n(r0) ].
[root@centos54-a ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by mockbuild@v20z-x86-64.home.local, 2009-08-29 14:07:55
m:res cs ro ds p mounted fstype
0:r0 Connected Secondary/Primary UpToDate/UpToDate C
บนเครื่องที่ 2 เปลี่ยนให้เป็นโหมด Secondary
[root@centos54-b ~]# drbdadm secondary all
0: State change failed: (-12) Device is held open by someone
Command 'drbdsetup 0 secondary' terminated with exit code 11
หากมีการเรียกใช้ดิสก์ drbd อยู่ จะไม่สามารถเปลี่ยนโหมดจาก Primary ไปเป็น Secondary ได้ ต้อง umount ดิสก์ออกไปก่อนถึงจะเปลี่ยนโหมดได้
[root@centos54-b ~]# umount /export [root@centos54-b ~]# drbdadm secondary all
ตรวจสอบสถานะบนเครื่องที่ 2
[root@centos54-b ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by mockbuild@v20z-x86-64.home.local, 2009-08-29 14:07:55
m:res cs ro ds p mounted fstype
0:r0 Connected Secondary/Secondary UpToDate/UpToDate C
เปลี่ยนโหมดบนเครื่องที่ 1 ให้เป็น Primary เพื่อเรียกใช้ดิสก์ได้
[root@centos54-a ~]# drbdadm primary all
[root@centos54-a ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by mockbuild@v20z-x86-64.home.local, 2009-08-29 14:07:55
m:res cs ro ds p mounted fstype
0:r0 Connected Primary/Secondary UpToDate/UpToDate C
mount ดิสก์ และตรวจสอบไฟล์ที่อยู่ใน drbd0 จะเห็นไฟล์ที่สร้างจากเครื่องที่ 2
[root@centos54-a ~]# mount /dev/drbd0 /export
[root@centos54-a ~]# cd /export/
[root@centos54-a export]# ls -l
total 20
drwx------ 2 root root 16384 Feb 6 15:46 lost+found
-rw-r--r-- 1 root root 24 Feb 6 17:17 test-file-on-node-2.txt
[root@centos54-a export]# cat test-file-on-node-2.txt
hello world from node 2
ตรวจสอบไฟล์ /var/log/messages
เมื่อมีปัญหาเกี่ยวกับการใช้ DBRD นอกจากการใช้คำสั่ง service เพื่อตรวจสอบสถานะแล้ว ไฟล์ /var/log/messages จะแสดงการเปลี่ยนแปลงทั้งหมดของ DRBD ที่เกิดขึ้น ซึ่งสามารถนำมาใช้ช่วยในการแก้ปัญหาได้
ตัวอย่างข้อความในไฟล์ /var/log/messages ที่เกิดขึ้น เมื่อมีการเปลี่ยนโหมด
[root@centos54-a ~]# tail /var/log/messages ... Feb 6 17:19:38 centos54-a kernel: block drbd0: peer( Primary -> Secondary ) Feb 6 17:21:04 centos54-a kernel: block drbd0: role( Secondary -> Primary ) ...
ข้อมูลอ้างอิง
DRBD (Distributed Replicated Block Device) เป็นโปรแกรมเพื่อใช้ในการ replicate ข้อมูลดิสก์ที่อยู่คนละเครื่องกัน ผ่านทางเน็ตเวิร์ก โดยจะทำในระดับ block ของดิสก์
เมื่อมีการเปลี่ยนแปลงข้อมูลที่อยูในดิสก์เครื่องหนึ่ง (primary) การเปลี่ยนแปลงนั้นจะถูก replicate ไปยังอีกเครื่อง (secondary) โดยอัตโนมัติ
DRBD ประกอบด้วยสองส่วนคือ
1. Kernel module – DRBD ทำหน้าที่ใน kernel โดยจะสร้าง virtual block device คั่นกลางระหว่าง physical disk กับ filesystem ที่สร้างอยู่ ไม่ว่าจะเป็น ext3, ext4, xfs
2. User space administration tools – เป็นโปรแกรมที่ช่วยในการจัดการ DRBD ได้ง่ายขึ้น หลักๆ จะเป็น drbdadm
ในบทความนี้จะแสดงการติดตั้งโปรแกรม DRBD และคอนฟิกเป็นแบบ Single-primary mode คือ ณ เวลาใดเวลาหนึ่ง มีเครื่องเดียวเท่านั้นที่สามารถ อ่าน เขียน ข้อมูลได้
คำเตือน ติดตั้งและคอนฟิก DRBD บนเครื่องทดสอบให้เข้าใจก่อน เพราะบางคำสั่งอาจกระทบข้อมูลดิสก์หรือ partition ที่มีอยู่ในเครื่องได้ ทำให้ข้อมูลเสียหายได้
เพิ่งทราบว่าโปรแกรม dvd+rw-tools (ณ เวลาที่เขียนบทความนี้) สามารถเขียนได้แต่แผ่นดีวีดี ถ้าใส่แผ่นซีดี (CD-R) เข้าไปในไดร์ฟที่เป็น DVD+RW แล้วใช้โปรแกรมนี้ทดลองเขียนไฟล์ลงบนแผ่นซีดี จะไม่สามารถเขียนได้
[root@server ~]# growisofs -Z /dev/dvd=data-backup.iso : -( /dev/dvd: media is not recognized as recordable DVD: 9
ถ้าลองใช้คำสั่ง dvd+rw-mediainfo ตรวจสอบก็จะมีข้อความฟ้องขึ้นมา
[root@server ~]# dvd+rw-mediainfo /dev/dvd INQUIRY: [ASUS ][DRW-2014L1T ][1.00] GET [CURRENT] CONFIGURATION: : -( non-DVD media mounted, exiting...
ถ้าต้องการเขียนไฟล์ลงบนแผ่นซีดี ต้องใช้คำสั่ง wodim (ชื่อเก่าคือโปรแกรม cdrecord) โดยจะมีวิธีการทำดังต่อไปนี้
หลังจากที่ประสบปัญหาเนื้อที่ในฮาร์ดดิสก์เหลือน้อยมาก จะลบไฟล์บางส่วนทิ้งไปเลยก็เสียดาย วันนี้เลยไปซื้อแผ่นดีวีดีมา เพื่อจัดการย้ายข้อมูลหรือไฟล์บางส่วนไรท์ลงแผ่นซะ (ถ้าจะให้ดูหรูหน่อยต้องบอกว่าเรากำลังทำ Information Lifecycle Management)
บทความนี้ขอแนะนำวิธีการเขียนไฟล์ลงแผ่นดีวีดีโดยใช้คำสั่งบนลินุกซ์
หมายเหตุ ถ้าต้องการเขียนซีดี สามารถวิธีการได้ที่ เขียนแผ่นซีดีบนลินุกซ์
read more…

