> 16年关系型数据库管理,从oracle 9i 、10g、11g、12c到Mysql5.5、5.6、5.7、8.0 到TiDB获得3个OCP、2个OCM;运维路上不平坦,跌过不少坑、熬过许多夜。把工作笔记整理出来分享给大伙儿,希望帮到大家少走弯路、少熬夜。
Oracle运维中常出现Tablespace空间满,导致挂库。
通常出现这类事件需要DBA紧急处理。
躺若DB数量上千台,表空间异常多,DBA手工排错耗时长、枯燥、易出错。
若是这类情况正好出现在半夜、周末,DBA怎一个苦字了得!
提问:有没有办法将DBA解放出来,让DB自动诊断,自动扩容表空间?
答: 用这套自动扩容脚本就好(我已多年不Coding,下午写的这套代码比较Low,仅抛砖引玉,各位大神可在此基础上改写以便更好地适应自己的DB环境)
实验环境
1.创建test_tab表,不断插入数据
declare
i int;
begin
for i in 1..5 loop
insert into test.test_tab select * from test.test_tab;
commit;
end loop;
end;
/
2.自动监控程序运行结果
Monitor tablespace and autoextend !
==================================================================================================
作者:John 杨漆
Automatically monitors the tablespace usage
Automatic capacity expansion When the tablespace usage exceeds 85%
For Oracle Database
For study and research only, shall not be used for production environment and commercial purposes
If there is any problem, please contact me on wechat john2000111
Disk usage GB显示 !
==================================================================================================
Monitor tablespace rate Finished !
空间使用情况
TBS_NAME TOTAL_GB USED_GB FREE_GB RATE MAXEXTEND_GB
------------------------------ ---------- ---------- ---------- ------- ------------
TEST 2 1.25 .75 62.50 2
DS_DATA 4.67 2.54 2.13 1.98 128
SYSAUX .55 .52 .03 1.63 32
SYSTEM 1.73 .73 1 1.14 64
USERS .09 0 .09 0.00 32
TEST表空间使用率超过60% (为方便实验环境自动扩展阈值设为60%)
3.查询数据文件情况
select name from v$datafile;
NAME
-------------------------------------------
/u01/app/oracle/oradata/orcl/system01.dbf
/u01/app/oracle/oradata/orcl/sysaux01.dbf
/u01/app/oracle/oradata/orcl/undotbs01.dbf
/u01/app/oracle/oradata/orcl/users01.dbf
/u01/app/oracle/oradata/orcl/DS_DATA01.dbf
/u01/app/oracle/oradata/orcl/DS_DATA02.dbf
/u01/app/oracle/oradata/orcl/SYSTEM1
/u01/app/oracle/oradata/orcl/DS_DATA03.dbf
/u01/app/oracle/oradata/orcl/DS_DATA04.dbf
/u01/app/oracle/oradata/orcl/test01.dbf
/u01/app/oracle/oradata/orcl/TEST02.dbf
4.调用自动诊断、扩容程序(实验手动调用,正式使用时放在Job里自动调用)
SQL> exec proc_monitor_tbs_rate;
thanks for you to use Tablespace Automatic extension program !
The author: John 杨漆
TEST add TEST03.dbf
Tablespace Automatic extension succeeded
5.再次查询空间使用情况
TBS_NAME TOTAL_GB USED_GB FREE_GB RATE MAXEXTEND_GB
------------------------------ ---------- ---------- ---------- ------- ------------
TEST 3 1.25 1.75 41.67 3
DS_DATA 4.67 2.54 2.13 1.98 128
SYSAUX .55 .52 .03 1.63 32
SYSTEM 1.73 .73 1 1.14 64
USERS .09 0 .09 0.00 32
使用率已从62.5% 降到41.67%
6.再次查询数据文件情况
select name from v$datafile;
NAME
-------------------------------------------
/u01/app/oracle/oradata/orcl/system01.dbf
/u01/app/oracle/oradata/orcl/sysaux01.dbf
/u01/app/oracle/oradata/orcl/undotbs01.dbf
/u01/app/oracle/oradata/orcl/users01.dbf
/u01/app/oracle/oradata/orcl/DS_DATA01.dbf
/u01/app/oracle/oradata/orcl/DS_DATA02.dbf
/u01/app/oracle/oradata/orcl/SYSTEM1
/u01/app/oracle/oradata/orcl/DS_DATA03.dbf
/u01/app/oracle/oradata/orcl/DS_DATA04.dbf
/u01/app/oracle/oradata/orcl/test01.dbf
/u01/app/oracle/oradata/orcl/TEST02.dbf
/u01/app/oracle/oradata/orcl/TEST03.dbf
增加了一个 TEST03,自动扩容成功!
-- Oracle表空间自动监控 自动扩容程序 修订版
## 放在OS定时任务里,每30分钟自动运行一次,监控DB表空间使用状况
vi monitor_tablespace_autoextend.sh
#!/bin/bash
echo " Monitor tablespace and autoextend !"
echo "=================================================================================================="
echo " 作者:John 杨漆"
echo " Automatically monitors the tablespace usage "
echo " Automatic capacity expansion When the tablespace usage exceeds 85% "
echo " For Oracle Database"
echo " For study and research only, shall not be used for production environment and commercial purposes"
echo " If there is any problem, please contact me on wechat john2000111"
echo " Disk usage GB显示 !"
echo "=================================================================================================="
source ~/.bash_profile
sqlplus -S "/ as sysdba"