Oracle即时客户端连接方法

记录Oracle即时客户端连接Oracle服务器端的方法。

下载Oracle即时客户端

对应Oracle11数据库,下载如下Oracle即时客户端:

  • instantclient-basic-windows.x64-12.1.0.2.0.zip
  • instantclient-sqlplus-windows.x64-12.1.0.2.0.zip

将instantclient-basic和instantclient-sqlplus两个安装包放在同一个目录下,解压到同一个文件夹内。

修改系统变量

  • 将Oracle即时客户端的路径E:\Oracle\instantclient_12_1添加到系统环境变量PATH中。
  • 新增系统变量NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252 –>> 服务器端字符集
  • TNS_ADMIN=E:\Oracle\instantclient_12_1 –>> 指定tnsnames.ora所在位置

新建tnsnames.ora

在E:\Oracle\instantclient_12_1 下新建tnsnames.ora配置文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
orcl_service_name =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = tcp)(HOST = localhost)(PORT = 1521)(QUEUESIZE = 100))
(CONNECT_DATA =
(SERVICE_NAME = bookstore.company.com)
)
)

orcl_sid =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.11)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SID = orcl)
)
)

用SQL*Plus登陆测试

Connect via SERVICE_NAME

sqlplus username/password@host:port/SERVICE_NAME

例如:

1
sqlplus admin/123456@192.168.1.11:1521/ORCL

Connect via SID

sqlplus username/password@host:port:SID,注意该方式在oracle instant client 12.1版本中没有验证通过,具体原因未知。

sqlplus system/password@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=192.168.0.49)(Port=1521))(CONNECT_DATA=(SID=ORCL)))

例如:

1
sqlplus admin/123456@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=192.168.0.49)(Port=1521))(CONNECT_DATA=(SID=ORCL)))

sqlplus命令

sqlplus登录后,使用如下sql命令查询相关信息:

  • 查看数据库名
1
SQL> select name from v$database;
  • 查询当前数据库实例名
1
SQL> select instance_name from v$instance;
  • 数据库中所有表
1
SQL> SELECT TABLE_NAME FROM DBA_TABLES;
  • 显示数据库对象结构
1
SQL> desc emp

使用node-oracledb连接数据库

连接字符串的网络服务名方法

1
2
3
4
5
6
7
8
9
10
const oracledb = require('oracledb');

const connection = await oracledb.getConnection(
{
user : "admin",
password : "123456",
connectStringForServiceName1 : "Ora11"
connectStringForServiceName2 : "192.168.0.49/ORCL",
}
);

对应的tnsnames.ora内容为:

1
2
3
4
5
6
7
8
Ora11 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.49)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ORCL)
)
)

连接字符串的网络服务标识方法

该方式可连接使用sid的oracle实例:

1
2
3
4
5
6
7
8
const connection = await oracledb.getConnection(
{
user : "admin",
password : "123456",
connectStringForSid1 : "Ora11",
connectStringForSid2 : "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=192.168.0.49)(Port=1521))(CONNECT_DATA=(SID=ORCL)))"
}
);

对应的tnsnames.ora内容为:

1
2
3
4
5
6
7
8
Ora11 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.49)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SID = ORCL)
)
)

oracledb读取Blob和Clob字段

请参考nodejs使用oracledb时,读取Blob和Clob字段遇到的问题

参考链接

  1. 如何安装Oracle客户端(Oracle Instant Client),by whatday.
  2. Oracle Instant Client(即时客户端) 安装与配置,by 长烟慢慢.
  3. 使用 SQL *Plus 管理 Oracle 数据库,by Node.
  4. nodejs使用oracledb时,读取Blob和Clob字段遇到的问题,by 欢欢2776479680.
  5. CLOB data type,by oracle.
  6. Oracle的CLOB大数据字段类型,by Grand-Jon.