Monday, 27 May 2013

Simple Oracle 11gR2 DBFS (Database Filesystem) Creation

 
The history of oracle database filesystem is not new to us and is introduced in beginning of the oracle 8i with name as oracle Internet File System (IFS), the IFS is later renamed and termed as content Management SDK. The oracle 11gR2 now come up with the new name called Database Filesystem (DBFS).
 
The DBFS is based on the secure filesystem technology to is used to store SecureLOBS. Storing the files in this manner provides better performance, security and availability. Storing files in this manner can be backed and recovered using along with relational database and can also be replicated using dataguard.
 
Currently, The DBFS is only supported on Linux and Solaris operating systems and hope this might be extended in future for other operating systems as well.
  
Step by Step for creating DBFS,
 
Prerequisites for creating and mounting DBFS filesystem
FUSE (Filesystem USErspace) :- The FUSE is an open source Linux project development which provides the fully functional filesystem in user space. The FUSE can be downloaded from http://fuse.sourceforge.net or if you are using OEL, then it can be installed from OEL CD/DVD media or also can be from oracle's public yum repositories.
 
FUSE Library Installation using yum,
 
Configure the yum repository for your Linux version, (Make sure you have internet connectivity)
[root@localhost-pub ~]# cd /etc/yum.repos.d
[root@localhost-pub ~]#  wget http://public-yum.oracle.com/public-yum-el5.repo
 
Login as root and install the FUSE,
[root@localhost-pub ~]#  yum install fuse*
 
Installation from Media,
Insert the media and configure the yum repository using the below syntax as root,
[root@localhost-pub ~]# vi data.repo
[DATA]
NAME=DATA
MEDIAID= <take the media id from disk.level>
BASEURL=file:///<mounted location>
enable=1
gpgcheck=0
 
save and exit from editor.
 
Install the fuse packages using below from Media. 
[root@localhost-pub ~]# yum install fuse*
 
Manual Installation
Download the FUSE library from link http://fuse.sourceforge.net for manual installation. Make sure the Linux Kernel development library must be installed before compiling the FUSE library.
 
Query the Kernel development package
[root@localhost-pub ~]# rpm -qa kernel-devel
kernel-devel-2.6.32-358.el6.i686
 
Determine your current Kernel directory
[root@localhost-pub ~]# echo /usr/src/kernels/`uname -r`-`uname -p`
/usr/src/kernels/2.6.39-400.17.1.el6uek.i686-i686
 
 Extract and Install the FUSE package.
[root@localhost-pub ~]# tar -xzvf fuse-2.7.3.tar.gz
 
Run the configuration script
[root@localhost-pub ~]# cd fuse-2.7.3
[root@localhost-pub ~]# ./configure --prefix=/usr --with-kernel=/usr/src/kernels/2.6.39-400.17.1.el6uek.i686-i686
 
Make and Build the Library
[root@localhost-pub ~]# make
[root@localhost-pub ~]# make install
 
Creation of DBFS filesystem
 
Create the tablespace for filesystem.
SQL> CREATE TABLESPACE dbfsts DATAFILE '/oradata/pr/dbfs01.dbf' SIZE 10M;
 
Create the non-sys user with quota unlimited on default tablespace.
Note--> Creating DBFS with SYS user will fail.
 
SQL> CREATE USER dbfsuser IDENTIFIED BY dbfsuser DEFAULT TABLESPACE dbfsts QUOTA UNLIMITED ON dbfsts;
 
Grant the DBFS_ROLE and other necessary privileges to dbfsuser
SQL> GRANT CREATE SESSION, RESOURCE, CREATE VIEW, DBFS_ROLE TO dbfsuser;
 
Connect as dbfsuser and execute the $ORACLE_HOME/rdbms/admin/dbfs_create_filesystem.sql script.
format::@dbfs_create_filesystem.sql <tablespace_name> <filesystem_name>
Want more control over filesystem creation, Then you are free to use
 
$ORACLE_HOME/rdbms/admin/dbfs_create_filesystem_advanced.sql
 
[oracle@localhost-pub admin]$ sqlplus dbfsuser/dbfsuser
 
SQL*Plus: Release 11.2.0.3.0 Production on Sun May 26 19:42:46 2013
 
Copyright (c) 1982, 2011, Oracle.  All rights reserved.
 
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
 
SQL> @dbfs_create_filesystem.sql dbfsts staging_area
No errors.
--------
CREATE STORE:
begin dbms_dbfs_sfs.createFilesystem(store_name => 'FS_STAGING_AREA', tbl_name
=> 'T_STAGING_AREA', tbl_tbs => 'dbfs_ts', lob_tbs => 'dbfs_ts', do_partition =>
false, partition_key => 1, do_compress => false, compression => '', do_dedup =>
false, do_encrypt => false); end;
--------
REGISTER STORE:
begin dbms_dbfs_content.registerStore(store_name=> 'FS_STAGING_AREA',
provider_name => 'sample1', provider_package => 'dbms_dbfs_sfs'); end;
--------
MOUNT STORE:
begin dbms_dbfs_content.mountStore(store_name=>'FS_STAGING_AREA',
store_mount=>'staging_area'); end;
--------
CHMOD STORE:
declare m integer; begin m := dbms_fuse.fs_chmod('/staging_area', 16895); end;
No errors.
SQL>
 
Mount the filesystem,
Now the filesystem created but at database level, the filesystem will not be visible through operating system. Oracle ships with the special binary for mounting the filesystem is dbfs_client and is available in $ORACLE_HOME/bin. The client machine must require to install the dbfs_client to access the DBFS.
 [oracle@localhost-pub ~]$ dbfs_client
dbfs_client: error while loading shared libraries: libclntsh.so.11.1: cannot open shared object file: No such file or directory
 
If the above errors appears means the LD_LIBRARY_PATH = $ORACLE_HOME/lib is not set properly. export the Library Path again and try,
 
[oracle@localhost-pub ~]$ export LD_LIBRARY_PATH=$ORACLE_HOME/lib
[oracle@localhost-pub ~]$ echo $LD_LIBRARY_PATH
/u01/app/oracle/product/11.2.0/dbhome_1/lib
[oracle@localhost-pub ~]$
[oracle@localhost-pub ~]$ dbfs_client
 
--------MOUNT mode:
 
usage: dbfs_client <db_user>@<db_server> [options] <mountpoint>
  db_user:              Name of Database user that owns DBFS content repository filesystem(s)
  db_server:            A valid connect string for Oracle database server
                        (for example, hrdb_host:1521/hrservice)
  mountpoint:           Path to mount Database File System(s)
                        All the file systems owned by the database user will be seen at the mountpoint.
DBFS options:
  -o direct_io          Bypass the Linux page cache. Gives much better performance for large files.
                        Programs in the file system cannot be executed with this option.
                        This option is recommended when DBFS is used as an ETL staging area.
  -o wallet             Run dbfs_client in background.
                        Wallet must be configured to get credentials.
  -o failover           dbfs_client fails over to surviving database instance with no data loss.
                        Some performance cost on writes, especially for small files.
  -o allow_root         Allows root access to the filesystem.
                        This option requires setting 'user_allow_other' parameter in '/etc/fuse.conf'.
  -o allow_other        Allows other users access to the file system.
                        This option requires setting 'user_allow_other' parameter in '/etc/fuse.conf'.
  -o rw                 Mount the filesystem read-write. [Default]
  -o ro                 Mount the filesystem read-only. Files cannot be modified.
  -o trace_file=STR     Tracing <filename> | 'syslog'
  -o trace_level=N      Trace Level: 1->DEBUG, 2->INFO, 3->WARNING, 4->ERROR, 5->CRITICAL [Default: 4]
  -h                    help
  -V                    version
 
--------COMMAND mode:
 
Usage:     dbfs_client <db_user>@<db_server> --command command [switches] [arguments]
 
           command:          Command to be executed, e.g., ls, cp, mkdir, rm
           switches:         Switches are described below for each command.
           arguments:        File names or directory names
 
NOTE:      All database pathnames must be absolute and preceded by dbfs:/
 
Commands 
ls
           dbfs_client <db_user>@<db_server> --command ls [switches] target
     Switches: 
           -a         Show all files including those starting with '.'
           -l         Use a long listing format. In addition to the name of each file
                      print the file type, permissions, size, user and group information
           -R         List subdirectories recursively
 
cp        
           dbfs_client <db_user>@<db_server> --command cp [switches] source destination
     Switches: 
           -r, -R      Copy a directory and its contents recursively into the destination directory
 
rm        
           dbfs_client <db_user>@<db_server> --command rm [switches] target
     Switches: 
           -r, -R      Removes a directory and its contents recursively
 
mkdir     
           dbfs_client <db_user>@<db_server> --command mkdir directory_name
 
Examples        
           dbfs_client ETLUser@DBConnectString --command ls -l -a dbfs:/staging_area/directory1
           dbfs_client ETLUser@DBConnectString --command cp -R  /tmp/1-Jan-2009-dump dbfs:/staging_area
           dbfs_client ETLUser@DBConnectString --command rm dbfs:/staging_area/hello.txt
           dbfs_client ETLUser@DBConnectString --command mkdir dbfs:/staging_area/directory2
 
[oracle@localhost-pub ~]$
 
Login as root and create the desired directory
[root@localhost-pub ~]# mkdir rajesh
 
change the ownership
 
[root@localhost-pub ~]# chown -R oracle:dba /rajesh
 
Add the below line to create new library path.
 
[root@localhost-pub ~]# echo "/usr/local/lib" >> /etc/ld.so.conf.d/usr_local_lib.conf
 
goto /usr/local/lib and create the soft links with oracles library path, check libfuse.so files into your system for correct location.
 
[root@localhost-pub ~]# export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome
[root@localhost-pub ~]# ln -s $ORACLE_HOME/lib/libclntsh.so.11.1
[root@localhost-pub ~]# ln -s $ORACLE_HOME/lib/libnnz11.so
[root@localhost-pub ~]# ln -s /lib/libfuse.so.2
 
Mount the filesystem here,
 
[oracle@localhost-pub ~]$ dbfs_client dbfs_user@pr /rajesh
fuse: failed to exec fusermount: Permission denied
 
if you have above error, try to fix it as below,
 
[root@localhost-pub ~]# chmod +x /bin/fusermount
 
[oracle@localhost-pub ~]$ dbfs_client dbfs_user@pr /rajesh
Password:
 
Note:- The terminal will not be released here until you unmount the FS. Better to execute this command in background using nohup.
 
Check whether the DBFS filesystem is mounted or not, here we go...
 
[oracle@localhost-pub staging_area]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      7.5G  3.6G  3.5G  51% /
tmpfs                 2.0G  977M  1.1G  49% /dev/shm
/dev/sda1             485M   52M  408M  12% /boot
/dev/mapper/binaries-lvol0
                       13G  9.4G  2.1G  83% /u01
/dev/mapper/binaries-lvol1
                       51G   15G   34G  31% /oradata
dbfs-dbfs_user@pr:/   960K  160K  800K  17% /rajesh
 
 
Unmount the DBFS filesystem,
 
Login as oracle and execute the below,
 
[oracle@localhost-pub ~]$ fusermount -u /rajesh
 
 
Drop the DBFS filesystem,
 
You can also drop the DBFS filesystem if no more required.
as oracle user execute the following,
 
SQL>@dbfs_drop_filesystem.sql staging_area
 
 Conclusion:- The above is documented to keep the things simpler, lot to go from here and watch this blog for more information.
 
TEST!TEST!TEST!
 

No comments:

Post a Comment