線上MYSQL同步報錯故障處理總結
常見錯誤
最常見的3種情況
Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND;
the event's master log mysql-bin.000006, end_log_pos 254
Error_code: 1062;
handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000006, end_log_pos 924
Error_code: 1032;
handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000010, end_log_pos 263
非同步半同步區別
非同步複製
簡單的說就是master把binlog傳送過去,不管slave是否接收完,也不管是否執行完,這一動作就結束了.
半同步複製
簡單的說就是master把binlog傳送過去,slave確認接收完,但不管它是否執行完,給master一個訊號我這邊收到了,這一動作就結束了。(谷歌寫的程式碼,5.5上正式應用。)
非同步的劣勢
當master上寫操作繁忙時,當前POS點例如是10,而slave上IO_THREAD執行緒接收過來的是3,此時master當機,會造成相差7個點未傳送到slave上而資料丟失。
特殊的情況
slave的中繼日誌relay-bin損壞。 Last_SQL_Error: Error initializing relay log position: I/O error reading the header from the binary log
Last_SQL_Error: Error initializing relay log position: Binlog has bad magic number;
It's not a binary log file that can be used by this version of MySQL
這種情況SLAVE在當機,或者非法關機,例如電源故障、主機板燒了等,造成中繼日誌損壞,同步停掉。
人為失誤需謹慎:多臺slave存在重複server-id
這種情況同步會一直延時,永遠也同步不完,error錯誤日誌裡一直出現上面兩行資訊。解決方法就是把server-id改成不一致即可。
Slave: received end packet from server, apparent master shutdown:
Slave I/O thread: Failed reading log event, reconnecting to retry, log 'mysql-bin.000012' at postion 106
問題處理
刪除失敗
在master上刪除一條記錄,而slave上找不到。
Last_SQL_Error: Could not execute Delete_rows event on table hcy.t1;
Can't find record in 't1',
Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND;
the event's master log mysql-bin.000006, end_log_pos 254
解決方法:
由於master要刪除一條記錄,而slave上找不到故報錯,這種情況主上都將其刪除了,那麼從機可以直接跳過。可用命令:
stop slave;
set global sql_slave_skip_counter=1;
start slave;
如果這種情況很多,可用我寫的一個指令碼skip_error_replcation.sh,預設跳過10個錯誤(只針對這種情況才跳,其他情況輸出錯誤結果,等待處理),這個指令碼是參考maakit工具包的mk-slave-restart原理用shell寫的,功能上定義了一些自己的東西,不是無論什麼錯誤都一律跳過。)
主鍵重複
在slave已經有該記錄,又在master上插入了同一條記錄。
Last_SQL_Error: Could not execute Write_rows event on table hcy.t1;
Duplicate entry '2' for key 'PRIMARY',
Error_code: 1062;
handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000006, end_log_pos 924
解決方法:
在slave上用desc hcy.t1; 先看下錶結構:
mysql> desc hcy.t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | 0 | |
| name | char(4) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
刪除重複的主鍵
mysql> delete from t1 where id=2;
Query OK, 1 row affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
……
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
……
mysql> select * from t1 where id=2;
在master上和slave上再分別確認一下。
更新丟失
在master上更新一條記錄,而slave上找不到,丟失了資料。
Last_SQL_Error: Could not execute Update_rows event on table hcy.t1;
Can't find record in 't1',
Error_code: 1032;
handler error HA_ERR_KEY_NOT_FOUND;
the event's master log mysql-bin.000010, end_log_pos 794
解決方法:
在master上,用mysqlbinlog 分析下出錯的binlog日誌在幹什麼。
/usr/local/mysql/bin/mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql-bin.000010 | grep -A '10' 794
#120302 12:08:36 server id 22 end_log_pos 794 Update_rows: table id 33 flags: STMT_END_F
### UPDATE hcy.t1
### WHERE
### @1=2 /* INT meta=0 nullable=0 is_null=0 */
### @2='bbc' /* STRING(4) meta=65028 nullable=1 is_null=0 */
### SET
### @1=2 /* INT meta=0 nullable=0 is_null=0 */
### @2='BTV' /* STRING(4) meta=65028 nullable=1 is_null=0 */
# at 794
#120302 12:08:36 server id 22 end_log_pos 821 Xid = 60
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
在slave上,查詢下更新後的那條記錄,應該是不存在的。
mysql> select * from t1 where id=2;
Empty set (0.00 sec)
然後再到master檢視
mysql> select * from t1 where id=2;
+----+------+
| id | name |
+----+------+
| 2 | BTV |
+----+------+
1 row in set (0.00 sec)
把丟失的資料在slave上填補,然後跳過報錯即可。
mysql> insert into t1 values (2,'BTV');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1 where id=2;
+----+------+
| id | name |
+----+------+
| 2 | BTV |
+----+------+
1 row in set (0.00 sec)
mysql> stop slave ;set global sql_slave_skip_counter=1;start slave;
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
……
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
……
中繼日誌損壞
slave的中繼日誌relay-bin損壞。
Last_SQL_Error: Error initializing relay log position: I/O error reading the header from the binary log
Last_SQL_Error: Error initializing relay log position: Binlog has bad magic number;
It's not a binary log file that can be used by this version of MySQL
手工修復
解決方法:找到同步的binlog和POS點,然後重新做同步,這樣就可以有新的中繼日值了。
例子:
mysql> show slave status\G;
*************************** 1. row ***************************
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 1191
Relay_Log_File: vm02-relay-bin.000005
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1593
Last_Error: Error initializing relay log position: I/O error reading the header from the binary log
Skip_Counter: 1
Exec_Master_Log_Pos: 821
Slave_IO_Running :接收master的binlog資訊
Master_Log_File
Read_Master_Log_Pos
Slave_SQL_Running:執行寫操作
Relay_Master_Log_File
Exec_Master_Log_Pos
以執行寫的binlog和POS點為準。
Relay_Master_Log_File: mysql-bin.000010
Exec_Master_Log_Pos: 821
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010',MASTER_LOG_POS=821;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.8.22
Master_User: repl
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 1191
Relay_Log_File: vm02-relay-bin.000002
Relay_Log_Pos: 623
Relay_Master_Log_File: mysql-bin.000010
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: 1191
Relay_Log_Space: 778
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:
Ibbackup
各種大招都用上了,無奈slave資料丟失過多,ibbackup(需要銀子)該你登場了。
Ibbackup熱備份工具,是付費的。xtrabackup是免費的,功能上一樣。
Ibbackup備份期間不鎖表,備份時開啟一個事務(相當於做一個快照),然後會記錄一個點,之後資料的更改儲存在ibbackup_logfile檔案裡,恢復時把ibbackup_logfile 變化的資料再寫入到ibdata裡。
Ibbackup 只備份資料( ibdata、.ibd ),表結構.frm不備份。
下面一個演示例子:
備份:ibbackup /bak/etc/my_local.cnf /bak/etc/my_bak.cnf
恢復:ibbackup --apply-log /bak/etc/my_bak.cnf
[root@vm01 etc]# more my_local.cnf
datadir =/usr/local/mysql/data
innodb_data_home_dir = /usr/local/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/data
innodb_buffer_pool_size = 100M
innodb_log_file_size = 5M
innodb_log_files_in_group=2
[root@vm01 etc]# ibbackup /bak/etc/my_local.cnf /bak/etc/my_bak.cnf
InnoDB Hot Backup version 3.0.0; Copyright 2002-2005 Innobase Oy
License A21488 is granted to vm01 (chunyang_he@126.com)
(--apply-log works in any computer regardless of the hostname)
Licensed for use in a computer whose hostname is 'vm01'
Expires 2012-5-1 (year-month-day) at 00:00
See for further information
Type ibbackup --license for detailed license terms, --help for help
Contents of /bak/etc/my_local.cnf:
innodb_data_home_dir got value /usr/local/mysql/data
innodb_data_file_path got value ibdata1:10M:autoextend
datadir got value /usr/local/mysql/data
innodb_log_group_home_dir got value /usr/local/mysql/data
innodb_log_files_in_group got value 2
innodb_log_file_size got value 5242880
Contents of /bak/etc/my_bak.cnf:
innodb_data_home_dir got value /bak/data
innodb_data_file_path got value ibdata1:10M:autoextend
datadir got value /bak/data
innodb_log_group_home_dir got value /bak/data
innodb_log_files_in_group got value 2
innodb_log_file_size got value 5242880
ibbackup: Found checkpoint at lsn 0 1636898
ibbackup: Starting log scan from lsn 0 1636864
120302 16:47:43 ibbackup: Copying log...
120302 16:47:43 ibbackup: Log copied, lsn 0 1636898
ibbackup: We wait 1 second before starting copying the data files...
120302 16:47:44 ibbackup: Copying /usr/local/mysql/data/ibdata1
ibbackup: A copied database page was modified at 0 1636898
ibbackup: Scanned log up to lsn 0 1636898
ibbackup: Was able to parse the log up to lsn 0 1636898
ibbackup: Maximum page number for a log record 0
120302 16:47:46 ibbackup: Full backup completed!
[root@vm01 etc]#
[root@vm01 etc]# cd /bak/data/
[root@vm01 data]# ls
ibbackup_logfile ibdata1
[root@vm01 data]# ibbackup --apply-log /bak/etc/my_bak.cnf
InnoDB Hot Backup version 3.0.0; Copyright 2002-2005 Innobase Oy
License A21488 is granted to vm01 (chunyang_he@126.com)
(--apply-log works in any computer regardless of the hostname)
Licensed for use in a computer whose hostname is 'vm01'
Expires 2012-5-1 (year-month-day) at 00:00
See for further information
Type ibbackup --license for detailed license terms, --help for help
Contents of /bak/etc/my_bak.cnf:
innodb_data_home_dir got value /bak/data
innodb_data_file_path got value ibdata1:10M:autoextend
datadir got value /bak/data
innodb_log_group_home_dir got value /bak/data
innodb_log_files_in_group got value 2
innodb_log_file_size got value 5242880
120302 16:48:38 ibbackup: ibbackup_logfile's creation parameters:
ibbackup: start lsn 0 1636864, end lsn 0 1636898,
ibbackup: start checkpoint 0 1636898
ibbackup: start checkpoint 0 1636898
InnoDB: Doing recovery: scanned up to log sequence number 0 1636898
InnoDB: Starting an apply batch of log records to the database...
InnoDB: Progress in percents: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 .....99
Setting log file size to 0 5242880
ibbackup: We were able to parse ibbackup_logfile up to
ibbackup: lsn 0 1636898
ibbackup: Last MySQL binlog file position 0 1191, file name ./mysql-bin.000010
ibbackup: The first data file is '/bak/data/ibdata1'
ibbackup: and the new created log files are at '/bak/data/'
120302 16:48:38 ibbackup: Full backup prepared for recovery successfully!
[root@vm01 data]# ls
ibbackup_logfile ibdata1 ib_logfile0 ib_logfile1
把ibdata1 ib_logfile0 ib_logfile1複製到從,把.frm也複製過去,啟動MYSQL後,做同步,那個點就是上面輸出的:
ibbackup: Last MySQL binlog file position 0 1191, file name ./mysql-bin.000010
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010',MASTER_LOG_POS=1191;
Maatkit工具包
?簡介
maatkit是一個開源的工具包,為mysql日常管理提供了幫助。目前,已被Percona公司收購併維護。其中:
mk-table-checksum是用來檢測master和slave上的表結構和資料是否一致。
mk-table-sync是發生主從資料不一致時,來修復的。
這兩個工具包,沒有在現網實際操作的經驗,這裡僅僅是新技術探討和學術交流,下面展示下如何使用。
[root@vm02]# mk-table-checksum h=vm01,u=admin,p=123456 h=vm02,u=admin,p=123456 -d hcy -t t1
Cannot connect to MySQL because the Perl DBI module is not installed or not found.
Run 'perl -MDBI' to see the directories that Perl searches for DBI.
If DBI is not installed, try:
Debian/Ubuntu apt-get install libdbi-perl
RHEL/CentOS yum install perl-DBI
OpenSolaris pgk install pkg:/SUNWpmdbi
提示缺少perl-DBI模組,那麼直接 yum install perl-DBI。
[root@vm02 bin]# mk-table-checksum h=vm01,u=admin,p=123456 h=vm02,u=admin,p=123456 -d hcy -t t1
DATABASE TABLE CHUNK HOST ENGINE COUNT CHECKSUM TIME WAIT STAT LAG
hcy t1 0 vm02 InnoDB NULL 1957752020 0 0 NULL NULL
hcy t1 0 vm01 InnoDB NULL 1957752020 0 0 NULL NULL
如果表資料不一致,CHECKSUM的值是不相等的。
解釋下輸出的意思:
DATABASE:資料庫名
TABLE:表名
CHUNK:checksum時的近似數值
HOST:MYSQL的地址
ENGINE:表引擎
COUNT:表的行數
CHECKSUM:校驗值
TIME:所用時間
WAIT:等待時間
STAT:MASTER_POS_WAIT()返回值
LAG:slave的延時時間
如果你想過濾出不相等的都有哪些表,可以用mk-checksum-filter這個工具,只要在後面加個管道符就行了。
[root@vm02 ~]# mk-table-checksum h=vm01,u=admin,p=123456 h=vm02,u=admin,p=123456 -d hcy | mk-checksum-filter
hcy t2 0 vm01 InnoDB NULL 1957752020 0 0 NULL NULL
hcy t2 0 vm02 InnoDB NULL 1068689114 0 0 NULL NULL
知道有哪些表不一致,可以用mk-table-sync這個工具來處理。
注:在執行mk-table-checksum時會鎖表,表的大小取決於執行的快慢。
MASTER上的t2表資料: SLAVE上的t2表資料:
mysql> select * from t2; mysql> select * from t2;
+----+------+ +----+------+
| id | name | | id | name |
+----+------+ +----+------+
| 1 | a | | 1 | a |
| 2 | b | | 2 | b |
| 3 | ss | | 3 | ss |
| 4 | asd | | 4 | asd |
| 5 | ss | +----+------+
+----+------+ 4 rows in set (0.00 sec)
5 rows in set (0.00 sec)
mysql> \! hostname;
mysql> \! hostname; vm02
vm01
[root@vm02 ~]# mk-table-sync --execute --print --no-check-slave --transaction --databases hcy h=vm01,u=admin,p=123456 h=vm02,u=admin,p=123456
INSERT INTO `hcy`.`t2`(`id`, `name`) VALUES ('5', 'ss') /*maatkit src_db:hcy src_tbl:t2 src_dsn:h=vm01,p=...,u=admin dst_db:hcy dst_tbl:t2
dst_dsn:h=vm02,p=...,u=admin lock:0 transaction:1 changing_src:0 replicate:0 bidirectional:0 pid:3246 user:root host:vm02*/;
它的工作原理是:先一行一行檢查主從庫的表是否一樣,如果哪裡不一樣,就執行刪除,更新,插入等操作,使其達到一致。表的大小決定著執行的快慢。
If C<--transaction> is specified, C is not used. Instead, lock
and unlock are implemented by beginning and committing transactions.
The exception is if L<"--lock"> is 3.
If C<--no-transaction> is specified, then C is used for any
value of L<"--lock">. See L<"--[no]transaction">.
When enabled, either explicitly or implicitly, the transaction isolation level
is set C and transactions are started C<with consistent SNAPSHOT>
MySQL複製監控
?原文:
MySQL常見錯誤型別
1005:建立表失敗
1006:建立資料庫失敗
1007:資料庫已存在,建立資料庫失敗
1008:資料庫不存在,刪除資料庫失敗
1009:不能刪除資料庫檔案導致刪除資料庫失敗
1010:不能刪除資料目錄導致刪除資料庫失敗
1011:刪除資料庫檔案失敗
1012:不能讀取系統表中的記錄
1020:記錄已被其他使用者修改
1021:硬碟剩餘空間不足,請加大硬碟可用空間
1022:關鍵字重複,更改記錄失敗
1023:關閉時發生錯誤
1024:讀檔案錯誤
1025:更改名字時發生錯誤
1026:寫檔案錯誤
1032:記錄不存在
1036:資料表是隻讀的,不能對它進行修改
1037:系統記憶體不足,請重啟資料庫或重啟伺服器
1038:用於排序的記憶體不足,請增大排序緩衝區
1040:已到達資料庫的最大連線數,請加大資料庫可用連線數
1041:系統記憶體不足
1042:無效的主機名
1043:無效連線
1044:當前使用者沒有訪問資料庫的許可權
1045:不能連線資料庫,使用者名稱或密碼錯誤
1048:欄位不能為空
1049:資料庫不存在
1050:資料表已存在
1051:資料表不存在
1054:欄位不存在
1065:無效的SQL語句,SQL語句為空
1081:不能建立Socket連線
1114:資料表已滿,不能容納任何記錄
1116:開啟的資料表太多
1129:資料庫出現異常,請重啟資料庫
1130:連線資料庫失敗,沒有連線資料庫的許可權
1133:資料庫使用者不存在
1141:當前使用者無權訪問資料庫
1142:當前使用者無權訪問資料表
1143:當前使用者無權訪問資料表中的欄位
1146:資料表不存在
1147:未定義使用者對資料表的訪問許可權
1149:SQL語句語法錯誤
1158:網路錯誤,出現讀錯誤,請檢查網路連線狀況
1159:網路錯誤,讀超時,請檢查網路連線狀況
1160:網路錯誤,出現寫錯誤,請檢查網路連線狀況
1161:網路錯誤,寫超時,請檢查網路連線狀況
1062:欄位值重複,入庫失敗
1169:欄位值重複,更新記錄失敗
1177:開啟資料表失敗
1180:提交事務失敗
1181:回滾事務失敗
1203:當前使用者和資料庫建立的連線已到達資料庫的最大連線數,請增大可用的資料庫連線數或重啟資料庫
1205:加鎖超時
1211:當前使用者沒有建立使用者的許可權
1216:外來鍵約束檢查失敗,更新子表記錄失敗
1217:外來鍵約束檢查失敗,刪除或修改主表記錄失敗
1226:當前使用者使用的資源已超過所允許的資源,請重啟資料庫或重啟伺服器
1227:許可權不足,您無權進行此操作
1235:MySQL版本過低,不具有本功能
複製監控指令碼
參考原文修改。
?原指令碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
#
#check_mysql_slave_replication_status
#
#
#
parasum=2
help_msg(){
cat << help
+---------------------+
+Error Cause:
+you must input $parasum parameters!
+1st : Host_IP
+2st : Host_Port
help
exit
}
[ $# -ne ${parasum} ] && help_msg #若引數不夠列印幫助資訊並退出
export HOST_IP=$1
export HOST_PORt=$2
MYUSER="root"
MYPASS="123456"
MYSQL_CMD="mysql -u$MYUSER -p$MYPASS"
MailTitle="" #郵件主題
Mail_Address_MysqlStatus="root@localhost.localdomain" #收件人郵箱
time1=$(date +"%Y%m%d%H%M%S")
time2=$(date +"%Y-%m-%d %H:%M:%S")
SlaveStatusFile=/tmp/salve_status_${HOST_PORT}.${time1} #郵件內容所在檔案
echo "--------------------Begin at: "$time2 > $SlaveStatusFile
echo "" >> $SlaveStatusFile
#get slave status
${MYSQL_CMD} -e "show slave status\G" >> $SlaveStatusFile #取得salve程式的狀態
#get io_thread_status,sql_thread_status,last_errno 取得以下狀態值
IOStatus=$(cat $SlaveStatusFile|grep Slave_IO_Running|awk '{print $2}')
SQLStatus=$(cat $SlaveStatusFile|grep Slave_SQL_Running |awk '{print $2}')
Errno=$(cat $SlaveStatusFile|grep Last_Errno | awk '{print $2}')
Behind=$(cat $SlaveStatusFile|grep Seconds_Behind_Master | awk '{print $2}')
echo "" >> $SlaveStatusFile
if [ "$IOStatus" == "No" ] || [ "$SQLStatus" == "No" ];then #判斷錯誤型別
if [ "$Errno" -eq 0 ];then #可能是salve執行緒未啟動
$MYSQL_CMD -e "start slave io_thread;start slave sql_thread;"
echo "Cause slave threads doesnot's running,trying start slsave io_thread;start slave sql_thread;" >> $SlaveStatusFile
MailTitle="[Warning] Slave threads stoped on $HOST_IP $HOST_PORT"
elif [ "$Errno" -eq 1007 ] || [ "$Errno" -eq 1053 ] || [ "$Errno" -eq 1062 ] || [ "$Errno" -eq 1213 ] || [ "$Errno" -eq 1032 ]\
|| [ "Errno" -eq 1158 ] || [ "$Errno" -eq 1159 ] || [ "$Errno" -eq 1008 ];then #忽略此些錯誤
$MYSQL_CMD -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
echo "Cause slave replication catch errors,trying skip counter and restart slave;stop slave ;set global sql_slave_skip_counter=1;slave start;" >> $SlaveStatusFile
MailTitle="[Warning] Slave error on $HOST_IP $HOST_PORT! ErrNum: $Errno"
else
echo "Slave $HOST_IP $HOST_PORT is down!" >> $SlaveStatusFile
MailTitle="[ERROR]Slave replication is down on $HOST_IP $HOST_PORT ! ErrNum:$Errno"
fi
fi
if [ -n "$Behind" ];then
Behind=0
fi
echo "$Behind" >> $SlaveStatusFile
#delay behind master 判斷延時時間
if [ $Behind -gt 300 ];then
echo `date +"%Y-%m%d %H:%M:%S"` "slave is behind master $Bebind seconds!" >> $SlaveStatusFile
MailTitle="[Warning]Slave delay $Behind seconds,from $HOST_IP $HOST_PORT"
fi
if [ -n "$MailTitle" ];then #若出錯或者延時時間大於300s則傳送郵件
cat ${SlaveStatusFile} | /bin/mail -s "$MailTitle" $Mail_Address_MysqlStatus
fi
#del tmpfile:SlaveStatusFile
> $SlaveStatusFile
?修改後指令碼 ?只做了簡單的整理,修正了Behind為NULL的判斷,但均未測試;
?應可考慮增加: ?對修復執行結果的判斷;多條錯誤的迴圈修復、檢測、再修復?
?取消SlaveStatusFile臨時檔案。
?Errno、Behind兩種告警分別發郵件,告警正文增加show slave結果原文。
?增加PATH,以便加到crontab中。
?考慮crontab中週期執行(加鎖避免執行衝突、執行週期選擇)
?增加執行日誌?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/sh
# check_mysql_slave_replication_status
# 參考:
Usage(){
echo Usage:
echo "$0 HOST PORT USER PASS"
}
[ -z "$1" -o -z "$2" -o -z "$3" -o -z "$4" ] && Usage && exit 1
HOST=$1
PORT=$2
USER=$3
PASS=$4
MYSQL_CMD="mysql -h$HOST -P$PORT -u$USER -p$PASS"
MailTitle="" #郵件主題
Mail_Address_MysqlStatus="root@localhost.localdomain" #收件人郵箱
time1=$(date +"%Y%m%d%H%M%S")
time2=$(date +"%Y-%m-%d %H:%M:%S")
SlaveStatusFile=/tmp/salve_status_${HOST_PORT}.${time1} #郵件內容所在檔案
echo "--------------------Begin at: "$time2 > $SlaveStatusFile
echo "" >> $SlaveStatusFile
#get slave status
${MYSQL_CMD} -e "show slave status\G" >> $SlaveStatusFile #取得salve程式的狀態
#get io_thread_status,sql_thread_status,last_errno 取得以下狀態值
IOStatus=$(cat $SlaveStatusFile|grep Slave_IO_Running|awk '{print $2}')
SQLStatus=$(cat $SlaveStatusFile|grep Slave_SQL_Running |awk '{print $2}')
Errno=$(cat $SlaveStatusFile|grep Last_Errno | awk '{print $2}')
Behind=$(cat $SlaveStatusFile|grep Seconds_Behind_Master | awk '{print $2}')
echo "" >> $SlaveStatusFile
if [ "$IOStatus" = "No" -o "$SQLStatus" = "No" ];then
case "$Errno" in
0)
# 可能是slave未啟動
$MYSQL_CMD -e "start slave io_thread;start slave sql_thread;"
echo "Cause slave threads doesnot's running,trying start slsave io_thread;start slave sql_thread;" >> $SlaveStatusFile
;;
1007|1053|1062|1213|1032|1158|1159|1008)
# 忽略這些錯誤
$MYSQL_CMD -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
echo "Cause slave replication catch errors,trying skip counter and restart slave;stop slave ;set global sql_slave_skip_counter=1;slave start;" >> $SlaveStatusFile
MailTitle="[Warning] Slave error on $HOST:$PORT! ErrNum: $Errno"
;;
*)
echo "Slave $HOST:$PORT is down!" >> $SlaveStatusFile
MailTitle="[ERROR]Slave replication is down on $HOST:$PORT! Errno:$Errno"
;;
esac
fi
if [ "$Behind" = "NULL" -o -z "$Behind" ];then
Behind=0
fi
echo "Behind:$Behind" >> $SlaveStatusFile
#delay behind master 判斷延時時間
if [ $Behind -gt 300 ];then
echo `date +"%Y-%m%d %H:%M:%S"` "slave is behind master $Bebind seconds!" >> $SlaveStatusFile
MailTitle="[Warning]Slave delay $Behind seconds,from $HOST $PORT"
fi
if [ -n "$MailTitle" ];then #若出錯或者延時時間大於300s則傳送郵件
cat ${SlaveStatusFile} | /bin/mail -s "$MailTitle" $Mail_Address_MysqlStatus
fi
#del tmpfile:SlaveStatusFile
> $SlaveStatusFile
最常見的3種情況
- 第一種:在master上刪除一條記錄,而slave上找不到。 Last_SQL_Error: Could not execute Delete_rows event on table hcy.t1;
Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND;
the event's master log mysql-bin.000006, end_log_pos 254
- 第二種:主鍵重複。在slave已經有該記錄,又在master上插入了同一條記錄。 Last_SQL_Error: Could not execute Write_rows event on table hcy.t1;
Error_code: 1062;
handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000006, end_log_pos 924
- 第三種:在master上更新一條記錄,而slave上找不到,丟失了資料。 Last_SQL_Error: Could not execute Update_rows event on table hcy.t1;
Error_code: 1032;
handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000010, end_log_pos 263
非同步半同步區別
非同步複製
簡單的說就是master把binlog傳送過去,不管slave是否接收完,也不管是否執行完,這一動作就結束了.
半同步複製
簡單的說就是master把binlog傳送過去,slave確認接收完,但不管它是否執行完,給master一個訊號我這邊收到了,這一動作就結束了。(谷歌寫的程式碼,5.5上正式應用。)
非同步的劣勢
當master上寫操作繁忙時,當前POS點例如是10,而slave上IO_THREAD執行緒接收過來的是3,此時master當機,會造成相差7個點未傳送到slave上而資料丟失。
特殊的情況
slave的中繼日誌relay-bin損壞。 Last_SQL_Error: Error initializing relay log position: I/O error reading the header from the binary log
Last_SQL_Error: Error initializing relay log position: Binlog has bad magic number;
It's not a binary log file that can be used by this version of MySQL
這種情況SLAVE在當機,或者非法關機,例如電源故障、主機板燒了等,造成中繼日誌損壞,同步停掉。
人為失誤需謹慎:多臺slave存在重複server-id
這種情況同步會一直延時,永遠也同步不完,error錯誤日誌裡一直出現上面兩行資訊。解決方法就是把server-id改成不一致即可。
Slave: received end packet from server, apparent master shutdown:
Slave I/O thread: Failed reading log event, reconnecting to retry, log 'mysql-bin.000012' at postion 106
問題處理
刪除失敗
在master上刪除一條記錄,而slave上找不到。
Last_SQL_Error: Could not execute Delete_rows event on table hcy.t1;
Can't find record in 't1',
Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND;
the event's master log mysql-bin.000006, end_log_pos 254
解決方法:
由於master要刪除一條記錄,而slave上找不到故報錯,這種情況主上都將其刪除了,那麼從機可以直接跳過。可用命令:
stop slave;
set global sql_slave_skip_counter=1;
start slave;
如果這種情況很多,可用我寫的一個指令碼skip_error_replcation.sh,預設跳過10個錯誤(只針對這種情況才跳,其他情況輸出錯誤結果,等待處理),這個指令碼是參考maakit工具包的mk-slave-restart原理用shell寫的,功能上定義了一些自己的東西,不是無論什麼錯誤都一律跳過。)
主鍵重複
在slave已經有該記錄,又在master上插入了同一條記錄。
Last_SQL_Error: Could not execute Write_rows event on table hcy.t1;
Duplicate entry '2' for key 'PRIMARY',
Error_code: 1062;
handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000006, end_log_pos 924
解決方法:
在slave上用desc hcy.t1; 先看下錶結構:
mysql> desc hcy.t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | 0 | |
| name | char(4) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
刪除重複的主鍵
mysql> delete from t1 where id=2;
Query OK, 1 row affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
……
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
……
mysql> select * from t1 where id=2;
在master上和slave上再分別確認一下。
更新丟失
在master上更新一條記錄,而slave上找不到,丟失了資料。
Last_SQL_Error: Could not execute Update_rows event on table hcy.t1;
Can't find record in 't1',
Error_code: 1032;
handler error HA_ERR_KEY_NOT_FOUND;
the event's master log mysql-bin.000010, end_log_pos 794
解決方法:
在master上,用mysqlbinlog 分析下出錯的binlog日誌在幹什麼。
/usr/local/mysql/bin/mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql-bin.000010 | grep -A '10' 794
#120302 12:08:36 server id 22 end_log_pos 794 Update_rows: table id 33 flags: STMT_END_F
### UPDATE hcy.t1
### WHERE
### @1=2 /* INT meta=0 nullable=0 is_null=0 */
### @2='bbc' /* STRING(4) meta=65028 nullable=1 is_null=0 */
### SET
### @1=2 /* INT meta=0 nullable=0 is_null=0 */
### @2='BTV' /* STRING(4) meta=65028 nullable=1 is_null=0 */
# at 794
#120302 12:08:36 server id 22 end_log_pos 821 Xid = 60
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
在slave上,查詢下更新後的那條記錄,應該是不存在的。
mysql> select * from t1 where id=2;
Empty set (0.00 sec)
然後再到master檢視
mysql> select * from t1 where id=2;
+----+------+
| id | name |
+----+------+
| 2 | BTV |
+----+------+
1 row in set (0.00 sec)
把丟失的資料在slave上填補,然後跳過報錯即可。
mysql> insert into t1 values (2,'BTV');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1 where id=2;
+----+------+
| id | name |
+----+------+
| 2 | BTV |
+----+------+
1 row in set (0.00 sec)
mysql> stop slave ;set global sql_slave_skip_counter=1;start slave;
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
……
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
……
中繼日誌損壞
slave的中繼日誌relay-bin損壞。
Last_SQL_Error: Error initializing relay log position: I/O error reading the header from the binary log
Last_SQL_Error: Error initializing relay log position: Binlog has bad magic number;
It's not a binary log file that can be used by this version of MySQL
手工修復
解決方法:找到同步的binlog和POS點,然後重新做同步,這樣就可以有新的中繼日值了。
例子:
mysql> show slave status\G;
*************************** 1. row ***************************
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 1191
Relay_Log_File: vm02-relay-bin.000005
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1593
Last_Error: Error initializing relay log position: I/O error reading the header from the binary log
Skip_Counter: 1
Exec_Master_Log_Pos: 821
Slave_IO_Running :接收master的binlog資訊
Master_Log_File
Read_Master_Log_Pos
Slave_SQL_Running:執行寫操作
Relay_Master_Log_File
Exec_Master_Log_Pos
以執行寫的binlog和POS點為準。
Relay_Master_Log_File: mysql-bin.000010
Exec_Master_Log_Pos: 821
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010',MASTER_LOG_POS=821;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.8.22
Master_User: repl
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 1191
Relay_Log_File: vm02-relay-bin.000002
Relay_Log_Pos: 623
Relay_Master_Log_File: mysql-bin.000010
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: 1191
Relay_Log_Space: 778
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:
Ibbackup
各種大招都用上了,無奈slave資料丟失過多,ibbackup(需要銀子)該你登場了。
Ibbackup熱備份工具,是付費的。xtrabackup是免費的,功能上一樣。
Ibbackup備份期間不鎖表,備份時開啟一個事務(相當於做一個快照),然後會記錄一個點,之後資料的更改儲存在ibbackup_logfile檔案裡,恢復時把ibbackup_logfile 變化的資料再寫入到ibdata裡。
Ibbackup 只備份資料( ibdata、.ibd ),表結構.frm不備份。
下面一個演示例子:
備份:ibbackup /bak/etc/my_local.cnf /bak/etc/my_bak.cnf
恢復:ibbackup --apply-log /bak/etc/my_bak.cnf
[root@vm01 etc]# more my_local.cnf
datadir =/usr/local/mysql/data
innodb_data_home_dir = /usr/local/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/data
innodb_buffer_pool_size = 100M
innodb_log_file_size = 5M
innodb_log_files_in_group=2
[root@vm01 etc]# ibbackup /bak/etc/my_local.cnf /bak/etc/my_bak.cnf
InnoDB Hot Backup version 3.0.0; Copyright 2002-2005 Innobase Oy
License A21488 is granted to vm01 (chunyang_he@126.com)
(--apply-log works in any computer regardless of the hostname)
Licensed for use in a computer whose hostname is 'vm01'
Expires 2012-5-1 (year-month-day) at 00:00
See for further information
Type ibbackup --license for detailed license terms, --help for help
Contents of /bak/etc/my_local.cnf:
innodb_data_home_dir got value /usr/local/mysql/data
innodb_data_file_path got value ibdata1:10M:autoextend
datadir got value /usr/local/mysql/data
innodb_log_group_home_dir got value /usr/local/mysql/data
innodb_log_files_in_group got value 2
innodb_log_file_size got value 5242880
Contents of /bak/etc/my_bak.cnf:
innodb_data_home_dir got value /bak/data
innodb_data_file_path got value ibdata1:10M:autoextend
datadir got value /bak/data
innodb_log_group_home_dir got value /bak/data
innodb_log_files_in_group got value 2
innodb_log_file_size got value 5242880
ibbackup: Found checkpoint at lsn 0 1636898
ibbackup: Starting log scan from lsn 0 1636864
120302 16:47:43 ibbackup: Copying log...
120302 16:47:43 ibbackup: Log copied, lsn 0 1636898
ibbackup: We wait 1 second before starting copying the data files...
120302 16:47:44 ibbackup: Copying /usr/local/mysql/data/ibdata1
ibbackup: A copied database page was modified at 0 1636898
ibbackup: Scanned log up to lsn 0 1636898
ibbackup: Was able to parse the log up to lsn 0 1636898
ibbackup: Maximum page number for a log record 0
120302 16:47:46 ibbackup: Full backup completed!
[root@vm01 etc]#
[root@vm01 etc]# cd /bak/data/
[root@vm01 data]# ls
ibbackup_logfile ibdata1
[root@vm01 data]# ibbackup --apply-log /bak/etc/my_bak.cnf
InnoDB Hot Backup version 3.0.0; Copyright 2002-2005 Innobase Oy
License A21488 is granted to vm01 (chunyang_he@126.com)
(--apply-log works in any computer regardless of the hostname)
Licensed for use in a computer whose hostname is 'vm01'
Expires 2012-5-1 (year-month-day) at 00:00
See for further information
Type ibbackup --license for detailed license terms, --help for help
Contents of /bak/etc/my_bak.cnf:
innodb_data_home_dir got value /bak/data
innodb_data_file_path got value ibdata1:10M:autoextend
datadir got value /bak/data
innodb_log_group_home_dir got value /bak/data
innodb_log_files_in_group got value 2
innodb_log_file_size got value 5242880
120302 16:48:38 ibbackup: ibbackup_logfile's creation parameters:
ibbackup: start lsn 0 1636864, end lsn 0 1636898,
ibbackup: start checkpoint 0 1636898
ibbackup: start checkpoint 0 1636898
InnoDB: Doing recovery: scanned up to log sequence number 0 1636898
InnoDB: Starting an apply batch of log records to the database...
InnoDB: Progress in percents: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 .....99
Setting log file size to 0 5242880
ibbackup: We were able to parse ibbackup_logfile up to
ibbackup: lsn 0 1636898
ibbackup: Last MySQL binlog file position 0 1191, file name ./mysql-bin.000010
ibbackup: The first data file is '/bak/data/ibdata1'
ibbackup: and the new created log files are at '/bak/data/'
120302 16:48:38 ibbackup: Full backup prepared for recovery successfully!
[root@vm01 data]# ls
ibbackup_logfile ibdata1 ib_logfile0 ib_logfile1
把ibdata1 ib_logfile0 ib_logfile1複製到從,把.frm也複製過去,啟動MYSQL後,做同步,那個點就是上面輸出的:
ibbackup: Last MySQL binlog file position 0 1191, file name ./mysql-bin.000010
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010',MASTER_LOG_POS=1191;
Maatkit工具包
?簡介
maatkit是一個開源的工具包,為mysql日常管理提供了幫助。目前,已被Percona公司收購併維護。其中:
mk-table-checksum是用來檢測master和slave上的表結構和資料是否一致。
mk-table-sync是發生主從資料不一致時,來修復的。
這兩個工具包,沒有在現網實際操作的經驗,這裡僅僅是新技術探討和學術交流,下面展示下如何使用。
[root@vm02]# mk-table-checksum h=vm01,u=admin,p=123456 h=vm02,u=admin,p=123456 -d hcy -t t1
Cannot connect to MySQL because the Perl DBI module is not installed or not found.
Run 'perl -MDBI' to see the directories that Perl searches for DBI.
If DBI is not installed, try:
Debian/Ubuntu apt-get install libdbi-perl
RHEL/CentOS yum install perl-DBI
OpenSolaris pgk install pkg:/SUNWpmdbi
提示缺少perl-DBI模組,那麼直接 yum install perl-DBI。
[root@vm02 bin]# mk-table-checksum h=vm01,u=admin,p=123456 h=vm02,u=admin,p=123456 -d hcy -t t1
DATABASE TABLE CHUNK HOST ENGINE COUNT CHECKSUM TIME WAIT STAT LAG
hcy t1 0 vm02 InnoDB NULL 1957752020 0 0 NULL NULL
hcy t1 0 vm01 InnoDB NULL 1957752020 0 0 NULL NULL
如果表資料不一致,CHECKSUM的值是不相等的。
解釋下輸出的意思:
DATABASE:資料庫名
TABLE:表名
CHUNK:checksum時的近似數值
HOST:MYSQL的地址
ENGINE:表引擎
COUNT:表的行數
CHECKSUM:校驗值
TIME:所用時間
WAIT:等待時間
STAT:MASTER_POS_WAIT()返回值
LAG:slave的延時時間
如果你想過濾出不相等的都有哪些表,可以用mk-checksum-filter這個工具,只要在後面加個管道符就行了。
[root@vm02 ~]# mk-table-checksum h=vm01,u=admin,p=123456 h=vm02,u=admin,p=123456 -d hcy | mk-checksum-filter
hcy t2 0 vm01 InnoDB NULL 1957752020 0 0 NULL NULL
hcy t2 0 vm02 InnoDB NULL 1068689114 0 0 NULL NULL
知道有哪些表不一致,可以用mk-table-sync這個工具來處理。
注:在執行mk-table-checksum時會鎖表,表的大小取決於執行的快慢。
MASTER上的t2表資料: SLAVE上的t2表資料:
mysql> select * from t2; mysql> select * from t2;
+----+------+ +----+------+
| id | name | | id | name |
+----+------+ +----+------+
| 1 | a | | 1 | a |
| 2 | b | | 2 | b |
| 3 | ss | | 3 | ss |
| 4 | asd | | 4 | asd |
| 5 | ss | +----+------+
+----+------+ 4 rows in set (0.00 sec)
5 rows in set (0.00 sec)
mysql> \! hostname;
mysql> \! hostname; vm02
vm01
[root@vm02 ~]# mk-table-sync --execute --print --no-check-slave --transaction --databases hcy h=vm01,u=admin,p=123456 h=vm02,u=admin,p=123456
INSERT INTO `hcy`.`t2`(`id`, `name`) VALUES ('5', 'ss') /*maatkit src_db:hcy src_tbl:t2 src_dsn:h=vm01,p=...,u=admin dst_db:hcy dst_tbl:t2
dst_dsn:h=vm02,p=...,u=admin lock:0 transaction:1 changing_src:0 replicate:0 bidirectional:0 pid:3246 user:root host:vm02*/;
它的工作原理是:先一行一行檢查主從庫的表是否一樣,如果哪裡不一樣,就執行刪除,更新,插入等操作,使其達到一致。表的大小決定著執行的快慢。
If C<--transaction> is specified, C is not used. Instead, lock
and unlock are implemented by beginning and committing transactions.
The exception is if L<"--lock"> is 3.
If C<--no-transaction> is specified, then C is used for any
value of L<"--lock">. See L<"--[no]transaction">.
When enabled, either explicitly or implicitly, the transaction isolation level
is set C and transactions are started C<with consistent SNAPSHOT>
MySQL複製監控
?原文:
MySQL常見錯誤型別
1005:建立表失敗
1006:建立資料庫失敗
1007:資料庫已存在,建立資料庫失敗
1008:資料庫不存在,刪除資料庫失敗
1009:不能刪除資料庫檔案導致刪除資料庫失敗
1010:不能刪除資料目錄導致刪除資料庫失敗
1011:刪除資料庫檔案失敗
1012:不能讀取系統表中的記錄
1020:記錄已被其他使用者修改
1021:硬碟剩餘空間不足,請加大硬碟可用空間
1022:關鍵字重複,更改記錄失敗
1023:關閉時發生錯誤
1024:讀檔案錯誤
1025:更改名字時發生錯誤
1026:寫檔案錯誤
1032:記錄不存在
1036:資料表是隻讀的,不能對它進行修改
1037:系統記憶體不足,請重啟資料庫或重啟伺服器
1038:用於排序的記憶體不足,請增大排序緩衝區
1040:已到達資料庫的最大連線數,請加大資料庫可用連線數
1041:系統記憶體不足
1042:無效的主機名
1043:無效連線
1044:當前使用者沒有訪問資料庫的許可權
1045:不能連線資料庫,使用者名稱或密碼錯誤
1048:欄位不能為空
1049:資料庫不存在
1050:資料表已存在
1051:資料表不存在
1054:欄位不存在
1065:無效的SQL語句,SQL語句為空
1081:不能建立Socket連線
1114:資料表已滿,不能容納任何記錄
1116:開啟的資料表太多
1129:資料庫出現異常,請重啟資料庫
1130:連線資料庫失敗,沒有連線資料庫的許可權
1133:資料庫使用者不存在
1141:當前使用者無權訪問資料庫
1142:當前使用者無權訪問資料表
1143:當前使用者無權訪問資料表中的欄位
1146:資料表不存在
1147:未定義使用者對資料表的訪問許可權
1149:SQL語句語法錯誤
1158:網路錯誤,出現讀錯誤,請檢查網路連線狀況
1159:網路錯誤,讀超時,請檢查網路連線狀況
1160:網路錯誤,出現寫錯誤,請檢查網路連線狀況
1161:網路錯誤,寫超時,請檢查網路連線狀況
1062:欄位值重複,入庫失敗
1169:欄位值重複,更新記錄失敗
1177:開啟資料表失敗
1180:提交事務失敗
1181:回滾事務失敗
1203:當前使用者和資料庫建立的連線已到達資料庫的最大連線數,請增大可用的資料庫連線數或重啟資料庫
1205:加鎖超時
1211:當前使用者沒有建立使用者的許可權
1216:外來鍵約束檢查失敗,更新子表記錄失敗
1217:外來鍵約束檢查失敗,刪除或修改主表記錄失敗
1226:當前使用者使用的資源已超過所允許的資源,請重啟資料庫或重啟伺服器
1227:許可權不足,您無權進行此操作
1235:MySQL版本過低,不具有本功能
複製監控指令碼
參考原文修改。
?原指令碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
#
#check_mysql_slave_replication_status
#
#
#
parasum=2
help_msg(){
cat << help
+---------------------+
+Error Cause:
+you must input $parasum parameters!
+1st : Host_IP
+2st : Host_Port
help
exit
}
[ $# -ne ${parasum} ] && help_msg #若引數不夠列印幫助資訊並退出
export HOST_IP=$1
export HOST_PORt=$2
MYUSER="root"
MYPASS="123456"
MYSQL_CMD="mysql -u$MYUSER -p$MYPASS"
MailTitle="" #郵件主題
Mail_Address_MysqlStatus="root@localhost.localdomain" #收件人郵箱
time1=$(date +"%Y%m%d%H%M%S")
time2=$(date +"%Y-%m-%d %H:%M:%S")
SlaveStatusFile=/tmp/salve_status_${HOST_PORT}.${time1} #郵件內容所在檔案
echo "--------------------Begin at: "$time2 > $SlaveStatusFile
echo "" >> $SlaveStatusFile
#get slave status
${MYSQL_CMD} -e "show slave status\G" >> $SlaveStatusFile #取得salve程式的狀態
#get io_thread_status,sql_thread_status,last_errno 取得以下狀態值
IOStatus=$(cat $SlaveStatusFile|grep Slave_IO_Running|awk '{print $2}')
SQLStatus=$(cat $SlaveStatusFile|grep Slave_SQL_Running |awk '{print $2}')
Errno=$(cat $SlaveStatusFile|grep Last_Errno | awk '{print $2}')
Behind=$(cat $SlaveStatusFile|grep Seconds_Behind_Master | awk '{print $2}')
echo "" >> $SlaveStatusFile
if [ "$IOStatus" == "No" ] || [ "$SQLStatus" == "No" ];then #判斷錯誤型別
if [ "$Errno" -eq 0 ];then #可能是salve執行緒未啟動
$MYSQL_CMD -e "start slave io_thread;start slave sql_thread;"
echo "Cause slave threads doesnot's running,trying start slsave io_thread;start slave sql_thread;" >> $SlaveStatusFile
MailTitle="[Warning] Slave threads stoped on $HOST_IP $HOST_PORT"
elif [ "$Errno" -eq 1007 ] || [ "$Errno" -eq 1053 ] || [ "$Errno" -eq 1062 ] || [ "$Errno" -eq 1213 ] || [ "$Errno" -eq 1032 ]\
|| [ "Errno" -eq 1158 ] || [ "$Errno" -eq 1159 ] || [ "$Errno" -eq 1008 ];then #忽略此些錯誤
$MYSQL_CMD -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
echo "Cause slave replication catch errors,trying skip counter and restart slave;stop slave ;set global sql_slave_skip_counter=1;slave start;" >> $SlaveStatusFile
MailTitle="[Warning] Slave error on $HOST_IP $HOST_PORT! ErrNum: $Errno"
else
echo "Slave $HOST_IP $HOST_PORT is down!" >> $SlaveStatusFile
MailTitle="[ERROR]Slave replication is down on $HOST_IP $HOST_PORT ! ErrNum:$Errno"
fi
fi
if [ -n "$Behind" ];then
Behind=0
fi
echo "$Behind" >> $SlaveStatusFile
#delay behind master 判斷延時時間
if [ $Behind -gt 300 ];then
echo `date +"%Y-%m%d %H:%M:%S"` "slave is behind master $Bebind seconds!" >> $SlaveStatusFile
MailTitle="[Warning]Slave delay $Behind seconds,from $HOST_IP $HOST_PORT"
fi
if [ -n "$MailTitle" ];then #若出錯或者延時時間大於300s則傳送郵件
cat ${SlaveStatusFile} | /bin/mail -s "$MailTitle" $Mail_Address_MysqlStatus
fi
#del tmpfile:SlaveStatusFile
> $SlaveStatusFile
?修改後指令碼 ?只做了簡單的整理,修正了Behind為NULL的判斷,但均未測試;
?應可考慮增加: ?對修復執行結果的判斷;多條錯誤的迴圈修復、檢測、再修復?
?取消SlaveStatusFile臨時檔案。
?Errno、Behind兩種告警分別發郵件,告警正文增加show slave結果原文。
?增加PATH,以便加到crontab中。
?考慮crontab中週期執行(加鎖避免執行衝突、執行週期選擇)
?增加執行日誌?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/sh
# check_mysql_slave_replication_status
# 參考:
Usage(){
echo Usage:
echo "$0 HOST PORT USER PASS"
}
[ -z "$1" -o -z "$2" -o -z "$3" -o -z "$4" ] && Usage && exit 1
HOST=$1
PORT=$2
USER=$3
PASS=$4
MYSQL_CMD="mysql -h$HOST -P$PORT -u$USER -p$PASS"
MailTitle="" #郵件主題
Mail_Address_MysqlStatus="root@localhost.localdomain" #收件人郵箱
time1=$(date +"%Y%m%d%H%M%S")
time2=$(date +"%Y-%m-%d %H:%M:%S")
SlaveStatusFile=/tmp/salve_status_${HOST_PORT}.${time1} #郵件內容所在檔案
echo "--------------------Begin at: "$time2 > $SlaveStatusFile
echo "" >> $SlaveStatusFile
#get slave status
${MYSQL_CMD} -e "show slave status\G" >> $SlaveStatusFile #取得salve程式的狀態
#get io_thread_status,sql_thread_status,last_errno 取得以下狀態值
IOStatus=$(cat $SlaveStatusFile|grep Slave_IO_Running|awk '{print $2}')
SQLStatus=$(cat $SlaveStatusFile|grep Slave_SQL_Running |awk '{print $2}')
Errno=$(cat $SlaveStatusFile|grep Last_Errno | awk '{print $2}')
Behind=$(cat $SlaveStatusFile|grep Seconds_Behind_Master | awk '{print $2}')
echo "" >> $SlaveStatusFile
if [ "$IOStatus" = "No" -o "$SQLStatus" = "No" ];then
case "$Errno" in
0)
# 可能是slave未啟動
$MYSQL_CMD -e "start slave io_thread;start slave sql_thread;"
echo "Cause slave threads doesnot's running,trying start slsave io_thread;start slave sql_thread;" >> $SlaveStatusFile
;;
1007|1053|1062|1213|1032|1158|1159|1008)
# 忽略這些錯誤
$MYSQL_CMD -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
echo "Cause slave replication catch errors,trying skip counter and restart slave;stop slave ;set global sql_slave_skip_counter=1;slave start;" >> $SlaveStatusFile
MailTitle="[Warning] Slave error on $HOST:$PORT! ErrNum: $Errno"
;;
*)
echo "Slave $HOST:$PORT is down!" >> $SlaveStatusFile
MailTitle="[ERROR]Slave replication is down on $HOST:$PORT! Errno:$Errno"
;;
esac
fi
if [ "$Behind" = "NULL" -o -z "$Behind" ];then
Behind=0
fi
echo "Behind:$Behind" >> $SlaveStatusFile
#delay behind master 判斷延時時間
if [ $Behind -gt 300 ];then
echo `date +"%Y-%m%d %H:%M:%S"` "slave is behind master $Bebind seconds!" >> $SlaveStatusFile
MailTitle="[Warning]Slave delay $Behind seconds,from $HOST $PORT"
fi
if [ -n "$MailTitle" ];then #若出錯或者延時時間大於300s則傳送郵件
cat ${SlaveStatusFile} | /bin/mail -s "$MailTitle" $Mail_Address_MysqlStatus
fi
#del tmpfile:SlaveStatusFile
> $SlaveStatusFile
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2134013/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 線上MYSQL同步報錯故障處理方法總結MySql
- Mysql自動處理同步報錯MySql
- MySQL 常見同步複製故障處理方法MySql
- 線上故障處理手冊
- 如何快速處理線上故障
- 【故障處理】 DBCA建庫報錯CRS-2566
- mysql複製報錯案例處理MySql
- 故障分析 | MySQL 從機故障重啟後主從同步報錯案例分析MySql主從同步
- MySQL show processlist故障處理MySql
- Mysql故障處理2則MySql
- 故障分析 | MySQL convert 函式導致的字符集報錯處理MySql函式
- 記一次dg故障的處理總結
- mysql,出現同步停止的錯誤的處理MySql
- MYSQL 主從庫同步 異常處理彙總MySql
- latch: cache buffers chains故障處理總結(轉載)AI
- Oracle DG同步失敗故障處理(二)Oracle
- 【故障處理】CRS-1153錯誤處理
- 【故障處理】ORA-19809錯誤處理
- 【總結】mysql半同步MySql
- 總結:整理 oracle異常錯誤處理 .Oracle
- 【故障處理】ORA-12162 錯誤的處理
- 處理網路連結故障技巧
- 膝上型電腦開機報錯故障原因及處理方式
- JVM故障處理工具,使用總結JVM
- oracle dataguard資料同步故障處理一例Oracle
- [zt]Logical standby同步故障的處理過程
- Extjs報錯處理JS
- DG報錯的處理
- errpt報錯處理
- ORA-01591錯誤故障處理
- 資料庫故障處理優質文章彙總(含Oracle、MySQL、MogDB等)資料庫OracleMySql
- emmc 報錯總結
- 處理動態連結庫報錯的問題
- 處理mysql複製故障一例薦MySql
- 【故障處理】一次RAC故障處理過程
- TiDB DM同步報錯ErrCode 44006處理一例TiDB
- MongoDB故障處理MongoDB
- shell字串處理總結字串