1. 程式人生 > >Mysql 主從報錯

Mysql 主從報錯

1、在從庫上面show slave status\G;出現下列情況,   Slave_IO_Running: Yes   Slave_SQL_Running: No   Seconds_Behind_Master: NULL 原因:程式可能在 slave 上進行來寫操作;slave 機器重啟後,事務會滾造成;同步過程中遇到某種錯誤,如果是這種情況,在檢視從庫狀態的時候會看到錯誤提示。 解決:進入 master;show master status; 然後去 slave 端執行手動同步。 slave stop change master to  master_host='...', master_user='...', master_passwd='', master_port=3306 ...... 2、slave 資料庫無法同步   show slave status顯示:   Slave_IO_Running: No   Slave_SQL_Running: Yes   Seconds_Behind_Master: NULL 原因:master 資料庫存在問題,由於連線主庫資訊錯誤、主庫資料庫掛掉如果說常見錯等原因引起的,在實際的操作中先重啟master後重啟slave即可解決這問題,出現此問題,必須要要重啟master資料庫。 解決:首先檢視資料庫的err日誌,檢視是什麼錯誤提示,看從庫連線主庫的IP、使用者、密碼等相關資訊是否有誤,如果有誤,重新執行同步;如果確認無誤,重啟主資料庫。 mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 98 | adb| mysql| +------------------+----------+--------------+------------------+ 進入從庫mysql,執行: slave stop; change master to Master_Log_File='mysql-bin.000001',Master_Log_Pos=98; slave start; 或是這樣: stop slave; set global sql_slave_skip_counter =1; start slave; 3、和第二題類似,重點看看解決思路 server_errno=1236 show slave status\G; Slave_IO_Running: NO  Slave_SQL_Running: Yes ...  Last_Errno: 0  Last_Error: ... Seconds_Behind_Master: NULL 排錯:檢視錯誤日誌 090605 9:13:20 [Note] Slave SQL thread initialized, starting replication in log ‘mysql-bin.000102′ at position 1029244974, relay log ‘./xxx-relay-bin.000634′ position: 98 090605 9:13:20 [Note] Slave I/O thread: connected to master ’[email protected]:3306′, replication started in log ‘mysql-bin.000102′ at position 1029244974 090605 9:13:20 [ERROR] Error reading packet from server: Client requested master to start replication from impossible position ( server_errno=1236) 090605 9:13:20 [ERROR] Got fatal error 1236: ‘Client requested master to start replication from impossible position’ from master when reading data from binary log 090605 9:13:20 [Note] Slave I/O thread exiting, read up to log ‘mysql-bin.000102′, position 1029244974 090605 9:13:52 [Note] Error reading relay log event: slave SQL thread was killed 原因:mysql-bin.000102 檔案中麼有 1029244974 這個位置,伺服器請求一個錯誤的位置而引起。 解決原理: 從相關的BIN LOG 裡最後一個日誌位置,與從伺服器上的日誌對比,如果LOG裡最後位置比錯誤的位置大,那麼,說明中間可能有斷點,需要把從伺服器的位置向前調,多試幾次.如果在LOG裡最後的位置比從伺服器指出的位置還小,那麼只要把從伺服器的位置設定成LOG的最後位置就可以了. 解決:1、slave stop;grant master to master_log_file='mysql-bin.000103', master_log_pos=0;      注意解決方法是重新同步,如果是按照上面的方法,跳過不再的位置,直接同步下一個檔案000103,那麼就可能造成主從資訊不一致,最好是從之前的位置同步      grant master to master_log_file='000102',master_log_pos='1019244974'      2、在 my.cnf 中加 slave-skip-errors 引數,否則會報主鍵重複的錯誤 4、報錯 Slave_IO_Running: No   Slave_SQL_Running: Yes Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 86400 原因:可能是主庫和從庫的 bin-log 一項不匹配, 解決:修改 bin-log 配置 >>> 重啟服務 >>> 進入從庫 >>> slave stop; change ...; slave start; show slave status; 5、slave 端報錯 Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 86400 原因:不能連線 master,master 授權錯誤;slave 密碼輸錯 排查:master 端, show grants for 'slaveuser2'@'%';  檢視是否是授權語句錯誤,注意這裡看不出密碼是多少,已經加密。 slave端, mysql -uslaveuser2 -P3306 -h 192.168.32.142 -p  輸入密碼,如果提示 denied by host,說明密碼不對, 解決:重新授權密碼,或者在master 端上翻出命令,檢視密碼是多少。 6、主從不能同步show slave status\G: Slave_SQL_Running: NO Seconds_Behind_Master: NULL Error xxx dosn't exist
原因:缺少某個具體的資料導致資訊不能同步 解決: slave stop;  set globle sql_slave_skip_counter = 1; slave start; 說明:SET GLOBAL SQL_SLAVE_SKIP_COUNTER=n  該命令在客戶端執行,用來跳過幾個事件,只有當同步程序出現錯誤而停止的時候才可以執行。 7、同步過程出現重複現象Last-Errno: 1062 Last-Error: Error 'Duplicate entry '15386' for key 1' on query. Default database: 'db'. Query: 'INSERT INTO db.table ( FIELDS ) VALUES ( VALUES )'  鍵重複了,必須跳過才能繼續。 原因: slave 開始複製資料的 position 已經存在,鍵發生重複 解決: 跳過該步驟,slave stop; set global sql_slave_skip_counter=1; slave start;       配置檔案 /etc/my.cnf 中寫入 --slave-skip-errors=[err_code1,err_code2....] 8、 No argument was provided to –log-bin, and –log-bin-index was not used; so replication may break when this MySQL server acts as a master and has his hostname changed!! Please use ‘–log-bin=mysql-bin’ to avoid this problem.
Neither –relay-log nor –relay-log-index were used; so replication may break when this MySQL server acts as a slave and has his hostname changed!! Please use ‘–relay-log=mysql-relay-bin’ to avoid this problem. 在my.cnf 的[mysqld]中加入 log-bin=mysql-bin 在my.cnf 的[mysqld]中加入 relay-log=mysql-relay-bin 9、 [ERROR] The slave I/O thread stops because master and slave have equal MySQL server ids
; these ids must be different for replication to work (or the –replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it). 在 my.cnf 中的 [mysqld] 中加入 replicate-same-server-id,但是如錯誤資訊所示,該方法並不總是有效,最好修改 server-id 10、[Note] Slave I/O thread: connected to master ‘test@:3306′,replication started in log ‘FIRST’ at position 4 [ERROR] Error reading packet from server: Access denied; you need the REPLICATION SLAVE privilege for this operation ( server_errno=1227) 可能是原來使用過 slave 連線,需要將 mysql 庫中的 master.info 刪除 11、從庫 slave 啟動錯誤  Could not initialize master info structure; more error messages can be found in the MySQL error log. 原因:初始化 master info 結構失敗 解決方法: 1、檢視MySQL錯誤日誌,如:同步的上一個Position是多少,很多情況下無法啟動服務是由於mysql識別的同步始終停留在上一個Position上。 2、檢視master.info和relay-log.info,master.info 記錄MASTER相關資訊,relay-log.info 記錄當前同步日誌資訊。 3、停止myslq服務,刪除master.info和relay-log.info。 4、啟動mysql服務。 5、重新CHANGE MASTER,重新啟動SLAVE服務。 12、主從資訊變更導致錯誤130311 14:15:46 mysqld started 130311 14:15:46 [Warning] option 'read_rnd_buffer_size': unsigned value 0 adjusted to 8200 130311 14:15:47 InnoDB: Started; log sequence number 15 2381115047 130311 14:15:47 [ERROR] Failed to open the relay log './localhost-relay-bin.000037' (relay_log_pos 52474065) 130311 14:15:47 [ERROR] Could not find target log during relay log initialization 130311 14:15:47 [ERROR] Failed to initialize the master info structure 130311 14:15:47 [Note] /usr/local/mysql/libexec/mysqld: ready for connections. Version: '5.0.87-log' socket: '/var/tmp/mysql2.sock' port: 3308 Source distribution 原因:由於變更來 slave 的服務埠和檔案路徑,mysql-relay-bin.index 中仍然保持著舊 relay 日誌檔案的路徑,這些路徑找不到合適的檔案,報錯。 解決:reset slave; change master to master_host。。。。。 13、Warning: a page in the doublewrite buffer is not within space 原因:多為磁碟故障引起。