- 原理
- 客户端向服务器端发送连接请求,监听器监听到客户端德连接请求
- 监听器监听到客户端德连接请求后,把客户端德连接请求交给数据库服务器处理
- 客户端和服务器端建立连接,连接建立后,服务器端和客户端直接通信,而不再需要监听器的参与。
监听器启动会读取listener.ora文件
- LISTENER 是监听的名称(可以自定义)
- SID_NAME监听器监听的数据库实例名
- PROTOCOL 是监听器监听的协议
- HOST 指定数据库服务器的主机名或IP地址
- PORT 监听器指定的端口号
[oracle@prod1 admin]$ more listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.137.127)(PORT = 1521))
)
)
SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME=beijing)
(SID_NAME=PROD)
(ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1)
)
)
TNS文件
- SERVER指定连接请求的服务类型是专有服务器连接(dedicated)还是共享服务器连接(shared)
[oracle@prod1 admin]$ more tnsnames.ora
bj=
(DESCRIPTION=
(ADDRESS= (PROTOCOL=tcp)(HOST=prod1)(PORT=1521))
(CONNECT_DATA=
(SERVICE_NAME=beijing)
(SERVER=dedicated)
)
)
登录远程数据库方式
#查看监听状态
[oracle@prod1 admin]$ lsnrctl status
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 19-FEB-2015 18:49:04
Copyright (c) 1991, 2009, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.137.127)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production
Start Date 19-FEB-2015 11:45:07
Uptime 0 days 7 hr. 3 min. 56 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/prod1/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.137.127)(PORT=1521)))
Services Summary...
Service "beijing" has 1 instance(s).
Instance "PROD", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
使用连接字符串登录
[oracle@prod1 admin]$ sqlplus system/beijing@192.168.137.127:1521/beijing
使用网络服务名登录
#根据tns文件
bj=
(DESCRIPTION=
(ADDRESS= (PROTOCOL=tcp)(HOST=prod1)(PORT=1521))
(CONNECT_DATA=
(SERVICE_NAME=beijing)
(SERVER=dedicated)
)
)
[oracle@prod1 admin]$ sqlplus system/beijing@bj
监听常见的错误
1
TNS-12541: TNS:no listener
- 可能原因:
- 客户端指定的目标主机IP有误
- 客户端指定的端口号有误
- 客户端指定的SID有误
- 服务器端监听没开启
#查看服务器端监听状态,如果是如下显示
[oracle@prod1 admin]$ lsnrctl status
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 19-FEB-2015 20:33:21
Copyright (c) 1991, 2009, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.137.127)(PORT=1521)))
TNS-12541: TNS:no listener
TNS-12560: TNS:protocol adapter error
TNS-00511: No listener
Linux Error: 111: Connection refused
#开启监听
[oracle@prod1 admin]$ lsnrctl start
2
[oracle@prod1 dbs]$ sqlplus sys/beijing@bj as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Thu Feb 19 20:49:38 2015
Copyright (c) 1982, 2009, Oracle. All rights reserved.
ERROR:
ORA-01031: insufficient privileges
出现这个错误说明oracle试图去找口令文件,但是找不到
解决方法
- file 指定口令文件的名称及路径
- password 指定用户sys的口令
- entries 指定最多可以有多少个超级用户(具有sysdba权限)
- force 是否覆盖已经存在的口令文件
#生成口令文件
[oracle@prod1 dbs]$ orapwd file='/u01/app/oracle/product/11.2.0/db_1/dbs/orapwPROD' password=beijing entries=5 force=y
#SYSDB为true,说明sys已经有了sysdba权限
sys@PROD> select * from v$pwfile_users;
USERNAME SYSDB SYSOP SYSAS
------------------------------ ----- ----- -----
SYS TRUE TRUE FALSE
#重新连接
[oracle@prod1 dbs]$ sqlplus sys/beijing@bj as sysdba
3
禁止以操作系统认证方式登录oracle
[oracle@prod1 admin]$ vi sqlnet.ora
SQLNET.AUTHENTICATION_SERVICES=(NTS)