1. 程式人生 > >Oracle基礎:2: sqlplus連線方式

Oracle基礎:2: sqlplus連線方式

在這裡插入圖片描述
通過sqlplus可以連線資料庫根據使用者許可權進行資料或者設定操作,這篇文章介紹一下常用的連線方式。

環境準備

使用Oracle的精簡版建立docker方式的demo環境,詳細可參看:

方式1(本機): / as sysdba

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

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

方式2(本機): sqlplus 使用者名稱/密碼

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

[email protected]:~$ sqlplus system/liumiao123

SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 08:21:27 2018

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> show user
USER is "SYSTEM"
SQL>

方式3: 通過tnsname方式

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

確認tns連線通暢

[email protected]:~$ tnsping XE

TNS Ping Utility for Linux: Version 11.2.0.2.0 - Production on 21-OCT-2018 10:32:55

Copyright (c) 1997, 2011, Oracle.  All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = e871d42341c0)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))
OK (0 msec)
[email protected]:~$

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

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

連線

[email protected]:~$ sqlplus system/[email protected]

SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 10:34:04 2018

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> show user
USER is "SYSTEM"
SQL> 

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

還可通過IP和port的方式定位Oracle例項進行連線:sqlplus 使用者名稱/密碼@//IP地址或者hostname:埠號/Oracle例項名

# netstat -tunlp |grep 1521
tcp6       0      0 :::1521                 :::*                    LISTEN      -               
# ip ad |grep 172.17
    inet 172.17.0.2/16 scope global eth0
# sqlplus system/[email protected]//172.17.0.2:1521/XE

SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 10:37:31 2018

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> 

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

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

# sqlplus /nolog

SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 11:19:50 2018

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

SQL> connect system/liumiao123
Connected.
SQL> connect system/[email protected]
Connected.
SQL> connect system/[email protected]//172.17.0.2:1521/XE
Connected.
SQL> show user
USER is "SYSTEM"
SQL>