1、環境介紹
服務器硬件:Dell R710
服務器OS:紅帽子Linux RHEL4.8
數據庫:Oracle 10g
2、出現的問題
因為數據表每天有上百萬的數據寫入表,加上建立索引,導致表空間不停增長,表空間被設置為自動增長,因此dbf文件在不斷增大,硬盤空間在每天約400M的速度減少。數據庫雖有自清理的腳本,清理3個月前的數據,但實際增加的數據太多,清理釋放的空間不能滿足需求了。
3、處理過程
解決思路是,該服務器的硬盤有預留空間,未完全划分的約有100G,可以建立新的分區,將已經存滿的分區的數據庫文件移動,來避免掛載點使用率達到100%,同時可以擴充表空間。
3.1 建立分區
以root賬號登陸,查看目前使用情況:
[root@ccsvr ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.1G 3.2G 3.6G 48% /
/dev/sda3 44G 9.6G 32G 24% /AFC/Data
/dev/sda5 15G 3.9G 9.9G 29% /AFC/Log
/dev/sda2 63G 54G 5.8G 91% /AFC_DB
/dev/sdb1 56G 39G 15G 73% /AFC_DB2
none 4.0G 0 4.0G 0% /dev/shm
/dev/sda6 4.9G 3.3G 1.4G 72% /oracle
/dev/sdb2 56G 52G 529M 100% /AFC_DB3
其中 /dev/sdb2已經快接近用完了。
查看下分區情況:
[root@ccsvr ~]# fdisk -l
Disk /dev/sda: 146.1 GB, 146163105792 bytes
255 heads, 63 sectors/track, 17769 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 936 7518388+ 83 Linux
/dev/sda2 937 9222 66557295 83 Linux
/dev/sda3 9223 14959 46082452+ 83 Linux
/dev/sda4 14960 17769 22571325 5 Extended
/dev/sda5 14960 16871 15358108+ 83 Linux
/dev/sda6 16872 17508 5116671 83 Linux
/dev/sda7 17509 17769 2096451 82 Linux swap
Disk /dev/sdb: 299.4 GB, 299439751168 bytes
255 heads, 63 sectors/track, 36404 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 7296 58605088+ 83 Linux
/dev/sdb2 7297 14592 58605120 83 Linux
/dev/sdb3 14593 32829 146488702+ 5 Extended
/dev/sdb5 14593 21888 58605088+ 83 Linux
可以看到有兩塊硬盤,sda為第一塊SCSI硬盤,sdb為第二塊SCSI硬盤,其實服務器時四塊硬盤,做的raid1。
其中,第一塊硬盤,dell的標配146G,安裝的操作系統,oracle數據庫,已經分完了,三個主分區,三個擴展分區,/dev/sda2 (/AFC_DB)是開始建立的數據庫文件存放目錄。
第二塊硬盤,是后來增加的300G,/dev/sdb1(/AFC_DB2),/dev/sdb2(/AFC_DB3)是兩個主分區,作用也是來放數據庫文件的,后面還有一個擴展分區/dev/sdb5,這個應該
也是后來加這塊硬盤的人創建的,但是沒有掛載上。
我的工作就是把/dev/sdb5掛載上,把硬盤還有的空間(32829~21888)的柱面建立成第二個擴展分區,/dev/sdb6。
[root@ccsvr ~]# fdisk /dev/sdb
Command (m for help): p
Disk /dev/sdb: 299.4 GB, 299439751168 bytes
255 heads, 63 sectors/track, 36404 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 7296 58605088+ 83 Linux
/dev/sdb2 7297 14592 58605120 83 Linux
/dev/sdb3 14593 32829 146488702+ 5 Extended
/dev/sdb5 14593 21888 58605088+ 83 Linux
/dev/sdb6 21889 32829 87883551 83 Linux
注:對第二塊硬盤進行分區操作,p:查看分區情況;
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (21889-32829, default 21889):
Using default value 21889
Last cylinder or +size or +sizeM or +sizeK (21889-32829, default 32829):
Using default value 32829
注:n:創建新的分區,
接下來選擇分區類型:l:邏輯分區,p主分區
l logical (5 or over),指創建邏輯分區,分區編號要大於5,因為已經存在db5了
p primary partition (1-4),指創建主分區,編號1—4,linux規定主分區只能有四個。
我這里輸入的l,創建邏輯分區。
First cylinder (21889-32829, default 21889):
Using default value 21889
注:這個就是填寫分區的start柱面,這里直接打回車,使用默認值;
Last cylinder or +size or +sizeM or +sizeK (21889-32829, default 32829):
Using default value 32829
注:這里填寫分區的end柱面,或者填寫+??M,K的方式,linux會自動算出柱面號。我這里要把剩余的硬盤空間全都分到這個分區,所以使用默認值。
Command (m for help): p
Disk /dev/sdb: 299.4 GB, 299439751168 bytes
255 heads, 63 sectors/track, 36404 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 7296 58605088+ 83 Linux
/dev/sdb2 7297 14592 58605120 83 Linux
/dev/sdb3 14593 32829 146488702+ 5 Extended
/dev/sdb5 14593 21888 58605088+ 83 Linux
/dev/sdb6 21889 32829 87883551 83 Linux
再次查看下分區情況,發現/dev/sdb6/已經創建好了。
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
執行W命令,寫入分區表。
注意:以上操作中如果有誤,可以隨時撤銷,但執行寫入后,就不能更改了。
發現,寫入后系統提示錯誤,資源正忙,新的分區表重啟后有效。
於是,重啟機器,注意重啟前,關閉生產程序進程,和oracle數據:
$xxx/bin/xxx_stop
[root@ccsvr ~]# su - oracle
[oracle@ccsvr ~]$ sqlplus / as sysdba;
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL>
重啟后,對/dev/sdb5,/dev/db6格式化:
[root@ccsvr /]# mkfs -t ext3 /dev/sdb6
mke2fs 1.35 (28-Feb-2004)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
10993664 inodes, 21970887 blocks
1098544 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=25165824
671 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
然后,創建需要掛載的目錄,將分區掛載:
[root@ccsvr /]# mkdir AFC_DB5
[root@ccsvr /]# mkdir AFC_DB6
[root@ccsvr /]# ll
total 216
drwxr-xr-x 6 afc aas 4096 Dec 2 2013 AAS
drwxrwxr-x 13 afc aas 4096 Dec 27 00:32 AFC
drwxr-xr-x 5 oracle oinstall 4096 Apr 14 2014 AFC_DB
drwxr-xr-x 6 oracle oinstall 4096 Apr 14 2014 AFC_DB2
drwxr-xr-x 4 oracle oinstall 4096 Apr 14 2014 AFC_DB3
drwxr-xr-x 2 root root 4096 May 17 21:18 AFC_DB5
drwxr-xr-x 2 root root 4096 May 17 21:18 AFC_DB6
這里有一步失誤,目錄AFC_DB5和AFC_DB6是用來存放oracle數據文件的,最好用oracle賬號創建,
我已經用root賬號創建了,就給它最高權限算了。
[root@ccsvr /]# chmod 777 AFC_DB5
[root@ccsvr /]# chmod 777 AFC_DB6
然后,進行掛載:
[root@ccsvr /]# mount /dev/sdb5 /AFC_DB5
[root@ccsvr /]# mount /dev/sdb6 /AFC_DB6
修改fstab文件,使開機啟動時自動掛載,
這是原fstab文件:
[root@ccsvr /]# more /etc/fstab
# This file is edited by fstab-sync - see 'man fstab-sync' for details
LABEL=/ / ext3 defaults 1 1
LABEL=/AFC/Data /AFC/Data ext3 defaults 1 2
LABEL=/AFC/Log /AFC/Log ext3 defaults 1 2
LABEL=/AFC_DB /AFC_DB ext3 defaults 1 2
/dev/sdb1 /AFC_DB2 ext3 defaults 1 2
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
LABEL=/oracle /oracle ext3 defaults 1 2
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
LABEL=SWAP-sda7 swap swap defaults 0 0
/dev/sdb2 /AFC_DB3 ext3 defaults 1 2
/dev/scd0 /media/cdrom auto pamconsole,fscontext=system_u:object_r:removable_t,exec,noauto,managed 0 0
使用vi編輯器添加兩條:
[root@ccsvr etc]# vi fstab
[root@ccsvr etc]# more fstab
# This file is edited by fstab-sync - see 'man fstab-sync' for details
LABEL=/ / ext3 defaults 1 1
LABEL=/AFC/Data /AFC/Data ext3 defaults 1 2
LABEL=/AFC/Log /AFC/Log ext3 defaults 1 2
LABEL=/AFC_DB /AFC_DB ext3 defaults 1 2
/dev/sdb1 /AFC_DB2 ext3 defaults 1 2
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
LABEL=/oracle /oracle ext3 defaults 1 2
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
LABEL=SWAP-sda7 swap swap defaults 0 0
/dev/sdb2 /AFC_DB3 ext3 defaults 1 2
/dev/sdb5 /AFC_DB5 ext3 defaults 1 2
/dev/sdb6 /AFC_DB6 ext3 defaults 1 2
/dev/scd0 /media/cdrom auto pamconsole,fscontext=system_u:object_r:removable_t,exec,noauto,managed 0 0
這里需要對fstab文件說明:
fstab(/etc/fstab)是Linux下比較重要的配置文件,它包含了系統在啟動時掛載文件系統和存儲設備的詳細信息。
這個文件有六列
第1列是需要掛載的文件系統或存儲設備;
第2列是掛載點;
第3列指定文件系統或分區的類型;
第4列為掛載選項,詳細參考man mount. 下面列出一些常用的選項:
auto: 系統自動掛載,fstab默認就是這個選項
ro: read-only
rw: read-write
defaults: rw, suid, dev, exec, auto, nouser, and async.
第5列為dump選項,設置是否讓備份程序dump備份文件系統,0為忽略,1為備份。
第6列為fsck選項,告訴fsck程序以什么順序檢查文件系統,0為忽略,其它數字順序。
掛載后,df命令就可以看到文件系統了:
[root@ccsvr etc]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.1G 3.2G 3.6G 48% /
/dev/sda3 44G 11G 31G 26% /AFC/Data
/dev/sda5 15G 5.8G 8.0G 42% /AFC/Log
/dev/sda2 63G 54G 5.8G 91% /AFC_DB
/dev/sdb1 56G 39G 15G 73% /AFC_DB2
none 4.0G 0 4.0G 0% /dev/shm
/dev/sda6 4.9G 3.3G 1.4G 72% /oracle
/dev/sdb2 56G 52G 400M 100% /AFC_DB3
/dev/sdb5 56G 85M 53G 1% /AFC_DB5
/dev/sdb6 83G 89M 79G 1% /AFC_DB6
然后,處理Oracle的問題:
以system賬戶登錄數據庫,這里我用的pl-sql工具,查詢表空間使用量:
查詢時間 |
表空間名稱 |
表空間所屬用戶 |
總空間(單位:MB) |
使用表空間(單位:MB) |
未使用空間(單位:MB) |
使用表空間占百分比 |
2015-05-17 22:32:07 |
DATA_OPERATION |
AFCDB |
10920 |
9530 |
1390 |
87.27% |
2015-05-17 22:32:07 |
DATA_PARAMETER |
AFCDB |
512 |
223 |
289 |
43.55% |
2015-05-17 22:32:07 |
DATA_STATIS |
AFCDB |
512 |
437 |
75 |
85.35% |
2015-05-17 22:32:07 |
DATA_UD_SALE |
AFCDB |
60222 |
60178 |
44 |
99.93% |
2015-05-17 22:32:07 |
IDX_OPERATION |
AFCDB |
9064 |
9062 |
2 |
99.98% |
2015-05-17 22:32:07 |
IDX_PARAMETER |
AFCDB |
256 |
104 |
152 |
40.63% |
2015-05-17 22:32:07 |
IDX_SPACE |
AFCDB |
256 |
61 |
195 |
23.83% |
2015-05-17 22:32:07 |
IDX_UD_SALE |
AFCDB |
49958 |
49924 |
34 |
99.93% |
可以看到三個表空間使用都達到99%以上了。
使用sql語句查詢表空間文件路徑:
1 select * from dba_data_files t where t.TABLESPACE_NAME = 'IDX_UD_SALE' 2 select * from dba_data_files t where t.TABLESPACE_NAME = 'IDX_OPERATION' 3 select * from dba_data_files t where t.TABLESPACE_NAME = 'DATA_UD_SALE'
執行結果為:
FILE_NAME |
FILE_ID |
TABLESPACE_NAME |
BYTES |
BLOCKS |
STATUS |
RELATIVE_FNO |
AUTOEXTENSIBLE |
MAXBYTES |
MAXBLOCKS |
INCREMENT_BY |
USER_BYTES |
USER_BLOCKS |
ONLINE_STATUS |
/AFC_DB/oradata/idx_operation01.dbf |
19 |
IDX_OPERATION |
4160749568 |
507904 |
AVAILABLE |
19 |
YES |
4292870144 |
524032 |
8192 |
4159700992 |
507776 |
ONLINE |
/AFC_DB2/oradata/idx_operation02.dbf |
30 |
IDX_OPERATION |
2080374784 |
253952 |
AVAILABLE |
30 |
YES |
3219128320 |
392960 |
8192 |
2079326208 |
253824 |
ONLINE |
/AFC_DB2/oradata/idx_operation03.dbf |
33 |
IDX_OPERATION |
557842432 |
68096 |
AVAILABLE |
33 |
YES |
3221225472 |
393216 |
1024 |
556793856 |
67968 |
ONLINE |
/AFC_DB2/oradata/idx_operation04.dbf |
34 |
IDX_OPERATION |
557842432 |
68096 |
AVAILABLE |
34 |
YES |
3221225472 |
393216 |
1024 |
556793856 |
67968 |
ONLINE |
/AFC_DB3/oradata/idx_operation05.dbf |
53 |
IDX_OPERATION |
2147483648 |
262144 |
AVAILABLE |
53 |
NO |
0 |
0 |
0 |
2146435072 |
262016 |
ONLINE |
(還有兩個表,就不貼出來了)
可以看到,新的數據文件都在/AFC_DB3/oradata目錄下,現在需要將這三個數據dbf文件移動,來釋放/AFC_DB3的空間。
這里我們使用較大的一個分區,/dev/sdb6來存放新的oracle數據庫文件,/dev/sdb5暫時閑置,留作以后再用。
[root@ccsvr /]# su - oracle
登陸oracle賬號。
[oracle@ccsvr ~]$ cd /AFC_DB6
[oracle@ccsvr AFC_DB6]$ mkdir oradata
[oracle@ccsvr AFC_DB6]$ ll
total 24
drwx------ 2 root root 16384 May 17 21:24 lost+found
drwxr-xr-x 2 oracle oinstall 4096 May 17 22:04 oradata
創建oradata目錄。
[oracle@ccsvr ~]$ sqlplus / as sysdba;
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
先停止數據庫。
然后,將已經快100%used的目錄/AFC_DB3中的三個表空間文件移走,要使用oracle賬戶移動文件:
[oracle@ccsvr AFC_DB6]$ mv /AFC_DB3/oradata/idx_ud_sale13.dbf /AFC_DB6/oradata/idx_ud_sale13.dbf
[oracle@ccsvr AFC_DB6]$ mv /AFC_DB3/oradata/dx_operation05.dbf /AFC_DB6/oradata/idx_operation05.dbf
[oracle@ccsvr AFC_DB6]$ mv /AFC_DB3/oradata/data_ud_sale23.dbf /AFC_DB6/oradata/data_ud_sale23.dbf
[oracle@ccsvr AFC_DB6]$ cd oradata/
[oracle@ccsvr oradata]$ ls -lh
total 14G
-rw-r----- 1 oracle oinstall 11G May 17 22:00 data_ud_sale23.dbf
-rw-r----- 1 oracle oinstall 2.1G May 17 22:00 idx_operation05.dbf
-rw-r----- 1 oracle oinstall 1.1G May 17 22:00 idx_ud_sale13.dbf
啟動數據庫掛載:
[oracle@ccsvr oradata]$ sqlplus / as sysdba;
SQL> startup mount;
ORACLE instance started.
Total System Global Area 2147483648 bytes
Fixed Size 1220432 bytes
Variable Size 218103984 bytes
Database Buffers 1912602624 bytes
Redo Buffers 15556608 bytes
Database mounted.
使用rename命名使移動后的數據文件生效:
SQL> alter database rename file '/AFC_DB3/oradata/idx_ud_sale13.dbf' to '/AFC_DB6/oradata/idx_ud_sale13.dbf';
Database altered.
SQL> alter database rename file '/AFC_DB3/oradata/dx_operation05.dbf' to '/AFC_DB6/oradata/idx_operation05.dbf';
Database altered.
SQL> alter database rename file '/AFC_DB3/oradata/data_ud_sale23.dbf' to '/AFC_DB6/oradata/data_ud_sale23.dbf';
Database altered.
打開數據庫:
SQL> alter database open;
Database altered.
最后,根據需要,增加三個數據庫文件,擴大表空間:
SQL> alter tablespace IDX_UD_SALE add datafile '/AFC_DB6/oradata/idx_ud_sale14.dbf' size 4096M;
Tablespace altered.
SQL> alter tablespace IDX_OPERATION add datafile '/AFC_DB6/oradata/idx_operation06.dbf' size 2048M;
Tablespace altered.
SQL> alter tablespace DATA_UD_SALE add datafile '/AFC_DB6/oradata/data_ud_sale24.dbf' size 8192M;
Tablespace altered.
檢查下AFC_DB6的數據文件:
[oracle@ccsvr oradata]$ pwd
/AFC_DB6/oradata
[oracle@ccsvr oradata]$ ls -alh
total 28G
drwxr-xr-x 2 oracle oinstall 4.0K May 17 22:48 .
drwxrwxrwx 4 root root 4.0K May 17 22:04 ..
-rw-r----- 1 oracle oinstall 11G May 17 22:30 data_ud_sale23.dbf
-rw-r----- 1 oracle oinstall 8.1G May 17 22:50 data_ud_sale24.dbf
-rw-r----- 1 oracle oinstall 2.1G May 17 22:30 idx_operation05.dbf
-rw-r----- 1 oracle oinstall 2.1G May 17 22:46 idx_operation06.dbf
-rw-r----- 1 oracle oinstall 1.1G May 17 22:30 idx_ud_sale13.dbf
-rw-r----- 1 oracle oinstall 4.1G May 17 22:44 idx_ud_sale14.dbf
即有移動過來的數據文件,也有新增的數據文件。
再查看表空間使用,已經下降了:
查詢時間 |
表空間名稱 |
表空間所屬用戶 |
總空間(單位:MB) |
使用表空間(單位:MB) |
未使用空間(單位:MB) |
使用表空間占百分比 |
2015-05-17 22:54:49 |
DATA_OPERATION |
AFCDB |
10920 |
9530 |
1390 |
87.27% |
2015-05-17 22:54:49 |
DATA_PARAMETER |
AFCDB |
512 |
223 |
289 |
43.55% |
2015-05-17 22:54:49 |
DATA_STATIS |
AFCDB |
512 |
437 |
75 |
85.35% |
2015-05-17 22:54:49 |
DATA_UD_SALE |
AFCDB |
68414 |
60179 |
8235 |
87.96% |
2015-05-17 22:54:49 |
IDX_OPERATION |
AFCDB |
11112 |
9063 |
2049 |
81.56% |
2015-05-17 22:54:49 |
IDX_PARAMETER |
AFCDB |
256 |
104 |
152 |
40.63% |
2015-05-17 22:54:49 |
IDX_SPACE |
AFCDB |
256 |
61 |
195 |
23.83% |
2015-05-17 22:54:49 |
IDX_UD_SALE |
AFCDB |
54054 |
49925 |
4129 |
92.36% |
磁盤使用情況也正常了。
至此,操作完成。
最后附上查看表空間的SQL代碼:

1 --查詢使用表空間百分比 2 /*connect system/--------@--------db;*/ 3 select to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS') as "查詢時間", 4 a.tablespace_name as "表空間名稱", 5 c.owner as "表空間所屬用戶", 6 a.bytes / 1024 / 1024 as "總空間(單位:MB)", 7 (a.bytes - b.bytes) / 1024 / 1024 as "使用表空間(單位:MB)", 8 b.bytes / 1024 / 1024 as "未使用空間(單位:MB)", 9 round(((a.bytes - b.bytes) / a.bytes) * 100, 2) || '%' as "使用表空間占百分比" 10 from (select tablespace_name, sum(bytes) bytes 11 from dba_data_files 12 group by tablespace_name) a, 13 (select tablespace_name, sum(bytes) bytes, max(bytes) largest 14 from dba_free_space 15 group by tablespace_name) b, 16 (select tablespace_name, owner 17 from dba_segments 18 group by tablespace_name, owner) c 19 where a.tablespace_name = b.tablespace_name 20 and a.tablespace_name = c.tablespace_name 21 and c.owner = '-----------' 22 order by c.owner, a.tablespace_name, ((a.bytes - b.bytes) / a.bytes) desc;
轉載:增加表空間大小的四種方法
原文地址:http://www.2cto.com/database/201109/103101.html
Meathod1:給表空間增加數據文件
ALTER TABLESPACE app_data ADD DATAFILE
'D:\ORACLE\PRODUCT\10.2.0\ORADATA\EDWTEST\APP03.DBF' SIZE 50M;
Meathod2:新增數據文件,並且允許數據文件自動增長
ALTER TABLESPACE app_data ADD DATAFILE
'D:\ORACLE\PRODUCT\10.2.0\ORADATA\EDWTEST\APP04.DBF' SIZE 50M
AUTOEXTEND ON NEXT 5M MAXSIZE 100M;
Meathod3:允許已存在的數據文件自動增長
ALTER DATABASE DATAFILE 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\EDWTEST\APP03.DBF'
AUTOEXTEND ON NEXT 5M MAXSIZE 100M;
Meathod4:手工改變已存在數據文件的大小
ALTER DATABASE DATAFILE 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\EDWTEST\APP02.DBF'
RESIZE 100M;