MySQL同步功能由3个线程(master上1个,slave上2个)来实现。执行 DE>START SLAVEDE> 语句后,slave就建立一个I/O线程。I/O线程链接到master上,并请求master发送二进制日志中的语句。master建立一个线程来把日 志的内容发送到slave上。这个线程在master上执行 DE>SHOW PROCESSLISTDE> 语句后的结果中的 DE>Binlog DumpDE> 线程即是。slave上的I/O线程读取master的 DE>Binlog DumpDE> 线程发送的语句,而且把它们拷贝到其数据目录下的中继日志(relay logs)中。第三个是SQL线程,salve用它来读取中继日志,而后执行它们来更新数据。mysql
如上所述,每一个master/slave上都有3个线程。每一个master上有多个线程,它为每一个slave链接都建立一个线程,每一个slave只有I/O和SQL线程。sql
show slave status 用于提供有关从属服务器线程的关键参数的信息数据库
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.2.40
Master_User: photorepl
Master_Port: 4331
Connect_Retry: 60
Master_Log_File: mysql-bin.005502
Read_Master_Log_Pos: 64401238
Relay_Log_File: mysqld-relay-bin.015418
Relay_Log_Pos: 13456757
Relay_Master_Log_File: mysql-bin.005152
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table: photo.%
Replicate_Wild_Ignore_Table: mysql.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 13456620
Relay_Log_Space: 36764898503
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 249904
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××服务器
SHOW SLAVE STATUS会返回如下字段:网络
Slave_IO_Statespa
SHOW PROCESSLIST输出的State字段的拷贝。SHOW PROCESSLIST用于从属I/O线程。若是线程正在试图链接到主服务器,正在等待来自主服务器的时间或正在链接到主服务器等,本语句会通知您线程
Master_User日志
被用于链接主服务器的当前用户。server
Master_Port接口
当前的主服务器接口。
Connect_Retry
--master-connect-retry选项的当前值
Master_Log_File
I/O线程当前正在读取的主服务器二进制日志文件的名称。
Read_Master_Log_Pos
在当前的主服务器二进制日志中,I/O线程已经读取的位置。
Relay_Log_File
SQL线程当前正在读取和执行的中继日志文件的名称。
Relay_Log_Pos
在当前的中继日志中,SQL线程已读取和执行的位置。
Relay_Master_Log_File
由SQL线程执行的包含多数近期事件的主服务器二进制日志文件的名称。
Slave_IO_Running
I/O线程是否被启动并成功地链接到主服务器上。
Slave_SQL_Running
SQL线程是否被启动。
Replicate_Do_DB,Replicate_Ignore_DB
使用--replicate-do-db和--replicate-ignore-db选项指定的数据库清单。
Replicate_Do_Table,Replicate_Ignore_Table,Replicate_Wild_Do_Table,Replicate_Wild_Ignore_Table
使用--replicate-do-table,--replicate-ignore-table,--replicate-wild-do-table和--replicate-wild-ignore_table选项指定的表清单。
Last_Errno,Last_Error
被多数最近被执行的查询返回的错误数量和错误消息。错误数量为0而且消息为空字符串意味着“没有错误”。若是Last_Error值不是空值,它也会在从属服务器的错误日志中做为消息显示。
举例说明:
Last_Errno: 1051
Last_Error: error 'Unknown table 'z'' on query 'drop table z'
该消息指示,表z曾经存在于在主服务器中并已被取消了,可是它没有在从属服务器中存在过,所以对于从属服务器,DROP TABLE失败。(举例说明,在设置复制时,若是您忘记了把此表拷贝到从属服务器中,则这有可能发生。)
Skip_Counter
最近被使用的用于SQL_SLAVE_SKIP_COUNTER的值。
Exec_Master_Log_Pos
来自主服务器的二进制日志的由SQL线程执行的上一个时间的位置(Relay_Master_Log_File)。在主服务器的二进制日志中的 (Relay_Master_Log_File,Exec_Master_Log_Pos)对应于在中继日志中的 (Relay_Log_File,Relay_Log_Pos)。
Relay_Log_Space
全部原有的中继日志结合起来的总大小。
Until_Condition,Until_Log_File,Until_Log_Pos
在START SLAVE语句的UNTIL子句中指定的值。
Until_Condition具备如下值:
若是没有指定UNTIL子句,则没有值
若是从属服务器正在读取,直到达到主服务器的二进制日志的给定位置为止,则值为Master
若是从属服务器正在读取,直到达到其中继日志的给定位置为止,则值为Relay
Until_Log_File和Until_Log_Pos用于指示日志文件名和位置值。日志文件名和位置值定义了SQL线程在哪一个点停止执行。
Master_SSL_Allowed,Master_SSL_CA_File,Master_SSL_CA_Path,Master_SSL_Cert,Master_SSL_Cipher,Master_SSL_Key
这些字段显示了被从属服务器使用的参数。这些参数用于链接主服务器。
Master_SSL_Allowed具备如下值:
若是容许对主服务器进行SSL链接,则值为Yes
若是不容许对主服务器进行SSL链接,则值为No
若是容许SSL链接,可是从属服务器没有让SSL支持被启用,则值为Ignored。
与SSL有关的字段的值对应于--master-ca,--master-capath,--master-cert,--master-cipher和--master-key选项的值。
Seconds_Behind_Master
本字段是从属服务器“落后”多少的一个指示。当从属SQL线程正在运行时(处理更新),本字段为在主服务器上由此线程执行的最近的一个事件的时间标 记开始,已通过的秒数。当此线程被从属服务器I/O线程遇上,并进入闲置状态,等待来自I/O线程的更多的事件时,本字段为零。总之,本字段测量从属服务器SQL线程和从属服务器I/O线程之间的时间差距,单位以秒计。
若是主服务器和从属服务器之间的网络链接较快,则从属服务器I/O线程会很是接近主服务器,因此本字段可以十分近似地指示,从属服务器SQL线程比 主服务器落后多少。若是网络较慢,则这种指示不许确;从属SQL线程常常会遇上读取速度较慢地从属服务器I/O线程,因 此,Seconds_Behind_Master常常显示值为0。即便I/O线程落后于主服务器时,也是如此。换句话说,本列只对速度快的网络有用。
即便主服务器和从属服务器不具备相同的时钟,时间差计算也会起做用(当从属服务器I/O线程启动时,计算时间差。并假定今后时之后,时间差保持不 变)。若是从属SQL线程不运行,或者若是从属服务器I/O线程不运行或未与主服务器链接,则Seconds_Behind_Master为NULL(意 义为“未知”)。举例说明,若是在从新链接以前,从属服务器I/O线程休眠了master-connect-retry秒,则显示NULL,由于从属服务 器不知道主服务器正在作什么,也不能有把握地说落后多少。
2、正常状态的信息 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.1.1.1 Master_User: rep_user Master_Port: 3306 Connect_Retry: 10 Master_Log_File: binlog.000026 Read_Master_Log_Pos: 446 Relay_Log_File: relay.000008 Relay_Log_Pos: 589 Relay_Master_Log_File: binlog.000026 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 446 Relay_Log_Space: 878 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 2211 1 row in set (0.00 sec) 状态解释: 1.Slave_IO_State: Waiting for master to send event 这里显示了当前slave I/O线程的状态。状态信息和使用show processlist显示的内容同样。 slave I/O线程的状态,有如下几种: 1) waiting for master update 这是connecting to master状态以前的状态 2) connecting to master I/O线程正尝试链接到master 3) checking master version 在与master创建链接后,会出现该状态。该状态出现的时间很是短暂。 4) registering slave on master 在与master创建链接后,会出现该状态。该状态出现的时间很是短暂。 5) requesting binlog dump 在与master创建链接后,会出现该状态。该状态出现的时间很是短暂。在这个状态下,I/O线程向master发送请求,请求binlog,位置从指定的binglog 名字和binglog的position位置开始。 6) waiting to reconnect after a failed binlog dump request 若是由于链接断开,致使binglog的请求失败,I/O线程会进入睡眠状态。而后按期尝试重连。尝试重连的时间间隔,可使用命令"change master to master_connect_trt=X;"改变。 7) reconnecting after a failed binglog dump request I/O进程正在尝试链接master 8) waiting for master to send event 说明,已经成功链接到master,正等待二进制日志时间的到达。若是master 空闲,这个状态会持续很长时间。若是等待的时间超过了slave_net_timeout(单位是秒)的值,会出现链接超时。在这种状态下,I/O线程会 人为链接失败,并开始尝试重连 9) queueing master event to the relay log 此时,I/O线程已经读取了一个event,并复制到了relay log 中。这样SQL 线程能够执行此event 10) waiting to reconnect after a failed master event read 读取时出现的错误(由于链接断开)。在尝试重连以前,I/O线程进入sleep状态,sleep的时间是master_connect_try的值(默认是60秒) 11) reconnecting after a failed master event read I/O线程正尝试重连master。若是链接创建,状态会变成"waiting for master to send event" 12) waiting for the slave sql thread to free enough relay log space 这是由于设置了relay_log_space_limit,而且relay log的大小已经整张到了最大值。I/O线程正在等待SQL线程经过删除一些relay log,来释放relay log的空间。 13) waiting for slave mutex on exit I/O线程中止时会出现的状态,出现的时间很是短。 2. Master_Host: 10.1.8.62 Master_User: rep_user Master_Port: 3306 这3条信息,显示了slave链接master时,使用的master的主机---master_host、链接master用的用户---master_user、链接master的端口---master_port。 3. Connect_Retry: 10 链接中断后,从新尝试链接的时间间隔。默认值是60秒。 4. Master_Log_File: binlog.000026 Read_Master_Log_Pos: 446 这两条信息,显示了与master相关的日志的信息。master_log_file:当前I/O线程正在读取的master 二进制日志的文件名;read_master_log_pos:当前I/O线程正在读取的二进制日志的位置 5. Relay_Log_File: relay.000008 Relay_Log_Pos: 589 Relay_Master_Log_File: binlog.000026 这3条信息,显示了与relay log相关的信息。relay_log_file:当前SQL线程正在读取并执行的relay log的文件名;relay_log_pos:当前SQL线程正在读取并执行的relay log文件的位置;relay_master_log_file:master 二进制日志的文件名。该文件包含当前SQL执行的事物 6. Slave_IO_Running: Yes Slave_SQL_Running: Yes 显示了当前I/O线程和SQL线程的状态 7. Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: 这部分显示的是关于复制DB和table的信息。 8. Last_Errno: 0 Last_Error: laster_errno和laster_error是 Last_SQL_Errno和Last_SQL_Error的同义词。 9. Skip_Counter: 0 系统参数sql_slave_skip_counter的值。sql_slave_skip_counter:slave应该跳过的事件数 10. Exec_Master_Log_Pos: 446 sql线程当前执行的事件,在master 二进制日志中的position 11. Relay_Log_Space: 878 全部存在relay log的大小 12. Seconds_Behind_Master: 0 这个值是时间戳的差值。是slave当前的时间戳和master记录该事件时的时间戳的差值 13. Replicate_Ignore_Server_Ids: slave当前会跳过的事件号 14. Master_Server_Id: 2211 master的server-id;若是master和slave的server-id相同,在启动slave时,会报错