Part1:寫在最前
在MysQL5.6版本中,可以把undo log 回滾日志分離到一個單獨的表空間里;其缺點是不能回收空間大小,until MysQL5.7,but MariadDB10.1暫不支持。
本文介紹並演示MysQL5.7是如何在線收縮undo log的。
undo log日志是保存在共享表空間ibdata1文件中的,隨着數據庫的運行時間的不斷增長,ibdata1文件會越來越大,在以往的MySQL數據庫版本中,如果我們想要回收ibdata1文件所占空間,會非常的復雜和困難,必須先將mysqldump -A全庫導出,然后刪掉data目錄,再重新對數據庫進行初始化,最后導入全庫備份,方可實現ibdata1的回收。
MySQL全庫備份方式可參考:
http://suifu.blog.51cto.com/9167728/1758022
MySQL5.7的安裝方式可參考:
http://suifu.blog.51cto.com/9167728/1855415
①首先我們需要調整my.cnf參數
innodb_undo_directory=
/data/undolog
innodb_undo_tablespaces=4
innodb_undo_logs=128
innodb_max_undo_log_size=1G
innodb_purge_rseg_truncate_frequency
innodb_undo_log_truncate=1
Warning:警告
其中innodb_undo_directory參數要在數據庫初始化時就需要寫入my.cnf,否則會報如下錯誤:
Expected to open 4 undo tablespaces but was able to find only 0 undo tablespaces. Set the innodb_undo_tablespaces parameter to the correct value and retry. Suggested value is 0
Part2:MySQL5.7在線回收undolog
①首先對一張100萬行的表進行整表更新
mysql> show variables
like
'innodb_undo%'
;
+
--------------------------+---------------+
| Variable_name | Value |
+
--------------------------+---------------+
| innodb_undo_directory | /data/undolog |
| innodb_undo_log_truncate |
ON
|
| innodb_undo_logs | 128 |
| innodb_undo_tablespaces | 4 |
+
--------------------------+---------------+
4
rows
in
set
(0.00 sec)
mysql>
update
helei
set
c1=1;
Query OK, 1000000
rows
affected (45.48 sec)
Rows
matched: 1000000 Changed: 1000000 Warnings: 0
mysql>
update
helei
set
c2=222;
Query OK, 1000000
rows
affected (43.25 sec)
Rows
matched: 1000000 Changed: 1000000 Warnings: 0
mysql>
update
helei
set
c4=
'heleiheleiheleiheleihel'
;
Query OK, 1000000
rows
affected (10.28 sec)
Rows
matched: 1000000 Changed: 1000000 Warnings: 0
②注意undolog的日志大小
[root@HE3 undolog]# ls -lshrt
total 412M
128M -rw-r----- 1 mysql mysql 128M Sep 26 16:56 undo004
76M -rw-r----- 1 mysql mysql 76M Sep 26 16:56 undo003
136M -rw-r----- 1 mysql mysql 136M Sep 26 16:56 undo001
72M -rw-r----- 1 mysql mysql 72M Sep 26 16:56 undo002
③error.log日志
2016-09-26T23:51:06.062828Z 0 [Note] InnoDB: Truncating UNDO tablespace with space identifier 1
2016-09-26T23:51:06.159077Z 0 [Note] InnoDB: Completed truncate of UNDO tablespace with space identifier 1
2016-09-26T23:51:06.159101Z 0 [Note] InnoDB: Truncating UNDO tablespace with space identifier 2
2016-09-26T23:51:06.242355Z 0 [Note] InnoDB: Completed truncate of UNDO tablespace with space identifier 2
2016-09-26T23:51:06.242378Z 0 [Note] InnoDB: Truncating UNDO tablespace with space identifier 3
2016-09-26T23:51:06.313036Z 0 [Note] InnoDB: Completed truncate of UNDO tablespace with space identifier 3
2016-09-26T23:51:06.313060Z 0 [Note] InnoDB: Truncating UNDO tablespace with space identifier 4
2016-09-26T23:51:06.403003Z 0 [Note] InnoDB: Completed truncate of UNDO tablespace with space identifier 4
④觀察物理文件
[root@HE3 undolog]# ls -lshrt
total 168M
76M -rw-r----- 1 mysql mysql 76M Sep 26 16:56 undo003
10M -rw-r----- 1 mysql mysql 10M Sep 26 16:56 undo001
10M -rw-r----- 1 mysql mysql 10M Sep 26 16:56 undo004
72M -rw-r----- 1 mysql mysql 72M Sep 26 16:56 undo002
可以看到超過100M設定值的undo日志已經回收,默認為10M,undo log空間得以釋放
Part3:意義
該功能降低了磁盤的使用率,並且可以提高諸如Xtrabackup這類物理備份軟件的備份速度。