如何从 unix 连接到 oracle 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29262596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:47:40  来源:igfitidea点击:

how to connect to an oracle database from unix

databaseoracleshellunixsqlplus

提问by Nidhin_toms

I am trying to connect to an oracle database from my unix machine. I am new to script writing in general. I know how to browse round unix and have written basic scripts ( read / display ) and execute them with bash command. Also I know how to view the variables in unix ( user and system). Could you tell me what i need to do to connect to an oracle database? do I use the sqlplus command? are there any variables I have to set before that?

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

采纳答案by Lalit Kumar B

Could you tell me what i need to do to connect to an oracle database? do I use the sqlplus command?

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

Well, yes of course, you need to use SQL*Plus. However, before that, you need to make sure of few things:

嗯,是的,当然,您需要使用 SQL*Plus。但是,在此之前,您需要确保以下几点:

  1. export ORACLE_HOME variable
  1. 导出 ORACLE_HOME 变量

For example,

例如,

export ORACLE_HOME=/u01/app/oracle/product/11.2.0
  1. export PATH variable
  1. 导出路径变量

For example,

例如,

export PATH=$PATH:$ORACLE_HOME/bin
  1. export SID
  1. 导出 SID

For example,

例如,

export ORACLE_SID="your database service name"
  1. Make sure you have the tnsnames.oraconfigured properly
  2. Make sure you have the listenerup and running and is listening to the correct port.
  1. 确保您已tnsnames.ora正确配置
  2. 确保您已listener启动并运行并且正在侦听正确的端口。

You should be able to connect to the database as:

您应该能够以如下方式连接到数据库:

sqlplus username/password@sid

回答by Mayank Kumar

Set ORACLE_HOME & ORACLE_SID environment variable. Then use sqlplus username@ORACLE_SID

设置 ORACLE_HOME 和 ORACLE_SID 环境变量。然后使用sqlplus username@ORACLE_SID

回答by Rajesh Chaudhary

Make sure you've exported all the necessary Oracle variable in your unix user path environment as below:

确保您已在您的 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

And Make sure tnsnames.ora file is configured properly and listener should be up and running as below.

并确保 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 ~]$ 

Now you've multiple ways to connect database from unix command prompt.

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

[oracle@OLE1 ~]$ sqlplus scott/tiger

[oracle@OLE1 ~]$ sqlplus scott/tiger

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

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

[oracle@OLE1 ~]$ sqlplus scott/[email protected]:1521/orcl

[oracle@OLE1 ~]$ sqlplus scott/[email protected]:1521/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@(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/[email protected]: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 ~]$