Oracle 12c中,增加了可插接数据库的概念,即PDB,允许一个数据库容器(CDB)承载多个可插拔数据库(PDB)。
CDB全称为Container Database,中文翻译为数据库容器,PDB全称为Pluggable Database,即可插拔数据库。
在ORACLE 12C之前,实例与数据库是一对一或多对一关系(RAC):即一个实例只能与一个数据库相关联,数据库可以被多个实例所加载。
而实例与数据库不可能是一对多的关系。当进入ORACLE 12C后,实例与数据库可以是一对多的关系。
下面是官方文档关于CDB与PDB的关系图。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
1 Container
Database
(CDB)
对于CDB,启动和关闭与之前传统的方式一样,具体语法如下:
STARTUP[NOMOUNT | MOUNT |
RESTRICT
| UPGRADE |
FORCE
|
READ
ONLY
]
SHUTDOWN[IMMEDIATE | ABORT]
要注意,在12c数据库创建完成后,默认情况下使用sqlplus /
as
sysdba 登录连接的是CDB。
[oracle@Ora12c~]$ sqlplus /
as
sysdba
SQL*Plus:Release 12.1.0.1.0 Production
on
Thu Apr 24 17:50:34 2014
Copyright(c) 1982, 2013, Oracle.
All
rights reserved.
Connected
to
:
Oracle
Database
12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With
the Partitioning, OLAP, Advanced Analytics
and
Real
Application Testing options
SQL>show con_name
CON_NAME
------------------------------
CDB$ROOT
SQL>
SQL>
select
con_id, dbid, guid,
name
, open_mode
from
v$pdbs;
CON_ID DBID GUID
NAME
OPEN_MODE
---------- ------------------------------------------ ------------------------------ ----------
2 4088301206 F7C1E3C96BBF0585E0430A01A8C05459PDB$SEED
READ
ONLY
3 426143573 F7C209EB1DFC0854E0430A01A8C0B787 PDBCNDBA
READ
WRITE
我们现在连接的是CDB,即root container。
我们关闭CDB:
SQL>shutdown immediate
Database
closed.
Database
dismounted.
ORACLE instance shut down.
我们关闭CDB之前PDB 是没有关闭的,所以这个操作也会把PDB 关闭掉。
SQL>startup
ORACLE instance started.
Total System
Global
Area 1269366784 bytes
Fixed
Size
2287912 bytes
Variable
Size
788530904 bytes
Database
Buffers 469762048 bytes
Redo Buffers 8785920 bytes
Database
mounted.
Database
opened.
SQL>
select
con_id, dbid, guid,
name
, open_modefrom v$pdbs;
CON_ID DBID GUID
NAME
OPEN_MODE
---------- ------------------------------------------ ------------------------------ ----------
2 4088301206F7C1E3C96BBF0585E0430A01A8C05459 PDB$SEED
READ
ONLY
3 426143573 F7C209EB1DFC0854E0430A01A8C0B787 PDBCNDBA MOUNTED
注意这里的PDB,在CDB 启动之,PDB 是自动启动到mount状态,而不是
OPEN
。 所以我们还需要手工去
open
它,当然,也可以通过在CDB中配置触发器来自动
open
。
2 Pluggable
Database
(PDB)
PDB 的操作可以通过SQL*PLUS 来操作,也可以通过
ALTER
PLUGGABLE
DATABASE
命令操作。
2.1 使用SQL*Plus 命令
因为默认连接的是CDB,所以必须先指定PDB才可以通过sqlplus来启动和关闭PDB,
具体语法和普通实例一样:
STARTUP
FORCE
;
STARTUP
OPEN
READ
WRITE [
RESTRICT
];
STARTUP
OPEN
READ
ONLY
[
RESTRICT
];
STARTUP UPGRADE;
SHUTDOWN[IMMEDIATE];
SQL>show con_name
CON_NAME
------------------------------
CDB$ROOT
SQL>
select
con_id, dbid, guid,
name
, open_mode
from
v$pdbs;
CON_ID DBID GUID
NAME
OPEN_MODE
-------------------- -------------------------------- --------------- ----------
2 4088301206F7C1E3C96BBF0585E0430A01A8C05459 PDB$SEED
READ
ONLY
3 426143573 F7C209EB1DFC0854E0430A01A8C0B787 PDBCNDBA MOUNTED
注意这里是mount,表示PDB 是关闭的。
--指定PDB 数据库:
SQL>
alter
session
set
container=pdbcndba;
Session altered.
SQL>startup
Pluggable
Database
opened.
SQL>
select
con_id, dbid, guid,
name
, open_mode
from
v$pdbs;
CON_ID DBID GUID
NAME
OPEN_MODE
-------------------- -------------------------------- --------------- ----------
3 426143573 F7C209EB1DFC0854E0430A01A8C0B787 PDBCNDBA
READ
WRITE
SQL>
启动成功。 当我们切换到PDB之后,就看不到seed PDB的信息了。
2.2 使用
ALTER
PLUGGABLE
DATABASE
命令
如果在PDB中可以使用如下语法:
ALTER
PLUGGABLE
DATABASE
OPEN
READ
WRITE [RESTRICTED] [
FORCE
];
ALTER
PLUGGABLE
DATABASE
OPEN
READ
ONLY
[RESTRICTED] [
FORCE
];
ALTER
PLUGGABLE
DATABASE
OPEN
UPGRADE [RESTRICTED];
ALTER
PLUGGABLE
DATABASE
CLOSE
[IMMEDIATE];
如果是在CDB中,可以使用如下语法:
ALTER
PLUGGABLE
DATABASE
ALTER
PLUGGABLE
DATABASE
ALTER
PLUGGABLE
DATABASE
ALTER
PLUGGABLE
DATABASE
ALL
:表示所有的PDBS。
ALL
EXCEPT
表示需要排除的PDBS。
如:
ALTER
PLUGGABLE
DATABASE
pdb1, pdb2
OPEN
READ
ONLY
FORCE
;
ALTER
PLUGGABLE
DATABASE
pdb1, pdb2
CLOSE
IMMEDIATE;
ALTER
PLUGGABLE
DATABASE
ALL
OPEN
;
ALTER
PLUGGABLE
DATABASE
ALL
CLOSE
IMMEDIATE;
ALTER
PLUGGABLE
DATABASE
ALL
EXCEPT
pdb1
OPEN
;
ALTER
PLUGGABLE
DATABASE
ALL
EXCEPT
pdb1
CLOSE
IMMEDIATE;
示例:
SQL>
alter
session
set
container=CDB$ROOT;
Session altered.
SQL>
select
con_id, dbid, guid,
name
, open_mode
from
v$pdbs;
CON_ID DBID GUID
NAME
OPEN_MODE
-------------------- -------------------------------- --------------- ----------
2 4088301206F7C1E3C96BBF0585E0430A01A8C05459 PDB$SEED
READ
ONLY
3 426143573 F7C209EB1DFC0854E0430A01A8C0B787 PDBCNDBA
READ
WRITE
SQL>
ALTER
PLUGGABLE
DATABASE
ALL
CLOSE
;
Pluggable
database
altered.
SQL>
select
con_id, dbid, guid,
name
, open_mode
from
v$pdbs;
CON_ID DBID GUID
NAME
OPEN_MODE
-------------------- -------------------------------- --------------- ----------
2 4088301206F7C1E3C96BBF0585E0430A01A8C05459 PDB$SEED
READ
ONLY
3 426143573 F7C209EB1DFC0854E0430A01A8C0B787PDBCNDBA MOUNTED
SQL>
alter
session
set
container=pdbcndba;
Session altered.
SQL>
ALTER
PLUGGABLE
DATABASE
OPEN
;
Pluggable
database
altered.
SQL>
select
con_id, dbid, guid,
name
, open_mode
from
v$pdbs;
CON_ID DBID GUID
NAME
OPEN_MODE
-------------------- -------------------------------- --------------- ----------
3 426143573 F7C209EB1DFC0854E0430A01A8C0B787PDBCNDBA
READ
WRITE
3 设置Pluggable
Database
(PDB) 的自动startup
默认情况下,在CDB 启动的时候,
all
的PDB 都是mount状态,也没有默认的机制,在CDB启动时自动启动PDB。
但这里可以通过触发器来实现PDB的自动
open
:
CREATE
OR
REPLACE
TRIGGER
open_pdbs
AFTER
STARTUP
ON
DATABASE
BEGIN
EXECUTE
IMMEDIATE
'ALTER PLUGGABLE DATABASE ALL OPEN'
;
END
open_pdbs;
/
示例:
SQL>show con_name
CON_NAME
------------------------------
PDBCNDBA
SQL>
alter
session
set
container=CDB$ROOT;
Session altered.
SQL>
CREATE
OR
REPLACE
TRIGGER
open_pdbs
2
AFTER
STARTUP
ON
DATABASE
3
BEGIN
4
EXECUTE
IMMEDIATE
'ALTER PLUGGABLE DATABASE ALL OPEN'
;
5
END
open_pdbs;
6 /
Triggercreated.
|