本文档风哥主要介绍Oracle ORA-12154 TNS解析错误相关知识,包括ORA-12154的概念、ORA-12154的常见原因、ORA-12154分析方法、ORA-12154错误分析配置、ORA-12154错误监控、ORA-12154错误故障处理等内容,由风哥教程参考Oracle官方文档Troubleshooting内容编写,适合DBA人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。
Part01-基础概念与理论知识
1.1 ORA-12154的概念
Oracle ORA-12154是Oracle数据库的TNS解析错误,表示无法解析指定的连接标识符。ORA-12154错误通常发生在TNS配置错误或TNS名称不存在时。ORA-12154错误需要检查TNS配置和网络配置。更多视频教程www.fgedu.net.cn
- TNS解析错误:无法解析连接标识符
- TNS配置:TNS配置错误
- TNS名称:TNS名称不存在
- 连接失败:无法连接数据库
- 需要检查:需要检查TNS配置
1.2 ORA-12154的常见原因
Oracle ORA-12154的常见原因:
- TNS配置错误:TNS配置错误
- TNS名称不存在:TNS名称不存在
- TNS文件路径错误:TNS文件路径错误
- TNS文件格式错误:TNS文件格式错误
- 环境变量错误:环境变量错误
- 网络配置错误:网络配置错误
- 权限问题:TNS文件权限问题
1.3 ORA-12154分析方法
Oracle ORA-12154分析方法:
- 错误信息分析:分析错误信息含义
- TNS配置分析:分析TNS配置
- TNS文件分析:分析TNS文件
- 环境变量分析:分析环境变量
- 网络配置分析:分析网络配置
- 权限分析:分析文件权限
Part02-生产环境规划与建议
2.1 ORA-12154错误处理规划
Oracle ORA-12154错误处理规划要点:
– 错误监控:监控ORA-12154错误发生
– 错误记录:记录ORA-12154错误信息
– 错误分析:分析ORA-12154错误原因
– 错误处理:处理ORA-12154错误问题
# 错误监控规划
– 监控工具:选择监控工具
– 监控频率:设置监控频率
– 监控范围:定义监控范围
– 告警配置:配置告警规则
# 错误记录规划
– 记录方式:选择记录方式
– 记录格式:定义记录格式
– 记录保留:设置记录保留时间
– 记录备份:备份错误记录
# 错误分析规划
– 分析方法:选择分析方法
– 分析工具:选择分析工具
– 分析流程:定义分析流程
– 分析报告:生成分析报告
# 错误处理规划
– 处理流程:定义处理流程
– 处理优先级:设置处理优先级
– 处理责任人:指定处理责任人
– 处理验证:验证处理结果
2.2 ORA-12154错误分析工具
Oracle ORA-12154错误分析工具:
- SQL*Plus:命令行工具
- SQL Developer:图形化工具
- Enterprise Manager:企业级管理工具
- tnsping:TNS解析测试工具
- ping:网络连通性测试工具
- netstat:网络状态查看工具
- 操作系统命令:操作系统命令
– SQL*Plus:快速查询错误信息
– SQL Developer:图形化分析错误
– Enterprise Manager:集中管理错误
– tnsping:测试TNS解析
– ping:测试网络连通性
– netstat:查看网络状态
– 操作系统命令:检查TNS配置
2.3 ORA-12154错误处理最佳实践
Oracle ORA-12154错误处理最佳实践:
- 定期监控:定期监控ORA-12154错误
- 及时处理:及时处理ORA-12154错误
- 文档记录:记录错误处理过程
- 经验积累:积累错误处理经验
- 预防措施:制定预防措施
Part03-生产环境项目实施方案
3.1 ORA-12154错误分析配置
3.1.1 配置TNS管理
$ cat /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
FGEDUDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = FGEDUDB)
)
)
# 2. 查看SQLNET配置文件
$ cat /oracle/app/oracle/product/19c/dbhome_1/network/admin/sqlnet.ora
NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
SQLNET.AUTHENTICATION_SERVICES= (NTS)
# 3. 查看环境变量
$ echo $TNS_ADMIN
/oracle/app/oracle/product/19c/dbhome_1/network/admin
$ echo $ORACLE_HOME
/oracle/app/oracle/product/19c/dbhome_1
$ echo $ORACLE_SID
FGEDUDB
# 4. 测试TNS解析
$ tnsping FGEDUDB
TNS Ping Utility for Linux: Version 19.0.0.0.0 – Production on 31-MAR-2026 10:00:00
Copyright (c) 1997, 2020, Oracle. All rights reserved.
Used parameter files:
/oracle/app/oracle/product/19c/dbhome_1/network/admin/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = FGEDUDB)))
OK (0 msec)
# 5. 查看TNS文件权限
$ ls -la /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
-rw-r–r– 1 oracle oinstall 1234 Mar 31 10:00 /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
# 6. 查看监听器状态
$ lsnrctl status
LSNRCTL for Linux: Version 19.0.0.0.0 – Production on 31-MAR-2026 10:00:00
Copyright (c) 1991, 2020, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.100)(PORT=1521)))
STATUS of the LISTENER
————————
Alias LISTENER
Version TNSLSNR for Linux: Version 19.0.0.0.0 – Production
Start Date 31-MAR-2026 09:00:00
Uptime 1 days 1 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /oracle/app/oracle/product/19c/dbhome_1/network/admin/listener.ora
Listener Log File /oracle/app/oracle/diag/tnslsnr/hostname/listener/alert/log.xml
Listening Endpoints Summary…
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.1.100)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary…
Service “FGEDUDB” has 1 instance(s).
Instance “FGEDUDB”, status READY, has 1 handler(s) for this service…
Service “FGEDUDBXDB” has 1 instance(s).
Instance “FGEDUDB”, status READY, has 1 handler(s) for this service…
The command completed successfully
3.1.2 配置TNS修复
$ sqlplus scott/tiger@WRONGDB
SQL*Plus: Release 19.0.0.0.0 – Production on Mon Mar 31 10:00:00 2026
Copyright (c) 1982, 2020, Oracle. All rights reserved.
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified
# 2. 查看TNS配置文件
$ cat /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
FGEDUDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = FGEDUDB)
)
)
# 3. 分析错误原因
# 错误代码:ORA-12154
# 错误信息:TNS:could not resolve the connect identifier specified
# 连接标识符:WRONGDB
# 错误原因:TNS名称不存在
# 4. 解决方案:使用正确的TNS名称
$ sqlplus scott/tiger@FGEDUDB
SQL*Plus: Release 19.0.0.0.0 – Production on Mon Mar 31 10:00:00 2026
Copyright (c) 1982, 2020, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 – Production
Version 19.3.0.0.0
SQL> select instance_name, status, database_status, startup_time
from v$instance;
INSTANCE_NAME STATUS DATABASE_STATUS STARTUP_TIME
—————- ———— —————– ——————-
FGEDUDB OPEN ACTIVE 31-MAR-26 09:00:00
# 5. 添加新的TNS配置
$ vi /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
FGEDUDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = FGEDUDB)
)
)
TESTDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.101)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = TESTDB)
)
)
# 6. 验证TNS配置
$ tnsping TESTDB
TNS Ping Utility for Linux: Version 19.0.0.0.0 – Production on 31-MAR-2026 10:00:00
Copyright (c) 1997, 2020, Oracle. All rights reserved.
Used parameter files:
/oracle/app/oracle/product/19c/dbhome_1/network/admin/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.101)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = TESTDB)))
OK (0 msec)
3.2 ORA-12154错误监控
3.2.1 配置ORA-12154错误监控脚本
$ vi /home/oracle/scripts/monitor_ora12154.sh
#!/bin/bash
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# ORA-12154错误监控脚本
export ORACLE_HOME=/oracle/app/oracle/product/19c/dbhome_1
export ORACLE_SID=FGEDUDB
export PATH=$ORACLE_HOME/bin:$PATH
# 定义变量
ERROR_LOG=”/home/oracle/scripts/ora12154_errors.log”
EMAIL=”admin@fgedu.net.cn”
TNS_NAMES=”FGEDUDB TESTDB”
# 检查ORA-12154错误
check_ora12154_errors() {
local last_check_file=”/home/oracle/scripts/last_ora12154_check.txt”
local current_time=$(date +%s)
if [ -f “$last_check_file” ]; then
local last_check=$(cat “$last_check_file”)
local time_diff=$((current_time – last_check))
if [ $time_diff -lt 3600 ]; then
echo “Last check was $time_diff seconds ago, skipping…”
return 0
fi
fi
# 测试TNS解析
for tns_name in $TNS_NAMES; do
local tnsping_result=$(tnsping $tns_name 2>&1)
if echo “$tnsping_result” | grep -q “TNS-“; then
echo “Found TNS error for $tns_name:” >> “$ERROR_LOG”
echo “$tnsping_result” >> “$ERROR_LOG”
echo “Timestamp: $(date)” >> “$ERROR_LOG”
echo “” >> “$ERROR_LOG”
# 发送告警邮件
echo “TNS error detected for $tns_name” | mail -s “ORA-12154 Alert” “$EMAIL”
# 分析TNS配置
analyze_tns_config
fi
done
# 更新最后检查时间
echo “$current_time” > “$last_check_file”
}
# 分析TNS配置
analyze_tns_config() {
local tns_config=$(cat /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora)
echo “TNS Configuration:” >> “$ERROR_LOG”
echo “$tns_config” >> “$ERROR_LOG”
echo “” >> “$ERROR_LOG”
}
# 主函数
main() {
check_ora12154_errors
echo “ORA-12154 error monitoring completed at $(date)” >> “$ERROR_LOG”
}
# 执行主函数
main
# 2. 设置脚本权限
$ chmod +x /home/oracle/scripts/monitor_ora12154.sh
# 3. 测试脚本
$ /home/oracle/scripts/monitor_ora12154.sh
# 4. 设置定期监控
$ crontab -e
# 每小时检查ORA-12154错误
0 * * * * /home/oracle/scripts/monitor_ora12154.sh >> /home/oracle/scripts/monitor_ora12154.log 2>&1
3.2.2 配置数据库监控
SQL> create or replace view ora12154_error_monitor as
select
to_char(timestamp, ‘YYYY-MM-DD HH24:MI:SS’) as error_time,
error_code,
error_message,
tns_name,
username,
osuser,
machine,
program
from (
select
to_date(substr(message, 1, 19), ‘YYYY-MM-DD HH24:MI:SS’) as timestamp,
substr(message, instr(message, ‘ORA-12154’), 9) as error_code,
substr(message, instr(message, ‘ORA-12154’), instr(message, chr(10)) – instr(message, ‘ORA-12154’)) as error_message,
null as tns_name,
null as username,
null as osuser,
null as machine,
null as program
from (
select column_value as message
from xmltable((‘”‘ || replace(
dbms_metadata.get_ddl(‘TABLE’, ‘DUMMY’),
chr(10),
‘” “‘
) || ‘”‘))
)
);
View created.
# 2. 创建TNS配置监控视图
SQL> create or replace view tns_config_monitor as
select
‘FGEDUDB’ as tns_name,
‘192.168.1.100’ as host,
‘1521’ as port,
‘FGEDUDB’ as service_name
from dual
union all
select
‘TESTDB’ as tns_name,
‘192.168.1.101’ as host,
‘1521’ as port,
‘TESTDB’ as service_name
from dual;
View created.
# 3. 查询TNS配置
SQL> select * from tns_config_monitor;
TNS_NAME HOST PORT SERVICE_NAME
——— ————— —- ————
FGEDUDB 192.168.1.100 1521 FGEDUDB
TESTDB 192.168.1.101 1521 TESTDB
# 4. 创建ORA-12154错误告警
SQL> create or replace procedure ora12154_error_alert as
v_error_count number;
v_email varchar2(100) := ‘admin@fgedu.net.cn’;
begin
— 检查ORA-12154错误
select count(*) into v_error_count
from ora12154_error_monitor
where error_time > sysdate – 1;
if v_error_count > 0 then
— 发送告警
utl_smtp.open_connection(
host => ‘smtp.fgedu.net.cn’,
port => 25,
c => v_conn
);
utl_smtp.helo(v_conn, ‘localhost’);
utl_smtp.mail(v_conn, ‘oracle@fgedu.net.cn’);
utl_smtp.rcpt(v_conn, v_email);
utl_smtp.open_data(v_conn);
utl_smtp.write_data(v_conn, ‘Subject: ORA-12154 Error Alert’ || utl_tcp.crlf);
utl_smtp.write_data(v_conn, ‘To: ‘ || v_email || utl_tcp.crlf);
utl_smtp.write_data(v_conn, utl_tcp.crlf);
utl_smtp.write_data(v_conn, ‘ORA-12154 errors detected: ‘ || v_error_count || utl_tcp.crlf);
utl_smtp.close_data(v_conn);
utl_smtp.quit(v_conn);
end if;
end ora12154_error_alert;
/
Procedure created.
# 5. 创建定时任务
SQL> begin
dbms_scheduler.create_job(
job_name => ‘ORA12154_ERROR_ALERT_JOB’,
job_type => ‘STORED_PROCEDURE’,
job_action => ‘ORA12154_ERROR_ALERT’,
start_date => systimestamp,
repeat_interval => ‘FREQ=HOURLY;INTERVAL=1’,
enabled => true
);
end;
/
PL/SQL procedure successfully completed.
3.3 ORA-12154错误故障处理
3.3.1 ORA-12154错误处理
$ sqlplus scott/tiger@WRONGDB
SQL*Plus: Release 19.0.0.0.0 – Production on Mon Mar 31 10:00:00 2026
Copyright (c) 1982, 2020, Oracle. All rights reserved.
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified
# 分析步骤
# 1. 查看TNS配置文件
$ cat /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
FGEDUDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = FGEDUDB)
)
)
# 2. 查看环境变量
$ echo $TNS_ADMIN
/oracle/app/oracle/product/19c/dbhome_1/network/admin
$ echo $ORACLE_HOME
/oracle/app/oracle/product/19c/dbhome_1
# 3. 测试TNS解析
$ tnsping WRONGDB
TNS Ping Utility for Linux: Version 19.0.0.0.0 – Production on 31-MAR-2026 10:00:00
Copyright (c) 1997, 2020, Oracle. All rights reserved.
Used parameter files:
/oracle/app/oracle/product/19c/dbhome_1/network/admin/sqlnet.ora
TNS-03505: Failed to resolve name
# 4. 分析错误原因
# 错误代码:ORA-12154
# 错误信息:TNS:could not resolve the connect identifier specified
# 连接标识符:WRONGDB
# 错误原因:TNS名称不存在
# 5. 解决方案:使用正确的TNS名称
$ sqlplus scott/tiger@FGEDUDB
SQL*Plus: Release 19.0.0.0.0 – Production on Mon Mar 31 10:00:00 2026
Copyright (c) 1982, 2020, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 – Production
Version 19.3.0.0.0
SQL> select instance_name, status, database_status, startup_time
from v$instance;
INSTANCE_NAME STATUS DATABASE_STATUS STARTUP_TIME
—————- ———— —————– ——————-
FGEDUDB OPEN ACTIVE 31-MAR-26 09:00:00
# 6. 预防措施
# – 定期检查TNS配置
# – 使用正确的TNS名称
# – 配置TNS名称验证
# – 定期测试TNS解析
3.3.2 TNS配置修复
$ cat /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
FGEDUDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = FGEDUDB)
)
)
# 2. 检查TNS文件权限
$ ls -la /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
-rw-r–r– 1 oracle oinstall 1234 Mar 31 10:00 /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
# 3. 修改TNS文件权限
$ chmod 644 /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
# 4. 验证TNS文件权限
$ ls -la /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
-rw-r–r– 1 oracle oinstall 1234 Mar 31 10:00 /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
# 5. 测试TNS解析
$ tnsping FGEDUDB
TNS Ping Utility for Linux: Version 19.0.0.0.0 – Production on 31-MAR-2026 10:00:00
Copyright (c) 1997, 2020, Oracle. All rights reserved.
Used parameter files:
/oracle/app/oracle/product/19c/dbhome_1/network/admin/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = FGEDUDB)))
OK (0 msec)
# 6. 测试数据库连接
$ sqlplus scott/tiger@FGEDUDB
SQL*Plus: Release 19.0.0.0.0 – Production on Mon Mar 31 10:00:00 2026
Copyright (c) 1982, 2020, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 – Production
Version 19.3.0.0.0
SQL> select instance_name, status, database_status, startup_time
from v$instance;
INSTANCE_NAME STATUS DATABASE_STATUS STARTUP_TIME
—————- ———— —————– ——————-
FGEDUDB OPEN ACTIVE 31-MAR-26 09:00:00
Part04-生产案例与实战讲解
4.1 ORA-12154错误分析案例
在生产环境中分析ORA-12154错误的完整案例:
4.1.1 场景描述
某企业生产数据库出现ORA-12154 TNS解析错误,需要分析错误原因并解决问题。
4.1.2 分析步骤
$ sqlplus scott/tiger@WRONGDB
SQL*Plus: Release 19.0.0.0.0 – Production on Mon Mar 31 10:00:00 2026
Copyright (c) 1982, 2020, Oracle. All rights reserved.
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified
# 2. 查看TNS配置文件
$ cat /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
FGEDUDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = FGEDUDB)
)
)
# 3. 查看环境变量
$ echo $TNS_ADMIN
/oracle/app/oracle/product/19c/dbhome_1/network/admin
$ echo $ORACLE_HOME
/oracle/app/oracle/product/19c/dbhome_1
# 4. 测试TNS解析
$ tnsping WRONGDB
TNS Ping Utility for Linux: Version 19.0.0.0.0 – Production on 31-MAR-2026 10:00:00
Copyright (c) 1997, 2020, Oracle. All rights reserved.
Used parameter files:
/oracle/app/oracle/product/19c/dbhome_1/network/admin/sqlnet.ora
TNS-03505: Failed to resolve name
# 5. 分析错误原因
# 错误代码:ORA-12154
# 错误信息:TNS:could not resolve the connect identifier specified
# 连接标识符:WRONGDB
# 错误原因:TNS名称不存在
# 6. 解决方案:使用正确的TNS名称
$ sqlplus scott/tiger@FGEDUDB
SQL*Plus: Release 19.0.0.0.0 – Production on Mon Mar 31 10:00:00 2026
Copyright (c) 1982, 2020, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 – Production
Version 19.3.0.0.0
SQL> select instance_name, status, database_status, startup_time
from v$instance;
INSTANCE_NAME STATUS DATABASE_STATUS STARTUP_TIME
—————- ———— —————– ——————-
FGEDUDB OPEN ACTIVE 31-MAR-26 09:00:00
4.2 ORA-12154错误故障处理
在ORA-12154错误故障处理过程中的方法和技巧:
4.2.1 故障处理流程
# 1. 错误识别
# – 监控TNS解析错误
# – 检查错误信息
# – 确认错误参数
# 2. 错误分析
# – 查看TNS配置
# – 检查环境变量
# – 分析TNS文件
# 3. 错误处理
# – 修正TNS配置
# – 添加TNS名称
# – 修正环境变量
# 4. 错误预防
# – 定期检查TNS配置
# – 使用正确的TNS名称
# – 制定预防措施
# 示例:ORA-12154错误处理
# 1. 错误识别
$ sqlplus scott/tiger@WRONGDB
SQL*Plus: Release 19.0.0.0.0 – Production on Mon Mar 31 10:00:00 2026
Copyright (c) 1982, 2020, Oracle. All rights reserved.
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified
# 2. 错误分析
$ cat /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
FGEDUDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = FGEDUDB)
)
)
# 3. 错误处理
$ sqlplus scott/tiger@FGEDUDB
SQL*Plus: Release 19.0.0.0.0 – Production on Mon Mar 31 10:00:00 2026
Copyright (c) 1982, 2020, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 – Production
Version 19.3.0.0.0
# 4. 验证处理结果
SQL> select instance_name, status, database_status, startup_time
from v$instance;
INSTANCE_NAME STATUS DATABASE_STATUS STARTUP_TIME
—————- ———— —————– ——————-
FGEDUDB OPEN ACTIVE 31-MAR-26 09:00:00
# 5. 错误预防
# – 定期检查TNS配置
# – 使用正确的TNS名称
# – 配置TNS名称验证
# – 定期测试TNS解析
4.3 ORA-12154错误优化
优化ORA-12154错误处理配置的最佳实践:
4.3.1 优化TNS管理
SQL> create or replace procedure optimize_tns as
v_tns_count number;
v_tns_names varchar2(4000);
begin
— 获取TNS名称数量
select count(*) into v_tns_count
from tns_config_monitor;
— 获取TNS名称列表
select listagg(tns_name, ‘, ‘) within group (order by tns_name) into v_tns_names
from tns_config_monitor;
— 输出优化建议
dbms_output.put_line(‘TNS Names Count: ‘ || v_tns_count);
dbms_output.put_line(‘TNS Names: ‘ || v_tns_names);
if v_tns_count = 0 then
dbms_output.put_line(‘Recommendation: Add TNS names to tnsnames.ora’);
end if;
end optimize_tns;
/
Procedure created.
# 2. 执行优化存储过程
SQL> set serveroutput on
SQL> exec optimize_tns;
TNS Names Count: 2
TNS Names: FGEDUDB, TESTDB
PL/SQL procedure successfully completed.
# 3. 应用优化建议
$ vi /oracle/app/oracle/product/19c/dbhome_1/network/admin/tnsnames.ora
FGEDUDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = FGEDUDB)
)
)
TESTDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.101)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = TESTDB)
)
)
PRODDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.102)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = PRODDB)
)
)
# 4. 验证TNS配置
$ tnsping PRODDB
TNS Ping Utility for Linux: Version 19.0.0.0.0 – Production on 31-MAR-2026 10:00:00
Copyright (c) 1997, 2020, Oracle. All rights reserved.
Used parameter files:
/oracle/app/oracle/product/19c/dbhome_1/network/admin/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.102)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = PRODDB)))
OK (0 msec)
# 5. 创建定时任务
SQL> begin
dbms_scheduler.create_job(
job_name => ‘OPTIMIZE_TNS_JOB’,
job_type => ‘STORED_PROCEDURE’,
job_action => ‘OPTIMIZE_TNS’,
start_date => systimestamp,
repeat_interval => ‘FREQ=DAILY;INTERVAL=1’,
enabled => true
);
end;
/
PL/SQL procedure successfully completed.
Part05-风哥经验总结与分享
5.1 ORA-12154错误总结
Oracle ORA-12154错误是TNS解析错误,具有以下特点:
- TNS解析错误:无法解析连接标识符
- TNS配置:TNS配置错误
- TNS名称:TNS名称不存在
- 连接失败:无法连接数据库
- 需要检查:需要检查TNS配置
5.2 ORA-12154错误检查清单
Oracle ORA-12154错误检查清单:
- 错误识别:识别错误信息和参数
- 错误分析:分析TNS配置
- 错误处理:处理错误问题
- 错误验证:验证处理结果
- 错误预防:制定预防措施
- 错误记录:记录处理过程
5.3 ORA-12154错误工具推荐
Oracle ORA-12154错误工具推荐:
- SQL*Plus:命令行工具
- SQL Developer:图形化工具
- Enterprise Manager:企业级管理工具
- tnsping:TNS解析测试工具
- ping:网络连通性测试工具
- netstat:网络状态查看工具
- 操作系统命令:操作系统命令
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
