Replication ในฐานข้อมูล SQL คือการคัดลอกข้อมูลจากเซิร์ฟเวอร์ต้นทาง (Master) ไปยังเซิร์ฟเวอร์ปลายทาง (Slave) หรือระหว่าง Master กับ Master เพื่อกระจายโหลด, High Availability และ Failover บทความนี้อธิบายการตั้งค่าทั้งสองโหมดบน CentOS 7
ติดตั้ง MariaDB (สรุป)
เพิ่ม Repository MariaDB 10.4+ แล้ว yum install MariaDB-server MariaDB-client -y เริ่ม Service และ Secure Installation
Master-Master Replication (Two-Way)
ทั้งสองเซิร์ฟเวอร์อ่าน/เขียนได้ ข้อมูลซิงค์สองทาง
1. กำหนดค่า Master-1 (Server ID = 1)
แก้ไข /etc/my.cnf เพิ่มใน Section [mysqld]:
server_id = 1
report_host = master1
log_bin = /var/lib/mysql/mariadb-bin
log_bin_index = /var/lib/mysql/mariadb-bin.index
relay_log = /var/lib/mysql/relay-bin
relay_log_index = /var/lib/mysql/relay-bin.index
Restart: systemctl restart mariadb
สร้าง User Replication:
CREATE USER 'repl_user'@'%' IDENTIFIED BY 'StrongPassword';
GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'%';
ดู Binlog Position:
SHOW MASTER STATUS;
# File: mariadb-bin.000002, Position: 664
2. กำหนดค่า Master-2 (Server ID = 2)
เพิ่มใน my.cnf:
server_id = 2
report_host = master2
log_bin = /var/lib/mysql/mariadb-bin
log_bin_index = /var/lib/mysql/mariadb-bin.index
relay_log = /var/lib/mysql/relay-bin
relay_log_index = /var/lib/mysql/relay-bin.index
Restart, สร้าง User Replication เหมือนกัน, ดู SHOW MASTER STATUS; (เช่น File: mariadb-bin.000001, Pos: 667)
3. ตั้งค่า Replication ระหว่างกัน
บน Master-2 ชี้ไป Master-1:
STOP SLAVE;
CHANGE MASTER TO MASTER_HOST='IP_MASTER1', MASTER_USER='repl_user', MASTER_PASSWORD='StrongPassword', MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=664;
START SLAVE;
บน Master-1 ชี้ไป Master-2:
STOP SLAVE;
CHANGE MASTER TO MASTER_HOST='IP_MASTER2', MASTER_USER='repl_user', MASTER_PASSWORD='StrongPassword', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=667;
START SLAVE;
4. ตรวจสอบ
SHOW SLAVE STATUS\G
# ต้องเห็น: Slave_IO_Running: Yes, Slave_SQL_Running: Yes, Seconds_Behind_Master: 0
ทดสอบ: สร้าง DB/Table บน Master-1 ต้องปรากฏบน Master-2 และในทางกลับกัน
Master-Slave Replication (One-Way)
Master รับเขียน, Slave รับอ่านอย่างเดียว (Scale-out Reads)
Master (Server ID = 1) – ตั้งค่าเหมือน Master-1 ข้างต้น
Slave (Server ID = 2) – my.cnf:
server_id = 2
report_host = slave1
log_bin = /var/lib/mysql/mariadb-bin
log_bin_index = /var/lib/mysql/mariadb-bin.index
relay_log = /var/lib/mysql/relay-bin
relay_log_index = /var/lib/mysql/relay-bin.index
read_only = 1 # สำคัญ: ป้องกันเขียนบน Slave
ตั้งค่า Slave ชี้ไป Master:
STOP SLAVE;
CHANGE MASTER TO MASTER_HOST='IP_MASTER', MASTER_USER='repl_user', MASTER_PASSWORD='StrongPassword', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=664;
START SLAVE;
สำคัญ: หากมีข้อมูลอยู่แล้ว ต้อง SET GLOBAL read_only = ON; บน Master, Dump ข้อมูล (mysqldump) แล้ว Restore บน Slave ก่อน Start Slave
ทดสอบ:
- สร้าง DB บน Master → ปรากฏบน Slave
- สร้าง DB บน Slave → ไม่ ปรากฏบน Master (One-way)
- Drop DB บน Slave → Master ไม่ได้รับผลกระทบ
ปัญหาที่พบบ่อยและแก้ไข
- Firewall: เปิด Port 3306 (TCP) ระหว่างเซิร์ฟเวอร์
iptables -I INPUT -p tcp -s SLAVE_IP --dport 3306 -j ACCEPT - bind-address: ใน my.cnf ให้ Comment ออก หรือตั้งเป็น 0.0.0.0
- Replicate Specific DB: ใช้
replicate-do-db=dbnameหรือbinlog-ignore-db=mysql,information_schema,performance_schema expire_logs_days = 10ลบ Binlog เก่าreplicate-rewrite-db=master_db->slave_dbเปลี่ยนชื่อ DB ระหว่าง Replicate