1.docker导出容器镜像
##docker save -o 导出后的镜像名称.tar 容器名称|镜像id
docker save -o oracle_11g.tar 3fa112fd3642
2.下载镜像上传镜像略
3.加载镜像
##docker load -i <archive_file>
docker load -i oracle11g11201.tar
4.添加版本号默认是没有版本号的
##docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
docker tag 3116 registry.cn-zhangjiakou.aliyuncs.com/jyzx/oracle_11.2.0.1:v1
5.初始化容器
docker run -itd --name oracle11g -h jyzx_oracle --restart=always --privileged=true -p 1521:1521 -v /Users/xx/dpdump:/u01/app/oracle/admin/ORCL/dpdump/ registry.cn-zhangjiakou.aliyuncs.com/jyzx/oracle_11.2.0.1:v1
6.查看状态
docker ps
7.进入容器
docker exec -it oracle11g bash
#这一步一定要
su - oracle
sqlplus /nolog
conn /as sysdba
剩下的就创建表空间用户了
默认用户名 system/system
1.创建表空间
CREATE TABLESPACE khgl DATAFILE '/opt/oracle/app/oradata/orcl/khgl01.dbf' SIZE 100M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED;
删除表空间:
DROP TABLESPACE khgl INCLUDING CONTENTS AND DATAFILES;
2.创建用户create user khgl identified by sa;
表空间给用户
ALTER USER khgl IDENTIFIED BY sa DEFAULT TABLESPACE khgl TEMPORARY TABLESPACE temp;
ALTER USER khgl QUOTA UNLIMITED ON khgl;
或者创建用户并分配表空间
create user khgl identified by sa default tablespace khgl;
并给用户赋予权限grant connect,resource,dba to khgl;
3.#容器外执行数据库备份和恢复
--1.备份
#docker exec 容器名|ID su oracle -lc "expdp 用户名/密码@127.0.0.1:1521/orcl directory=DATA_PUMP_DIR dumpfile=备份文件名.dmp logfile=备份文件名.log compression=all"
示例
docker exec oracle11g su oracle -lc "expdp khgl/sa@127.0.0.1:1521/orcl directory=DATA_PUMP_DIR dumpfile=khgl_20240806_1800.dmp logfile=khgl_20240806_1800.log compression=all"
结果展示
Dump file set for KHGL.SYS_EXPORT_SCHEMA_03 is:
/opt/oracle/dpdump/khgl_20240806_1800.dmp
Job "KHGL"."SYS_EXPORT_SCHEMA_03" successfully completed at 10:10:44
2.容器外面拷贝 把容器内把文件拷贝出来
##docker cp <container_id>:/path/to/container/file /path/to/host/destination
docker cp oracle11g:/opt/oracle/dpdump/khgl_20240806_1800.dmp /Users/liuyulong/dpdump/
3.恢复
1.把本地文件复制到容器内dump目录 直接在本地目录好像不行
docker cp /Users/liuyulong/dpdump/khgl_20240805_230018.dmp oracle11g:/opt/oracle/dpdump/khgl_20240805_230018.dmp
--恢复
#语法:docker exec oracle11g su oracle -lc "impdp 用户名/密码@127.0.0.1:1521/orcl directory=DATA_PUMP_DIR remap_schema=原用户名:导入后的用户名 REMAP_TABLESPACE=原表空间名:目标表空间名 dumpfile=备份文件名.dmp"
2.导入备份文件到数据库
示例:
docker exec oracle11g su oracle -lc "impdp khgl/sa@127.0.0.1:1521/orcl directory=DATA_PUMP_DIR remap_schema=khgl:khgl REMAP_TABLESPACE=khgl:khgl dumpfile=khgl_20240805_230018.dmp"