Oracle 12C配置PDB自启动
在12C版本,CDB启动的时候,默认情况下,PDB之后启动到MOUNT状态,而不是OPEN状态。
oracle@hm-oradb-01]$ sqlplus / as sysdba SQL*Plus: Release 12.2.0.1.0 Production on Tue Sep 18 09:39:22 2018 Copyright (c) 1982, 2016, Oracle. All rights reserved. Connected to an idle instance. SQL> startup ORACLE instance started. Total System Global Area 1258291200 bytes Fixed Size 8792296 bytes Variable Size 469763864 bytes Database Buffers 771751936 bytes Redo Buffers 7983104 bytes Database mounted. Database opened. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 MOUNTED 4 PDB2 MOUNTED
根据个人习惯不同,有些人可能会配置ORACLE的开机自启动,这样一旦服务器发生故障重启时,数据库会自动启动。可是12C版本的PDB默认之后启动到MOUNT状态,因为MOUNT状态的数据库并不能正常提供服务,因此根本上来讲就相当于自启动失败。
在12.1.0.1版本,PDB是不支持自启动的,一般都是配置数据库启动触发器来实现。
CREATE OR REPLACE TRIGGER auto_open_pdbs AFTERSTARTUP ON DATABASE BEGIN EXECUTE IMMEDIATE 'ALTER PLUGGABLE DATABASE ALL OPEN'; END auto_open_pdbs; /
从12.1.0.2版本,ORACLE支持PDB自启动功能,通过ALTER PLUGGABLE DATABASE XXX SAVE STATE命令实现,这个命令会记录PDB的当前状态并保存到数据字典中,当CDB启动时,会查询数据字典中PDB是否保存了启动的状态,如果查询到PDB保存了OPEN状态,就将相应的PDB启动,可以通过DBA_PDB_SAVED_STATES视图查看PDB保存的状态信息。
如下是实验过程:
首先先讲PDB调整为OPEN状态。
SQL> alter pluggable database pdb1 open; Pluggable database altered. SQL> alter pluggable database pdb2 open; Pluggable database altered. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 READ WRITE NO 4 PDB2 READ WRITE NO
再将PDB1的状态保存下来。
SQL> alter pluggable database pdb1 save state; Pluggable database altered.
此时,可以通过DBA_PDB_SAVED_STATES视图查看到PDB1被保存为OPEN状态。
SQL> select CON_NAME,STATE from DBA_PDB_SAVED_STATES; CON_NAME STATE ---------- -------------- PDB1 OPEN
重启CDB,查看PDB的状态。
SQL> startup force ORACLE instance started. Total System Global Area 1258291200 bytes Fixed Size 8792296 bytes Variable Size 469763864 bytes Database Buffers 771751936 bytes Redo Buffers 7983104 bytes Database mounted. Database opened. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 READ WRITE NO 4 PDB2 MOUNTED
可见,当CDB启动或者重启时,保存了OPEN状态的PDB1成功启动到了OPEN状态,而没有进行设置的PDB2还是默认的MOUNT状态。
DBA_PDB_SAVED_STATES视图中的STATE字段代表的并不是PDB的当前状态,而是当CDB启动时,PDB应该启动的状态。关闭PDB1,再查看DBA_PDB_SAVED_STATES视图,就可以验证DBA_PDB_SAVED_STATES视图中的STATE字段代表的是什么含义了。
SQL> alter pluggable database pdb1 close immediate; Pluggable database altered. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 MOUNTED 4 PDB2 MOUNTED SQL> select CON_NAME,STATE from DBA_PDB_SAVED_STATES; CON_NAME STATE ---------- -------------- PDB1 OPEN
当然,也可以取消记录PDB的自启动功能,通过下面的命令实现。
SQL> alter pluggable database pdb1 discard state; Pluggable database altered.
取消PDB的自启动功能,会删除数据字典中相应的记录,DBA_PDB_SAVED_STATES视图也就查询不到PDB的信息了。
SQL> select * from DBA_PDB_SAVED_STATES; no rows selected
重启CDB验证。
SQL> startup force ORACLE instance started. Total System Global Area 1258291200 bytes Fixed Size 8792296 bytes Variable Size 469763864 bytes Database Buffers 771751936 bytes Redo Buffers 7983104 bytes Database mounted. Database opened. SQL> show pdbs CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 PDB1 MOUNTED 4 PDB2 MOUNTED
RAC环境,默认情况下,PDB自启动只会记录当前实例的信息。
我个人并不习惯配置数据库的自启动功能,但不得不说,有时候配置数据库自启动,会在发生故障导致服务器重启时起到相当重要的作用。