5_Oracle_Admin_Oracle的启动模式和关闭

 

 

只使用startup命令,将按照默认值启动数据库,供普通用户使用;而指定PFILE则会根据自定义的参数来启动数据库。

 

[FORCE]表示强制启动数据库,使用该选项通常是因为上次关闭数据库时没有关闭完整,表现为数据文件或联机日志文件不干净,也可能在内存中残留一些东西如ipcs资源等,从而导致启动失败(因为内存中已经有资源了),这就需要使用FORCE选项,将内存中残留的数据都清空。

 

[RESTRICT]受限模式是只数据库只对部分具有特定权限的用户开发,通常是数据库管理员在进行备份恢复时采取的模式。(DBA在进行备份恢复可以有两种方式:将数据库变为mount模式,但是mount模式下可执行的操作有限;使用RESTRICT模式open数据库,这样除了部分授权用户可以连接数据库,其他用户都不能连接数据库了。)

如果数据库已经是open状态了,则可以使用alter命令将其调整为restrict模式:

 

SQL> alterdatabase open;

 

Database altered.

 

SQL> altersystem enable restricted session;

 

System altered.

 

/*=====演示RESTRICT模式下用户连接的状况=======*/

 

[oracle@localhostdbs]$ lsnrctl start

 

LSNRCTL for Linux:Version 11.2.0.1.0 - Production on 09-SEP-2014 04:47:47
 
Copyright (c) 1991, 2009,Oracle.  All rights reserved.
 
TNS-01106: Listener usinglistener name LISTENER has already been started
# 启动litsner


 

[oracle@localhost ~]$sqlplus /nolog

 

SQL*Plus: Release11.2.0.1.0 Production on Tue Sep 9 04:48:29 2014
 
Copyright (c) 1982, 2009,Oracle.  All rights reserved.

 

SQL> conn / assysdba

Connected to an idleinstance.


SQL> startup

ORACLE instance started.
 
Total System GlobalArea  422670336 bytes
Fixed Size                  1336960 bytes
Variable Size             318769536 bytes
Database Buffers           96468992 bytes
Redo Buffers                6094848 bytes
Database mounted.
Database opened.

 

SQL> create usermickey identified by 123;

 
User created.
-- 创建一个普通用户

 

SQL> grantconnect, resource to mickey;

 

Grant succeeded.
--为该用户授权

 

[oracle@localhost ~]$sqlplus /nolog 

SQL*Plus: Release11.2.0.1.0 Production on Tue Sep 9 04:54:24 2014
 
Copyright (c) 1982, 2009,Oracle.  All rights reserved.

 

SQL> connmickey/123

Connected.
--用户mickey已经可以连接到数据库了


SQL> select *from dual;

 
D
-
X
--mickey可以正常使用数据库


 

SQL> quit

Disconnected from OracleDatabase 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning,OLAP, Data Mining and Real Application Testing options
-- mickey用户退出数据库

 

SQL> altersystem enable restricted session;

-- 切换成SYSDBA用户,alter数据库的模式为RESTRICT。
System altered.

 

[oracle@localhost ~]$sqlplus mickey/123

 
SQL*Plus: Release11.2.0.1.0 Production on Tue Sep 9 04:58:11 2014
Copyright (c) 1982, 2009,Oracle.  All rights reserved.
 
ERROR:
ORA-01035: ORACLE onlyavailable to users with RESTRICTED SESSION privilege
-- mickey用户已经无法连接数据库了,因为该用户只有connect 和 resource权限,而没有RESTRICTED SESSION权限。


 

[oracle@localhost ~]$sqlplus system

 
SQL*Plus: Release11.2.0.1.0 Production on Tue Sep 9 04:59:54 2014
 
Copyright (c) 1982, 2009,Oracle.  All rights reserved.
 
Enter password: 
Connected to:
Oracle Database 11gEnterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning,OLAP, Data Mining and Real Application Testing options
# system这个用户可以正常连接,该用户有比较高的权限。


 

/*============演示在restrict模式下如何踢除某些用户==============*/

如上所示,system用户在restrict模式下还可以连接数据库,但是管理员不希望这个用户连接,那么可以将其踢除出去:

 

SQL> descv$session;

 Name                                     Null?    Type
 ------------------------------------------------- ----------------------------
 SADDR                                             RAW(4)
 SID                                               NUMBER
 SERIAL#                                           NUMBER
...
 CREATOR_ADDR                                       RAW(4)
 CREATOR_SERIAL#                                    NUMBER
 ECID                                              VARCHAR2(64)

 

SQL> selectsaddr, sid, serial# from v$session;

 

SQL> select sid,serial#, username, saddr from v$session;

 
       SID   SERIAL# USERNAME                      SADDR
---------- ---------------------------------------- --------
         1          5 SYS                            38AF0070
         2          1                                38AED604
         3          1                                38AEAB98
      ...
        22          5                                38AB8594
        24          4                                38AB30BC
        25         33                                38AB0650
        27         44 SYSTEM                         38AAB178
        29          1                                38AA5CA0
 
23 rows selected.

 

根据sidserial#可以踢除相应的用户:

 

SQL> altersystem kill session ‘27,44‘;

 
System altered.

 

/*=====切换成system用户,会发现已经不能使用数据库了======*/

SQL> select *from dual;

select * from dual
*
ERROR at line 1:
ORA-00028: your sessionhas been killed

 

不能kill自己:

SQL> altersystem kill session ‘1,5‘;

alter system kill session‘1,5‘
*
ERROR at line 1:
ORA-00027: cannot killcurrent session

 

Read-Only模式,顾名思义只能做查询,不能做增删改命令;Read-Only模式下,用户可以让某些data files 离线或上线,但不能对tablespaces离线或上线;也可以对offline状态下的data filestablespaces进行恢复。

 

/*=============演示只读模式下的用户连接状况===============*/

SQL> shutdownimmediate

Database closed.
Database dismounted.
ORACLE instance shutdown.

 

SQL> startupmount;

ORACLE instance started.
 
Total System GlobalArea  422670336 bytes
Fixed Size                  1336960 bytes
Variable Size             318769536 bytes
Database Buffers           96468992 bytes
Redo Buffers                6094848 bytes
Database mounted.

 

SQL> alterdatabase open read only;

 
Database altered.
-- 管理员将数据库调整为read-only模式


 

/*===普通用户mickey登陆数据库===*/


[oracle@localhost ~]$sqlplus mickey/123

SQL*Plus: Release11.2.0.1.0 Production on Tue Sep 9 05:17:29 2014
Copyright (c) 1982, 2009,Oracle.  All rights reserved.
 
 
Connected to:
Oracle Database 11gEnterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning,OLAP, Data Mining and Real Application Testing options

 

SQL> createtable tastbl(id integer);

create table tastbl(idinteger)
*
ERROR at line 1:
ORA-00604: error occurredat recursive SQL level 1
ORA-16000: database openfor read-only access
--普通用户不能创建表,因为是只读模式

 

SQL> select *from dual;

 

D
-
X
--但是可以进行查询操作


 

 

关闭数据库和启动数据库的过程是对应的:

. Close a Database

1.SGA中的数据分别写入到datafilesredo log files中;

2.关闭联机数据文件和redolog file(离线的数据文件和tablespace不受影响)

3.普通用户无法再访问数据库了,但好似controlfiles仍然是open状态,管理员还能继续使用。

 

二. Unmounta Database

1.断开数据库和实例的联系;

2.实例仍然驻留在内存中;

3.当数据库unmounted了之后,control files将关闭

 

三. ShutDown an Instance

1.关闭进程

2.从内存中释放SGA占据的空间

3.终止后台进程

 

使用ABORTIMMEDIATETRANSACTIONAL的方式会强制终止所有用户的会话,是一种比较粗暴的方式,其中ABORT方式会导致不完整关闭,因为无法完成检查点的创建,因此在下一次启动数据库时,smont进程会自动进行数据恢复。

 

 

IMMEDIATE是最为常用的关闭方式因为NORMAL TRANSACTION这两种方式都要等用户把活干完才能关,这样不但浪费时间,而且如果用户一直不结束任务,数据库就一直无法关闭。

 

 

===============演示NORMAL关闭过程========================

 

[oracle@localhost ~]$sqlplus /nolog

 

SQL*Plus: Release11.2.0.1.0 Production on Wed Aug 27 19:44:25 2014
 
Copyright (c) 1982, 2009,Oracle.  All rights reserved.

SQL> conn / assysdba

Connected to an idleinstance.

SQL> startup

ORACLE instance started.
 
Total System GlobalArea  422670336 bytes
Fixed Size                  1336960 bytes
Variable Size             318769536 bytes
Database Buffers           96468992 bytes
Redo Buffers                6094848 bytes
Database mounted.
Database opened.

 

===========开启另一个终端登录数据库===============

 

[oracle@localhost ~]$sqlplus /nolog

 

SQL*Plus: Release11.2.0.1.0 Production on Wed Aug 27 19:47:19 2014
 
Copyright (c) 1982, 2009,Oracle.  All rights reserved.

 

SQL> conn mickey

Enter password:
Connected.

  

===================切回SYS用户=======================

 

SQL> select sid,serial#, username from v$session;

 
       SID   SERIAL# USERNAME
---------- ----------------------------------------
         1          5 SYS
         2          1
…
        28          1
        29         10 MICKEY
 
23 rows selected.

 

SQL> shutdownnormal

 

--此时光标一直在闪烁,数据库无法关闭,因为用户mickey还没有退出,NORMAL模式必须等用户退出才能关闭数据库

 

 

===================切回mickey用户=======================

 

SQL> select *from dual;

 

D
-
X
-- mickey用户还能进行查询操作


 

SQL> exit

-- mickey用户主动退出

 

===================切回SYS用户=======================

SQL> shutdownnormal

Database closed.
Database dismounted.
ORACLE instance shut down.
-- 数据库终于关闭了

 

===============演示TRANSACTIONAL的关闭过程========================

 

 

SQL> startup

-- SYS 用户再次启动数据库
ORACLE instance started.
 
Total System Global Area 422670336 bytes
Fixed Size                 1336960 bytes
Variable Size            318769536 bytes
Database Buffers          96468992 bytes
Redo Buffers               6094848 bytes
Database mounted.
Database opened.

 

==========mickey 用户登录建表===========

 

[oracle@localhost ~]$ sqlplus /nolog

 

SQL*Plus: Release 11.2.0.1.0 Production on Wed Aug 2720:57:59 2014
 
Copyright (c) 1982, 2009, Oracle.  All rights reserved.

 

SQL> connmickey/123

Connected.

 

SQL> createtable test(id integer, name char(10));

 

Table created.

 

SQL> insert into test values(0, ‘mickey‘);

 

1 row created.

 

SQL> select *from test;

 

        ID NAME
---------- ----------
         0 mickey
-- mickey用户建了表并插入了一条数据,但这个数据只保存在内存中,还没有写入data file里,因为没有commit。


 

==========SYS查看transaction ===========

 

SQL> selectaddr, status from v$transaction;

 
ADDR     STATUS
-------- ----------------
37FD24A8 ACTIVE
-- 有一条记录,说明用户尚未commit


 

===========mickey用户提交数据==============

SQL> commit

 
Commit complete.

 

==========SYS查看transaction ===========

SQL> selectaddr, status from v$transaction;

 
no rows selected
-- 没有记录了,说明没有正在进行中的transaction


 

============mickey用户再插入一条数据================

 

SQL> insert intotest values(1, ‘chris‘);

 

1 row created.
 
-- 没有commit

==========SYS查看transaction 并试图关闭数据库===========

SQL> selectaddr, status from v$transaction;

 
ADDR     STATUS
-------- ----------------
37FD24A8 ACTIVE

 

SQL> shutdowntransactional

 

--光标闪烁,无法关闭,因为用户没有commit

 

 

===========mickey用户提交数据==============

SQL> commit 

Commit complete.

 

==========SYS用户所在的终端显示数据库关闭===========

SQL> shutdowntransactional

Database closed.
Database dismounted.
ORACLE instance shut down.


本文出自 “重剑无锋 大巧不工” 博客,请务必保留此出处http://wuyelan.blog.51cto.com/6118147/1553544

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。