1、环境介绍服务器硬件:Dell R710服务器OS:红帽子Linux RHEL4.8数据库:Oracle 10g2、出现的问题因为数据表每天有上百万的数据写入表,加上建立索引,导致表空间不停增长,表空间被设置为自动增长,因此dbf文件在不断增大,硬盘空间在每天约400M的速度减少。数据库虽有自清理的脚本,清理3个月前的数据,但实际增加的数据太多,清理释放的空间不能满足需求了。3、处理过程解决思路是,该服务器的硬盘有预留空间,未完全划分的约有100G,可以建立新的分区,将已经存满的分区的数据库文件移动,来避免挂载点使用率达到100%,同时可以扩充表空间。3.1 建立分区以root账号登陆,查看目前使用情况:[root@ccsvr ~]# df -hFilesystem 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_DB2none 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 -lDisk /dev/sda: 146.1 GB, 146163105792 bytes255 heads, 63 sectors/track, 17769 cylindersUnits = 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 bytes255 heads, 63 sectors/track, 36404 cylindersUnits = 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/sdbCommand (m for help): p Disk /dev/sdb: 299.4 GB, 299439751168 bytes255 heads, 63 sectors/track, 36404 cylindersUnits = 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): nCommand action l logical (5 or over) p primary partition (1-4)lFirst cylinder (21889-32829, default 21889):Using default value 21889Last 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 bytes255 heads, 63 sectors/track, 36404 cylindersUnits = 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): wThe 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 - ProductionWith 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/sdb6mke2fs 1.35 (28-Feb-2004)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)10993664 inodes, 21970887 blocks1098544 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=25165824671 block groups32768 blocks per group, 32768 fragments per group16384 inodes per groupSuperblock 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): doneWriting superblocks and filesystem accounting information: done This filesystem will be automatically checked every 24 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override. 然后,创建需要挂载的目录,将分区挂载:[root@ccsvr /]# mkdir AFC_DB5[root@ccsvr /]# mkdir AFC_DB6[root@ccsvr /]# lltotal 216drwxr-xr-x 6 afc aas 4096 Dec 2 2013 AASdrwxrwxr-x 13 afc aas 4096 Dec 27 00:32 AFCdrwxr-xr-x 5 oracle oinstall 4096 Apr 14 2014 AFC_DBdrwxr-xr-x 6 oracle oinstall 4096 Apr 14 2014 AFC_DB2drwxr-xr-x 4 oracle oinstall 4096 Apr 14 2014 AFC_DB3drwxr-xr-x 2 root root 4096 May 17 21:18 AFC_DB5drwxr-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 detailsLABEL=/ / ext3 defaults 1 1LABEL=/AFC/Data /AFC/Data ext3 defaults 1 2LABEL=/AFC/Log /AFC/Log ext3 defaults 1 2LABEL=/AFC_DB /AFC_DB ext3 defaults 1 2/dev/sdb1 /AFC_DB2 ext3 defaults 1 2none /dev/pts devpts gid=5,mode=620 0 0none /dev/shm tmpfs defaults 0 0LABEL=/oracle /oracle ext3 defaults 1 2none /proc proc defaults 0 0none /sys sysfs defaults 0 0LABEL=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 detailsLABEL=/ / ext3 defaults 1 1LABEL=/AFC/Data /AFC/Data ext3 defaults 1 2LABEL=/AFC/Log /AFC/Log ext3 defaults 1 2LABEL=/AFC_DB /AFC_DB ext3 defaults 1 2/dev/sdb1 /AFC_DB2 ext3 defaults 1 2none /dev/pts devpts gid=5,mode=620 0 0none /dev/shm tmpfs defaults 0 0LABEL=/oracle /oracle ext3 defaults 1 2none /proc proc defaults 0 0none /sys sysfs defaults 0 0LABEL=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 -hFilesystem 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_DB2none 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 | |
(还有两个表,就不贴出来了)可以看到,新的数据文件都在/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]$ lltotal 24drwx------ 2 root root 16384 May 17 21:24 lost+founddrwxr-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 - ProductionWith 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 -lhtotal 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 bytesFixed Size 1220432 bytesVariable Size 218103984 bytesDatabase Buffers 1912602624 bytesRedo Buffers 15556608 bytesDatabase 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 -alhtotal 28Gdrwxr-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代码:--查询使用表空间百分比
/*connect system/--------@--------db;*/
select to_char(sysdate, "YYYY-MM-DD HH24:MI:SS") as "查询时间",
a.tablespace_name as "表空间名称",
c.owner as "表空间所属用户",
a.bytes / 1024 / 1024 as "总空间(单位:MB)",
(a.bytes - b.bytes) / 1024 / 1024 as "使用表空间(单位:MB)",
b.bytes / 1024 / 1024 as "未使用空间(单位:MB)",
round(((a.bytes - b.bytes) / a.bytes) * 100, 2) || "%" as "使用表空间占百分比"
from (select tablespace_name, sum(bytes) bytes
from dba_data_files
group by tablespace_name) a,
(select tablespace_name, sum(bytes) bytes, max(bytes) largest
from dba_free_space
group by tablespace_name) b,
(select tablespace_name, owner
from dba_segments
group by tablespace_name, owner) c
where a.tablespace_name = b.tablespace_name
and a.tablespace_name = c.tablespace_name
and c.owner = "-----------"
order by c.owner, a.tablespace_name, ((a.bytes - b.bytes) / a.bytes) desc;增加表空间大小的四种方法Meathod1:给表空间增加数据文件
ALTER TABLESPACE app_data ADD DATAFILE
"D:ORACLEPRODUCT10.2.0ORADATAEDWTESTAPP03.DBF" SIZE 50M;
Meathod2:新增数据文件,并且允许数据文件自动增长
ALTER TABLESPACE app_data ADD DATAFILE
"D:ORACLEPRODUCT10.2.0ORADATAEDWTESTAPP04.DBF" SIZE 50M
AUTOEXTEND ON NEXT 5M MAXSIZE 100M;
Meathod3:允许已存在的数据文件自动增长
ALTER DATABASE DATAFILE "D:ORACLEPRODUCT10.2.0ORADATAEDWTESTAPP03.DBF"
AUTOEXTEND ON NEXT 5M MAXSIZE 100M;
Meathod4:手工改变已存在数据文件的大小
ALTER DATABASE DATAFILE "D:ORACLEPRODUCT10.2.0ORADATAEDWTESTAPP02.DBF"
RESIZE 100M;更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12
本文永久更新链接地址