oracle數據庫鏈接及工具使用sqlplus經常使用命令操做手冊 周萬春 微信:loveoracle11g QQ:574344136 當咱們把全部的數據庫安裝過程所有作完後,開始咱們的正式課。 ------------------------------------------------------------------------------------------------------------- [root@zhouwanchun ~]# su - oracle [oracle@zhouwanchun ~]$ ps -elf | grep ora_ | grep orcl 0 S oracle 4318 1 0 75 0 - 185771 ? 23:00 ? 00:00:00 ora_pmon_orcl 0 S oracle 4322 1 0 58 - - 185209 ? 23:00 ? 00:00:00 ora_vktm_orcl 0 S oracle 4328 1 0 75 0 - 185540 ? 23:00 ? 00:00:00 ora_gen0_orcl 0 S oracle 4332 1 0 78 0 - 185209 ? 23:00 ? 00:00:00 ora_diag_orcl 0 S oracle 4336 1 0 75 0 - 186115 ? 23:00 ? 00:00:00 ora_dbrm_orcl 0 S oracle 4340 1 0 75 0 - 185725 ? 23:00 ? 00:00:00 ora_psp0_orcl 0 S oracle 4344 1 0 78 0 - 185337 ? 23:00 ? 00:00:00 ora_dia0_orcl 0 S oracle 4348 1 1 78 0 - 185209 ? 23:00 ? 00:00:03 ora_mman_orcl 0 S oracle 4352 1 0 75 0 - 188009 ? 23:00 ? 00:00:00 ora_dbw0_orcl 0 S oracle 4356 1 0 75 0 - 191644 ? 23:00 ? 00:00:00 ora_lgwr_orcl 0 S oracle 4360 1 0 75 0 - 186247 ? 23:00 ? 00:00:00 ora_ckpt_orcl 0 S oracle 4364 1 0 75 0 - 186111 ? 23:00 ? 00:00:00 ora_smon_orcl 0 S oracle 4368 1 0 75 0 - 186113 ? 23:00 ? 00:00:00 ora_reco_orcl 0 S oracle 4372 1 0 75 0 - 185862 ? 23:00 ? 00:00:00 ora_rbal_orcl 0 S oracle 4376 1 0 78 0 - 185662 pipe_w 23:00 ? 00:00:00 ora_asmb_orcl 0 S oracle 4380 1 0 75 0 - 187327 ? 23:00 ? 00:00:00 ora_mmon_orcl 0 S oracle 4384 1 0 78 0 - 185209 ? 23:00 ? 00:00:00 ora_mmnl_orcl 0 S oracle 4388 1 0 78 0 - 185668 ? 23:00 ? 00:00:00 ora_d000_orcl 0 S oracle 4396 1 0 78 0 - 185475 ? 23:00 ? 00:00:00 ora_s000_orcl 0 S oracle 4400 1 0 75 0 - 187150 ? 23:00 ? 00:00:00 ora_mark_orcl 0 S oracle 4459 1 0 75 0 - 185559 ? 23:00 ? 00:00:00 ora_o000_orcl 0 S oracle 4469 1 0 75 0 - 185560 ? 23:00 ? 00:00:00 ora_o001_orcl 0 S oracle 4483 1 0 75 0 - 185559 ? 23:00 ? 00:00:00 ora_o002_orcl 0 S oracle 4491 1 0 75 0 - 186110 ? 23:00 ? 00:00:00 ora_qmnc_orcl 0 S oracle 4526 1 0 75 0 - 186893 ? 23:01 ? 00:00:00 ora_cjq0_orcl 0 S oracle 4568 1 0 75 0 - 186935 ? 23:01 ? 00:00:00 ora_q000_orcl 0 S oracle 4572 1 0 78 0 - 185208 ? 23:01 ? 00:00:00 ora_q001_orcl 0 S oracle 4576 1 0 75 0 - 186116 ? 23:01 ? 00:00:00 ora_q002_orcl 0 S oracle 4683 1 0 75 0 - 185732 ? 23:04 ? 00:00:00 ora_j000_orcl 0 S oracle 4687 1 0 75 0 - 185208 ? 23:04 ? 00:00:00 ora_j001_orcl [oracle@zhouwanchun ~]$ [root@zhouwanchun ~]# su - grid [grid@zhouwanchun ~]$ crs_stat -t -v Name Type R/RA F/FT Target State Host ---------------------------------------------------------------------- ora.DATA.dg ora....up.type 0/5 0/ ONLINE ONLINE zhouwanchun ora.FRA.dg ora....up.type 0/5 0/ ONLINE ONLINE zhouwanchun ora....ER.lsnr ora....er.type 0/5 0/ ONLINE ONLINE zhouwanchun ora.asm ora.asm.type 0/5 0/ ONLINE ONLINE zhouwanchun ora.cssd ora.cssd.type 0/5 0/5 ONLINE ONLINE zhouwanchun ora.diskmon ora....on.type 0/10 0/5 ONLINE ONLINE zhouwanchun ora.orcl.db ora....se.type 0/2 0/1 ONLINE ONLINE zhouwanchun [grid@zhouwanchun ~]$ lsnrctl status LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 19-APR-2018 23:06:54 Copyright (c) 1991, 2009, Oracle. All rights reserved. Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521))) STATUS of the LISTENER ------------------------ Alias LISTENER Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production Start Date 19-APR-2018 22:59:59 Uptime 0 days 0 hr. 6 min. 54 sec Trace Level off Security ON: Local OS Authentication SNMP OFF Listener Parameter File /u01/app/grid/product/11.2.0/grid/network/admin/listener.ora Listener Log File /u01/app/grid/diag/tnslsnr/zhouwanchun/listener/alert/log.xml Listening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521))) (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=zhouwanchun.example.com)(PORT=1521))) Services Summary... Service "+ASM" has 1 instance(s). Instance "+ASM", status READY, has 1 handler(s) for this service... Service "orcl.example.com" has 1 instance(s). Instance "orcl", status READY, has 1 handler(s) for this service... Service "orclXDB.example.com" has 1 instance(s). Instance "orcl", status READY, has 1 handler(s) for this service... The command completed successfully [grid@zhouwanchun ~]$ -------------------------------------------------------------------------------------------------------------------- 提醒各位咱們數據庫作完了下一步就是用,怎麼用?第一步登陸 登陸數據庫,檢查數據庫的狀態? 怎麼登陸? 1 本地登陸------》用的就是ipc進程間通信協議,和網路沒有關係。 先登陸數據庫所在的操做系統,而後在鏈接數據庫。 2 遠程登陸------》經過tcp/ip協議。(先配置網絡) 3 管理員登陸------》sys、system 4 普通用戶登陸------》scott、hr、sh等。 管理員的本地登陸 必須先指定你要登陸哪一個數據庫------》ORACLE_SID export ORACLE_SID=orcl ------》表示我要鏈接orcl數據庫 export ORACLE_SID=ocp ------》表示我要鏈接ocp數據庫 [oracle@zhouwanchun ~]$ export ORACLE_SID=orcl [oracle@zhouwanchun ~]$ sqlplus sys/oracle SQL*Plus: Release 11.2.0.1.0 Production on Sun Aug 31 12:59:09 2014 Copyright (c) 1982, 2009, Oracle. All rights reserved. ERROR: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER(提示:sys用戶登陸必須指定角色sysdba) Enter user-name:(按兩次回車退出) 上面的登陸方式不正確,正確的登陸方式以下: [oracle@zhouwanchun ~]$ export ORACLE_SID=orcl [oracle@zhouwanchun ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.1.0 Production on Sun Aug 31 13:00:46 2014 Copyright (c) 1982, 2009, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, Automatic Storage Management, OLAP, Data Mining and Real Application Testing options SQL> show user ; USER is "SYS" 擴展:在oracle數據庫中不存在忘記密碼登陸不了數據庫,你只要輸入dba組的帳戶登陸數據庫服務器,就必定登陸數據庫 --------------------------------------------------------------------------------------- 普通用戶本地登陸 注意:默認普通用戶都是被鎖定的,必須先解鎖 管理員先解鎖帳戶 alter user 帳戶名 account unlock ; 例如: SQL> alter user scott account unlock ; User altered. 給用戶設置密碼 alter user scott identified by 新密碼 ; 例如: SQL> alter user scott identified by oracle ; User altered. SQL> conn scott/oracle ; Connected. 這個時候咱們能夠登陸到scott用戶,若是不能登陸scott 第一判斷scott是否啓用,第二判斷scott是否有權限登陸數據庫。 SQL> show user ; USER is ""黑洞帳號 SQL> conn / as sysdba ; Connected to an idle instance. SQL> startup ; ORACLE instance started. Total System Global Area 534462464 bytes Fixed Size 2215064 bytes Variable Size 369099624 bytes Database Buffers 159383552 bytes Redo Buffers 3764224 bytes Database mounted. Database opened. SQL> conn scott/oracle ; Connected. SQL> show user ; USER is "SCOTT" ------------------------------------------------------------ 關閉數據庫------》sysdba export ORACLE_SID=orcl sqlplus / as sysdba SQL> shutdown immediate ; 數據庫關閉後咱們sysdba是能夠登陸的,可是咱們普通用戶是不能登陸的。 ------------------------------------------------------- 總結:普通用戶登陸 1 解鎖 2 保證數據庫是open ,怎麼檢查數據庫是否是OPEN呢? export ORACLE_SID=orcl sqlplus / as sysdba SQL> select status from v$instance ; STATUS ------------ OPEN 最後總結: sys用戶操做系統驗證,本地登陸sqlplus / as sysdba 普通用戶數據字典驗證,保證數據庫開啓後登陸。 接下來咱們來熟悉sqlplus這個工具。 SQL> help index ; 顯示的是你能夠輸入的sqlplus命令 Enter Help [topic] for help. @ COPY PAUSE SHUTDOWN @@ DEFINE PRINT SPOOL / DEL PROMPT SQLPLUS ACCEPT DESCRIBE QUIT START APPEND DISCONNECT RECOVER STARTUP ARCHIVE LOG EDIT REMARK STORE ATTRIBUTE EXECUTE REPFOOTER TIMING BREAK EXIT REPHEADER TTITLE BTITLE GET RESERVED WORDS (SQL) UNDEFINE CHANGE HELP RESERVED WORDS (PL/SQL) VARIABLE CLEAR HOST RUN WHENEVER OSERROR COLUMN INPUT SAVE WHENEVER SQLERROR COMPUTE LIST SET XQUERY CONNECT PASSWORD SHOW 各位不要看着這麼多sql語句就瘋了啊 1 怎麼輸入sql語句 sql語句自身是不區分大小寫 英文分號 ; 表示結束 回車執行 例子: SQL> select * from scott.dept ; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON 2、剛執行的語句到sql buffer裏面去了。 如何查看我剛剛輸入的語句呢? SQL> ? list LIST ---- Lists one or more lines of the most recently executed SQL command or PL/SQL block which is stored in the SQL buffer. Enter LIST with no clauses to list all lines. In SQL*Plus command-line you can also use ";" to list all the lines in the SQL buffer. The buffer has no command history list and does not record SQL*Plus commands. L[IST] [n | n m | n * | n LAST | * | * n | * LAST | LAST] SQL> SQL> list 1* select * from scott.dept 擴展其實輸入一個l也是能夠的。 ---------------------------------------------------- 3、我想修改一下緩存,好比我想加條件where deptno=10 SQL> ? edit EDIT ---- Invokes an operating system text editor on the contents of the specified file or on the contents of the SQL buffer. The buffer has no command history list and does not record SQL*Plus commands. ED[IT] [file_name[.ext]] SQL> ed Wrote file afiedt.buf 28 ? ? q 1* select * from scott.dept q退出 windows ------>記事本 unix/linux ------>不少選擇,必須指定用那個 方法1:臨時指定 define _editor=vim(這種方法我很最喜歡,爲何呢?很簡單由於第二種方法不是全部設備都能讓你去改環境變量滴) 方法2:修改環境變量 [oracle@zhouwanchun ~]$ cd /home/oracle/ [oracle@zhouwanchun ~]$ vim .bash_profile 加入:export EDITOR=vim [oracle@zhouwanchun ~]$ source .bash_profile [oracle@zhouwanchun ~]$ echo $EDITOR vim SQL> define _editor=vim SQL> ed select * from scott.emp where deptno=10 / :wq SQL> / EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------- DEPTNO ---------- 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7839 KING PRESIDENT 17-NOV-81 5000 10 7934 MILLER CLERK 7782 23-JAN-82 1300 10 ------------------------------------------------------------------ 4、如何執行sql buffer裏的語句呢? SQL> ? / / (slash) --------- Executes the most recently executed SQL command or PL/SQL block which is stored in the SQL buffer. Use slash (/) at the command prompt or line number prompt in SQL*Plus command line. The buffer has no command history and does not record SQL*Plus commands. / (解釋:上面經過vim編輯後的sql語句執行) ---------------------------------------------------------------------------- 5、我想把sql buffer中的語句保存起來,下次再使用,怎麼辦? SQL> ? save SAVE ---- Saves the contents of the SQL buffer in a script. The buffer has no command history list and does not record SQL*Plus commands. SAV[E] [FILE] file_name[.ext] [CRE[ATE] | REP[LACE] | APP[END]] ----------------------- save 文件名字(建議絕對路徑)默認create save 文件名字(建議絕對路徑)replace(覆蓋rep簡寫) save 文件名字(建議絕對路徑)append 追加 例如: SQL> select * from scott.emp ; SQL> l 1* select * from scott.emp SQL> save /u01/app/1.sql ; Created file /u01/app/1.sql --------------------------------------------------------------------------- 6、我是否保存成功,到操做系統上看,但我又不想退出sqlplus SQL> ? host HOST ---- Executes an operating system command without leaving SQL*Plus. Enter HOST without command to display an operating system prompt. You can then enter multiple operating system commands. HO[ST] [command] 輸入host回車進入操做系統,再輸入exit回到sqlplus SQL> host ; [oracle@zhouwanchun ~]$ cd /u01/app/ 保存的文件在哪? [oracle@zhouwanchun app]$ ll total 16 -rw-r--r-- 1 oracle oinstall 27 Oct 22 01:27 1.sql drwxrwxr-x 7 grid oinstall 4096 Oct 21 23:27 grid drwxrwxr-x 7 oracle oinstall 4096 Oct 22 00:00 oracle drwxrwx--- 6 grid oinstall 4096 Oct 21 23:50 oraInventory [oracle@zhouwanchun app]$ cat 1.sql 查看保存的文件內容 select * from scott.emp where deptno=10 / [oracle@zhouwanchun app]$ exit exit SQL> ------------------------------------------------------- host 加操做系統命令 clear 清屏---------------->host clear pwd 顯示當前目錄------------>host pwd ls 列出目錄信息----------->host ls SQL> host ls /u01/app ; 1.sql grid oracle oraInventory cp 複製--------------> host cp /u01/app/1.sql /tmp/2.sql ; rm 刪除--------------> host rm /tmp/2.sql ; ----------------------------------- 注意:host在sqlplus中的命令 若是在linux平臺host等價命令 ! host clear 等價於 ! clear ---------------------------------------------------------------------------- 7、退出sqlplus exit quit SQL> exit ; Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, Automatic Storage Management, OLAP, Data Mining and Real Application Testing options ---------------------------------------------------------------------------- 8、在/u01/app/1.sql腳本,如何在sqlplus中調用 [oracle@zhouwanchun ~]$ sqlplus / as sysdba SQL> ? @ @ ("at" sign) ------------- Runs the SQL*Plus statements in the specified script. The script can be called from the local file system or a web server. @ {url|file_name[.ext]} [arg ...] where url supports HTTP and FTP protocols in the form: http://host.domain/script.sql @@ (double "at" sign) --------------------- Runs the specified script. This command is almost identical to the @ command. It is useful for running nested scripts because it has the additional functionality of looking for the nested script in the same url or path as the calling script. @@ {url|file_name[.ext]} [arg ...] SQL> ? start START ----- Runs the SQL*Plus statements in the specified script. The script can be called from the local file system or a web server. STA[RT] {url|file_name[.ext]} [arg ...] where url supports HTTP and FTP protocols in the form: http://host.domain/script.sql STARTUP ------- Starts an Oracle instance with several options, including mounting, and opening a database. STARTUP options | upgrade_options where options has the following syntax: [FORCE] [RESTRICT] [PFILE=filename] [QUIET] [ MOUNT [dbname] | [ OPEN [open_options] [dbname] ] | NOMOUNT ] where open_options has the following syntax: READ {ONLY | WRITE [RECOVER]} | RECOVER and where upgrade_options has the following syntax: [PFILE=filename] {UPGRADE | DOWNGRADE} [QUIET] @ 腳本的名字 start 腳本名字 SQL> @ /u01/app/1.sql ; SQL> start /u01/app/1.sql ; ----------------------------------------------------------------------------------------- 9、我只想在SQLPLUS 看腳本內容,編輯後再執行。 先把腳本調入sql buffer,而後edit編輯,最後執行。 SQL> ? get GET --- Loads a SQL statement or PL/SQL block from a script into the SQL buffer. The buffer has no command history list and does not record SQL*Plus commands. GET [FILE] file_name[.ext] [LIST | NOLIST] SQL> get /u01/app/1.sql ; 1 select * from scott.emp 2* where deptno=10 SQL> ed select * from scott.emp where deptno=20 / :wq SQL> / -------------------------------------------------------------------------------------- 10、我須要把個人屏幕全部輸出保存爲一個日誌,怎麼解決? SQL> ? spool SPOOL ----- Stores query results in a file, or optionally sends the file to a printer. SPO[OL] [file_name[.ext] [CRE[ATE] | REP[LACE] | APP[END]] | OFF | OUT] SQL> spool /u01/app/1.log append ; 輸入不少操做 SQL> select status from v$instance ; SQL> select * from scott.emp ; SQL> spool off ; 結束 結束後咱們不用退出 SQL> host ; [oracle@zhouwanchun ~]$ cd /u01/app/ [oracle@zhouwanchun app]$ ll total 24 -rw-r--r-- 1 oracle oinstall 5517 Oct 22 02:01 1.log -rw-r--r-- 1 oracle oinstall 43 Oct 22 01:38 1.sql drwxrwxr-x 7 grid oinstall 4096 Oct 21 23:27 grid drwxrwxr-x 7 oracle oinstall 4096 Oct 22 00:00 oracle drwxrwx--- 6 grid oinstall 4096 Oct 21 23:50 oraInventory [oracle@zhouwanchun app]$ cat 1.log 這樣咱們就能夠看見全部正確錯誤的命令所有都保存下來,生成了一個日誌) -------------------------------------------------------------------------------- 11、常常在帳戶之間切換 語法:CONNECT conn 帳戶名/密碼 as 角色 SQL> show user ; USER is "SYS" SQL> conn scott/oracle ; Connected. SQL> show user ; USER is "SCOTT" SQL> conn system/oracle ; Connected. SQL> show user ; USER is "SYSTEM" SQL> conn / as sysdba ; Connected. SQL> show user ; USER is "SYS" SQL> exit ; 鏈接數據庫用 [oracle@zhouwanchun ~]$ sqlplus /nolog 至少打開sqlplus工具沒有鏈接數據庫。 SQL*Plus: Release 11.2.0.1.0 Production on Sun Oct 22 02:08:48 2017 Copyright (c) 1982, 2009, Oracle. All rights reserved. SQL> SQL> show user ; USER is "" 鏈接數據庫 SQL> conn / as sysdba ; Connected. SQL> show user ; USER is "SYS" ---------------------------------------------------------------------------------- 12、在sqlplus能夠關閉和啓動數據庫 SQL> shutdown immediate ; Database closed. Database dismounted. ORACLE instance shut down. SQL> startup ; ORACLE instance started. Total System Global Area 534462464 bytes Fixed Size 2215064 bytes Variable Size 369099624 bytes Database Buffers 159383552 bytes Redo Buffers 3764224 bytes Database mounted. Database opened. SQL> 完成以上命令咱們就學完一個DBA最基礎的命令了。