客服微信
本文为云贝教育 刘峰 原创,请尊重知识产权,转发请注明出处,不接受任何抄袭、演绎和未经注明出处的转载。
一、 NORMAL
语法:SHUTDOWN NORMAL
阻止任何用户建立新的连接。
等待当前所有正在连接的用户主动断开连接(此方式下 Oracle 不会立即断掉当前用户的连接,这些用户仍然操作相关的操作)。一旦所有的用户都断开连接,则立即关闭、卸载数据库,并终止实例(所以,一般以正常方式关闭数据库时,应该通知所有在线的用户尽快断开连接)
官方文档
实验
1、连接到pdb,开启一个会话 [oracle@database ~]$ sqlplus sys/oracle@pdb2 as sysdba SYS@pdb2> select sid from v$mystat where rownum=1; SID ---------- 29 2、新开一个会话,sid=30,spid=25592 [oracle@database ~]$ sqlplus sys/oracle@pdb2 as sysdba SYS@pdb2> select p.SPID,s.sid from v$process p,v$session s where p.ADDR=s.PADDR and s.sid=(select sid from v$mystat where rownum=1); SPID SID ------------------------ ---------- 25592 30 3、在第一个会话中执行shutdown normal SYS@pdb2> shutdown normal 。。。等待中 4、再新开个窗口,尝试连接数据库,被拒绝 [oracle@database ~]$ sqlplus sys/oracle@pdb2 as sysdba SQL*Plus: Release 19.0.0.0.0 - Production on Fri Jul 14 22:07:37 2023 Version 19.3.0.0.0 Copyright (c) 1982, 2019, Oracle. All rights reserved. ERROR: ORA-03135: connection lost contact Process ID: 0 Session ID: 0 Serial number: 0 Enter user-name: 5、查看alert日志 2023-07-14T22:02:36.988011-07:00 ORCLPDB1(3):ALTER PLUGGABLE DATABASE CLOSE 2023-07-14T22:02:37.082467-07:00 ORCLPDB1(3):JIT: pid 25437 requesting stop 2023-07-14T22:07:43.253095-07:00 ORCLPDB1(3):Active process 25592 user 'oracle' program 'oracle@database', waiting for 'SQL*Net message from client' #数据库在等待活动进程PID=25592关闭 ORCLPDB1(3): ORCLPDB1(3):SHUTDOWN: waiting for logins to complete. 6、在SID=30中会话中执行exit,观察数据库关闭命令立刻完成 --sid=30 SYS@pdb2> exit --观察SID=29的会话窗口 SYS@pdb2> shutdown normal 。。。等待中 Pluggable Database closed. #关闭成功
二、TRANSACTIONAL
语法:SHUTDOWN TRANSACTIONAL
阻止任何用户建立新的连接,同时阻止当前连接的用户开始任何新的事务。
等待所有未提交的活动事务提交完毕,然后立即断开用户的连接,直接关闭、卸载数据库,并终止实例。
文官文档
At this point, the instance shuts down just as it would when a SHUTDOWN IMMEDIATE statement is submitted.
实验
1、连接到pdb,开启一个会话 [oracle@database ~]$ sqlplus sys/oracle@pdb2 as sysdba SYS@pdb2> select sid from v$mystat where rownum=1; SID ---------- 462 2、新开一个会话,sid=468,spid=25887 SYS@pdb2> select p.SPID,s.sid 2 from v$process p,v$session s 3 where p.ADDR=s.PADDR and s.sid=(select sid from v$mystat where rownum=1); SPID SID ------------------------ ---------- 25887 468 --执行update SYS@pdb2> update t1 set name='aaa' where id=1; 1 row updated. 3、再新开一个会话,sid=459,spid=25955,不做操作 SYS@pdb2> select p.SPID,s.sid 2 from v$process p,v$session s 3 where p.ADDR=s.PADDR and s.sid=(select sid from v$mystat where rownum=1); SPID SID ------------------------ ---------- 25955 459 4、在sid=462会话中执行shutdown transaction SYS@pdb2> shutdown transaction 。。。等待中 5、再新开个窗口,尝试连接数据库,被拒绝 [oracle@database ~]$ sqlplus sys/oracle@pdb2 as sysdba SQL*Plus: Release 19.0.0.0.0 - Production on Fri Jul 14 22:28:40 2023 Version 19.3.0.0.0 Copyright (c) 1982, 2019, Oracle. All rights reserved. ERROR: ORA-28547: connection to server failed, probable Oracle Net admin error Enter user-name: 6、在会话,sid=459,spid=25955,执行事务操作,不受关闭操作影响 SYS@pdb2> begin 2 insert into t1 values(3,'cccc'); 3 commit; 4 end; 5 / PL/SQL procedure successfully completed. SYS@pdb2> insert into t1 values(2,'bbb'); 1 row created. SYS@pdb2> commit; Commit complete. 7、关闭开启的两个会话,在会话中执行exits,发现数据库立即关闭。 SYS@pdb2> SHUTDOWN TRANSACTIONAL 。。。等待中 Pluggable Database closed. 8、观察alter日志 ORCLPDB1(3):ALTER PLUGGABLE DATABASE CLOSE 2023-07-14T22:22:29.354379-07:00 ORCLPDB1(3):JIT: pid 25437 requesting stop 2023-07-14T22:27:35.572219-07:00 ORCLPDB1(3):Active process 25950 user 'oracle' program 'oracle@database', waiting for 'SQL*Net message from client' ORCLPDB1(3): ORCLPDB1(3):Active process 25955 user 'oracle' program 'oracle@database', waiting for 'SQL*Net message from client' ORCLPDB1(3): ORCLPDB1(3):SHUTDOWN: waiting for logins to complete. 2023-07-14T22:38:37.235598-07:00 ORCLPDB1(3):Buffer Cache flush started: 3 ORCLPDB1(3):Buffer Cache flush finished: 3 Pluggable database ORCLPDB1 closed ORCLPDB1(3):Completed: ALTER PLUGGABLE DATABASE CLOSE #因为有会话正在连接中,且有活动事务,数据库无法关闭 #在活动会话中止且退出连接,数据库关闭成功
提问:在未提交的事物会话窗口中,直接执行exits,这个事物是回滚了还是提交了?
三、IMMEDIATE
语法:SHUTDOWN IMMEDIATE
阻止任何用户建立新的连接,同时阻止当前连接的用户开始任何新的事务。
Oracle 不等待在线用户主动断开连接,强制终止用户的当前事务,将任何未提交的事务回退。(如果存在太多未提交的事务,此方式将会耗费很长时间终止和回退事务),直接关闭、卸载数据库,并终止实例。
引用官方文档
实验
1、窗口1连接到pdb [oracle@database ~]$ sqlplus sys/oracle@pdb2 as sysdba SYS@pdb2> 2、窗口2打开一个会话,不做任何操作 [oracle@database ~]$ sqlplus sys/oracle@pdb2 as sysdba SYS@pdb2> 3、窗口3打开一个会话,开启一个事务 [oracle@database ~]$ sqlplus sys/oracle@pdb2 as sysdba SYS@pdb2> update t1 set name='aaa' where id=1; 1 row updated 4、在窗口中执行shutdown immediate,发现立即就执行 SYS@pdb2> shutdown immediate Pluggable Database closed. 5、观察alter日志 2023-07-14T22:54:30.696987-07:00 ORCLPDB1(3):ALTER PLUGGABLE DATABASE CLOSE IMMEDIATE 2023-07-14T22:54:30.713935-07:00 ORCLPDB1(3):JIT: pid 25437 requesting stop 2023-07-14T22:54:30.721716-07:00 ORCLPDB1(3):Process termination requested for pid 26215 [source = rdbms], [info = 2] [request issued by pid: 25437, uid: 54321] 2023-07-14T22:54:30.772752-07:00 ORCLPDB1(3):KILL SESSION for sid=(453, 50463): ORCLPDB1(3): Reason = PDB close immediate ORCLPDB1(3): Mode = KILL HARD FORCE -/-/- ORCLPDB1(3): Requestor = USER (orapid = 34, ospid = 25437, inst = 1) ORCLPDB1(3): Owner = Process: USER (orapid = 49, ospid = 26215) ORCLPDB1(3): Result = ORA-0 2023-07-14T22:54:30.774561-07:00 ORCLPDB1(3):Process termination requested for pid 26220 [source = rdbms], [info = 2] [request issued by pid: 25437, uid: 54321] 2023-07-14T22:54:30.824159-07:00 ORCLPDB1(3):KILL SESSION for sid=(461, 19180): ORCLPDB1(3): Reason = PDB close immediate ORCLPDB1(3): Mode = KILL HARD FORCE -/-/- ORCLPDB1(3): Requestor = USER (orapid = 34, ospid = 25437, inst = 1) ORCLPDB1(3): Owner = Process: USER (orapid = 50, ospid = 26220) ORCLPDB1(3): Result = ORA-0 2023-07-14T22:54:31.946295-07:00 ORCLPDB1(3):Buffer Cache flush started: 3 ORCLPDB1(3):Buffer Cache flush finished: 3 Pluggable database ORCLPDB1 closed ORCLPDB1(3):Completed: ALTER PLUGGABLE DATABASE CLOSE IMMEDIATE #从日志观察到,已经连接的进程被终止了 6、同时已经连接的会话无法执行操作 SYS@pdb2> / select * from t1 * ERROR at line 1: ORA-03113: end-of-file on communication channel Process ID: 26215 Session ID: 453 Serial number: 50463
四、ABORT
语法:SHUTDOWN ABORT
注意:这是比较粗暴的一种关闭方式,当前面 3 种方式都无法关闭时,可以尝试使用终止方式来关闭数据库。 但是以这种方式关闭数据库将会丢失一部份数据信息,当重新启动实例并打开数据库时,后台进程 SMON 会执行实例恢复操作。
阻止任何用户建立新的连接,同时阻止当前连接的用户开始任何新的事务。
立即终止当前正在执行的 SQL 语句。
任何未提交的事务均不被退名。
直接断开所有用户的连接,关闭、卸载数据库,并终止实例。
官方文档
实验
1)、CDB下abort关闭(归档或非归档模式下均可)
1、连接到CDB SYS@ORCLCDB> shutdown abort ORACLE instance shut down. 2、查看alter日志 alter database open 2023-07-14T23:14:38.041690-07:00 Ping without log force is disabled: instance mounted in exclusive mode. 2023-07-14T23:14:38.055224-07:00 Crash Recovery excluding pdb 2 which was cleanly closed. 2023-07-14T23:14:38.060619-07:00 Beginning crash recovery of 1 threads Thread 1: Recovery starting at checkpoint rba (logseq 43 block 271917), scn 0 2023-07-14T23:14:38.135495-07:00 Started redo scan 2023-07-14T23:14:38.294412-07:00 Completed redo scan read 676 KB redo, 71 data blocks need recovery 2023-07-14T23:14:38.389389-07:00 Started redo application at Thread 1: logseq 43, block 271917, offset 0 2023-07-14T23:14:38.389735-07:00 Recovery of Online Redo Log: Thread 1 Group 1 Seq 43 Reading mem 0 Mem# 0: /opt/oracle/oradata/ORCLCDB/redo01.log 2023-07-14T23:14:38.428828-07:00 Completed redo application of 0.08MB 2023-07-14T23:14:38.442043-07:00 Completed crash recovery at Thread 1: RBA 43.273269.16, nab 273269, scn 0x0000000000bdd115 71 data blocks read, 71 data blocks written, 676 redo k-bytes read Endian type of dictionary set to little
2)、PDB(需要开启归档)
1、未开归档的情况,无法执行shutdown abort SYS@pdb2> shutdown abort ORA-16078: media recovery disabled 2、在开启归档之后,pdb执行shutdown abort SYS@pdb2> archive log list Database log mode Archive Mode Automatic archival Enabled Archive destination /opt/oracle/product/19c/dbhome_1/dbs/arch Oldest online log sequence 42 Next log sequence to archive 44 Current log sequence 44 SYS@pdb2> shutdown abort Pluggable Database closed. 3、观察启动过程的恢复日志 ORCLPDB1(3):ALTER PLUGGABLE DATABASE OPEN ORCLPDB1(3):Autotune of undo retention is turned on. 2023-07-14T23:22:01.248100-07:00 Pdb ORCLPDB1 hit error 1113 during open read write (1) and will be closed. 2023-07-14T23:22:01.248498-07:00 Errors in file /opt/oracle/diag/rdbms/orclcdb/ORCLCDB/trace/ORCLCDB_ora_27252.trc: ORA-01113: file 30 needs media recovery ORA-01110: data file 30: '/opt/oracle/oradata/ORCLCDB/ORCLPDB1/ORCLCDB/FE8CF63D04B91F62E053814AA8C03B8A/datafile/o1_mf_apps_lbylw6vm_.dbf' ORCLPDB1(3):JIT: pid 27252 requesting stop ORCLPDB1(3):Buffer Cache flush deferred for PDB 3 ORCLPDB1(3):Media Recovery Start 2023-07-14T23:22:01.497852-07:00 Errors in file /opt/oracle/diag/rdbms/orclcdb/ORCLCDB/trace/ORCLCDB_mz00_27284.trc: ORA-01110: data file 9: '/opt/oracle/oradata/ORCLCDB/ORCLPDB1/system01.dbf' ORA-01208: data file is an old version - not accessing current version 2023-07-14T23:22:01.519218-07:00 ORCLPDB1(3):Serial Media Recovery started ORCLPDB1(3):max_pdb is 3 ORCLPDB1(3):WARNING! Recovering data file 30 from a fuzzy backup. It might be an online ORCLPDB1(3):backup taken without entering the begin backup command. 2023-07-14T23:22:01.683207-07:00 ORCLPDB1(3):Recovery of Online Redo Log: Thread 1 Group 2 Seq 44 Reading mem 0 ORCLPDB1(3): Mem# 0: /opt/oracle/oradata/ORCLCDB/redo02.log 2023-07-14T23:22:01.783007-07:00 ORCLPDB1(3):Media Recovery Complete (ORCLCDB) Checker run found 1 new persistent data failures