(一)、复制工作原理:
复制(replication)是 MySQL 数据库提供的一种高可用高性能的解决方案,一般用来建立大型的应用。
总体来说,replication 的工作原理分以下三个步骤:
1)主服务器(master)把数据更改记录到二进制(binlog)中。
2)从服务器(slave)把主服务器的二进制日志复制到自己的中继日志(relay log)中。
3)从服务器重做中继日志中的日志,把更新应用到自己的数据库上,以达到数据的最终一致性。
( 一) MySQL 数据库的复制工作原理
从服务器有2个线程,一个是 I/O 线程,负责读取主服务器的二进制日志,并将其保存为中继日志;另一个是 SQL 线程负责执行中继日志,复制执行中继日志。MySQL 4.0 版本之前,从服务器只有一个线程,既负责读取二进制日志,又负责执行二进制日志中的 SQL 语句。这种方式不符合高性能的要求,目前已淘汰。
如果查看从服务器的状态可以看到如下内容:
cami@dbs07.cn 11:14: [(none)]> show processlist \G
*************************** 1. row ***************************
Id: 364
User: repl
Host:
db: NULL
Command: Connect
Time: 4925954
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 365
User: system user
Host:
db: NULL
Command: Connect
Time: 1
State: Slave has read all relay log; waiting for more updates
Info: NULL
可以看到 ID 为364 的线程就是 I/O 线程,目前状态就是等待主服务器发送二进制日志。ID 365 的线程是 SQL 线程,负责读取中继日志并执行。目前的状态是已经读取所有的中继日志,等待中继日志被 I/O 线程更新。
在 replication 主服务器可以看到 一个线程负责发送二进制日志,类似如下:
cami@dbs06.cn 11:22: [(none)]> show full processlist\G
*************************** 1. row ***************************
Id: 633
User: repl
Host: 192.168.24.5:56947
db: NULL
Command: Binlog Dump GTID
Time: 4926431
State: Master has sent all binlog to slave; waiting for more updates
Info: NULL
可以通过 performance_schema.threads 查看当前线程对应的 thread_id,thread_os_id
cami@dbs06.cn 16:09: [performance_schema]> select * from performance_schema.threads where processlist_user='repl'\G
THREAD_ID: 684
NAME: thread/sql/one_connection
TYPE: FOREGROUND
PROCESSLIST_ID: 633
PROCESSLIST_USER: repl
PROCESSLIST_HOST: 192.168.24.5
PROCESSLIST_DB: NULL
PROCESSLIST_COMMAND: Binlog Dump GTID
PROCESSLIST_TIME: 4943609
PROCESSLIST_STATE: Master has sent all binlog to slave; waiting for more updates
PROCESSLIST_INFO: NULL
PARENT_THREAD_ID: NULL
ROLE: NULL
INSTRUMENTED: YES
HISTORY: YES
CONNECTION_TYPE: TCP/IP
THREAD_OS_ID: 23724
2 rows in set (0.00 sec)
可以通过pstack 找到详细的线程信息( pstack 这里不做详细介绍)
root@dbs06 15:21:43:~# pstack 23724
Thread 1 (process 23724):
#0 0x00007fe0efaf7ab2 in pthread_cond_timedwait@@GLIBC_2.3.2 ()
可以通过 show slave status \G 查看当前复制的运行状态(基于 GTIDs 复制的显示格式)
cami@dbs07.cn 11:32: [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.24.4
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000315
Read_Master_Log_Pos: 407133885
Relay_Log_File: relay.001115
Relay_Log_Pos: 407132713
Relay_Master_Log_File: mysql-bin.000315
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: 407132500
Relay_Log_Space: 407134382
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: 3663306
Master_UUID: a7d58405-558b-11e7-8fc2-9418820184e8
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: a7d58405-558b-11e7-8fc2-9418820184e8:60750507-412995077
Executed_Gtid_Set: 89715acc-0269-11e8-8984-941882017490:1-2,
a7d58405-558b-11e7-8fc2-9418820184e8:1-412995077
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
Slave_IO_State: Waiting for master to send event #显示当前 IO 线程状态,这里显示的是等待主服务器发送二进制日志
Master_Log_File: mysql-bin.000109
#显示当前同步的主服务的二进制日志,这里显示当前同步的是主服务器的 mysql-bin.000109
Read_Master_Log_Pos: 224294986
#显示当前同步到主服务器上二进制日志的偏移量位置,单位字节,此处显示当前同步到 mysql-bin.000109 的 224294986偏移量位置,即已同步了 mysql-bin-000109 这个二进制日志中 213M(224294986/1024/1024)的内容
Relay_Log_File: dbs01-relay-bin.000025
# 显示当前写入的中继日志
Relay_Log_Pos: 224295199
#显示当前执行到中继日志的偏移量位置
Relay_Master_Log_File: mysql-bin.000109
#当前中继日志同步的二进制日志
Slave_IO_Running: Yes
#从服务器中IO线程的运行状态,YES表示运行正常
Slave_SQL_Running: Yes
#从服务器中SQL线程的运行状态,YES表示运行正常
Exec_Master_Log_Pos: 224294986
#表示同步到主服务器的二进制日志偏移量位置,(Read_Master_Log_Pos-Exec_Master_Log_Pos) 这里当前主从服务器是完全同步的
Seconds_Behind_Master: 0
#表示主从之间延迟的时间,单位是秒,如果为null表示未知数,一般主从出问题了会出现null情况
Last_IO_Errno: 0
#显示slave 的sql线程读取日志参数的错误数量
Last_IO_Error:
#显示 slave 的sql 进程读取日志参数的错误消息,为空字符串时表示没有错误
Master_UUID: 1dc90799-aa42-11e6-a619-fa163e1f3df6
#记录master 的uuid
SQL_Delay: 0
#记录slave设置延迟复制的时间,0表示无延迟
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
#记录slave SQL线程运行状态
Retrieved_Gtid_Set: 1dc90799-aa42-11e6-a619-fa163e1f3df6:52161402-56611343
#执行二进制日志集合,对应sql线程