SlideShare une entreprise Scribd logo
1  sur  18
Télécharger pour lire hors ligne
MySQL Multi-Master Replication
using Tungsten Replicator 2.0.5
1/18
Table of Contents
บทนำ................................................................................................................................................3
Replication ทำงานอย่างไร.............................................................................................................. 3
ความแต่กต่างของ Replication แบบ Built-in เทียบกับ External Replication...................................4
ความแต่กต่างของ Log Replication เทียบกับ Trigger-Based Replication........................................4
สถาปัตยกรรมของ Tungsten Replicator...........................................................................................4
MySQL Replication.........................................................................................................................5
การติดตั้ง Tungsten Replicator บน Debian Linux........................................................................... 6
การเตรียม Debian Linux.............................................................................................................6
ทำการเซ็ทให้การเชื่อมต่อผ่าน SSH ทำได้โดยไม่ต้องใช้ password...........................................6
การเตรียมฐานข้อมูล MySQL......................................................................................................7
การสร้าง MySQL user................................................................................................................ 9
การติดตั้ง Ruby........................................................................................................................... 9
การเซ็ทค่า JAVA_HOME......................................................................................................... 10
การติดตั้ง Tungsten Replicator..................................................................................................10
วิธีเซ็ทให้ replicator service ทำงานอัตโนมัติ เมื่อ boot OS....................................................... 12
วิธีตรวจสอบการทำงานของ Tungsten Replicator..........................................................................13
การแก้ไขปัญหา............................................................................................................................. 14
Reference....................................................................................................................................... 16
Index of Figure
Figure 1: Master/Slave Replication......................................................................................................3
Figure 2: Replication Architecture....................................................................................................... 5
Figure 3: MySQL Replication Architecture......................................................................................... 5
2/18
บทนำ
Tungsten Replicator [1] เป็นซอฟต์แวร์ที่มีไว้เพื่อให้ DBMS สามารถทำ replication ได้ นอกเหนือจากความสามารถในการทำ
replication ซึ่งเป็นความสามารถพื้นฐานภายในตัว DBMS เอง แต่ Tungsten Replicator มีความยืดหยุ่นในการทำ replication
มากกว่า โดยการทำ replication ใน Tungsten Replicator จะเป็นแบบ master/slave แต่สามารถประยุคใช้ในการทำ replication
ได้หลายรูปแบบ เช่น All-masters Fan-in และ Star schema [2] นอกจากนั้นยังสามารถทำ replication ข้าม DBMS ต่างชนิดกัน
ได้เช่น ระหว่าง MySQL กับ Oracle เป็นต้น
Replication ทำงานอย่างไร
เนื่องจาก Tungsten Replicator ใช้ master/slave replication โดยที่การ update ใดๆจะทำจาก database server ตัวเดียว ซึ่ง
เรานิยมเรียก database ตัวนี้ว่า master และการ update นั้นจะถูกส่งต่อไปที่ทุกๆ database ที่เป็น replica โดยอัตโนมัติ หรือที่
เรานิยมเรียก database พวกนี้ว่า slave นั้นเอง
การทำ replication แบบ master/slave อาศัยหลักการง่ายๆคือ สมมติว่า database สองตัวเริ่มทำงานด้วยข้อมูลที่เหมือนกันทุก
ประการ (จาก snapshot เดียวกัน) เมื่อมีการเปลี่ยนแปลงใดๆใน database หนึ่ง การเปลี่ยนแปลงนั้นจะถูกบันทึกไว้ตามลำดับ เรา
สามารถทำให้ database อีกตัวเปลี่ยนแปลงเหมือนตัวแรกได้โดยนำบันทึกการเปลี่ยนแปลงไป replay บน database ตัวที่เหลือ
สาเหตุที่ Master/slave replication ได้รับความนิยมได้แก่
• database สามารถสร้าง และ reload snapshot โดยใช้ backup tools
• นอกจากการที่ database สามารถ update ข้อมูลได้อย่างรวดเร็วแล้ว มันยังสามารถเขียนการเปลี่ยนแปลงนั้นๆลง log
ไฟล์ โดย process ภายนอกสามารถเข้ามาอ่านเพื่อนำไปใช้งานต่อได้ได้ ซึ่งเป็นวิธีที่ Tungsten Replicator ใช้ในการทำ
replication
• ทำงานได้ดีแม้ใน network ที่มี latency สูง เช่นใน wide area networks (WAN)
อย่างไรก็ตาม Master/slave replication ก็มีข้อเสีย เช่น
• master database จะเป็น single point of failure การใช้ Master/slave จะต้องมีวิธีที่จะทำให้ master database ไม่
ล่ม
• การ update ใน slave จะช้ากว่า master เสมอ เนื่องจากขบวนการ update ข้อมูลมักจะเร็วกว่าขบวนการ replicate
ข้อมูลเพื่อไป update ใน slave
Tungsten Replicator ถูกออกแบบมาเพื่อแก้ไขข้อเสียของ Master/slave replication แบบเดิมๆ เช่น การจัดการขบวนการ
master failover ให้ถูกต้อง หรือหาวิธีให้ขบวนการ update บน replica ทำได้อย่างรวดเร็วขึ้น นอกจากนั้นยังมีความสามารถเช่น
filtering และ transformation ซึ่งทำให้สามารถจัดการกับปัญหาในการทำ heterogeneous data integration ได้ดีขึ้น
3/18
Figure 1: Master/Slave Replication
ความแต่กต่างของ Replication แบบ Built-in เทียบกับ External Replication
Replication เป็นเทคโนโลยีที่สำคัญ ซึ่งเป็นความสามารถพื้นฐานที่ทุกๆ DBMS ควรมี ดังนั้น DBMS ส่วนมากจึงได้พัฒนาความ
สามารถนี้และใส่มาใน DBMS ของตัวเอง
ข้อดีของ Built-in replication คือสามารถทำ replication ได้ดีกับ database ชนิดเดียวกัน แต่ก็มีข้อจำกัดในการทำ replication
กับ database คนละชนิด ต่าง version หรือแม้กระทั่งต่าง operating system นอกจากนั้นใน commercial database บางตัว
การทำ replication จะซับซ้อน และเป็นส่วนเสริมที่มีราคาแพง
Tungsten Replicator จัดว่าเป็น external replication ซึ่งมีข้อดีหลายอย่าง เช่นสามารถรองรับปัญหาอย่าง availability และ
scaling ได้ นอกจากนั้นยังมีข้อจำกัดน้อยกว่า เช่นมีข้อจำกัดในเรื่องการทำงานต่าง platform น้อยกว่า และสามารถทำงานกับ
database ต่างชนิดกันได้ดีกว่าเป็นต้น
ความแต่กต่างของ Log Replication เทียบกับ Trigger-Based Replication
Log-based replication จะอ่าน SQL update จาก recovery log ของ database ซึ่งภายในจะมี list ของการเปลี่ยนแปลง โดยตัว
database จะใช้ log นี้ในขบวนการ recovery เมื่อถูก restart นอกจากนั้น Log-based replication ยังมี overhead ในการทำ
replication ต่ำที่สุดเทียบกับการทำ replication ด้วยวิธีอื่น สามารถรองรับการเปลี่ยนแปลงได้กว้างกว่า และมีผลกับการบริหาร
จัดการน้อยกว่าอีกด้วย แต่อย่างไรก็ตาม Log-based replication ก็ implement ได้ยากกว่า เนื่องจากความซับซ้อนของ log
format ซึ่งใน RDBMS บางตัวไม่ได้มีเอกสารอธิบายรูปแบบข้อมูลในส่วนนี้อย่างเพียงพอ
Trigger-based replication จะเป็นการติดตั้ง trigger เพื่จับการ update ใน table โดยวิธีการนี้ implement ได้ง่ายกว่า Log-
based replication แต่ก็มีข้อเสียโดยจะทำให้ master database ทำงานช้าลง เพิ่มความซับซ้อนในการบริหารจัดการ และมีข้อจำกัด
ในการเปลี่ยนแปลง schema
Tungsten Replicator ใช้ log-based replication เนื่องจากมีประสิทธิภาพ และความยืดหยุ่นดีกว่า ถึงแม้ว่า Tungsten
Replicator จะเป็น open source ซอฟต์แวร์ แต่ใน database บางตัว ส่วนของ log reader จะไม่ open source ต้องเสียเงินซื้อ
เพิ่ม
สถาปัตยกรรมของ Tungsten Replicator
Tungsten Replicator เป็น process ที่ทำงานบนทุก host ใน cluster สถาปัตยกรรมของ Tungsten Replicator แสดงดังรูปที่ 2
4/18
Figure 2: Replication Architecture
จากรูปที่ 2 สถาปัตยกรรมของ Tungsten Replicator มีส่วนประกอบดังนี้
• Master DBMS – เป็น Database Management System (DBMS) ซึ่งทำหน้าที่เป็น master ในกระบวนการ
replication สถานะความเป็น master สามารถเปลี่ยนได้ โดยที่ DBMS ใดๆในระบบที่ทำ replication อาจถูกเลือกเป็น
master ก็ได้
• Slave DBMS - เป็น slave DBMS ซึ่งทำหน้าที่รับ replication event จาก master DBMS และเปลี่ยนแปลงข้อมูลตาม
event ที่ได้รับจาก master ในระบบที่ทำ replication อาจมีได้หลาย slave และเราสามารถเรียก slave ได้อีกอย่างว่า
replica
• Replication Event Extractor – ทำหน้าที่ในการอ่าน replication event จาก master DBMS logs โดย event อาจ
อยู่ในรูปแบบของ SQL statement หรือ row
• Transaction History Log – เป็นที่สำหรับใช้ในการเก็บ replication event และสื่อสารกับ transaction history log
อื่นๆใน cluster
• Replication Event Applier – ทำหน้าที่นำคำสั่งจาก replication event ไป update ลงใน slave DBMS.
• Node Manager – หมายถึงส่วนที่ทำหน้าที่จัดการส่วนอื่นๆใน Tungsten Replicator ซึ่งทำงานบน master node หรือ
slave node โดย node manager จะเชื่อมต่อกับ Tungsten service manager ในระดับที่สูงกว่า
MySQL Replication
Tungsten Replicator สามารถอ่าน events และ replicate MySQL ตั้งแต่ version 5.0 ขึ้นไป ซึ่งเปิดใช้ binary log (binlog)
Binary log มีหน้าที่เก็บข้อมูล native replication ของ MySQL Tungsten Replicator จะอ่านไฟล์ binlog ทำการ parsing และดึง
event ใหม่ออกมา โดย event นี้จะถูกเก็บใน Transaction History Log และถูกส่งต่อไปที่ Tungsten Replicator instance อื่น
รูปด้านล่างแสดง Tungsten Replicator architecture สำหรับ MySQL replication.
Tungsten Replicator มีความสามารถเหนือกว่า native MySQL replication ในหลายๆอย่างดังนี้
• สามารถจัดการ ขบวนการ master failover ในกรณีที่มี slave หลายตัว โดย Tungsten Replicator จะใส่ global
sequence number เข้าไปในทุกๆ SQL request ซึ่งทำให้ slave สามารถถูก promote ขึ้นมาเป็น master และการจาย
event ไปสู่ slave ที่เหลือได้ต่อไป
• สามารถทำ replication จาก MySQL version ใหม่ไป version ที่เก่ากว่าได้ เช่นจาก MySQL version 5.0 ไป version
4.1
• สามารถจัดการ replication ได้หลายรูปแบบ เช่น หนึ่ง master ไปสู่ หลาย slaves (fan-out) หลาย masters ไปสู่ หนึ่ง
slave (fan-in) และ circular replication ระหว่าง master สองตัว หรือมากกว่าได้
• สามารถ replicate จาก/ไปสู่ database ชนิดอื่น โดยใช้การ transform และ filter ข้อมูลที่ถูก replicate
5/18
Figure 3: MySQL Replication Architecture
การติดตั้ง Tungsten Replicator บน Debian Linux
การเตรียม Debian Linux
การทำ Multi-Master Replication จะใช้ database server อย่างน้อยสองเครื่องขึ้นไป ในเอกสารชุดนี้สมมติว่ามี MySQL server
ซึ่งถูกติดตั้งอยู่ใน server สองเครื่อง ทั้งสองเครื่องที่ถูกติดตั้งอยู่คนละ data center และเชื่อมต่อกันทาง VPN โดยที่เครื่องแรกให้ชื่อ
ว่า master1 และอีกเครื่องให้ชื่อว่า master2 ทั้งสองเครื่องมีซอฟต์แวร์ OpenSSH ติดตั้งและทำงานอยู่ที่ port 4005 สำหรับ VPN
master1 ใช้ IP 10.8.0.1 master2 ใช้ IP 10.8.0.1
ทั้ง master1 และ master2 ติดตั้งซอฟต์แวร์ที่จะเป็นได้แก่ rsync ruby และ jdk ด้วยคำสั่งดังนี้
$ sudo apt-get install rsync
$ sudo apt-get install ruby
$ sudo apt-get install default-jdk
ทั้ง master1 และ master2 แก้ไข hosts file เพื่อให้สามารถใช้ชื่อ master1 และ master2 แทนการอ้างอิงจาก IP ได้
$ sudo nano -w /etc/hosts
127.0.0.1 localhost
10.8.0.1 master1
10.8.0.2 master2
...
ทำการเซ็ทให้การเชื่อมต่อผ่าน SSH ทำได้โดยไม่ต้องใช้ password
master1
เพิ่ม user ชื่อ tungsten
$ sudo useradd -m -Gmysql -s /bin/bash tungsten
เซ็ท password ให้ user tungsten ตามต้องการ
$ sudo passwd tungsten
แก้ไขไฟล์ /etc/sudoers โดยใช้คำสั่ง # sudoedit /etc/sudoers และเพิ่มข้อมลด้านล่างเข้าไป
tungsten ALL=(ALL) NOPASSWD: ALL
เปลี่ยนจาก user root เป็น user tungsten
# su tungsten
ทำการสร้าง key ของ secure shell
$ ssh-keygen
6/18
เพิ่ม 2 บรรทัดนี้เข้าไป
copy key ไปที่ master2
$ ssh-copy-id '-p 4002 -i ~/.ssh/id_rsa.pub master2'
ทดสอบการเชื่อมต่อไปที่ master2
$ ssh master2
master2
เพิ่ม user ชื่อ tungsten
# useradd -m -Gmysql -s /bin/bash tungsten
เซ็ท password ให้ user tungsten ตามต้องการ
# passwd tungsten
แก้ไขไฟล์ /etc/sudoers โดยใช้คำสั่ง # sudoedit /etc/sudoers และเพิ่มข้อมลด้านล่างเข้าไป
tungsten ALL=(ALL) NOPASSWD: ALL
เปลี่ยนจาก user root เป็น user tungsten
# su tungsten
ทำการสร้าง key ของ secure shell
$ ssh-keygen
copy key ไปที่ master1
$ ssh-copy-id '-p 4001 -i ~/.ssh/id_rsa.pub master1'
ทดสอบการเชื่อมต่อไปที่ master2
$ ssh master1
การเตรียมฐานข้อมูล MySQL
Master1
ทำการแก้ไขไฟล์ configuration ของ MySQL
$ sudo vim /etc/mysql/my.cnf
แก้ไขข้อมูลของ MySQL configuration ดังนี้
[mysqld]
# bind-address = 127.0.0.1
server-id = 1
7/18
# set increment for up to 4 servers
auto_increment_increment = 4
# increment offset for this server, next server would be 2
auto_increment_offset = 1
log_bin = mysql-bin
innodb_buffer_pool_size = 512M
# Recommended InnoDB settings for Tungsten.
default-storage-engine=InnoDB
innodb_flush_log_at_trx_commit=2
sync_binlog=0
# Recommended general settings.
# max_allowed_packet must be greater than
# the size of the largest transaction.
max_allowed_packet=48m
default-table-type=InnoDB
innodb_flush_log_at_trx_commit=2
master2
ทำการแก้ไขไฟล์ configuration ของ MySQL
$ sudo vim /etc/mysql/my.cnf
แก้ไขข้อมูลของ MySQL configuration ดังนี้
[mysqld]
#bind-address = 127.0.0.1
server-id = 2
# set increment for up to 4 servers
auto_increment_increment = 4
# increment offset for this server, next server would be 2
auto_increment_offset = 2
log_bin = mysql-bin
8/18
innodb_buffer_pool_size = 512M
# Recommended InnoDB settings for Tungsten.
default-storage-engine=InnoDB
innodb_flush_log_at_trx_commit=2
sync_binlog=0
# Recommended general settings.
# max_allowed_packet must be greater than
# the size of the largest transaction.
max_allowed_packet=48m
default-table-type=InnoDB
innodb_flush_log_at_trx_commit=2
การสร้าง MySQL user
ทั้ง master1 และ master2 ให้เพิ่ม user tungsten ให้ฐานข้อมูล MySQL ดังนี้
$ mysql -u root -p
mysql> grant all on *.* to tungsten@'%' identified by 'tungsten' with grant option;
mysql> FLUSH PRIVILEGES;
การติดตั้ง Ruby
ติดตั้ง Ruby และ library ที่จำเป็นใน master1 และ master2
$ sudo apt-get install -y ruby libopenssl-ruby
ทดสอบ Ruby ว่าติดตั้งเรียบร้อยหรือไม่
$ echo "p 'hello'" | ruby -ropenssl
จะต้องแสดงข้อความ "hello"
9/18
การเซ็ทค่า JAVA_HOME
ทำการเซ็ท JAVA_HOME และ PATH ในไฟล์ /etc/bash.bashrc ดังนี้
$sudo vim /etc/bash.bashrc
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_06
export PATH=$PATH:$JAVA_HOME/bin
การติดตั้ง Tungsten Replicator
master1
กระจายไฟล์ installer ของ Tungsten Replicator ไปที่ /opt ด้วยคำสั่ง
$ sudo tar zxfv tungsten-replicator-2.0.5.tar.gz -C /opt/
สร้างไฟล์ setup-masters.sh
$ sudo vim /opt/tungsten-replicator-2.0.5/setup-masters.sh
ใส่ข้อมูลด้านล่างนี้เข้าไป
#! /bin/bash
TUNGSTEN_HOME=/home/tungsten
MYSQL_CONF=/etc/mysql/my.cnf
MASTER1=master1
MASTER2=master2
./tools/tungsten-installer 
--master-slave 
--master-host=$MASTER1 
--datasource-mysql-conf=$MYSQL_CONF 
--datasource-user=tungsten 
--datasource-password=tungsten 
--service-name=dc 
--home-directory=$TUNGSTEN_HOME 
--cluster-hosts=$MASTER1 
--start-and-report
./tools/tungsten-installer 
--master-slave 
--master-host=$MASTER2 
--datasource-mysql-conf=$MYSQL_CONF 
--datasource-user=tungsten 
--datasource-password=tungsten 
--service-name=dr 
--home-directory=$TUNGSTEN_HOME 
--cluster-hosts=$MASTER2 
--start-and-report
10/18
สร้างไฟล์ setup-slaves.sh
$ sudo vim /opt/tungsten-replicator-2.0.5/setup-slaves.sh
ใส่ข้อมูลนี้เข้าไป
#! /bin/bash
MASTER1=master1
MASTER2=master2
TUNGSTEN_TOOLS=tools
$TUNGSTEN_TOOLS/configure-service 
--host $MASTER1 
-C -q 
--local-service-name=dc 
--role=slave 
--service-type=remote 
--datasource=$MASTER1 
--master-thl-host=$MASTER2 
--svc-start dr
$TUNGSTEN_TOOLS/configure-service 
--host $MASTER2 
-C -q 
--local-service-name=dr 
--role=slave 
--service-type=remote 
--datasource=$MASTER2 
--master-thl-host=$MASTER1 
--svc-start dc
ทำการเซ็ทให้ owner และ group ของ installer directory เป็น tungsten โดยใช้คำสั่ง
$ sudo chown -R tungsten:tungsten /opt/tungsten-replicator-2.0.5
เปลี่ยน user ปัจจุบันให้เป็น tungsten
$ su tungsten
ทำการติดตั้ง Tungsten Replicator โดยใช้คำสั่งด้านล่าง ตัว Tungsten Replicator จะทำการติดตั้งลงใน master1 และ master2
โดยอัตโนมัติ
$ cd /opt/tungsten-replicator-2.0.5
$ sh setup-masters.sh
$ cd ~
$ cd tungsten
$ sh setup-slaves.sh
11/18
วิธีเซ็ทให้ replicator service ทำงานอัตโนมัติ เมื่อ boot OS
# ln -s /home/tungsten/tungsten/tungsten-replicator/bin/replicator /etc/init.d/replicator
# update-rc.d replicator defaults
12/18
วิธีตรวจสอบการทำงานของ Tungsten Replicator
เราสามารถตรวจสอบการทำงานของ Tungsten Replicator ได้โดย login เข้าไปในระบบที่ติดตั้ง Tungsten Replicator จากนั้น
เปลี่ยน user ให้เป็น user tungsten ด้วยคำสั่ง
# su – tungsten
จากนั้นเข้าไปใน directory tungsten/tungsten-replicator/
$ cd tungsten/tungsten-replicator/
พิมพ์คำสั่ง bin/trepctl services
$ bin/trepctl services
จะได้ผลลัพธ์ดังนี้
Processing services command...
NAME VALUE
---- -----
appliedLastSeqno: 13154
appliedLatency : 1.0
role : master
serviceName : dc
serviceType : local
started : true
state : ONLINE
NAME VALUE
---- -----
appliedLastSeqno: 8876
appliedLatency : 4363.722
role : slave
serviceName : dr
serviceType : remote
started : true
state : ONLINE
Finished services command...
13/18
ในส่วนของ state ของ service ทั้งสองฝั่งจะต้องเป็น ONLINE เท่านั้น จึงจะถือว่าสถานะการทำ replication เป็นปกติ นอกจาก
ONLINE แล้ว state อาจมีค่าเป็น GOING-ONLINE:SYNCHRONIZING หมายความว่า Tungsten Replication กำลัง replicate
ข้อมูล ซึ่งถ้าสามารถ replicate โดยที่ไม่พบปัญหาใดๆ state ก็จะเปลี่ยนกลับมาเป็น ONLINE แต่ถ้าพบปัญหา state จะเปลี่ยนไป
เป็น OFFLINE:ERROR ซึ่งต้องทำการแก้ไขเพื่อให้ replication กลับมาทำงานได้ปกติ
14/18
15/18
การแก้ไขปัญหา
1. กรณีข้อมูลไม่ถูก replicate ไปที่ slave
ใช้คำสั่ง
tungsten-replicator/bin/trepctl services
เพื่อตรวจสอบว่า service ทั้งหมดอยู่ในสถานะ ONLINE ถ้าหากมี service ใดไม่อยูในสถานะ ONLINE ให้ใช้คำสั่ง
tungsten-replicator/bin/trepctl -service service_name status
เพื่อดูข้อมูลเพิ่มเติมว่ามีปัญหาอะไรเกิดขึ้น
ทำการ review log file ใน tungsten-replicator/log/user.log และ
tungsten-replicator/log/trepsvc.log เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับสาเหตุของปัญหา และเมื่อแก้ปัญหาได้แล้วให้
restart replication service โดยใช้คำสั่ง
tungsten-replicator/bin/trepctl -service service_name online
2. กรณีพบ error ใน log file บอกว่า Event application failed: seqno=3 fragno=0
message=java.sql.SQLException: Statement failed on slave but succeeded on master
การแก้ไขปัญหานี้จะต้องแก้ไขที่ transaction ที่พบปัญหา จากนั้นจึง restart replication เพื่อสั่งให้ replication ทำงาน
ต่อ
ขั้นแรกให้ทำการหา error com.continuent.tungsten.replicator.applier.ApplierException ใน log file
tungsten-replicator/log/trepsvc.log stacktrace จะเป็นตัวบอกว่าปัญหาเกิดจากอะไร ถ้าหากเราต้องการข้อมูลเพิ่มเติม
เกี่ยวกับ transaction เราสามารถใช้ โปรแกรม thl ในการดูข้อมูลของ event ได้โดยตรง เช่นใช้คำสั่ง thl เพื่อดูข้อมูล
transaction ของ service ชื่อ service_name sequence number 3 สามารถใช้คำสั่งดังนี้
./tungsten-replicator/bin/thl -service service_name list -seqno 3

โปรแกรม thl จะแสดงผลลัพธ์ดังนี้
SEQ# = 3 / FRAG# = 0 (last frag)
- TIME = 2011-09-14 09:06:42.0
- EPOCH# = 0
- EVENTID = mysql-bin.000002:0000000000000592;11
- SOURCEID = db1
- METADATA = [mysql_server_id=101;service=service_name;shard=test]
- TYPE = com.continuent.tungsten.replicator.event.ReplDBMSEvent
- SQL(0) = SET INSERT_ID = 2
- OPTIONS = [##charset = ISO8859_1, autocommit = 1, sql_auto_is_null = 1, foreign_key_checks = 1, unique_checks = 1,
16/18
sql_mode = '', character_set_client = 8, collation_connection = 8, collation_server = 8]
- SCHEMA =
- SQL(1) = insert into test.names (name) values ('Robert')
การแก้ปัญหาสามารถทำได้หลายวิธี บางครั้งวิธีที่เหมาะสมอาจทำได้โดยสั่ง statement ที่มีปัญหานั้นๆด้วยตัวเองโดยอาจ
จะทำจาก command line interface หรือ GUI tool ก็ได้
บางครั้งเราอาจต้อง skip transaction ที่มีปัญหานั้นๆไปก่อนเพื่อทำให้ replication service online ได้อีกครั้ง จากนั้นจึง
ค่อยนำข้อมูลที่ขาดหายไปใส่เข้าไปในฐานข้อมูลอีกครั้งถ้าจำเป็น เช่นถ้าต้องการ skip transaction sequence number
2340 ของ service service_name ซึ่งมีปัญหาอยู่ สามารถทำได้โดยใช้คำสั่ง
./tungsten-replicator/bin/trepctl -service service_name online -skip-seqno 2340
เมื่อ skip transaction ที่มีปัญหาหมดแล้ว จึงสั่งให้ replication online อีกครั้งด้วยคำสั่ง
./tungsten-replicator/bin/trepctl -service service_name online
17/18
Reference
[1] Tungsten Replicator, Available At. URL: http://code.google.com/p/tungsten-replicator/
[2] Tungsten Replicator cookbook. Advanced replication topologies made easy, Available At. URL:
http://datacharmer.blogspot.com/2012/10/tungsten-replicator-cookbook-advanced.html
[3] How To Set Up Multi-Master Replication Using Tungsten And MySQL-Proxy For MySQL High Availability On
Ubuntu 10.04.3 LTS; http://www.howtoforge.com/how-to-set-up-multi-master-replication-using-tungsten-and-
mysql-proxy-for-mysql-high-availability-on-ubuntu-10.04.3-lts
[4] Tungsten Replicator Pre-Requisites; http://code.google.com/p/tungsten-replicator/wiki/InstallationPreRequisites
[5] Understanding Tungsten Replication Services; http://scale-out-blog.blogspot.com/2011/03/understanding-
tungsten-replication.html
[6] Tungsten Replication for MySQL users; http://code.google.com/p/tungsten-replicator/wiki/Cheat_Sheet
[7] How To Back Up MySQL Databases With mylvmbackup On Debian Squeeze;
http://www.howtoforge.com/how-to-back-up-mysql-databases-with-mylvmbackup-on-debian-squeeze
18/18

Contenu connexe

Tendances

ลักษณะข้อดีและข้อด้อยของอุปกรณ์เชื่อมต่อคอมพิวเตอร์
ลักษณะข้อดีและข้อด้อยของอุปกรณ์เชื่อมต่อคอมพิวเตอร์ลักษณะข้อดีและข้อด้อยของอุปกรณ์เชื่อมต่อคอมพิวเตอร์
ลักษณะข้อดีและข้อด้อยของอุปกรณ์เชื่อมต่อคอมพิวเตอร์Silver Bullet
 
Universal Description, Discovery and Integration (UDDI) by ANIKET CHOUDHURY
Universal Description, Discovery and Integration (UDDI) by ANIKET CHOUDHURYUniversal Description, Discovery and Integration (UDDI) by ANIKET CHOUDHURY
Universal Description, Discovery and Integration (UDDI) by ANIKET CHOUDHURYअनिकेत चौधरी
 
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentalsDB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentalsJohn Beresniewicz
 
แนวข้อสอบภาษาต่างประเทศ นสต ม6
แนวข้อสอบภาษาต่างประเทศ นสต ม6แนวข้อสอบภาษาต่างประเทศ นสต ม6
แนวข้อสอบภาษาต่างประเทศ นสต ม6Marr Ps
 
แบบร่างโครงงาน
แบบร่างโครงงานแบบร่างโครงงาน
แบบร่างโครงงานJaraweekorn Udkhammee
 
พ.อ.ดร.เศรษฐพงค์ Infrastructure sharing
พ.อ.ดร.เศรษฐพงค์ Infrastructure sharingพ.อ.ดร.เศรษฐพงค์ Infrastructure sharing
พ.อ.ดร.เศรษฐพงค์ Infrastructure sharingSettapong-Broadband
 
Informatica
InformaticaInformatica
Informaticamukharji
 
Enterprise application development
Enterprise application developmentEnterprise application development
Enterprise application developmentcolleges
 
AWR Ambiguity: Performance reasoning when the numbers don't add up
AWR Ambiguity: Performance reasoning when the numbers don't add upAWR Ambiguity: Performance reasoning when the numbers don't add up
AWR Ambiguity: Performance reasoning when the numbers don't add upJohn Beresniewicz
 
Informatica partitions
Informatica partitionsInformatica partitions
Informatica partitionssingh100
 
Oracle Database Security
Oracle Database SecurityOracle Database Security
Oracle Database SecurityTroy Kitch
 

Tendances (16)

ลักษณะข้อดีและข้อด้อยของอุปกรณ์เชื่อมต่อคอมพิวเตอร์
ลักษณะข้อดีและข้อด้อยของอุปกรณ์เชื่อมต่อคอมพิวเตอร์ลักษณะข้อดีและข้อด้อยของอุปกรณ์เชื่อมต่อคอมพิวเตอร์
ลักษณะข้อดีและข้อด้อยของอุปกรณ์เชื่อมต่อคอมพิวเตอร์
 
Universal Description, Discovery and Integration (UDDI) by ANIKET CHOUDHURY
Universal Description, Discovery and Integration (UDDI) by ANIKET CHOUDHURYUniversal Description, Discovery and Integration (UDDI) by ANIKET CHOUDHURY
Universal Description, Discovery and Integration (UDDI) by ANIKET CHOUDHURY
 
153 Oracle dba interview questions
153 Oracle dba interview questions153 Oracle dba interview questions
153 Oracle dba interview questions
 
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentalsDB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
 
แนวข้อสอบภาษาต่างประเทศ นสต ม6
แนวข้อสอบภาษาต่างประเทศ นสต ม6แนวข้อสอบภาษาต่างประเทศ นสต ม6
แนวข้อสอบภาษาต่างประเทศ นสต ม6
 
Query processing
Query processingQuery processing
Query processing
 
ssis lab
ssis labssis lab
ssis lab
 
Database Security
Database SecurityDatabase Security
Database Security
 
แบบร่างโครงงาน
แบบร่างโครงงานแบบร่างโครงงาน
แบบร่างโครงงาน
 
พ.อ.ดร.เศรษฐพงค์ Infrastructure sharing
พ.อ.ดร.เศรษฐพงค์ Infrastructure sharingพ.อ.ดร.เศรษฐพงค์ Infrastructure sharing
พ.อ.ดร.เศรษฐพงค์ Infrastructure sharing
 
Ch06th
Ch06thCh06th
Ch06th
 
Informatica
InformaticaInformatica
Informatica
 
Enterprise application development
Enterprise application developmentEnterprise application development
Enterprise application development
 
AWR Ambiguity: Performance reasoning when the numbers don't add up
AWR Ambiguity: Performance reasoning when the numbers don't add upAWR Ambiguity: Performance reasoning when the numbers don't add up
AWR Ambiguity: Performance reasoning when the numbers don't add up
 
Informatica partitions
Informatica partitionsInformatica partitions
Informatica partitions
 
Oracle Database Security
Oracle Database SecurityOracle Database Security
Oracle Database Security
 

Similaire à MySQL Multi-Master Replication Using Tungsten Replicator 2.0.5

Java Web Programming [Servlet/JSP] Using GlassFish and NetBeans
Java Web Programming [Servlet/JSP] Using GlassFish and NetBeansJava Web Programming [Servlet/JSP] Using GlassFish and NetBeans
Java Web Programming [Servlet/JSP] Using GlassFish and NetBeansIMC Institute
 
Java Web Programming Using NetBeans 6.5
Java Web Programming Using NetBeans 6.5Java Web Programming Using NetBeans 6.5
Java Web Programming Using NetBeans 6.5Thanachart Numnonda
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์nattarikaii
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์nattarikaii
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์nattarikaii
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์nattarikaii
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์nattarikaii
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์nattarikaii
 
Operating System Chapter 1
Operating System Chapter 1Operating System Chapter 1
Operating System Chapter 1Nuth Otanasap
 
Scalable Distributed Data Structures for Internet Construction
Scalable Distributed Data Structures for Internet ConstructionScalable Distributed Data Structures for Internet Construction
Scalable Distributed Data Structures for Internet Constructionknopporn
 
ระบบคอมพิวเตอร์ส่ง
ระบบคอมพิวเตอร์ส่งระบบคอมพิวเตอร์ส่ง
ระบบคอมพิวเตอร์ส่งpanida21
 
ระบบคอมพิวเตอร์ส่ง
ระบบคอมพิวเตอร์ส่งระบบคอมพิวเตอร์ส่ง
ระบบคอมพิวเตอร์ส่งorawan34
 

Similaire à MySQL Multi-Master Replication Using Tungsten Replicator 2.0.5 (20)

Database Tuning for e-Learning
Database Tuning for e-LearningDatabase Tuning for e-Learning
Database Tuning for e-Learning
 
Computer systemarchitecture
Computer systemarchitectureComputer systemarchitecture
Computer systemarchitecture
 
Java Web Programming [Servlet/JSP] Using GlassFish and NetBeans
Java Web Programming [Servlet/JSP] Using GlassFish and NetBeansJava Web Programming [Servlet/JSP] Using GlassFish and NetBeans
Java Web Programming [Servlet/JSP] Using GlassFish and NetBeans
 
Java Web programming Using NetBeans
Java Web programming Using NetBeansJava Web programming Using NetBeans
Java Web programming Using NetBeans
 
Java Web Programming Using NetBeans 6.5
Java Web Programming Using NetBeans 6.5Java Web Programming Using NetBeans 6.5
Java Web Programming Using NetBeans 6.5
 
Active Directory
Active DirectoryActive Directory
Active Directory
 
Kafka for developer
Kafka for developerKafka for developer
Kafka for developer
 
J2 ee คืออะไร
J2 ee คืออะไรJ2 ee คืออะไร
J2 ee คืออะไร
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์
 
ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์ระบบคอมพิวเตอร์
ระบบคอมพิวเตอร์
 
Operating System Chapter 1
Operating System Chapter 1Operating System Chapter 1
Operating System Chapter 1
 
Scalable Distributed Data Structures for Internet Construction
Scalable Distributed Data Structures for Internet ConstructionScalable Distributed Data Structures for Internet Construction
Scalable Distributed Data Structures for Internet Construction
 
ระบบคอมพิวเตอร์ส่ง
ระบบคอมพิวเตอร์ส่งระบบคอมพิวเตอร์ส่ง
ระบบคอมพิวเตอร์ส่ง
 
ระบบคอมพิวเตอร์ส่ง
ระบบคอมพิวเตอร์ส่งระบบคอมพิวเตอร์ส่ง
ระบบคอมพิวเตอร์ส่ง
 
Ch1 com tech
Ch1 com techCh1 com tech
Ch1 com tech
 
650 1
650 1650 1
650 1
 

Plus de Nont Banditwong

Regular expressions-cheat-sheet-v2
Regular expressions-cheat-sheet-v2Regular expressions-cheat-sheet-v2
Regular expressions-cheat-sheet-v2Nont Banditwong
 
การประมูลหมายเลขทะเบียนรถ
การประมูลหมายเลขทะเบียนรถการประมูลหมายเลขทะเบียนรถ
การประมูลหมายเลขทะเบียนรถNont Banditwong
 
The Function Pointer Tutorials
The Function Pointer TutorialsThe Function Pointer Tutorials
The Function Pointer TutorialsNont Banditwong
 
Google Product Dev Process
Google Product Dev ProcessGoogle Product Dev Process
Google Product Dev ProcessNont Banditwong
 

Plus de Nont Banditwong (7)

Regex cheatsheet
Regex cheatsheetRegex cheatsheet
Regex cheatsheet
 
Regular expressions-cheat-sheet-v2
Regular expressions-cheat-sheet-v2Regular expressions-cheat-sheet-v2
Regular expressions-cheat-sheet-v2
 
การประมูลหมายเลขทะเบียนรถ
การประมูลหมายเลขทะเบียนรถการประมูลหมายเลขทะเบียนรถ
การประมูลหมายเลขทะเบียนรถ
 
Svn refcard
Svn refcardSvn refcard
Svn refcard
 
Svn workflow
Svn workflowSvn workflow
Svn workflow
 
The Function Pointer Tutorials
The Function Pointer TutorialsThe Function Pointer Tutorials
The Function Pointer Tutorials
 
Google Product Dev Process
Google Product Dev ProcessGoogle Product Dev Process
Google Product Dev Process
 

MySQL Multi-Master Replication Using Tungsten Replicator 2.0.5

  • 1. MySQL Multi-Master Replication using Tungsten Replicator 2.0.5 1/18
  • 2. Table of Contents บทนำ................................................................................................................................................3 Replication ทำงานอย่างไร.............................................................................................................. 3 ความแต่กต่างของ Replication แบบ Built-in เทียบกับ External Replication...................................4 ความแต่กต่างของ Log Replication เทียบกับ Trigger-Based Replication........................................4 สถาปัตยกรรมของ Tungsten Replicator...........................................................................................4 MySQL Replication.........................................................................................................................5 การติดตั้ง Tungsten Replicator บน Debian Linux........................................................................... 6 การเตรียม Debian Linux.............................................................................................................6 ทำการเซ็ทให้การเชื่อมต่อผ่าน SSH ทำได้โดยไม่ต้องใช้ password...........................................6 การเตรียมฐานข้อมูล MySQL......................................................................................................7 การสร้าง MySQL user................................................................................................................ 9 การติดตั้ง Ruby........................................................................................................................... 9 การเซ็ทค่า JAVA_HOME......................................................................................................... 10 การติดตั้ง Tungsten Replicator..................................................................................................10 วิธีเซ็ทให้ replicator service ทำงานอัตโนมัติ เมื่อ boot OS....................................................... 12 วิธีตรวจสอบการทำงานของ Tungsten Replicator..........................................................................13 การแก้ไขปัญหา............................................................................................................................. 14 Reference....................................................................................................................................... 16 Index of Figure Figure 1: Master/Slave Replication......................................................................................................3 Figure 2: Replication Architecture....................................................................................................... 5 Figure 3: MySQL Replication Architecture......................................................................................... 5 2/18
  • 3. บทนำ Tungsten Replicator [1] เป็นซอฟต์แวร์ที่มีไว้เพื่อให้ DBMS สามารถทำ replication ได้ นอกเหนือจากความสามารถในการทำ replication ซึ่งเป็นความสามารถพื้นฐานภายในตัว DBMS เอง แต่ Tungsten Replicator มีความยืดหยุ่นในการทำ replication มากกว่า โดยการทำ replication ใน Tungsten Replicator จะเป็นแบบ master/slave แต่สามารถประยุคใช้ในการทำ replication ได้หลายรูปแบบ เช่น All-masters Fan-in และ Star schema [2] นอกจากนั้นยังสามารถทำ replication ข้าม DBMS ต่างชนิดกัน ได้เช่น ระหว่าง MySQL กับ Oracle เป็นต้น Replication ทำงานอย่างไร เนื่องจาก Tungsten Replicator ใช้ master/slave replication โดยที่การ update ใดๆจะทำจาก database server ตัวเดียว ซึ่ง เรานิยมเรียก database ตัวนี้ว่า master และการ update นั้นจะถูกส่งต่อไปที่ทุกๆ database ที่เป็น replica โดยอัตโนมัติ หรือที่ เรานิยมเรียก database พวกนี้ว่า slave นั้นเอง การทำ replication แบบ master/slave อาศัยหลักการง่ายๆคือ สมมติว่า database สองตัวเริ่มทำงานด้วยข้อมูลที่เหมือนกันทุก ประการ (จาก snapshot เดียวกัน) เมื่อมีการเปลี่ยนแปลงใดๆใน database หนึ่ง การเปลี่ยนแปลงนั้นจะถูกบันทึกไว้ตามลำดับ เรา สามารถทำให้ database อีกตัวเปลี่ยนแปลงเหมือนตัวแรกได้โดยนำบันทึกการเปลี่ยนแปลงไป replay บน database ตัวที่เหลือ สาเหตุที่ Master/slave replication ได้รับความนิยมได้แก่ • database สามารถสร้าง และ reload snapshot โดยใช้ backup tools • นอกจากการที่ database สามารถ update ข้อมูลได้อย่างรวดเร็วแล้ว มันยังสามารถเขียนการเปลี่ยนแปลงนั้นๆลง log ไฟล์ โดย process ภายนอกสามารถเข้ามาอ่านเพื่อนำไปใช้งานต่อได้ได้ ซึ่งเป็นวิธีที่ Tungsten Replicator ใช้ในการทำ replication • ทำงานได้ดีแม้ใน network ที่มี latency สูง เช่นใน wide area networks (WAN) อย่างไรก็ตาม Master/slave replication ก็มีข้อเสีย เช่น • master database จะเป็น single point of failure การใช้ Master/slave จะต้องมีวิธีที่จะทำให้ master database ไม่ ล่ม • การ update ใน slave จะช้ากว่า master เสมอ เนื่องจากขบวนการ update ข้อมูลมักจะเร็วกว่าขบวนการ replicate ข้อมูลเพื่อไป update ใน slave Tungsten Replicator ถูกออกแบบมาเพื่อแก้ไขข้อเสียของ Master/slave replication แบบเดิมๆ เช่น การจัดการขบวนการ master failover ให้ถูกต้อง หรือหาวิธีให้ขบวนการ update บน replica ทำได้อย่างรวดเร็วขึ้น นอกจากนั้นยังมีความสามารถเช่น filtering และ transformation ซึ่งทำให้สามารถจัดการกับปัญหาในการทำ heterogeneous data integration ได้ดีขึ้น 3/18 Figure 1: Master/Slave Replication
  • 4. ความแต่กต่างของ Replication แบบ Built-in เทียบกับ External Replication Replication เป็นเทคโนโลยีที่สำคัญ ซึ่งเป็นความสามารถพื้นฐานที่ทุกๆ DBMS ควรมี ดังนั้น DBMS ส่วนมากจึงได้พัฒนาความ สามารถนี้และใส่มาใน DBMS ของตัวเอง ข้อดีของ Built-in replication คือสามารถทำ replication ได้ดีกับ database ชนิดเดียวกัน แต่ก็มีข้อจำกัดในการทำ replication กับ database คนละชนิด ต่าง version หรือแม้กระทั่งต่าง operating system นอกจากนั้นใน commercial database บางตัว การทำ replication จะซับซ้อน และเป็นส่วนเสริมที่มีราคาแพง Tungsten Replicator จัดว่าเป็น external replication ซึ่งมีข้อดีหลายอย่าง เช่นสามารถรองรับปัญหาอย่าง availability และ scaling ได้ นอกจากนั้นยังมีข้อจำกัดน้อยกว่า เช่นมีข้อจำกัดในเรื่องการทำงานต่าง platform น้อยกว่า และสามารถทำงานกับ database ต่างชนิดกันได้ดีกว่าเป็นต้น ความแต่กต่างของ Log Replication เทียบกับ Trigger-Based Replication Log-based replication จะอ่าน SQL update จาก recovery log ของ database ซึ่งภายในจะมี list ของการเปลี่ยนแปลง โดยตัว database จะใช้ log นี้ในขบวนการ recovery เมื่อถูก restart นอกจากนั้น Log-based replication ยังมี overhead ในการทำ replication ต่ำที่สุดเทียบกับการทำ replication ด้วยวิธีอื่น สามารถรองรับการเปลี่ยนแปลงได้กว้างกว่า และมีผลกับการบริหาร จัดการน้อยกว่าอีกด้วย แต่อย่างไรก็ตาม Log-based replication ก็ implement ได้ยากกว่า เนื่องจากความซับซ้อนของ log format ซึ่งใน RDBMS บางตัวไม่ได้มีเอกสารอธิบายรูปแบบข้อมูลในส่วนนี้อย่างเพียงพอ Trigger-based replication จะเป็นการติดตั้ง trigger เพื่จับการ update ใน table โดยวิธีการนี้ implement ได้ง่ายกว่า Log- based replication แต่ก็มีข้อเสียโดยจะทำให้ master database ทำงานช้าลง เพิ่มความซับซ้อนในการบริหารจัดการ และมีข้อจำกัด ในการเปลี่ยนแปลง schema Tungsten Replicator ใช้ log-based replication เนื่องจากมีประสิทธิภาพ และความยืดหยุ่นดีกว่า ถึงแม้ว่า Tungsten Replicator จะเป็น open source ซอฟต์แวร์ แต่ใน database บางตัว ส่วนของ log reader จะไม่ open source ต้องเสียเงินซื้อ เพิ่ม สถาปัตยกรรมของ Tungsten Replicator Tungsten Replicator เป็น process ที่ทำงานบนทุก host ใน cluster สถาปัตยกรรมของ Tungsten Replicator แสดงดังรูปที่ 2 4/18 Figure 2: Replication Architecture
  • 5. จากรูปที่ 2 สถาปัตยกรรมของ Tungsten Replicator มีส่วนประกอบดังนี้ • Master DBMS – เป็น Database Management System (DBMS) ซึ่งทำหน้าที่เป็น master ในกระบวนการ replication สถานะความเป็น master สามารถเปลี่ยนได้ โดยที่ DBMS ใดๆในระบบที่ทำ replication อาจถูกเลือกเป็น master ก็ได้ • Slave DBMS - เป็น slave DBMS ซึ่งทำหน้าที่รับ replication event จาก master DBMS และเปลี่ยนแปลงข้อมูลตาม event ที่ได้รับจาก master ในระบบที่ทำ replication อาจมีได้หลาย slave และเราสามารถเรียก slave ได้อีกอย่างว่า replica • Replication Event Extractor – ทำหน้าที่ในการอ่าน replication event จาก master DBMS logs โดย event อาจ อยู่ในรูปแบบของ SQL statement หรือ row • Transaction History Log – เป็นที่สำหรับใช้ในการเก็บ replication event และสื่อสารกับ transaction history log อื่นๆใน cluster • Replication Event Applier – ทำหน้าที่นำคำสั่งจาก replication event ไป update ลงใน slave DBMS. • Node Manager – หมายถึงส่วนที่ทำหน้าที่จัดการส่วนอื่นๆใน Tungsten Replicator ซึ่งทำงานบน master node หรือ slave node โดย node manager จะเชื่อมต่อกับ Tungsten service manager ในระดับที่สูงกว่า MySQL Replication Tungsten Replicator สามารถอ่าน events และ replicate MySQL ตั้งแต่ version 5.0 ขึ้นไป ซึ่งเปิดใช้ binary log (binlog) Binary log มีหน้าที่เก็บข้อมูล native replication ของ MySQL Tungsten Replicator จะอ่านไฟล์ binlog ทำการ parsing และดึง event ใหม่ออกมา โดย event นี้จะถูกเก็บใน Transaction History Log และถูกส่งต่อไปที่ Tungsten Replicator instance อื่น รูปด้านล่างแสดง Tungsten Replicator architecture สำหรับ MySQL replication. Tungsten Replicator มีความสามารถเหนือกว่า native MySQL replication ในหลายๆอย่างดังนี้ • สามารถจัดการ ขบวนการ master failover ในกรณีที่มี slave หลายตัว โดย Tungsten Replicator จะใส่ global sequence number เข้าไปในทุกๆ SQL request ซึ่งทำให้ slave สามารถถูก promote ขึ้นมาเป็น master และการจาย event ไปสู่ slave ที่เหลือได้ต่อไป • สามารถทำ replication จาก MySQL version ใหม่ไป version ที่เก่ากว่าได้ เช่นจาก MySQL version 5.0 ไป version 4.1 • สามารถจัดการ replication ได้หลายรูปแบบ เช่น หนึ่ง master ไปสู่ หลาย slaves (fan-out) หลาย masters ไปสู่ หนึ่ง slave (fan-in) และ circular replication ระหว่าง master สองตัว หรือมากกว่าได้ • สามารถ replicate จาก/ไปสู่ database ชนิดอื่น โดยใช้การ transform และ filter ข้อมูลที่ถูก replicate 5/18 Figure 3: MySQL Replication Architecture
  • 6. การติดตั้ง Tungsten Replicator บน Debian Linux การเตรียม Debian Linux การทำ Multi-Master Replication จะใช้ database server อย่างน้อยสองเครื่องขึ้นไป ในเอกสารชุดนี้สมมติว่ามี MySQL server ซึ่งถูกติดตั้งอยู่ใน server สองเครื่อง ทั้งสองเครื่องที่ถูกติดตั้งอยู่คนละ data center และเชื่อมต่อกันทาง VPN โดยที่เครื่องแรกให้ชื่อ ว่า master1 และอีกเครื่องให้ชื่อว่า master2 ทั้งสองเครื่องมีซอฟต์แวร์ OpenSSH ติดตั้งและทำงานอยู่ที่ port 4005 สำหรับ VPN master1 ใช้ IP 10.8.0.1 master2 ใช้ IP 10.8.0.1 ทั้ง master1 และ master2 ติดตั้งซอฟต์แวร์ที่จะเป็นได้แก่ rsync ruby และ jdk ด้วยคำสั่งดังนี้ $ sudo apt-get install rsync $ sudo apt-get install ruby $ sudo apt-get install default-jdk ทั้ง master1 และ master2 แก้ไข hosts file เพื่อให้สามารถใช้ชื่อ master1 และ master2 แทนการอ้างอิงจาก IP ได้ $ sudo nano -w /etc/hosts 127.0.0.1 localhost 10.8.0.1 master1 10.8.0.2 master2 ... ทำการเซ็ทให้การเชื่อมต่อผ่าน SSH ทำได้โดยไม่ต้องใช้ password master1 เพิ่ม user ชื่อ tungsten $ sudo useradd -m -Gmysql -s /bin/bash tungsten เซ็ท password ให้ user tungsten ตามต้องการ $ sudo passwd tungsten แก้ไขไฟล์ /etc/sudoers โดยใช้คำสั่ง # sudoedit /etc/sudoers และเพิ่มข้อมลด้านล่างเข้าไป tungsten ALL=(ALL) NOPASSWD: ALL เปลี่ยนจาก user root เป็น user tungsten # su tungsten ทำการสร้าง key ของ secure shell $ ssh-keygen 6/18 เพิ่ม 2 บรรทัดนี้เข้าไป
  • 7. copy key ไปที่ master2 $ ssh-copy-id '-p 4002 -i ~/.ssh/id_rsa.pub master2' ทดสอบการเชื่อมต่อไปที่ master2 $ ssh master2 master2 เพิ่ม user ชื่อ tungsten # useradd -m -Gmysql -s /bin/bash tungsten เซ็ท password ให้ user tungsten ตามต้องการ # passwd tungsten แก้ไขไฟล์ /etc/sudoers โดยใช้คำสั่ง # sudoedit /etc/sudoers และเพิ่มข้อมลด้านล่างเข้าไป tungsten ALL=(ALL) NOPASSWD: ALL เปลี่ยนจาก user root เป็น user tungsten # su tungsten ทำการสร้าง key ของ secure shell $ ssh-keygen copy key ไปที่ master1 $ ssh-copy-id '-p 4001 -i ~/.ssh/id_rsa.pub master1' ทดสอบการเชื่อมต่อไปที่ master2 $ ssh master1 การเตรียมฐานข้อมูล MySQL Master1 ทำการแก้ไขไฟล์ configuration ของ MySQL $ sudo vim /etc/mysql/my.cnf แก้ไขข้อมูลของ MySQL configuration ดังนี้ [mysqld] # bind-address = 127.0.0.1 server-id = 1 7/18
  • 8. # set increment for up to 4 servers auto_increment_increment = 4 # increment offset for this server, next server would be 2 auto_increment_offset = 1 log_bin = mysql-bin innodb_buffer_pool_size = 512M # Recommended InnoDB settings for Tungsten. default-storage-engine=InnoDB innodb_flush_log_at_trx_commit=2 sync_binlog=0 # Recommended general settings. # max_allowed_packet must be greater than # the size of the largest transaction. max_allowed_packet=48m default-table-type=InnoDB innodb_flush_log_at_trx_commit=2 master2 ทำการแก้ไขไฟล์ configuration ของ MySQL $ sudo vim /etc/mysql/my.cnf แก้ไขข้อมูลของ MySQL configuration ดังนี้ [mysqld] #bind-address = 127.0.0.1 server-id = 2 # set increment for up to 4 servers auto_increment_increment = 4 # increment offset for this server, next server would be 2 auto_increment_offset = 2 log_bin = mysql-bin 8/18
  • 9. innodb_buffer_pool_size = 512M # Recommended InnoDB settings for Tungsten. default-storage-engine=InnoDB innodb_flush_log_at_trx_commit=2 sync_binlog=0 # Recommended general settings. # max_allowed_packet must be greater than # the size of the largest transaction. max_allowed_packet=48m default-table-type=InnoDB innodb_flush_log_at_trx_commit=2 การสร้าง MySQL user ทั้ง master1 และ master2 ให้เพิ่ม user tungsten ให้ฐานข้อมูล MySQL ดังนี้ $ mysql -u root -p mysql> grant all on *.* to tungsten@'%' identified by 'tungsten' with grant option; mysql> FLUSH PRIVILEGES; การติดตั้ง Ruby ติดตั้ง Ruby และ library ที่จำเป็นใน master1 และ master2 $ sudo apt-get install -y ruby libopenssl-ruby ทดสอบ Ruby ว่าติดตั้งเรียบร้อยหรือไม่ $ echo "p 'hello'" | ruby -ropenssl จะต้องแสดงข้อความ "hello" 9/18
  • 10. การเซ็ทค่า JAVA_HOME ทำการเซ็ท JAVA_HOME และ PATH ในไฟล์ /etc/bash.bashrc ดังนี้ $sudo vim /etc/bash.bashrc export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_06 export PATH=$PATH:$JAVA_HOME/bin การติดตั้ง Tungsten Replicator master1 กระจายไฟล์ installer ของ Tungsten Replicator ไปที่ /opt ด้วยคำสั่ง $ sudo tar zxfv tungsten-replicator-2.0.5.tar.gz -C /opt/ สร้างไฟล์ setup-masters.sh $ sudo vim /opt/tungsten-replicator-2.0.5/setup-masters.sh ใส่ข้อมูลด้านล่างนี้เข้าไป #! /bin/bash TUNGSTEN_HOME=/home/tungsten MYSQL_CONF=/etc/mysql/my.cnf MASTER1=master1 MASTER2=master2 ./tools/tungsten-installer --master-slave --master-host=$MASTER1 --datasource-mysql-conf=$MYSQL_CONF --datasource-user=tungsten --datasource-password=tungsten --service-name=dc --home-directory=$TUNGSTEN_HOME --cluster-hosts=$MASTER1 --start-and-report ./tools/tungsten-installer --master-slave --master-host=$MASTER2 --datasource-mysql-conf=$MYSQL_CONF --datasource-user=tungsten --datasource-password=tungsten --service-name=dr --home-directory=$TUNGSTEN_HOME --cluster-hosts=$MASTER2 --start-and-report 10/18
  • 11. สร้างไฟล์ setup-slaves.sh $ sudo vim /opt/tungsten-replicator-2.0.5/setup-slaves.sh ใส่ข้อมูลนี้เข้าไป #! /bin/bash MASTER1=master1 MASTER2=master2 TUNGSTEN_TOOLS=tools $TUNGSTEN_TOOLS/configure-service --host $MASTER1 -C -q --local-service-name=dc --role=slave --service-type=remote --datasource=$MASTER1 --master-thl-host=$MASTER2 --svc-start dr $TUNGSTEN_TOOLS/configure-service --host $MASTER2 -C -q --local-service-name=dr --role=slave --service-type=remote --datasource=$MASTER2 --master-thl-host=$MASTER1 --svc-start dc ทำการเซ็ทให้ owner และ group ของ installer directory เป็น tungsten โดยใช้คำสั่ง $ sudo chown -R tungsten:tungsten /opt/tungsten-replicator-2.0.5 เปลี่ยน user ปัจจุบันให้เป็น tungsten $ su tungsten ทำการติดตั้ง Tungsten Replicator โดยใช้คำสั่งด้านล่าง ตัว Tungsten Replicator จะทำการติดตั้งลงใน master1 และ master2 โดยอัตโนมัติ $ cd /opt/tungsten-replicator-2.0.5 $ sh setup-masters.sh $ cd ~ $ cd tungsten $ sh setup-slaves.sh 11/18
  • 12. วิธีเซ็ทให้ replicator service ทำงานอัตโนมัติ เมื่อ boot OS # ln -s /home/tungsten/tungsten/tungsten-replicator/bin/replicator /etc/init.d/replicator # update-rc.d replicator defaults 12/18
  • 13. วิธีตรวจสอบการทำงานของ Tungsten Replicator เราสามารถตรวจสอบการทำงานของ Tungsten Replicator ได้โดย login เข้าไปในระบบที่ติดตั้ง Tungsten Replicator จากนั้น เปลี่ยน user ให้เป็น user tungsten ด้วยคำสั่ง # su – tungsten จากนั้นเข้าไปใน directory tungsten/tungsten-replicator/ $ cd tungsten/tungsten-replicator/ พิมพ์คำสั่ง bin/trepctl services $ bin/trepctl services จะได้ผลลัพธ์ดังนี้ Processing services command... NAME VALUE ---- ----- appliedLastSeqno: 13154 appliedLatency : 1.0 role : master serviceName : dc serviceType : local started : true state : ONLINE NAME VALUE ---- ----- appliedLastSeqno: 8876 appliedLatency : 4363.722 role : slave serviceName : dr serviceType : remote started : true state : ONLINE Finished services command... 13/18
  • 14. ในส่วนของ state ของ service ทั้งสองฝั่งจะต้องเป็น ONLINE เท่านั้น จึงจะถือว่าสถานะการทำ replication เป็นปกติ นอกจาก ONLINE แล้ว state อาจมีค่าเป็น GOING-ONLINE:SYNCHRONIZING หมายความว่า Tungsten Replication กำลัง replicate ข้อมูล ซึ่งถ้าสามารถ replicate โดยที่ไม่พบปัญหาใดๆ state ก็จะเปลี่ยนกลับมาเป็น ONLINE แต่ถ้าพบปัญหา state จะเปลี่ยนไป เป็น OFFLINE:ERROR ซึ่งต้องทำการแก้ไขเพื่อให้ replication กลับมาทำงานได้ปกติ 14/18
  • 15. 15/18
  • 16. การแก้ไขปัญหา 1. กรณีข้อมูลไม่ถูก replicate ไปที่ slave ใช้คำสั่ง tungsten-replicator/bin/trepctl services เพื่อตรวจสอบว่า service ทั้งหมดอยู่ในสถานะ ONLINE ถ้าหากมี service ใดไม่อยูในสถานะ ONLINE ให้ใช้คำสั่ง tungsten-replicator/bin/trepctl -service service_name status เพื่อดูข้อมูลเพิ่มเติมว่ามีปัญหาอะไรเกิดขึ้น ทำการ review log file ใน tungsten-replicator/log/user.log และ tungsten-replicator/log/trepsvc.log เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับสาเหตุของปัญหา และเมื่อแก้ปัญหาได้แล้วให้ restart replication service โดยใช้คำสั่ง tungsten-replicator/bin/trepctl -service service_name online 2. กรณีพบ error ใน log file บอกว่า Event application failed: seqno=3 fragno=0 message=java.sql.SQLException: Statement failed on slave but succeeded on master การแก้ไขปัญหานี้จะต้องแก้ไขที่ transaction ที่พบปัญหา จากนั้นจึง restart replication เพื่อสั่งให้ replication ทำงาน ต่อ ขั้นแรกให้ทำการหา error com.continuent.tungsten.replicator.applier.ApplierException ใน log file tungsten-replicator/log/trepsvc.log stacktrace จะเป็นตัวบอกว่าปัญหาเกิดจากอะไร ถ้าหากเราต้องการข้อมูลเพิ่มเติม เกี่ยวกับ transaction เราสามารถใช้ โปรแกรม thl ในการดูข้อมูลของ event ได้โดยตรง เช่นใช้คำสั่ง thl เพื่อดูข้อมูล transaction ของ service ชื่อ service_name sequence number 3 สามารถใช้คำสั่งดังนี้ ./tungsten-replicator/bin/thl -service service_name list -seqno 3
 โปรแกรม thl จะแสดงผลลัพธ์ดังนี้ SEQ# = 3 / FRAG# = 0 (last frag) - TIME = 2011-09-14 09:06:42.0 - EPOCH# = 0 - EVENTID = mysql-bin.000002:0000000000000592;11 - SOURCEID = db1 - METADATA = [mysql_server_id=101;service=service_name;shard=test] - TYPE = com.continuent.tungsten.replicator.event.ReplDBMSEvent - SQL(0) = SET INSERT_ID = 2 - OPTIONS = [##charset = ISO8859_1, autocommit = 1, sql_auto_is_null = 1, foreign_key_checks = 1, unique_checks = 1, 16/18
  • 17. sql_mode = '', character_set_client = 8, collation_connection = 8, collation_server = 8] - SCHEMA = - SQL(1) = insert into test.names (name) values ('Robert') การแก้ปัญหาสามารถทำได้หลายวิธี บางครั้งวิธีที่เหมาะสมอาจทำได้โดยสั่ง statement ที่มีปัญหานั้นๆด้วยตัวเองโดยอาจ จะทำจาก command line interface หรือ GUI tool ก็ได้ บางครั้งเราอาจต้อง skip transaction ที่มีปัญหานั้นๆไปก่อนเพื่อทำให้ replication service online ได้อีกครั้ง จากนั้นจึง ค่อยนำข้อมูลที่ขาดหายไปใส่เข้าไปในฐานข้อมูลอีกครั้งถ้าจำเป็น เช่นถ้าต้องการ skip transaction sequence number 2340 ของ service service_name ซึ่งมีปัญหาอยู่ สามารถทำได้โดยใช้คำสั่ง ./tungsten-replicator/bin/trepctl -service service_name online -skip-seqno 2340 เมื่อ skip transaction ที่มีปัญหาหมดแล้ว จึงสั่งให้ replication online อีกครั้งด้วยคำสั่ง ./tungsten-replicator/bin/trepctl -service service_name online 17/18
  • 18. Reference [1] Tungsten Replicator, Available At. URL: http://code.google.com/p/tungsten-replicator/ [2] Tungsten Replicator cookbook. Advanced replication topologies made easy, Available At. URL: http://datacharmer.blogspot.com/2012/10/tungsten-replicator-cookbook-advanced.html [3] How To Set Up Multi-Master Replication Using Tungsten And MySQL-Proxy For MySQL High Availability On Ubuntu 10.04.3 LTS; http://www.howtoforge.com/how-to-set-up-multi-master-replication-using-tungsten-and- mysql-proxy-for-mysql-high-availability-on-ubuntu-10.04.3-lts [4] Tungsten Replicator Pre-Requisites; http://code.google.com/p/tungsten-replicator/wiki/InstallationPreRequisites [5] Understanding Tungsten Replication Services; http://scale-out-blog.blogspot.com/2011/03/understanding- tungsten-replication.html [6] Tungsten Replication for MySQL users; http://code.google.com/p/tungsten-replicator/wiki/Cheat_Sheet [7] How To Back Up MySQL Databases With mylvmbackup On Debian Squeeze; http://www.howtoforge.com/how-to-back-up-mysql-databases-with-mylvmbackup-on-debian-squeeze 18/18