MySQL Master-Slave Replication Principles
MySQL Master-Slave Replication Principles
1. Setting the Slave Node as Read-Only
Node B, even if not directly accessed, should be set to read-only (
readonly) mode.Reasons:
- Some operational queries might be executed on the slave. Setting to readonly prevents mishaps.
- To prevent inconsistencies during the switch-over due to bugs or dual writes.
Readonlystatus can be used to determine the role of the node.
A long connection is maintained between Slave B and Master A.
Replication Process:
- On Slave B, execute the
change mastercommand with Master A’s IP, port, username, password, and the position in binlog. - Execute the
start slavecommand on Slave B to initiate theio_threadandsql_thread. - Master A verifies credentials and sends the appropriate binlog entries to Slave B.
- Binlogs received by Slave B are saved as relay logs.
- The
sql_threadon Slave B processes and executes commands from the relay logs.
- On Slave B, execute the
Binlog Formats Comparison
1. Statement Format
- Records the original SQL statement.
2. Row Format
- Replaces the SQL statement with two events:
Table_mapandDelete_rows. - Ensures consistent replication by capturing the exact primary key of the changed row.
3. Mixed Format
- Combines Statement and Row formats.
- MySQL automatically determines the format based on the possibility of causing inconsistencies.
Replication Loop Issues
- Replication loops can occur when binlogs are replicated back to the originator.
- Solutions:
- Each server must have a unique
server id. - Binlogs created by a slave during replication should have the same
server idas the original binlog. - Nodes should discard binlogs with their own
server id.
- Each server must have a unique
Master-Slave Delay
Important Timestamps:
- Master A finishes a transaction and writes to binlog (T1).
- Slave B receives the binlog (T2).
- Slave B executes the transaction (T3).
Delay = T3 - T1.
Causes of Delay:
- The slave’s hardware might be inferior.
- Read operations during replication can cause additional load on the slave.
- Queries on the slave consuming excessive resources.
Solutions:
- One master to multiple slaves to distribute read loads.
- Output binlog to external systems like Hadoop for statistical queries.
- Large transactions cause delays. Avoid long-running transactions.
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.