Mysql 主从复制常用管理任务介绍
Mysql主從日常管理任務主要包括兩種:
查看復制狀態
復制任務控制
一、查看復制狀態
?要檢查主從復制當前的狀態,需要在從庫服務器上執行語句:
??
show?slave?status?執行結果如下所示:
?
mysql>?show?slave?status\G ***************************?1.?row?***************************Slave_IO_State:?Waiting?for?master?to?send?eventMaster_Host:?192.168.200.20Master_User:?replicaMaster_Port:?3306Connect_Retry:?10Master_Log_File:?mysql-binlog.000017Read_Master_Log_Pos:?120Relay_Log_File:?mysql-relaylog.000007Relay_Log_Pos:?286Relay_Master_Log_File:?mysql-binlog.000017Slave_IO_Running:?YesSlave_SQL_Running:?YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno:?0Last_Error:Skip_Counter:?0Exec_Master_Log_Pos:?120Relay_Log_Space:?624Until_Condition:?NoneUntil_Log_File:Until_Log_Pos:?0Master_SSL_Allowed:?NoMaster_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:?NoLast_IO_Errno:?0Last_IO_Error:Last_SQL_Errno:?0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id:?1Master_UUID:?e84592e0-0e5b-11e4-a5d3-000c29e88022Master_Info_File:?/usr/local/mysql/data/master.infoSQL_Delay:?0SQL_Remaining_Delay:?NULLSlave_SQL_Running_State:?Slave?has?read?all?relay?log;?waiting?for?the?slave?I/O?thread?to?update?itMaster_Retry_Count:?86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position:?0 1?row?in?set?(0.00?sec)查詢結果報表中比較關鍵的幾個字段:
Slave_IO_Status: 從服務器的IO狀態。
Slave_IO_Runing: 從服務器IO線程是否正在運行,即從服務器是否正在讀取主服務的二進制日志。
Slave_SQL_Running:從服務器SQL線程是否正在運行,即服務器是夠正在執行中繼日志中的事件.
Last_IO_Error,Last_SQL_Error: IO和SQL線程最新遇到的錯誤.
Seconds_Behind_Master:從服務器SQL線程處理中繼日志的時間(秒),通常可以用來不精確性的評估主從
復制的延遲大小. 0 秒 意味著基本同步,數值越大意味著偏移量越大,同步延遲較高。
*_Log_*: 基本上都是和日志文件有關的信息,比如當前的二進制日志文件名稱、位置等,都很容易理
解.
也可以在主服務器上查看當前已經連接的從服務器的狀態,
包括以下語句:
show?processlist?show?slave?hosts顯示效果如下所示:
?
mysql>?show?processlist\G ***************************?1.?row?***************************Id:?1User:?replicaHost:?192.168.200.21:40781db:?NULL Command:?Binlog?DumpTime:?1929State:?Master?has?sent?all?binlog?to?slave;?waiting?for?binlog?to?be?updatedInfo:?NULL ***************************?2.?row?***************************Id:?2User:?rootHost:?localhostdb:?NULL Command:?QueryTime:?0State:?initInfo:?show?processlist 2?rows?in?set?(0.00?sec)mysql>?show?slave?hosts; +-----------+------+------+-----------+--------------------------------------+ |?Server_id?|?Host?|?Port?|?Master_id?|?Slave_UUID???????????????????????????| +-----------+------+------+-----------+--------------------------------------+ |?????????2?|??????|?3306?|?????????1?|?624856ee-1d1c-11e4-8605-000c29972cfa?| +-----------+------+------+-----------+--------------------------------------+ 1?row?in?set?(0.00?sec)在主從復制過程中,是從服務器主動去主服務器獲取二進制日志數據,因此從服務器控制復制過程,占
據主動地位,也因此在主服務器上查看Slave 狀態時報表信息量很少.
二、復制任務控制
啟停復制控制語句:
#啟動復制 mysql>?start?slave; #停止復制 mysql>?stop?slave;"stop slave" 指令執行后,從服務器IO線程不再從主服務器讀取二進制日志文件并寫入到中繼日志,
?SQL 線程也不再從中繼日志讀取事件并執行事件語句。如果想單獨控制IO或者SQL線程,需要使用如
?下指令分別控制:
??
#停止IO線程 mysql>?stop?slave?io_thread; #停止SQL線程 mysql>?stop?slave?sql_thread;#分別啟用IO和SQL線程: mysql>?start?slave?io_thread; mysql>?start?slave?sql_thread;模擬控制線程并查看主從復制狀態:
停止IO線程,并查看主從復制狀態:
mysql>?stop?slave?io_thread; Query?OK,?0?rows?affected?(0.09?sec)mysql>?show?slave?status\G ***************************?1.?row?***************************Slave_IO_State:Master_Host:?192.168.200.20Master_User:?replicaMaster_Port:?3306Connect_Retry:?10Master_Log_File:?mysql-binlog.000017Read_Master_Log_Pos:?120Relay_Log_File:?mysql-relaylog.000007Relay_Log_Pos:?286Relay_Master_Log_File:?mysql-binlog.000017Slave_IO_Running:?NoSlave_SQL_Running:?YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno:?0Last_Error:Skip_Counter:?0Exec_Master_Log_Pos:?120Relay_Log_Space:?624Until_Condition:?NoneUntil_Log_File:Until_Log_Pos:?0Master_SSL_Allowed:?NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master:?NULL Master_SSL_Verify_Server_Cert:?NoLast_IO_Errno:?0Last_IO_Error:Last_SQL_Errno:?0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id:?1Master_UUID:?e84592e0-0e5b-11e4-a5d3-000c29e88022Master_Info_File:?/usr/local/mysql/data/master.infoSQL_Delay:?0SQL_Remaining_Delay:?NULLSlave_SQL_Running_State:?Slave?has?read?all?relay?log;?waiting?for?the?slave?I/O?thread?to?update?itMaster_Retry_Count:?86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position:?0 1?row?in?set?(0.00?sec)可以看到Slave_IO_Status 狀態值為No,而Slave_SQL_Status 狀態值仍為 Yes.
Slave_IO_Status 也為空值了。
再停止SQL線程:
mysql>?stop?slave?sql_thread; Query?OK,?0?rows?affected?(0.10?sec)mysql>?show?slave?status\G ***************************?1.?row?***************************Slave_IO_State:Master_Host:?192.168.200.20Master_User:?replicaMaster_Port:?3306Connect_Retry:?10Master_Log_File:?mysql-binlog.000017Read_Master_Log_Pos:?120Relay_Log_File:?mysql-relaylog.000007Relay_Log_Pos:?286Relay_Master_Log_File:?mysql-binlog.000017Slave_IO_Running:?NoSlave_SQL_Running:?NoReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno:?0Last_Error:Skip_Counter:?0Exec_Master_Log_Pos:?120Relay_Log_Space:?624Until_Condition:?NoneUntil_Log_File:Until_Log_Pos:?0Master_SSL_Allowed:?NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master:?NULL Master_SSL_Verify_Server_Cert:?NoLast_IO_Errno:?0Last_IO_Error:Last_SQL_Errno:?0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id:?1Master_UUID:?e84592e0-0e5b-11e4-a5d3-000c29e88022Master_Info_File:?/usr/local/mysql/data/master.infoSQL_Delay:?0SQL_Remaining_Delay:?NULLSlave_SQL_Running_State:Master_Retry_Count:?86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position:?0 1?row?in?set?(0.00?sec)可以看到Slave_IO_Status 、Slave_SQL_Status 狀態值全為 No.
再次執行"start slave" 或者分別執行"start slave io_thread"和"start slave sql_thread":
mysql>?start??slave?; mysql>?show?slave?status\G ***************************?1.?row?***************************Slave_IO_State:?Waiting?for?master?to?send?eventMaster_Host:?192.168.200.20Master_User:?replicaMaster_Port:?3306Connect_Retry:?10Master_Log_File:?mysql-binlog.000017Read_Master_Log_Pos:?120Relay_Log_File:?mysql-relaylog.000008Relay_Log_Pos:?286Relay_Master_Log_File:?mysql-binlog.000017Slave_IO_Running:?YesSlave_SQL_Running:?YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno:?0Last_Error:Skip_Counter:?0Exec_Master_Log_Pos:?120Relay_Log_Space:?624Until_Condition:?NoneUntil_Log_File:Until_Log_Pos:?0Master_SSL_Allowed:?NoMaster_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:?NoLast_IO_Errno:?0Last_IO_Error:Last_SQL_Errno:?0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id:?1Master_UUID:?e84592e0-0e5b-11e4-a5d3-000c29e88022Master_Info_File:?/usr/local/mysql/data/master.infoSQL_Delay:?0SQL_Remaining_Delay:?NULLSlave_SQL_Running_State:?Slave?has?read?all?relay?log;?waiting?for?the?slave?I/O?thread?to?update?itMaster_Retry_Count:?86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position:?0 1?row?in?set?(0.00?sec)查看復制狀態,主從復制恢復正常.
轉載于:https://blog.51cto.com/magic3/1538469
總結
以上是生活随笔為你收集整理的Mysql 主从复制常用管理任务介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: httpd配置详解
- 下一篇: TCP/IP之TCP连接的建立与中止状态