前幾天遇到了ORA-12154&TNS-03505這個錯誤, 因為對其數據庫環境不瞭解, 遠程登錄過去檢查的時候, 瞭解到一些大概情況:此數據庫安裝在Windows服務器.遠程客户端能登錄數據庫,但是本地服務器使用普通賬號登錄數據庫時報ORA-12154錯誤,如下所示:
C:\Users\Administrator>sqlplus <user_name>/<passwod>@test
SQL*Plus: Release 19.0.0.0.0 - Production on Thu Oct 16 08:35:04 2025
Version 19.23.0.0.0
Copyright (c) 1982, 2023, Oracle. All rights reserved.
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified
Enter user-name:
使用tnsping檢查報TNS-03505錯誤,如下所示:
C:\Users\Administrator>tnsping test
TNS Ping Utility for 64-bit Windows: Version 19.0.0.0.0 - Production on 14-OCT-2025 17:04:05
Copyright (c) 1997, 2024, Oracle. All rights reserved.
Used parameter files:
D:\App\Oracle19c\product\19.3\db_1\network\admin\sqlnet.ora
TNS-03505: Failed to resolve name
檢查D:\App\Oracle19c\product\19.3\db_1\network\admin\tnsnames.ora中的網絡服務名test的配置信息, 完全正確.沒有任何問題.然後仔細檢查,發現Windows環境變量中有兩個Oracle Client的配置(當時不瞭解這些情況),具體如下所示:
C:\Users\Administrator>echo %path%
D:\App\Oracle19c\product\19.3\db_1\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;D:\Nuzarsurf\oracle_client\x64;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;
檢查後發現這台機器上其實不僅僅安裝了Oracle數據庫軟件,還安裝一個Oracle instance client(D:\Nuzarsurf\oracle_client\x64), 由於環境變量的順序設置,決定了其優先級,此時讀取的是Oracle instance client下的網絡服務名,而這位朋友在(D:\App\Oracle19c\product\19.3\db_1\network\admin\tnsnames.ora)這個路徑下面的tnsnames.ora設置的網絡服務名,其實此時沒有生效,其實要麼在(D:\Nuzarsurf\oracle_client\x64)路徑中的tnsnames.ora設置網絡服務名,可以解決這個問題.也可以通過調整PATH的先後順序解決這個問題. 如下所示:
我的電腦->屬性->高級->環境變量,找到PATH變量進行順序調整
另外,出現ORA-12154 & TNS-03505還有其它原因,2016年曾經總結過一篇ORA-12154 & TNS-03505 案例分享. 相同錯誤,不同案例. 這裏就不重複展開了。