在 Linux 環境下以 sqlplus 啟動資料庫時,遭遇錯誤:
ORA-12154: TNS:could not resolve the connect identifier specified
或
ORA-12541: TNS:no listener
核心原因通常是 環境變數未正確載入,導致 ORACLE_SID、ORACLE_HOME 未設定,或監聽器未啟動。
$ su - oracle
使用 su -(帶減號)可載入使用者的登入環境(.bash_profile/.profile)。
$ echo $ORACLE_SID
若無輸出或顯示錯誤 SID,表示環境變數未設定或未載入。
$ cd ~
$ source .bash_profile
或直接執行 . .bash_profile。
$ echo $ORACLE_SID
$ echo $ORACLE_HOME
$ echo $PATH | grep $ORACLE_HOME
確認三者皆正確顯示。若仍異常,編輯 .bash_profile 手動補齊:
export ORACLE_SID=orcl
export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
$ lsnrctl start
成功訊息:
LSNRCTL for Linux: Version 19.0.0.0.0 - Production
Starting /u01/app/oracle/product/19.0.0/dbhome_1/bin/tnslsnr: please wait...
TNSLSNR for Linux: Version 19.0.0.0.0 - Production
...
The listener supports no services
The command completed successfully
註:資料庫實例啟動後,PMON 會自動向監聽器註冊服務。
$ sqlplus /nolog
SQL> CONNECT / AS SYSDBA
SQL> STARTUP
正常輸出:
ORACLE instance started.
Total System Global Area 1610612736 bytes
Fixed Size 8896512 bytes
Variable Size 520093696 bytes
Database Buffers 1073741824 bytes
Redo Buffers 7880704 bytes
Database mounted.
Database opened.
$ lsnrctl status
確認輸出包含 Service "orcl" has 1 instance(s)。
$ sqlplus scott/tiger@//localhost:1521/orcl
| 情況 | 原因 | 處理 |
|---|---|---|
ORA-01031: insufficient privileges | 非 dba 群組用戶執行 startup | 確認用戶屬於 dba 群組:groups oracle |
ORA-00845: MEMORY_TARGET not supported | /dev/shm 空間不足 | 掛載較大 tmpfs 或改用 SGA_TARGET |
| 監聽器啟動失敗 | Port 1521 衝突或權限不足 | netstat -an | grep 1521 排查;root 啟動或調整防火牆 |
| 環境變數每次登入皆遺失 | .bash_profile 未正確設定 | 編輯 ~/.bash_profile 永久生效 |
將上述流程封裝為 Shell 腳本 start_oracle.sh,並納入系統開機啟動(systemd 或 /etc/rc.local),實現無人值守啟動。