一個MySQL的備份突然變小了很多,但實際的數據量卻一直在增長。備份腳本也沒有調整過。為什么呢?
重現了一下備份過程,發現備份中遇到了如下錯誤:
mysqldump: Got error: 1356: View 'wordpress.v_t1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them when using LOCK TABLES
原因: 視圖引用的表不存在。
原理:mysqldump在備份時要對表加讀鎖,加鎖失敗的時候。備份就終止了。
解決方法:在備份時加上--force參數
模擬測試如下:
正常情況:
1.創建表t1並插入數據
CREATE TABLE `t1` (
`a` int(11) NOT NULL,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
PRIMARY KEY (`a`),
UNIQUE KEY `b` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into t1 values(1,3,5);
2.基於表t1創建視圖
create view v_t1 as select * from t1;
3.模擬備份,可以正常備份出數據
mysqldump -ubkpuser -ps3cret wordpress>wordpress.sql
模擬視圖引用的表被刪除:
1.刪除表t1
mysql> drop table t1;
Query OK, 0 rows affected
mysql> select * from v_t1;
1356 - View 'wordpress.v_t1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
2. 模擬備份,失敗
$mysqldump -ubkpuser -ps3cret wordpress >wordpress1.sql
Warning: Using a password on the command line interface can be insecure.
mysqldump: Got error: 1356: View 'wordpress.v_t1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them when using LOCK TABLES
3. 增加force參數后,雖然有錯誤,但不影響備份
$mysqldump -ubkpuser -ps3cret --force wordpress >wordpress1.sql
Warning: Using a password on the command line interface can be insecure.
mysqldump: Got error: 1356: View 'wordpress.v_t1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them when using LOCK TABLES
mysqldump: Couldn't execute 'SHOW FIELDS FROM `v_t1`': View 'wordpress.v_t1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them (1356)
[clouder@server2-86 ~]$ /home/clouder/vs/program/mysql/bin/mysqldump -uroot -p11 -S /home/clouder/vs/program/mysql/run/mysql.sock -h 172.16.2.86 --all-databases --master-data=1 --flush-logs --force > /home/clouder/backup/alldatabase`date +%F-%H-%M-%S`.sql
mysqldump: Couldn't execute 'SHOW FIELDS FROM `ApiCommandAliasView`': View 'api.ApiCommandAliasView' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them (1356)
mysqldump: Couldn't execute 'SHOW FIELDS FROM `ApiReset`': View 'api.ApiReset' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them (1356)