源库为ORCL中的orclpdb1,目标库为ORCL2。版本均为19c。
我们将利用19c中dbca新支持的-createFromRemotePDB
选项来从远端克隆PDB。
确认源库中有业务数据:
SQL> connect hr@orclpdb1
Enter password:
Connected.
SQL> select count(*) from hr.employees;
COUNT(*)
----------
107
在源库中创建common user。后续dbca会用这个用户来连接源库。
SQL> connect system
Enter password:
Connected.
SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT
SQL> CREATE USER c##remote_user IDENTIFIED BY Welcome1 CONTAINER=ALL;
User created.
SQL> GRANT create session, create pluggable database TO c##remote_user CONTAINER=ALL;
Grant succeeded.
SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.20.0.0.0
在目标库所在数据库服务器上,静默方式执行dbca命令,克隆远端pdb:
dbca -silent -createPluggableDatabase -createFromRemotePDB \
-remotePDBName orclpdb1 \
-remoteDBConnString ORCL \
-remoteDBSYSDBAUserName sys -remoteDBSYSDBAUserPassword Welcome1 \
-sysDBAUserName sys -sysDBAPassword Welcome1 \
-dbLinkUsername c##remote_user -dbLinkUserPassword Welcome1 -sourceDB ORCL2 -pdbName pdbclone
输出如下:
Prepare for db operation
50% complete
Create pluggable database using remote clone operation
100% complete
Pluggable database "pdbclone" plugged successfully.
Look at the log file "/u01/app/oracle/cfgtoollogs/dbca/ORCLCDB2/pdbclone/ORCLCDB2.log" for further details.
日志如下:
$cat /u01/app/oracle/cfgtoollogs/dbca/ORCLCDB2/pdbclone/ORCLCDB2.log
[ 2023-10-20 07:47:55.920 GMT ] Prepare for db operation
DBCA_PROGRESS : 50%
[ 2023-10-20 07:47:55.982 GMT ] Create pluggable database using remote clone operation
DBCA_PROGRESS : 100%
[ 2023-10-20 07:48:23.910 GMT ] Pluggable database "pdbclone" plugged successfully.
验证,注意PDBCLONE被自动open了:
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PDBCLONE READ WRITE NO
SQL> alter session set container=pdbclone;
Session altered.
SQL> select count(*) from hr.employees;
COUNT(*)
----------
107
清理:
alter pluggable database pdbclone close;
drop pluggable database pdbclone including datafiles;
在clone的过程中,dbca会自动创建database link,名称固定为PDBCLONE_CLONE_LINK
,完成后会自动删除:
SQL> select * from all_db_links;
OWNER DB_LINK USERNAME HOST CREATED HID SHA VAL INT
---------- ------------------------------ ---------------- ---------------- --------- --- --- --- ---
SYS SYS_HUB SEEDDATA 17-APR-19 NO NO YES NO
SYS PDBCLONE_CLONE_LINK C##REMOTE_USER ORCL 20-OCT-23 NO NO YES NO
...
SQL> /
OWNER DB_LINK USERNAME HOST CREATED HID SHA VAL INT
---------- ------------------------------ ---------------- ---------------- --------- --- --- --- ---
SYS SYS_HUB SEEDDATA 17-APR-19 NO NO YES NO
dbca创建database link的SQL类似于:
CREATE DATABASE LINK PDBCLONE_CLONE_LINK CONNECT TO c##remote_user IDENTIFIED BY Welcome1 USING 'ORCL';
drop database link PDBCLONE_CLONE_LINK;
当然,你不能主动创建,否则后续dbca克隆时会报错:
[FATAL] [DBT-19405] Database link (PDBCLONE_CLONE_LINK) is already exists.
这里遇到一个非常奇怪的错误,就是当把-remoteDBConnString参数ORCL改为小写orcl时,居然会报错:
[FATAL] [DBT-08101] The selected PDB (orclpdb1) is not open.
CAUSE: PDB has to be open in READ WRITE mode to perform the configuration.
这个错误和这个帖子类似。
也和我创建ORCL2的语句有关:
dbca -silent -createDatabase -templateName General_Purpose.dbc -gdbname ORCLCDB2 -sid ORCL2 -responseFile NO_VALUE -characterSet AL32UTF8 -sysPassword Welcome1 -systemPassword Welcome1 -createAsContainerDatabase true -pdbName ORCLPDB1 -numberOfPDBs 1 -pdbAdminPassword Welcome1
估计全部用小写就没事了。
果然,删除时,如果指定数据库为orcl2,会报错:
[WARNING] [DBT-11503] The instance (orcl2) is not running on the local node. This may result in partial delete of Oracle database.
CAUSE: A locally running instance is required for complete deletion of Oracle database instance and database files.
ACTION: Specify a locally running database, or execute DBCA on a node where the database instance is running.
改成ORCL2就成功了:
dbca -silent -deleteDatabase -sourceDB ORCL2
Enter SYS user password:
[WARNING] [DBT-19202] The Database Configuration Assistant will delete the Oracle instances and datafiles for your database. All information in the database will be destroyed.
Prepare for db operation
32% complete
Connecting to database
35% complete
39% complete
42% complete
45% complete
48% complete
52% complete
65% complete
Updating network configuration files
68% complete
Deleting instance and datafiles
84% complete
100% complete
Database deletion completed.
重建,这回全部用小写:
dbca -silent -createDatabase -templateName General_Purpose.dbc -gdbname ORCL2 -sid ORCL2 -responseFile NO_VALUE -characterSet AL32UTF8 -sysPassword Welcome1 -systemPassword Welcome1 -createAsContainerDatabase true -pdbName orclpdb2 -numberOfPDBs 1 -pdbAdminPassword Welcome1
另外,-remoteDBConnString还是建议指向CDB。虽然在这篇博客中有成功的例证,但我没有成功。
以下是-remoteDBConnString分别指定为orclpdb1
和localhost:1521/ORCLPDB1
时的报错:
orcl2 $dbca -silent -createPluggableDatabase -createFromRemotePDB -remotePDBName ORCLPDB1 -remoteDBConnString orclpdb1 -remoteDBSYSDBAUserName sys -remoteDBSYSDBAUserPassword Welcome1 -sysDBAUserName sys -sysDBAPassword Welcome1 -dbLinkUsername c##remote_user -dbLinkUserPassword Welcome1 -sourceDB ORCL2 -pdbName pdbclone
[FATAL] [DBT-08101] The selected PDB (ORCLPDB1) is not open.
CAUSE: PDB has to be open in READ WRITE mode to perform the configuration.
orcl2 $dbca -silent -createPluggableDatabase -createFromRemotePDB -remotePDBName ORCLPDB1 -remoteDBConnString localhost:1521/ORCLPDB1 -remoteDBSYSDBAUserName sys -remoteDBSYSDBAUserPassword Welcome1 -sysDBAUserName sys -sysDBAPassword Welcome1 -dbLinkUsername c##remote_user -dbLinkUserPassword Welcome1 -sourceDB ORCL2 -pdbName pdbclone
[FATAL] [DBT-19407] Database option (SAMPLE_SCHEMA) is not installed in Local CDB (ORCL2).
CAUSE: The database options installed on the Remote CDB(ORCLPDB1) must be the same as the database options installed on the Local CDB(ORCL2).