How to use sqlplus to connect to an Oracle Database located on another host without modifying my own tnsnames.ora

Lou*_*hys 91 oracle sqlplus

I want to connect to an oracle database located on another host using sqlplus. This page suggested adding an item on my tnsnames to conenct to that database

local_SID =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL= TCP)(Host= hostname.network)(Port= 1521))
    (CONNECT_DATA = (SID = remote_SID))
  )
Run Code Online (Sandbox Code Playgroud)

and then use that in sqlplus

sqlplus user/pass@local_SID
Run Code Online (Sandbox Code Playgroud)

However, in my circumstances modifying the local tnsnames is not possible. Is it possible to connect to a remote database just by using sqlplus argument without having to change tnsnames? Something like

sqlplus user/pass@remote_SID@hostname.network ;( I know, this one is not valid)
Run Code Online (Sandbox Code Playgroud)

Ren*_*ger 95

 sqlplus user/pass@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname.network)(Port=1521))(CONNECT_DATA=(SID=remote_SID)))
Run Code Online (Sandbox Code Playgroud)

Maybe, and this might be dependant on the command line environment you're using, you need to quote the string, something like

 sqlplus "user/pass@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname.network)(Port=1521))(CONNECT_DATA=(SID=remote_SID)))"
Run Code Online (Sandbox Code Playgroud)

or

 sqlplus 'user/pass@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname.network)(Port=1521))(CONNECT_DATA=(SID=remote_SID)))'
Run Code Online (Sandbox Code Playgroud)

  • 如果使用便携式数据库,请使用它代替 sid : `(CONNECT_DATA=(SERVICE_NAME=remote_service_name))` (2认同)

Ale*_*ole 39

You can use easy connect for this:

sqlplus usr/pass@hostname.network/remote_service_name
Run Code Online (Sandbox Code Playgroud)

To enable easy connect on your machine, you need to add it to the NAMES.DIRECTORY_PATH in sqlnet.ora, e.g.:

NAMES.DIRECTORY_PATH=(EZCONNECT)
Run Code Online (Sandbox Code Playgroud)

If your listener is on a non-default port use ...@hostname.network:port/....

Actually it seems you have to supply a service name, not a SID; they may be the same but if not you'll need to obtain that from the server.

  • 如果服务名称与主机名相同,则连接时甚至不需要指定服务名称。(实际上没有人这样做,但很高兴知道。) (2认同)

Phi*_*lᵀᴹ 21

在您可以写入的目录中创建 tnsnames.ora 文件的副本,相应地修改该文件,然后将 TNS_ADMIN 环境变量设置为该目录的位置。

例如:

cp $ORACLE_HOME/network/admin/tnsnames.ora /tmp/tnsnames.ora
# edit the /tmp/tnsnames.ora file to add your entries

# Set the $TNS_ADMIN environment variable so that sqlplus knows where to look 
export TNS_ADMIN=/tmp
Run Code Online (Sandbox Code Playgroud)

  • 这是一个更好的回应 (2认同)

mir*_*173 5

在 Unix/Linux 系统上,您可以使用用户级配置文件来覆盖系统级条目。

系统级用户级 
配置文件 配置文件
------------------ -------------------
sqlnet.ora $HOME/.sqlnet.ora
tnsnames.ora $HOME/.tnsnames.ora

系统级配置文件可以在目录中找到$TNS_ADMIN。如果TNS_ADMIN未设置变量,则在目录中搜索它们$ORACLE_HOME/network/admin

用户级配置文件不会整体替换系统级配置文件(如TNS_ADMIN目录替换整个$ORACLE_HOME/network/admin目录),而是添加或更改系统级配置文件的条目。如果用户级配置文件中存在条目,则使用该条目,如果用户级配置文件中不存在该条目,则使用系统级配置文件中的条目。