MySQL replication capabilities are implemented using three threads (one on the master server and two on the slave):
Slave I/O thread.
When a START SLAVE
statement
is issued on a slave server, the slave creates an
I/O thread, which connects to the
master and asks it to send the updates recorded in its
binary logs.
The slave I/O thread reads the updates that the master'
Binlog Dump
thread sends (see next item)
and copies them to local files — known as
relay logs ‐ in the slave's
data directory.
The state of this thread is shown as
Slave_IO_running
in the output of
SHOW SLAVE STATUS
or as
Slave_running
in the output
of SHOW STATUS
.
Binlog dump thread.
The master creates a thread to send the binary log contents
to the slave. This thread can be identified in the output of
SHOW PROCESSLIST
on the
master as the Binlog Dump
thread.
The binlog dump thread acquires a lock on the master's binary log for reading each event that is to be sent to the slave. As soon as the event has been read, the lock is released, even before the event is sent to the slave.
Slave SQL thread. The slave creates this thread to read the relay logs that were written by the slave I/O thread. The slave SQL thread is also used to execute the updates contained in the relay logs.
MySQL Enterprise. For constant monitoring of the status of slaves subscribe to the MySQL Enterprise Monitor. For more information, see http://www.mysql.com/products/enterprise/advisors.html.
In the preceding description, there are three threads per master/slave connection. A master that has multiple slaves creates one binlog dump thread for each currently connected slave, and each slave has its own I/O and SQL threads.
The slave uses two threads so that reading updates from the master and executing them can be separated into two independent tasks. Thus, the task of reading statements is not slowed down if statement execution is slow. For example, if the slave server has not been running for a while, its I/O thread can quickly fetch all the binary log contents from the master when the slave starts, even if the SQL thread lags far behind. If the slave stops before the SQL thread has executed all the fetched statements, the I/O thread has at least fetched everything so that a safe copy of the statements is stored locally in the slave's relay logs, ready for execution the next time that the slave starts. This enables the master server to purge its binary logs sooner because it no longer needs to wait for the slave to fetch their contents.
The SHOW PROCESSLIST
statement
provides information that tells you what is happening on the
master and on the slave regarding replication. See
Section 7.5.6, “Examining Thread Information”, for descriptions of all
replicated-related states.
The following example illustrates how the three threads show up in
the output from SHOW PROCESSLIST
.
On the master server, the output from SHOW
PROCESSLIST
looks like this:
mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
Id: 2
User: root
Host: localhost:32931
db: NULL
Command: Binlog Dump
Time: 94
State: Has sent all binlog to slave; waiting for binlog to
be updated
Info: NULL
Here, thread 2 is a Binlog Dump
replication
thread for a connected slave. The State
information indicates that all outstanding updates have been sent
to the slave and that the master is waiting for more updates to
occur. If you see no Binlog Dump
threads on a
master server, this means that replication is not running —
that is, that no slaves are currently connected.
On the slave server, the output from SHOW
PROCESSLIST
looks like this:
mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
Id: 10
User: system user
Host:
db: NULL
Command: Connect
Time: 11
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 11
User: system user
Host:
db: NULL
Command: Connect
Time: 11
State: Has read all relay log; waiting for the slave I/O
thread to update it
Info: NULL
This information indicates that thread 10 is the I/O thread that
is communicating with the master server, and thread 11 is the SQL
thread that is processing the updates stored in the relay logs. At
the time that the SHOW PROCESSLIST
was run, both threads were idle, waiting for further updates.
The value in the Time
column can show how late
the slave is compared to the master. See
Section 16.3.4, “Replication FAQ”. The amount of time that the
slave lags behind the master that is required before the master
determines that the slave is no longer connected — as with
any other client connection — is dependent on the values of
net_write_timeout
and
net_retry_count
; for more information about
these, see Section 5.1.4, “Server System Variables”.
User Comments
Add your own comment.