1. 程式人生 > >【mysql】 資料同步出現Slave_IO_Running:No問題的解決方法小結

【mysql】 資料同步出現Slave_IO_Running:No問題的解決方法小結

 MySQL replication中slave機器上有兩個關鍵的程序,死一個都不行,一個是slave_sql_running,一個是Slave_IO_Running,一個負責與主機的io通訊,一個負責自己的slave mysql程序。
下面寫一下,這兩個要是有no了,怎麼恢復。。 
 
如果是slave_io_runningno了,那麼就我個人看有三種情況,一個是網路有問題,連線不上,像有一次我用虛擬機器搭建replication,使用了nat的網路結構,就是死都連不上,第二個是有可能my.cnf有問題,配置檔案怎麼寫就不說了,網上太多了,最後一個是授權的問題,replication slave和file許可權是必須的。如果不怕死就all咯。。 
 
一旦io為no了先看err日誌,看看爆什麼錯,很可能是網路,也有可能是包太大收不了,這個時候主備上改max_allowed_packet這個引數。 
 
如果是slave_sql_runningno了,那麼也有兩種可能,一種是slave機器上這個表中出現了其他的寫操作,就是程式寫了,這個是會有問題的,今天我想重現,但是有時候會有問題,有時候就沒有問題,現在還不是太明瞭,後面再更新,還有一種佔絕大多數可能的是slave程序重啟,事務回滾造成的,這也是mysql的一種自我保護的措施,像關鍵時候只讀一樣。 
 
這個時候想恢復的話,只要停掉slave,set GLOBALSQL_SLAVE_SKIP_COUNTER=1;再開一下slave就可以了,這個全域性變數賦值為N的意思是: 
 
This statement skips the next N events from the master. This isuseful for recovering from replication stops caused by a statement. 
 
This statement is valid only when the slave thread is not running.Otherwise, it produces an error. 
 
呵呵,講的比我清楚。 
 
MYSQL映象伺服器因錯誤停止的恢復 
 
下午主伺服器,由於一些原因,導致宕機,重啟後,發現從伺服器的資料沒有跟上。 
配好MYSQL主從也才前幾天的事,沒多少經驗,第一次碰上這問題,有點焦急。不過,自己試了下,還算解決了:) 
 
從伺服器上 
Master_Log_File: mysqlhxmaster.000007 
Read_Master_Log_Pos: 84285377 
 
看一下主伺服器:mysqlhxmaster.000007| 84450528 | 
已經過後很多了,確實沒跟上。 
 
show slave status\G 
Slave_IO_Running: Yes 
Slave_SQL_Running: No 
 
有問題了,Slave_SQL_Running應該是Yes才對。 
 
 
再往下看,有錯誤的提示: 
 
Last_Errno: 1053 
Last_Error: Query partially completed on the master(error on master: 1053) and was aborted. There is a chance that your master isinconsistent at this point. If you are sure that your master is ok, run thisquery manually on the slave and then restart the slave with SET GLOBALSQL_SLAVE_SKIP_COUNTER=1; START SLAVE; . Query: 'INSERT INTO hx_stat_record......(一句SQL語句)' 
 
這裡有說明要怎麼操作了:) 
 
先stop slave,然後執行了一下提示的語句,再SETGLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE; 
 
show slave status\G 
 
Slave_IO_Running: Yes 
Slave_SQL_Running: Yes 
 
OK了,從伺服器也在幾分鐘內把堆積的log處理完了,兩邊又同步了:) 
 
從MYSQL伺服器Slave_IO_Running:No的解決2 
 
早晨機房意外斷電,導致了發現mysql從伺服器同步異常。使用以前碰到的Slave_SQL_Running為No的解決辦法無效,仍然無法同步。 
 
檢視一下狀態showslave status 
Master_Log_File: mysqlmaster.000079 
Read_Master_Log_Pos: 183913228 
Relay_Log_File: hx-relay-bin.002934 
Relay_Log_Pos: 183913371 
Relay_Master_Log_File: mysqlmaster.000079 
Slave_IO_Running: No 
Slave_SQL_Running: Yes 
 
主伺服器showmaster status\G 
File: mysqlmaster.000080 
Position: 13818288 
Binlog_Do_DB: 
Binlog_Ignore_DB: mysql,test 
 
mysql錯誤日誌: 
100512 9:13:17 [Note] Slave SQL thread initialized, startingreplication in log 'mysqlmaster.000079' at position 183913228, relay log'./hx-relay-bin.002934' position: 183913371 
100512 9:13:17 [Note] Slave I/O thread: connected to master'
[email protected]
:3306', replication started in log 'mysqlmaster.000079'at position 183913228 
100512 9:13:17 [ERROR] Error reading packet from server: Clientrequested master to start replication from impossible position (server_errno=1236) 
100512 9:13:17 [ERROR] Got fatal error 1236: 'Client requestedmaster to start replication from impossible position' from master when readingdata from binary log 
100512 9:13:17 [Note] Slave I/O thread exiting, read up to log'mysqlmaster.000079', position 183913228 
 
這次是Slave_IO_Running為No,從日誌上來看,伺服器讀mysqlmaster.000079這個Log的183913228這個位置時發生錯誤,這個位置不存在,於是無法同步。 
 
檢視一下這個Log的最後幾行: 
/*!40019 SET @@session.max_insert_delayed_threads=0*/; 
/*!50003
[email protected]
[email protected]@COMPLETION_TYPE,COMPLETION_TYPE=0*/; 
# at 4 
#100511 9:35:15 server id 1 end_log_pos 98 Start: binlog v 4,server v 5.0.27-standard-log created 100511 9:35:15 
# Warning: this binlog was not closed properly. Most probablymysqld crashed writing it. 
 
嘗試從損壞之前的位置開始 
SLAVE STOP; 
CHANGE MASTER TO MASTER_LOG_FILE='mysqlcncnmaster.000079',MASTER_LOG_POS=183913220; 
SLAVE START; 
無效! 
只好從新的日誌開始 
SLAVE STOP; 
CHANGE MASTER TO MASTER_LOG_FILE='mysqlcncnmaster.000080',MASTER_LOG_POS=0; 
SLAVE START; 
此時Slave_IO_Running恢復為Yes,同步進行了!觀察了會兒,沒有任何出錯跡象,問題解決。 
 
另外,出現Slave_IO_Running:NO還有一個原因是slave上沒有許可權讀master上的資料。
轉自:http://www.jb51.net/article/27220.htm