oracle数据库链接及工具使用sqlplus经常使用命令操做手册

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最基础的命令了。
相关文章
相关标签/搜索