如何从unix连接到oracle数据库

时间:2015-03-25 17:33:49

标签: database oracle shell unix sqlplus

我正在尝试从我的unix机器连接到oracle数据库。我是剧本写作的新手。我知道如何浏览循环unix并编写基本脚本(读/显示)并使用bash命令执行它们。我也知道如何在unix(用户和系统)中查看变量。你能告诉我连接到oracle数据库需要做什么吗?使用sqlplus命令?在那之前我必须设置任何变量吗?

3 个答案:

答案 0 :(得分:1)

设置ORACLE_HOME& ORACLE_SID环境变量。 然后用 sqlplus用户名@ ORACLE_SID

答案 1 :(得分:1)

  

你能告诉我连接到oracle数据库需要做什么吗?使用sqlplus命令?

嗯,是的,当然,你需要使用SQL * Plus。但是,在此之前,您需要确保一些事情:

  1. 导出ORACLE_HOME变量
  2. 例如,

    export ORACLE_HOME=/u01/app/oracle/product/11.2.0
    
    1. 导出PATH变量
    2. 例如,

      export PATH=$PATH:$ORACLE_HOME/bin
      
      1. 导出SID
      2. 例如,

        export ORACLE_SID="your database service name"
        
        1. 确保您已正确配置tnsnames.ora
        2. 确保您已启动并运行listener并正在侦听正确的端口。
        3. 您应该能够以:

          连接到数据库
          sqlplus username/password@sid
          

答案 2 :(得分:0)

确保您已在unix用户路径环境中导出所有必需的Oracle变量,如下所示:

ORACLE_BASE=/home/oracle/app; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/oracle/product/11.2.0/dbhome_1/; export ORACLE_HOME
ORACLE_SID=orcl; export ORACLE_SID
PATH=$ORACLE_HOME/bin:$HOME/bin:$PATH; export PATH

并确保正确配置tnsnames.ora文件,并且应启动并运行监听器,如下所示。

[oracle@OLE1 admin]$ cat tnsnames.ora 
# tnsnames.ora Network Configuration File: /home/oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = ole1)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )

[oracle@OLE1 ~]$ tnsping orcl
TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on 12-JUL-2017 23:12:35
Copyright (c) 1997, 2009, Oracle.  All rights reserved.

Used parameter files:
/home/oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/sqlnet.ora

Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ole1)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl)))
OK (20 msec)
[oracle@OLE1 ~]$ 

[oracle@OLE1 ~]$ cat /etc/hosts
127.0.0.1   localhost
::1         localhost
192.168.244.128 ole1
[oracle@OLE1 ~]$ 

现在您有多种方法可以从unix命令提示符连接数据库。

[oracle @ OLE1~] $ sqlplus scott / tiger

[oracle @ OLE1~] $ sqlplus scott / tiger @ orcl

[oracle @ OLE1~] $ sqlplus scott /tiger@192.168.244.128:1521 / orcl

[oracle @ OLE1~] $ sqlplus scott / tiger @ // 192.168.244.128:1521/orcl

[oracle @ OLE1~] $ sqlplus" scott / tiger @(DESCRIPTION =(ADDRESS =(PROTOCOL = TCP)(HOST = ole1)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)( SERVICE_NAME = ORCL)))"

[oracle@OLE1 ~]$ sqlplus scott/tiger
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jul 12 23:29:30 2017
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

[oracle@OLE1 ~]$ sqlplus scott/tiger@orcl
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jul 12 23:30:00 2017
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Connected
[oracle@OLE1 ~]$ 

[oracle@OLE1 ~]$ sqlplus scott/tiger@192.168.244.128:1521/orcl
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jul 12 23:30:00 2017
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Connected
[oracle@OLE1 ~]$ 


[oracle@OLE1 ~]$ sqlplus scott/tiger@//192.168.244.128:1521/orcl
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jul 12 23:30:00 2017
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Connected
[oracle@OLE1 ~]$ 
[oracle@OLE1 ~]$ sqlplus "scott/tiger@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ole1)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)))"

SQL*Plus: Release 11.2.0.1.0 Production on Thu Jul 13 12:30:23 2017

Copyright (c) 1982, 2009, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> 
SQL> show user
USER is "SCOTT"
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@OLE1 ~]$
相关问题