ITPub博客

首页 > 数据库 > Oracle > [20141105]不同用户访问oracle数据库.txt

[20141105]不同用户访问oracle数据库.txt

原创 Oracle 作者:lfree 时间:2014-11-05 11:12:55 0 删除 编辑

[20141105]不同用户访问oracle数据库.txt

--我们有一台机器,上面安装了两个数据库,字符集不一样。要访问和操作是要切换ORACLE_SID以及NLS_LANG环境变量。
--非常繁琐,而且容易出错,非常不方便。

--自己也想了几种方式:

1.建立一个脚本,用来切换用户:

#  cat chsid
#! /bin/bash
##############
# Desc : switch oracle sid , change sid and env NLS_LANG
######################################################
case "$1" in
    dbcn1|cn1)
                unset   ORACLE_SID
                export  ORACLE_SID=dbcn1
                unset   NLS_LANG
                export  NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
                ;;
    dben1|en1)
                unset   ORACLE_SID
                export  ORACLE_SID=dben1
                unset   NLS_LANG
                export  NLS_LANG=AMERICAN_AMERICA.US7ASCII
                ;;
    *)
                echo "$0 ORACLE_SID"
                ;;
esac

--执行时使用 . chsid en1或者source chsid en1,就可以实现切换。
--注意前面要使用一个. 或者source ,不然环境变量的修改无效,我不知道什么往上传环境变量(子进程到父进程传递环境变量,好像不行)。

2.建立一个新用户,这样比较科学一些,不容易出错。
--简单记录一下步骤。

$ id
uid=503(oracle11g) gid=501(oinstall) groups=501(oinstall),502(dba)

--建立新用户包含oracle的全部组。

# useradd oraen -g oinstall -G dba
[root@hisdg IP=40 ~ 6]# passwd oraen
Changing password for user oraen.
New UNIX password:
BAD PASSWORD: it is too short
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

--再修改.bash_profile环境变量,就ok了。

3.读写存在问题吗?
--这个一直是我担心的问题,测试以后不存在。

# ls -l users01.dbf
-rw-r-----  1 oracle11g oinstall 805314560 2014-11-05 02:08:11 users01.dbf

--可以发现用户oracle11g有读写权限,而组oinstall仅仅有读权限。sqlplus访问执行执行DML可以吗?测试看看。

SCOTT@test> select * from t;

        ID C2
---------- --------------------
         1 1
         2 2
         3 3
         4 4
         5 5


SCOTT@test> update t set id=100 where id=1;
1 row updated.

SCOTT@test> commit ;
Commit complete.

SCOTT@test> select * from t where rownum<=5;
        ID C2
---------- --------------------
       100 1
         2 2
         3 3
         4 4
         5 5
SCOTT@test> alter system checkpoint;
System altered.

--为什么能成功呢?
SCOTT@test> select spid from v$process where addr in (select paddr from v$session where sid in (select sid from v$mystat where rownum=1));
SPID
------
14416

$ ps -ef
F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
...
4 S root     14380 11086  0  76   0 - 17836 wait   10:08 pts/3    00:00:00 su - oraen
4 S oraen    14381 14380  0  76   0 - 14175 wait   10:08 pts/3    00:00:00 -bash
0 S oraen    14415 14381  0  76   0 - 15372 -      10:08 pts/3    00:00:00 sqlplus
0 S 503      14416 14415  0  76   0 - 453967 pipe_w 10:08 ?       00:00:00 oracletest (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))

--可以发现进程号=14416的用户503,就是oracle11g。
--注意如果用户名的长度大于8位,ps 显示UID是用户id。oracle11g的用户id就是503.

# id oracle11g
uid=503(oracle11g) gid=501(oinstall) groups=501(oinstall),502(dba)

$ ls -l `which oracle `
-rwsr-s--x  1 oracle11g oinstall 232272740 2013-03-21 09:02:54 /u01/app/oracle11g/product/11.2.0/db_2/bin/oracle

--可以发现执行文件oracle带有s权限,这样当用户调用执行它时,可以继承命令的uid以及gid,这样用户就具有了访问数据文件的权限。

--unix/linux下一些管理命令都具有这样的特性。比如修改口令的程序。

# ls -l `which passwd`
-r-s--x--x  1 root root 27728 2005-06-17 22:40:47 /usr/bin/passwd

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-1321046/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    4006
  • 访问量
    7969584