故障現象:
進入slave服務器,運行:
mysql> show slave status\G ....... Relay_Log_File: localhost-relay-bin.000535 Relay_Log_Pos: 21795072 Relay_Master_Log_File: localhost-bin.000094 Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: ......
解決辦法一、
Slave_SQL_Running: No
1.程序可能在slave上進行了寫操作
2.也可能是slave機器重起后,事務回滾造成的.
一般是事務回滾造成的:
解決辦法:
mysql> slave stop; mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; mysql> slave start;
解決辦法二、
首先停掉Slave服務:slave stop
到主服務器上查看主機狀態:
記錄File和Position對應的值
進入master
mysql> show master status; +----------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +----------------------+----------+--------------+------------------+ | localhost-bin.000094 | 33622483 | | | +----------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
然后到slave服務器上執行手動同步:
mysql> change master to > master_host='master_ip', > master_user='user', > master_password='pwd', > master_port=3306, > master_log_file=localhost-bin.000094', > master_log_pos=33622483 ; 1 row in set (0.00 sec) mysql> slave start; 1 row in set (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** ........ Master_Log_File: localhost-bin.000094 Read_Master_Log_Pos: 33768775 Relay_Log_File: localhost-relay-bin.000537 Relay_Log_Pos: 1094034 Relay_Master_Log_File: localhost-bin.000094 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB:
手動同步需要停止master的寫操作!
查看mysql主從配置的狀態及修正 slave不啟動問題
1、查看master的狀態
show master status; //Position不應該為0 show processlist; //state狀態應該為Has sent all binlog to slave; waiting for binlog to be updated
2、查看slave狀態
show slave status; //Slave_IO_Running 與 Slave_SQL_Running 狀態都要為Yes show processlist; //應該有兩行state值為: Has read all relay log; waiting for the slave I/O thread to update it Waiting for master to send event
3、錯誤日志
MySQL安裝目錄 /usr/local/mysql MySQL日志目錄 /usr/local/mysql/data/ 形如,Hostname.err
4、Change master to
如果從庫的Slave未啟動,Slave_IO_Running為NO。
可能是主庫是的master的信息有變化,
查看主庫show master status;
記錄下File,Position字段,假設為‘mysql-bin.000004’,98;
在從庫執行:
mysql>stop slave; mysql>change master to master_log_file='mysql-bin.000004',master_log_pos=98; mysql>start slave;
5、SET global sql_slave_skip_counter=n;
如果從庫的slave_sql_running為NO。
Err文件中記錄:
Slave:Error "Duplicate entry '1' for key 1" on query.....
可能是master未向slave同步成功,但slave中已經有了記錄。造成的沖突可以在從庫上執行
set global sql_slave_skip_counter=n;
跳過幾步。再restart slave就可以了。
6、同步錯誤處理
發現mysql slave服務器經常因為一些特殊字符或者符號產生的更新語句報錯,整個同步也會因此而卡在那,最初的辦法只是手動去出錯的機器執行下面三條SQL語句,跳過錯誤即可。
mysql>slave stop; mysql>set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; mysql>slave start;
說明:
Slave_IO_Running:連接到主庫,並讀取主庫的日志到本地,生成本地日志文件
Slave_SQL_Running:讀取本地日志文件,並執行日志里的SQL命令。