今天组里同事问:

怎么重启指定端口,lsnrctl reload 1521 这样

也就是想重启指定端口的监听,但是他发的是 lsnrctl reload 1521 这个 1521 明显是个端口,在我的记忆里 lsnrctl <commadn> 后面是跟 监听名称的,他这 1521 明显是个端口号。其实不是在记忆里,在文档里,就是监听名称,文档如下:

1.1 Listener Control Utility Overview

1.1 Listener Control Utility Overview

The Listener Control utility enables you to administer listeners. To perform basic management functions on one or more listeners, you can use the Listener Control utility commands. You can also view and change parameter settings.

The basic syntax of Listener Control utility commands is as follows:

1
lsnrctl command listener_name

In the preceding command, listener_name is the name of the listener that you want to administer. If you do not specify a specific listener in the command string, then the command is directed to the default listener name, LISTENER.

image-20241108064342071

也就是 listener_name 是监听的名称,如果不指定就是默认的 LISTENER

那如果要重启指定端口的监听如何操作呢,那很显然是要找的对应端口的监听名,这个如果查找呢,如果监听在运行着,可以通过ps 命令来查找,这里你要重启,那说明监听肯定在运行着,ps 命令如下: ps -ef |grep tns

1
2
3
4
[oracle@liups ~]$ ps -ef |grep tns
root 29 2 0 Oct31 ? 00:00:00 [netns]
oracle 14991 1 0 06:10 ? 00:00:00 /data/app/oracle/product/19.3.0.0/dbhome_1/bin/tnslsnr LISTENER -inherit
oracle 18099 1 0 06:48 ? 00:00:00 /data/app/oracle/product/19.3.0.0/dbhome_1/bin/tnslsnr LISTENER_19C -inherit

可以看到监听进程对应的名称分别为:LISTENERLISTENER_19C

这里只有名称,但是看不到对应的端口,通过名称也无法知道端口,怎么确认端口呢,通过 status 命令查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[oracle@liups ~]$ lsnrctl status LISTENER

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 08-NOV-2024 06:50:30

Copyright (c) 1991, 2019, Oracle. All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=liups)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date 08-NOV-2024 06:10:36
Uptime 0 days 0 hr. 39 min. 54 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /data/app/oracle/product/19.3.0.0/dbhome_1/network/admin/listener.ora
Listener Log File /data/app/oracle/diag/tnslsnr/liups/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=liups)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "ora19cl" has 1 instance(s).
Instance "ora19cl", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

这里可以看到监听的信息包括端口等。

如果监听没有在运行如何确认呢,那可以通过配置文件来看。通过上面的status 可以看到本机的监听文件为:/data/app/oracle/product/19.3.0.0/dbhome_1/network/admin/listener.ora

查看此文件即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[oracle@liups ~]$ cat /data/app/oracle/product/19.3.0.0/dbhome_1/network/admin/listener.ora
# listener.ora Network Configuration File: /data/app/oracle/product/19.3.0.0/dbhome_1/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER_19C =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = liups)(PORT = 1519))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1519))
)
)

LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = liups)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = ora19cl)
(ORACLE_HOME = /data/app/oracle/product/19.3.0.0/dbhome_1)
(SID_NAME = ora19cl)
)
)

可以看到对应的监听名称和端口信息。

那要重启指定端口的监听,比如要重启本例子中的 1519 端口的监听,就可以通过以下命令重启了。实际上是reload,并不是重启。

1
lsnrctl reload LISTENER_19C

关于 reload 命令相见官方文档:https://docs.oracle.com/en/database/oracle/oracle-database/19/netrf/listener-control-utility.html#GUID-1BE648E7-3AB8-4DE2-97DA-E136DF6F4280

其实这个例子比较有趣的是,如果执行 lsnrctl reload 1521 会发生什么?

刚看到这个命令的时候,我想肯定是报 监听名称不存在的。实际上呢,并不是。实际的报错如下:

1
2
3
4
5
6
7
8
9
10
11
12
[oracle@liups ~]$ lsnrctl reload 1521

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 08-NOV-2024 06:55:43

Copyright (c) 1991, 2019, Oracle. All rights reserved.

Connecting to (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=tcp)(HOST=0.0.5.241)(PORT=1521)))
TNS-12532: TNS:invalid argument
TNS-12560: TNS:protocol adapter error
TNS-00502: Invalid argument
Linux Error: 22: Invalid argument
[oracle@liups ~]$

这里报了 TNS 的一些信息,这通常跟网络有关系,仔细查看发现输出的 host 是 HOST=0.0.5.241,这个IP有点奇怪,这里立马就想到了了是把数字转换为了IP地址,大家都知道 int 类型可以转换为IP的,如果不知道,我这里告诉你,是可以的,早期MySQL在存放 IP地址是用 int类型还是varchar 类型,曾经有过很长时间的争辩。这里找到一个数字转IP的网站进行转换:

image-20241108070252351

果然,1521 转换为 IP地址就是上面的 HOST 的地址:0.0.5.241

你说神奇不神奇。

查了下官方文档,没找到这个特性,我这里进行了如下测试:

将我本地的 IP 转换为数字之后为:167778308,执行 lsnrctl status 167778308 竟然是可以的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[oracle@liups ~]$ lsnrctl status 167778308

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 08-NOV-2024 07:05:12

Copyright (c) 1991, 2019, Oracle. All rights reserved.

Connecting to (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=tcp)(HOST=10.0.24.4)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date 08-NOV-2024 06:10:36
Uptime 0 days 0 hr. 54 min. 36 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /data/app/oracle/product/19.3.0.0/dbhome_1/network/admin/listener.ora
Listener Log File /data/app/oracle/diag/tnslsnr/liups/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=liups)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "ora19cl" has 1 instance(s).
Instance "ora19cl", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

我在后面再加上监听名称的时候报错了,提示只能是0-1个参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[oracle@liups ~]$  lsnrctl status 167778308  LISTENER_19C

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 08-NOV-2024 07:06:18

Copyright (c) 1991, 2019, Oracle. All rights reserved.

NL-00857: wrong number (0 - 1 needed) of arguments to "status"
[oracle@liups ~]$ lsnrctl status 167778308 LISTENER

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 08-NOV-2024 07:06:25

Copyright (c) 1991, 2019, Oracle. All rights reserved.

NL-00857: wrong number (0 - 1 needed) of arguments to "status"
[oracle@liups ~]$

也就是 lsnrctl 的参数是0-1。

没用的知识又增加了:

也就是 lsnrctl 后面不光可以跟监听名称,也可以是数字,但是这个数字并不是端口的意思,而是把数字转换为了IP地址。我试过也可以是IP。

1
2
3
4
5
6
7
8
9
10
11
12
13
[oracle@liups ~]$  lsnrctl status 127.0.0.1

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 08-NOV-2024 07:09:04

Copyright (c) 1991, 2019, Oracle. All rights reserved.

Connecting to (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date 08-NOV-2024 06:10:36
Uptime 0 days 0 hr.

参考资料:

Listener Control Utility

原文作者: liups.com

原文链接: http://liups.com/posts/9d1fb721/

许可协议: 知识共享署名-非商业性使用 4.0 国际许可协议