通過sqlplus可以連接數據庫根據用戶權限進行數據或者設定操作,這篇文章介紹一下常用的連接方式。

環境準備

使用Oracle的精簡版創建docker方式的demo環境。

方式1(本機): / as sysdba

在oracle服務器,可以直接通過操作系統權限認證,使用sysdba方式登陸,前提是你可以登入服務器,并且擁有此權限。

  1. [email protected]:~$ id
  2. uid=1000(oracle) gid=1000(dba) groups=1000(dba)
  3. [email protected]:~$ sqlplus / as sysdba
  4. SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 08:20:51 2018
  5. Copyright (c) 1982, 2011, Oracle. All rights reserved.
  6. Connected to:
  7. Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  8. SQL> show user
  9. USER is "SYS"
  10. SQL>

方式2(本機): sqlplus 用戶名/密碼

在本機除了sysdba,還可以通過用戶名和密碼的方式登陸進來

  1. [email protected]:~$ sqlplus system/liumiao123
  2. SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 08:21:27 2018
  3. Copyright (c) 1982, 2011, Oracle. All rights reserved.
  4. Connected to:
  5. Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  6. SQL> show user
  7. USER is "SYSTEM"
  8. SQL>

方式3: 通過tnsname方式

通過tns設定,保證聯通性的情況下使用 sqlplus 用戶名/密碼@Oracle實例名 的方式進行連接。

確認tns連接通暢

  1. [email protected]:~$ tnsping XE
  2. TNS Ping Utility for Linux: Version 11.2.0.2.0 - Production on 21-OCT-2018 10:32:55
  3. Copyright (c) 1997, 2011, Oracle. All rights reserved.
  4. Used parameter files:
  5. Used TNSNAMES adapter to resolve the alias
  6. Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = e871d42341c0)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))
  7. OK (0 msec)
  8. [email protected]:~$

確認oracle的監聽進程正常啟動

  1. [email protected]:~$ ps -ef |grep lsnr |grep -v grep
  2. oracle   27   1 0 Oct16 ?    00:00:28 /u01/app/oracle/product/11.2.0/xe/bin/tnslsnr LISTENER -inherit
  3. [email protected]:~$

連接

  1. [email protected]:~$ sqlplus system/[email protected]
  2. SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 10:34:04 2018
  3. Copyright (c) 1982, 2011, Oracle. All rights reserved.
  4. Connected to:
  5. Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  6. SQL> show user
  7. USER is "SYSTEM"
  8. SQL>

方式4: IP和port的方式定位

還可通過IP和port的方式定位Oracle實例進行連接:sqlplus 用戶名/密碼@//IP地址或者hostname:端口號/Oracle實例名

  1. # netstat -tunlp |grep 1521
  2. tcp6    0   0 :::1521         :::*          LISTEN   -       
  3. # ip ad |grep 172.17
  4.   inet 172.17.0.2/16 scope global eth0
  5. # sqlplus system/[email protected]//172.17.0.2:1521/XE
  6. SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 10:37:31 2018
  7. Copyright (c) 1982, 2011, Oracle. All rights reserved.
  8. Connected to:
  9. Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
  10. SQL>

方法5: 使用nolog 和 connect實現連接

準確的來說,這種方式和方式2/方式3/方式4沒有本質區別,無非就是用戶名/密碼以及實例名的信息的寫法不同而已,詳細如下:

  1. # sqlplus /nolog
  2. SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 11:19:50 2018
  3. Copyright (c) 1982, 2011, Oracle. All rights reserved.
  4. SQL> connect system/liumiao123
  5. Connected.
  6. SQL> connect system/[email protected]
  7. Connected.
  8. SQL> connect system/[email protected]//172.17.0.2:1521/XE
  9. Connected.
  10. SQL> show user
  11. USER is "SYSTEM"
  12. SQL>

總結

以上就是oracle數據庫通過sqlplus連接的幾種方式的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,