Linux环境下使用lsnrctl修改监听器端口的步骤
$ORACLE_HOME、$PATH)已正确配置(可通过echo $ORACLE_HOME验证);在终端输入以下命令,进入lsnrctl交互式命令行界面:
lsnrctl
在lsnrctl控制台中,输入STATUS命令,查看监听器的当前配置(包括端口、协议、主机等信息)。示例输出:
LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 01-NOV-2025 10:00:00
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date 01-NOV-2025 09:30:00
Uptime 0 days 0 hr. 30 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/19c/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/localhost/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521)))
Services Summary...
Service "orcl" has 1 instance(s).
Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully
(注:上述输出中,PORT=1521为当前监听端口)
若服务器配置了多个监听器(如LISTENER、LISTENER2),需通过SET CURRENT_LISTENER命令指定要修改的监听器。例如:
SET CURRENT_LISTENER LISTENER
在lsnrctl控制台中,输入MODIFY命令修改端口。语法如下:
MODIFY LISTENER
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=tcp)(HOST=your_host_name)(PORT=new_port_number))
)
your_host_name替换为服务器主机名或IP地址(如localhost、192.168.1.100);new_port_number替换为目标端口号(如1522,需确保端口未被占用)。示例(将端口修改为1522):
MODIFY LISTENER
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1522))
)
执行后,若返回The command completed successfully,则表示修改成功。
修改完成后,输入SAVE_CONFIG命令将更改保存到监听器配置文件(listener.ora)中。示例:
SAVE_CONFIG
返回Saved LISTENER configuration to file '/u01/app/oracle/product/19c/network/admin/listener.ora',表示配置已保存。
输入EXIT命令退出lsnrctl:
EXIT
通过以下命令重启监听器(推荐使用RESTART命令,无需手动停止再启动):
lsnrctl restart
或分步执行:
lsnrctl stop
lsnrctl start
lsnrctl status,查看Listening Endpoints Summary中的PORT是否已更新为新端口;telnet命令测试新端口是否可达(从另一台服务器执行):telnet your_host_name new_port_number
若连接成功,表示端口已生效。若服务器开启了防火墙(如firewalld、iptables),需允许新端口的流量通过。以firewalld为例:
sudo firewall-cmd --zone=public --add-port=new_port_number/tcp --permanent
sudo firewall-cmd --reload
listener.ora文件(路径:$ORACLE_HOME/network/admin/listener.ora);