Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

หยุด Replication ชั่วคราวสำหรับ SQL Statements เฉพาะใน MySQL (sql_log_bin)


แนวคิด

ตั้งค่า sql_log_bin = 0 เพื่อปิด binary logging ชั่วคราว → คำสั่งใน session นั้นจะไม่ replicate ไป slave

รูปแบบคำสั่ง

SET sql_log_bin = 0;
-- รันคำสั่งที่ไม่ต้องการ replicate
SET sql_log_bin = 1;

ตัวอย่างครบถ้วน

1. สร้างตาราง

mysql> create table SQLStatementsDemo 
    -> ( 
    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    -> UserName varchar(20) 
    -> ); 
Query OK, 0 rows affected (0.79 sec)

2. เพิ่มข้อมูล 6 แถว

mysql> insert into SQLStatementsDemo(UserName) values('John'); 
Query OK, 1 row affected (0.18 sec)
mysql> insert into SQLStatementsDemo(UserName) values('Carol'); 
Query OK, 1 row affected (0.14 sec)
mysql> insert into SQLStatementsDemo(UserName) values('Bob'); 
Query OK, 1 row affected (0.14 sec)
mysql> insert into SQLStatementsDemo(UserName) values('Mike'); 
Query OK, 1 row affected (0.14 sec)
mysql> insert into SQLStatementsDemo(UserName) values('Sam'); 
Query OK, 1 row affected (0.14 sec)
mysql> insert into SQLStatementsDemo(UserName) values('David'); 
Query OK, 1 row affected (0.14 sec)

3. ดูข้อมูล

mysql> select * from SQLStatementsDemo;
+--------+----------+
| UserId | UserName |
+--------+----------+
| 1      | John     |
| 2      | Carol    |
| 3      | Bob      |
| 4      | Mike     |
| 5      | Sam      |
| 6      | David    |
+--------+----------+

4. ปิด binlog แล้ว UPDATE/INSERT/DELETE

mysql> SET sql_log_bin = 0; 
Query OK, 0 rows affected (0.00 sec)

mysql> update SQLStatementsDemo set UserName = 'Maxwell' where UserId = 6; 
Query OK, 1 row affected (0.05 sec) Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from SQLStatementsDemo;
+--------+----------+
| UserId | UserName |
+--------+----------+
| 1      | John     |
| 2      | Carol    |
| 3      | Bob      |
| 4      | Mike     |
| 5      | Sam      |
| 6      | Maxwell  |
+--------+----------+

mysql> insert into SQLStatementsDemo(UserName) values('Chris'); 
Query OK, 1 row affected (0.16 sec)

mysql> select * from SQLStatementsDemo;
+--------+----------+
| UserId | UserName |
+--------+----------+
| 1      | John     |
| 2      | Carol    |
| 3      | Bob      |
| 4      | Mike     |
| 5      | Sam      |
| 6      | Maxwell  |
| 7      | Chris    |
+--------+----------+

mysql> delete from SQLStatementsDemo where UserId = 7; 
Query OK, 1 row affected (0.10 sec)

mysql> SET sql_log_bin = 1; 
Query OK, 0 rows affected (0.00 sec)

ข้อสังเกตสำคัญ

  • ต้องมีสิทธิ์ SUPER หรือ SESSION_VARIABLES_ADMIN (MySQL 8.0+)
  • มีผลต่อ session นั้นเท่านั้น - session อื่นไม่ได้รับผลกระทบ
  • ไม่ถาวร - ตั้งค่ากลับเป็น 1 ทันทีหลังเสร็จ
  • ไม่ส่งผลต่อ GTID หรือ binlog_format อื่นๆ

กรณีใช้งานจริง

  • Bulk data load / ETL ที่ไม่ต้อง replicate
  • Maintenance operations (rebuild index, alter table)
  • Data cleanup / archiving บน master เท่านั้น
  • Testing queries บน production master

ตัวเลือกอื่น: replicate-do-table / replicate-ignore-table

ตั้งค่าใน my.cnf ของ slave เพื่อกรอง table ระดับการตั้งค่า (ไม่ใช่ per-session)

คำเตือน: ใช้อย่างระมัดระวัง - ข้อมูลบน master และ slave จะไม่สอดคล้องกันชั่วคราว