共計 5497 個字符,預(yù)計需要花費 14 分鐘才能閱讀完成。
如何為 Zabbix MySQL 設(shè)置獨立表空間 innodb_file_per_table,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面丸趣 TV 小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
一臺 zabbix 監(jiān)控系統(tǒng)后臺使用的 MySQL DB 宕掉,連上 MySQL DB server 看到硬盤快用滿了,發(fā)現(xiàn) zabbix 使用到的 MySQL ibdata1 文件有 300 多 G,幾乎占據(jù)了整個硬盤的空間
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 99G 15G 79G 17% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 8.4M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 488M 105M 348M 24% /boot
/dev/sda2 378G 355G 4.1G 99% /data
tmpfs 798M 0 798M 0% /run/user/0
# ll
total 371225844
-rw-r----- 1 mysql mysql 16384 Apr 17 21:42 aria_log.00000001
-rw-r----- 1 mysql mysql 52 Apr 17 21:42 aria_log_control
-rw-rw---- 1 mysql mysql 1224704 Apr 22 22:38 ddl_log.log
-rw-r----- 1 mysql mysql 380123480064 Apr 23 13:20 ibdata1
-rw-r----- 1 mysql mysql 5242880 Apr 23 13:20 ib_logfile0
-rw-r----- 1 mysql mysql 5242880 Apr 23 13:20 ib_logfile1
drwx------ 2 mysql mysql 4096 Apr 17 21:42 mysql
drwx------ 2 mysql mysql 4096 Apr 17 21:42 performance_schema
drwx------ 2 mysql mysql 4096 Apr 22 22:38 zabbix
一看 db 版本,還是使用的 MariaDB 5.5.56
# mysql -V
mysql Ver 15.1 Distrib 5.5.56-MariaDB, for Linux (x86_64) using readline 5.1
這個版本的 DB 會默認(rèn)使用共用表空間,估計沒有設(shè)置獨立表空間,查看果然:
MariaDB [(none)] show variables like innodb_file_per_table
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_file_per_table | OFF |
+-----------------------+-------+
1 row in set (0.00 sec)
這里使用了共用表空間,即使對 zabbix 大表歷史數(shù)據(jù)清理并執(zhí)行 alter table xxx engine=innodb; 也無法收縮空間。
臨時改善對策:
因為磁盤已滿,為了讓 zabbix 監(jiān)控盡快恢復(fù)使用,請用戶對 server 臨時增加了一塊 500G 硬盤。把 DB 數(shù)據(jù) copy 到空間更大的新盤,并重新掛載原來盤為 /data1, 掛載新盤為原 /data
開啟 DB 服務(wù)后,zabbix 監(jiān)控恢復(fù)正常
永久改善對策:
Zabbix MySQL DB 使用磁盤過大,雖然已經(jīng)設(shè)置了清理歷史分區(qū)數(shù)據(jù)任務(wù),但監(jiān)控數(shù)據(jù)過多導(dǎo)致現(xiàn)有保持策略還是會用滿硬盤。上面說到共用表空間使用的 ibdata1 文件無法回收,只能想辦法刪除一部分歷史數(shù)據(jù)且改用獨立表空間。
思想:將 DB 數(shù)據(jù)導(dǎo)出備份 (大表只備份近期歷史數(shù)據(jù)),刪除原有共用表空間 ibdata1 文件,修改獨立表空間配置,再導(dǎo)入備份數(shù)據(jù),修改清理歷史分區(qū)數(shù)據(jù)策略。
查 zabbix DB 中各表使用大小:
select TABLE_NAME,(sum(DATA_LENGTH)+sum(INDEX_LENGTH))/1024/1024 from information_schema.tables
where table_schema= zabbix
GROUP BY TABLE_NAME
ORDER BY 2 DESC
TABLE_NAME (sum(DATA_LENGTH)+sum(INDEX_LENGTH))/1024/1024
history_uint 104518.12500000
history 24653.62500000
trends_uint 5394.67187500
events 2808.06250000
event_recovery 1188.37500000
trends 1111.68750000
history_str 200.14062500
1. 停止 zabbix 服務(wù)
# systemctl stop zabbix-server
2. 導(dǎo)出 zabbix DB 除兩個最大歷史表之外基本表結(jié)構(gòu)和數(shù)據(jù)
# mysqldump -h227.0.0.1 -uroot -p password –default-character-set=utf8 –databases zabbix -R –ignore-table=zabbix.history –ignore-table=zabbix.history_uint –log-error=zabbix_base.log zabbix_base.sql
主要參數(shù)說明:
-R 導(dǎo)出 procedure 和 function
–ignore-table 指定不想導(dǎo)出的表名,如果有多個表不想導(dǎo)出就寫多個 –ignore-table
3. 導(dǎo)出 zabbix DB history 和 history_uint 近 7 天表數(shù)據(jù)和結(jié)構(gòu)
因為 zabbix table 中保存的是時間戳,查出時間對應(yīng)的時間戳
MariaDB [(none)] select unix_timestamp( 2020-4-16
+-----------------------------+
| unix_timestamp(2020-4-16) |
+-----------------------------+
| 1586966400 |
+-----------------------------+
1 row in set (0.00 sec)
導(dǎo)出 history 近 7 天表數(shù)據(jù)和結(jié)構(gòu)
# mysqldump -h227.0.0.1 -uroot -p password –default-character-set=utf8 –databases zabbix –tables history –where= clock 1586966400 –log-error=history.log history.sql
導(dǎo)出 history_uint 近 7 天表數(shù)據(jù)和結(jié)構(gòu)
# mysqldump -h227.0.0.1 -uroot -p password –default-character-set=utf8 –databases zabbix –tables history_uint –where= clock 1586966400 –log-error=history_uint.log history_uint.sql
4. 關(guān)閉 DB 服務(wù)
# systemctl stop mariadb.service
5. 修改 innodb_file_per_table 獨立表空間參數(shù)
# vi /etc/my.cnf
innodb_file_per_table=1
6. 刪除 ibdata1 和日志文件 (注:操作之前盡量做好備份)
# rm -rf ibdata1
# rm -rf ib_logfile0
# rm -rf ib_logfile1
注,刪除 ibdata1 主要為了釋放空間,重啟 DB 服務(wù)后會自動重建一個空的。刪除日志文件是為了避免下面 error 出現(xiàn):
[Note] InnoDB: The first innodb_system data file ibdata1 did not exist. A new tablespace will be created!
[ERROR] InnoDB: redo log file ./ib_logfile0 exists. Creating system tablespace with existing redo log files is not recommended. Please delete all redo log files before creating new system tablespace.
7. 開啟 DB 服務(wù)
# systemctl start mariadb.service
注,開啟 DB 后,ibdata1 雖然被刪除了,但 zabbix DB 依然存在,只是 DB 下面 table 無法訪問了
8. 導(dǎo)入上面?zhèn)浞莩鰜淼臄?shù)據(jù)
# mysql -uroot -ppassword -h227.0.0.1 zabbix zabbix_base.sql
# mysql -uroot -ppassword -h227.0.0.1 zabbix history.sql
# mysql -uroot -ppassword -h227.0.0.1 zabbix history_uint.sql
至此,共用表空間改為獨立表空間完成,且 /data 硬盤使用空間大幅收縮 (/data1 為臨時對策時加的盤,為遷移前 DB 文件大小)
[root@vswhzb01 mysql]# du -sh *
16K aria_log.00000001
4.0K aria_log_control
128M ibdata1
64M ib_logfile0
5.0M ib_logfile0_old
64M ib_logfile1
5.0M ib_logfile1_old
1016K mysql
212K performance_schema
41G zabbix
[root@vswhzb01 mysql]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 99G 16G 79G 17% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 8.4M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 488M 105M 348M 24% /boot
tmpfs 798M 0 798M 0% /run/user/0
/dev/sda2 378G 55G 304G 16% /data
/dev/sdb1 493G 355G 113G 76% /data1
9. 開啟 zabbix 服務(wù)
# systemctl start mariadb.service
10. 最后,記得調(diào)整歷史分區(qū)刪除策略,不然監(jiān)控數(shù)據(jù)多了硬盤還是會用完
注:zabbix 歷史分區(qū)刪除設(shè)定可參考之前文章
http://blog.itpub.net/25583515/viewspace-2638892/
DROP PROCEDURE IF EXISTS zabbix.partition_maintenance_all;
DELIMITER $$
CREATE PROCEDURE `partition_maintenance_all`(SCHEMA_NAME VARCHAR(32))
BEGIN
CALL partition_maintenance(SCHEMA_NAME, history , 30, 24, 7);
CALL partition_maintenance(SCHEMA_NAME, history_log , 30, 24, 7);
CALL partition_maintenance(SCHEMA_NAME, history_str , 30, 24, 7);
CALL partition_maintenance(SCHEMA_NAME, history_text , 30, 24, 7);
CALL partition_maintenance(SCHEMA_NAME, history_uint , 15, 24, 7);
CALL partition_maintenance(SCHEMA_NAME, trends , 180, 24, 7);
CALL partition_maintenance(SCHEMA_NAME, trends_uint , 180, 24, 7);
END$$
DELIMITER ;
后續(xù),因為已經(jīng)使用了獨立表空間 innodb_file_per_table 設(shè)定,即使硬盤再次被 DB 用滿,使用 drop partition 等方式可以釋放 OS 磁盤空間
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注丸趣 TV 行業(yè)資訊頻道,感謝您對丸趣 TV 的支持。