Docker搭建Oracle数据库

搭建测试Oracle数据库 :源码编译安装数据库太繁琐浪费时间,可以用Docker搭建Oracle数据库,搭建起来方便、快速。

一、Docker快速搭建Oracle数据库

1、拉取镜像
    docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g

2、新建容器
    docker run --name oracle -p 1521:1521 --privileged=true --restart=always \
    -e TZ=Asia/Shanghai \
    -itd registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g

3、进入容器,配置oracle信息
    docker exec -it -u 0 oracle bash        #-u 0   指定root用户访问
    
    #配置oracle环境变量
    vi /etc/profile
        export ORACLE_BASE=/home/oracle/app/oracle
        export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_2
        export ORACLE_SID=helowin
        export PATH=$PATH:$ORACLE_HOME/bin
    source /etc/profile
   su oracle    #切换到oracle用户
    #登录数据库,修改sys、system用户密码
        sqlplus /nolog       #登录oracle数据库
        conn /as sysdba      #切换管理用户
        alter user system identified by oracle;        #修改system用户账号密码;
        alter user sys identified by oracle;        #修改sys用户账号密码;
        create user clcs identified by clcs;         #创建内部管理员账号密码;
        grant connect,resource,dba to clcs;         #将dba权限授权给内部管理员账号和密码;
        ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;         #修改密码规则策略为密码永不过期;(会出现坑,后面讲解)
        alter system set processes=1000 scope=spfile;         #修改数据库最大连接数据;

二、持久化存储

1、进行持久化存储的准备工作
    #创建持久化存储目录
    sudo mkdir -p /home/docker/server/oracle/data/helowin
    #将oracle数据复制到本机
    sudo docker cp oracle:/home/oracle/app/oracle/oradata/helowin/ /home/docker/server/oracle/data
    #并且把 helowin 目录所有者赋予 500,因为500是容器内 oracle 组合用户的 id
    sudo chown -R 500:500 /home/docker/server/oracle/data

2、删除容器,并新建容器(挂载卷) 
    docker rm -f oracle
    docker run --name oracle -p 1521:1521 --privileged=true --restart=always \
    -v /home/docker/server/oracle/data/helowin:/home/oracle/app/oracle/oradata/helowin \
    -itd registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
3、进入容器再配置:
    #需要进入容器里删除新生成的版本控制文件,将数据卷中的版本控制文件复制为新生成的版本控制文件
        docker exec -it oracle bash
        cd /home/oracle
        source .bash_profile     #加载oracle环境变量
        rm -rf /home/oracle/app/oracle/flash_recovery_area/helowin/control02.ctl
        cp /home/oracle/app/oracle/oradata/helowin/control01.ctl /home/oracle/app/oracle/flash_recovery_area/helowin/control02.ctl
        sqlplus / as sysdba
        
        #修改信息后,重新启动数据库
        shutdown immediate; --关闭数据库
        startup; --启动数据库
        exit:退出软链接    

可以配置PLSQL Developer客户端连接数据库,进行SQL操作。

#搭建完数据库要做的一些SQL操作

创建表空间
    create tablespace TS_CLCS_APP datafile '${ORACLE_BASE}/oradata/helowin/TS_CLCS_APP.dbf' size 100M reuse autoextend on next 40M maxsize unlimited default storage(initial 128k next 128k minextents 2 maxextents unlimited);
删除表空间
    drop tablespace TS_CLCS_APP including contents and datafiles cascade constraints;        
修改表空间名称
    alter tablespace  TS_CLCS_APP rename to TS_CLCS;
创建用户和密码,并为用户指定表空间            
    create user clcs identified by clcs default tablespace TS_CLCS_APP;        
将dba权限授权给用户;
    grant connect,resource,dba to clcs;

 #已经搭建了oracle数据库,想要新增一个独立的库来存放另一个项目的数据:

查看表空间的存储位置
    select name from v$datafile;
创建表空间
    create temporary tablespace test_panda tempfile '/home/oracle/app/oracle/oradata/helowin/test_panda.dbf' size 100m reuse autoextend on next 20m maxsize unlimited;
创建用户,并为用户指定表空间
    create user test_panda identified by test_panda default tablespace test_panda;
为用户授权
    grant connect,resource,dba to test_panda;

 

注意:
  出现错误 database not open
  执行:
    alter database mount;
    alter database open;

 

参考原文:https://blog.csdn.net/hl449006540/article/details/124993749

 

posted @ 2022-06-23 16:41  等风来~~  阅读(3038)  评论(0编辑  收藏  举报