1、oracle cdb和pdb
- CDB :容器数据库,名称为 CDB$ROOT。其作用就是系统数据库,sys等账号都保存在里面。同时它可以管理PDB数据库
- PDB :可插拔的数据库。用户可以在PDB自建数据库
-
- Oracle安装成功后有个默认的pdb数据库(在安装Oracle的时候自己设定)
- PDB中自带有PDB$SEED,属于PDB的模板数据库,自己创建数据库的时候以此库为模板
一、cdb 和 pdb 常见操作
1. 查看PDB信息
sql
SQL> show pdbs --查看所有pdb
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PDB1 READ WRITE NO
4 PDB2 READ WRITE NO
5 SBDB1 READ WRITE NO
sql
SQL> col name for a20
SQL> select name,dbid,open_mode from v$pdbs; --v$pdbs为PDB信息视图
SQL> select name,dbid,open_mode from v$pdbs;
NAME DBID OPEN_MODE
-------------------- ---------- ----------
PDB$SEED 943290432 READ ONLY
PDB1 1666453508 READ WRITE
PDB2 1345465961 READ WRITE
SBDB1 3044255387 READ WRITE
2.切换容器
sql
SQL> alter session set container=sbdb1 --切换到PDB sbdb1
SQL> alter session set container=CDB$ROOT --切换到CDB容器
查看当前属于哪个容器
sql
SQL> show con_name;/show pdbs; --用show查看当前属于哪个容器
SQl> select sys_context('USERENV','CON_NAME') from dual; --使用sys_context查看属于哪个容
3. 启动PDB
sql
SQL> alter pluggable database sbdb1 open; --开启指定PDB
SQL> alter pluggable database all open; --开启所有PDB
SQL> alter session set container=sbdb1; --切换到PDB进去开启数据库
SQL> startup
4.关闭PDB
sql
SQL> alter pluggable database sbdb1 close; --关闭指定的PDB
SQL> alter pluggable database all close; --关闭所有PDB
SQL> alter session set container=sbdb1; --切换到PDB进去关闭数据库
SQL> shutdown immediate
5.创建PDB
创建或克隆前要指定文件映射的位置(需要CBD下sysdba权限)
sql
SQL> alter system set db_create_file_dest='/u01/app/oracle/oradata';
创建一个新的PDB(需要CBD下sysdba权限)
sql
SQL> create pluggable database sbdb2 admin user sbdb2_user identified by oracle;
SQL> alter pluggable database sbdb2 open; --将PDB sbdb2 打开
6.克隆PDB
(需要CBD下sysdba权限)
sql
SQL> create pluggable database sbdb3 from sbdb1; --sbdb1必须是打开的,才可以被克隆
SQL> alter pluggable database sbdb3 open; --然后打开这个pdb
7. 删除PDB
(需要CBD下sysdba权限)
sql
SQL> alter pluggable database sbdb3 close; --关闭之后才能删除
SQL> drop pluggable database sbdb3 including datafiles; --删除PDB sbdb3
8.设置CDB中PDB自动启动
方式一:设置触发器
sql
CREATE TRIGGER open_all_pdbs
AFTER STARTUP
ON DATABASE
BEGIN
EXECUTE IMMEDIATE 'alter pluggable database all open';
END open_all_pdbs;
/
方式二:使用save state参数(12cR2及以上版本)
通过SAVE STATE保存CDB重启前PDB的状态。这样重启CDB之后,会自动打开PDB到之前的状态。
sql
SQL> ALTER PLUGGABLE DATABASE sbdb1 SAVE STATE;
SQL> ALTER PLUGGABLE DATABASE all SAVE STATE;
取消保存CDB重启前PDB的状态
sql
SQL> ALTER PLUGGABLE DATABASE sbdb1 DISCARD STATE;
SQL> ALTER PLUGGABLE DATABASE all DISCARD STATE;
二、直连PDB
检查监听文件是否有下面的内容,没有则加上,其中的IP地址是虚拟机的ip :
三、解决连接错误(ORA-01034: ORACLE not available)
查看监听进程状态
lua
lsnrctl status
查看进程
perl
ps -ef|grep ora_pm
查看 环境变量
bash
env|grep SID
由此可以看出是环境变量有问题 临时解决
ini
export ORACLE_SID=XE;
永久解决
2、搭建Oracle11g
bash
docker pull rohitbasu77/oracle11g
docker run -d --name oracle11g -p 40022:22 -p 41521:1521 -p 48080:8080 rohitbasu77/oracle11g:latest
连接信息
vbnet
Connect database:
hostname: docker machine ip
port: 41521
sid: xe
username: system
password: oracle
Password for SYS & SYSTEM is oracle
Password for fareuser, searchuser, bookinguser, checkinuser is rohit123
3、搭建 oracle12c
bash
docker pull fluigfws/oracle12c
docker run -d --name oracle11g -p 5500:5500 -p 1522:1521 fluigfws/oracle12c
连接信息
yaml
Connect database:
hostname: docker machine ip
port: 1522
sid: xe
username: system
password: oracle
Password for SYS & SYSTEM is oracle
4、搭建oracle 19c
bash
docker pull registry.cn-hangzhou.aliyuncs.com/zhuyijun/oracle:19c
docker run -d -p 1528:1521 -p 5505:5500 -e ORACLE_SID=XE -e ORACLE_PDB=ORCLPDB1 -e ORACLE_PWD=123456 -e ORACLE_EDITION=standard -e ORACLE_CHARACTERSET=AL32UTF8 -v /mydata/oracle/oradata:/opt/oracle/oradata --name orcl19c_03 registry.cn-hangzhou.aliyuncs.com/zhuyijun/oracle:19c
连接信息
vbnet
Connect database:
hostname: docker machine ip
port: 1528
sid: ORCLPDB1
username: system
password: 123456
Password for SYS & SYSTEM is oracle
docker exec -it orcl19c_03 /bin/bash sqlplus / as sysdba show pdbs;
如许就能够了 还能够经由过程拜访https://localhost:5505/em
username:sys password:123456 Container Name:ORCLPDB1
5、搭建 oracle 21c
bash
docker pull nomhiro1204/oracle21c
mkdir Oracle21
docker run -d --name oracle21 \
-p 31521:1521 -p 35500:5500 \
-e ORACLE_SID=XE \
-e ORACLE_PDB=ORCLPDB1 \
-e ORACLE_PWD=123456 \
nomhiro1204/oracle21c:latest
配置信息
bash
docker exec -it oracle21 bash
依次执行如下命令进入oracle并设置 PDB
grep $ORACLE_HOME /etc/oratab | cut -d: -f1
export ORACLE_SID=ORCLCDB
sqlplus / as sysdba
- 设置pdb
show pdbs;
alter session set container=ORCLPDB1;
要是忘记密码可以进入容器使用脚本设置密码
./setPassword.sh 新密码
连接信息
vbnet
Connect database:
hostname: docker machine ip
port: 31521
sid: ORCLPDB1
username: system
password: 123456
Password for SYS & SYSTEM is oracle
6、搭建oracle 23c
bash
docker pull holgerdewall/oracle23c_apex
docker run -d -it --name my_oracle -p 1523:1521 -p 5555:5500 -e ORACLE_PWD=123456 container-registry.oracle.com/database/free:latest
连接信息
yaml
Connect database:
hostname: docker machine ip
port: 1523
sid: FREEPDB1
username: system
password: 123456
Password for SYS & SYSTEM is oracle