在 Oracle 中,如何更改“默认”数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1563564/
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
In Oracle, how do you change the 'default' database?
提问by Nick Pierpoint
I really should know this, but would someone tell me how to change the default database on Linux?
我真的应该知道这一点,但是有人会告诉我如何更改 Linux 上的默认数据库吗?
For example:
例如:
I have a database test1
on server1
with ORACLE_SID=test1
. So, to connect to test1
I can use:
我有一个数据库test1
上server1
使用ORACLE_SID=test1
。因此,要连接到test1
我可以使用:
sqlplus myuser/password
Connects to the default database, test1
连接到默认数据库, test1
I would now like the default sqlplus connection to go to database test2
on server server2
.
我现在希望默认的 sqlplus 连接转到test2
服务器上的数据库server2
。
So, I've updated tnsnames so that the old test1
entry now points to test2@server2
. I've also added a separate entry for test2
that points to the same place. However, the default connection still seems to go to test1@server1
.
因此,我更新了 tnsnames,以便旧test1
条目现在指向test2@server2
. 我还test2
为该点添加了一个单独的条目到同一个地方。但是,默认连接似乎仍然转到test1@server1
.
The following both work fine and go to database test2
on server2
:
以下都工作得不错,去数据库test2
上server2
:
sqlplus myuser/password@test1
sqlplus myuser/password@test2
But the default connection, sqlplus myuser/password
, goes to test1@server1
.
但默认连接sqlplus myuser/password
,转到test1@server1
。
Any ideas?
有任何想法吗?
Thanks.
谢谢。
采纳答案by kdgregory
To expand on kerchingo's answer: Oracle has multiple ways to identify a database.
扩展kerchingo的答案:Oracle 有多种方法可以识别数据库。
The best way -- the one that you should always use -- is USER/PASSWORD@SERVER
. This will use the Oracle naming lookup (tnsnames.ora) to find the actual server, which might be on a different physical host every time you connect to it. You can also specify an Oracle connection string as SERVER
, but pretend you can't.
最好的方法——你应该一直使用的方法——是USER/PASSWORD@SERVER
. 这将使用 Oracle 命名查找 (tnsnames.ora) 来查找实际服务器,每次连接到该服务器时该服务器可能位于不同的物理主机上。您还可以将 Oracle 连接字符串指定为SERVER
,但假装您不能。
There are also two ways to specify a default server via environment variables. The first is TWO_TASK
, which uses the naming lookup, and the second is ORACLE_SID
, which assumes that the server is running on the current machine. ORACLE_SID
takes precedence over TWO_TASK
.
还有两种方法可以通过环境变量指定默认服务器。第一个是TWO_TASK
,它使用命名查找,第二个是ORACLE_SID
,它假设服务器正在当前机器上运行。ORACLE_SID
优先于TWO_TASK
.
The reason that you should always use an explicit connect string is that you have no idea whether the user has set TWO_TASK
, ORACLE_SID
, both, or neither; nor do you know what they might be set to. Setting both to different values is a particularly painful problem to diagnose, particularly over the phone with a person who doesn't really understand how Oracle works (been there, done that).
您应该始终使用显式连接字符串的原因是您不知道用户是否设置了TWO_TASK
、ORACLE_SID
、两者或两者都不设置;你也不知道他们可能会被设置成什么。将两者设置为不同的值是一个特别痛苦的诊断问题,尤其是通过电话与一个并不真正了解 Oracle 工作原理的人(去过那里,做过)。
回答by DCookie
Assuming you're logged into server1, you'll need to connect to test2 using
假设您已登录 server1,则需要使用以下命令连接到 test2
sqlplus myuser/password@test2
because you have to go through a listener to get to server2. The string test2 identifies an entry in your tnsnames.ora file that specifies how to connect to test2. You won't be able to connect to a different server using the first form of your sqlplus command.
因为你必须通过一个监听器才能到达 server2。字符串 test2 标识了 tnsnames.ora 文件中的一个条目,该条目指定了如何连接到 test2。您将无法使用 sqlplus 命令的第一种形式连接到不同的服务器。
If both instances (test1, test2) were on server1, then you could, as @kerchingo states, set the ORACLE_SID environment variable to point at another instance.
如果两个实例(test1、test2)都在 server1 上,那么您可以如@kerchingo 所述,将 ORACLE_SID 环境变量设置为指向另一个实例。
回答by Christian13467
Defining a enviroment variable LOCAL
with the tns alias of your database.
LOCAL
使用数据库的 tns 别名定义环境变量。
> set LOCAL=test1
> sqlplus myuser/password
> ... connected to test1
> set LOCAL=test2
> sqlplus myuser/password
> ... connected to test2
This works on windows client, not shure about other os.
这适用于 Windows 客户端,不适用于其他操作系统。
回答by Bernaridho Hutabarat
The correct question is 'How do I change the default service'? Oracle DBMS offers two types of connection request: explicit and implicit. In an explicit request, you supply three operands like sqlplus username/password@service. In an implicit request, you ignore the third operand.
正确的问题是“如何更改默认服务”?Oracle DBMS 提供两种类型的连接请求:显式和隐式。在显式请求中,您提供三个操作数,如 sqlplus username/password@service。在隐式请求中,您忽略第三个操作数。
Implicit connection applies only when the client-host and server-host are the same. Consequently, the listener is on the same host.
隐式连接仅适用于客户端-主机和服务器-主机相同的情况。因此,侦听器位于同一主机上。
The listener is the one that initially responds to connection request. In handling the implicit-connection request from the same host, it checks whether the instance name has been set. It checks the value of shell-variable ORACLE_SID.
侦听器是最初响应连接请求的那个。在处理来自同一主机的隐式连接请求时,它检查是否已设置实例名称。它检查外壳变量 ORACLE_SID 的值。
If set, then it can handle implicit-connection request. Otherwise, it cannot, and you must perform explicit-connection request, supplying the third operand.
如果设置,则它可以处理隐式连接请求。否则,它不能,您必须执行显式连接请求,提供第三个操作数。
The listener-config file name listener.ora associates instance with service. To change the default service you connect to, change the default value of instance. Therefore, change the default value of shell-variable ORACLE_SID. You do it in the OS user config file such as .profile or similar config files.
listener-config 文件名 listener.ora 将实例与服务相关联。要更改您连接的默认服务,请更改实例的默认值。因此,更改外壳变量 ORACLE_SID 的默认值。您可以在操作系统用户配置文件(例如 .profile 或类似配置文件)中执行此操作。
Hope this helps.
希望这可以帮助。
回答by AdamW
I think it is set in your environment, can you echo $ORACLE_SID?
我认为它是在您的环境中设置的,您可以 echo $ORACLE_SID 吗?