1. 程式人生 > >啟動與關閉CDB、PDB

啟動與關閉CDB、PDB

一、連線工具

sqlplus、pl/sql developer、TOAD等。

Oracle DBA常用的是sqlplus,登入過程如下:

SQL> sqlplus / as sysdba  --在本機,以超級管理員身份登入
SQL> startup  --預設是open狀態,資料庫將開啟資料檔案並進行一系列的檢查工作,這些檢查工具用於資料恢復

建立的例項名為:oracs133

    

二、PDB與CDB

Oracle 12c中,增加了可插接資料庫的概念,即PDB,允許一個數據庫容器(CDB)承載多個可插拔資料庫(PDB)

CDB全稱為Container Database,中文翻譯為資料庫容器,PDB全稱為Pluggable Database,即可插拔資料庫。

在ORACLE 12C之前,例項與資料庫是一對一或多對一關係(RAC):即一個例項只能與一個數據庫相關聯,資料庫可以被多個例項所載入。而例項與資料庫不可能是一對多的關係。當進入ORACLE 12C後,例項與資料庫可以是一對多的關係。

下面是官方文件關於CDB與PDB的關係圖。

三、啟動和停止資料庫

(一)啟動資料庫

  • CDB啟動

使用命令:

/*在本機,以超級管理員身份登入*/
SQL> sqlplus / as sysdba
/*開啟startup,預設是open*/
SQL> startup
/*檢視目前CDB的狀態*/
SQL> select status from v$instance;

12c通過“sqlplus / as sysdba”登入連線的是CDB,現在連線上的是root container

執行過程如下:

[[email protected] ~]$ sqlplus / as sysdba

SQL*Plus: Release 12.1.0.2.0 Production on Thu Sep 27 16:36:15 2018

Copyright (c) 1982, 2014, Oracle.  All rights reserved.


Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> startup
ORA-01081: cannot start already-running ORACLE - shut it down first
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area 1593835520 bytes
Fixed Size                  2924880 bytes
Variable Size            1023413936 bytes
Database Buffers          553648128 bytes
Redo Buffers               13848576 bytes
Database mounted.
Database opened.

SQL> select status from v$instance;

STATUS
------------------------
OPEN

STARTUP的選項如下:

  • NOMOUNT:只啟動資料庫例項,此時讀取引數檔案
  • MOUNT:根據引數檔案中的控制檔案位置找到並開啟控制檔案,讀取控制檔案中的各種引數資訊,如資料檔案和日誌檔案的位置,但此時並不開啟資料檔案;
  • OPEN:開啟資料檔案、日誌檔案等,並進行一系列檢查工作,這些檢查工作用語資料恢復(預設情況下,如果不加任何引數即是啟動到OPEN狀態)
  •     OPEN也有兩個選項:OPEN READ ONLY(只讀模式開啟資料庫),OPEN READ WRITE(預設是這個,讀寫模式開啟資料庫
  • FORCE:啟動資料庫,與OPEN選項的區別是,用FORCE選項啟動,如果當前資料庫已經啟動不會報錯,而是自動SHUTOWN ABORT當前資料庫,然後再啟動,可以理解為RESTART
  • RESTRICT:
  • PFILE:
  • PDB啟動

使用命令:

SQL> select name,open_mode from v$pdbs;  --檢視當前pdb的狀態
SQL> alter session set container = PDB1;  
SQL> alter pluggable database open;
SQL> select name,open_mode from v$pdbs;

具體過程如下:

SQL> select name,open_mode from v$pdbs;

NAME
------------------------------------------------------------
OPEN_MODE
--------------------
PDB$SEED
READ ONLY

PDB1
MOUNTED


SQL> alter session set container=PDB1;

Session altered.

SQL> alter pluggable database open;

Pluggable database altered.

SQL>  select name,open_mode from v$pdbs;

NAME
------------------------------------------------------------
OPEN_MODE
--------------------
PDB1
READ WRITE

PDB$SEED是系統建立的,下面的ORACS133PDB1是使用者建立的,預設在CDB 啟動之後,PDB 是自動啟動到mount狀態,而不是OPEN。所以需要手動到open狀態

(二)關閉資料庫

繼續上面的操作,目前在pdb1中,如果要退出Oracle,需要先關閉PDB1,再關閉資料庫。

使用命令:

SQL> alter pluggable database pdb1 close;

執行過程:

SQL> alter pluggable database pdb1 close;

Pluggable database altered.

SQL> select name,open_mode from v$pdbs;

NAME
------------------------------------------------------------
OPEN_MODE
--------------------
PDB1
MOUNTED


SQL> show con_name;

CON_NAME
------------------------------
PDB1

選擇CDB之後,執行資料庫關閉。

使用命令:

SQL> alter session set container=CDB$ROOT;
SQL> show con_name;
SQL> shutdown immediate;

執行過程如下:

SQL> alter session set container=CDB$ROOT;

Session altered.

SQL> show con_name;

CON_NAME
------------------------------
CDB$ROOT
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

也可以分解關閉步驟:

依次關閉資料檔案、日誌檔案等(close),控制檔案(dismount),最後關閉例項(shutdown)。

也可以一步完成資料庫關閉:

我們關閉CDB之前PDB 是沒有關閉的,所以這個操作也會把PDB 關閉掉。

2、SHUTDOWN的幾個引數及含義

exit退出資料庫後,資料本身的連線狀態沒有變化。

SHUTDOWN NORMAL:shutdown的預設方式,a.不允許新的資料庫連線;b.只有當所有連線都斷開後才能關閉,效率較低

SHUTDOWN IMMIDIATE:shutdown的常用方式,a.不允許建立新連線;b.已經建立的連線,如果有未執行完的SQL語句,等待其完成,如果沒有立刻斷開;c.未提交事務全部回滾

SHUTDOWN TRANSACTIONAL:使用率很低

SHUTDOWN ABORT:a.未提交事務不回滾;b.終止所有SQL操作;c.所有連線都斷開。資料庫關閉迅速,但是下一次開啟需要進行例項恢復,啟動慢;而且回滾段資料與資料檔案可能不一致。

資料庫開啟和關閉的語句總結(順序執行,有上下文關係):

//開啟資料庫

  • sqlplus / as sysdba;                  //登入連線CDB,預設是root container;
  • startup open;                         //open 模式,但此時的pdb模式是mounted,需要手工開啟
  • alter session set container=ORACS133PDB1;                   //選定pdb
  • alter pluggable database open;                                  //開啟pdb

//關閉資料庫

  • alter pluggable database all close;
  • alter session set container = CDB$ROOT;
  • shutdown immediate;

(三)建立使用者

1、schema

為Oracle 12c新增scott使用者的方法:https://www.2cto.com/database/201610/553547.html