步骤:
- 先不挂载宿主机目录启动oracle实例
- 将docker内容文件拷贝到宿主机中
- 给宿主机的文件夹赋权限
- 修改docker-compose.yml 文件挂载上宿主机目录
- 重启docker服务
- 重启报错,进入docker服务解决错误。每次重启都需要此步操作!!!
编写docker-compose 启动实例
docker-compose.yml
version: '3.1'
services:
master:
image: registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
container_name: oracle
privileged: true
ports:
- 1521:1521
启动成功,进入容器
docker-compose up -d
docker exec -it oracle bash
配置环境,并创建用户
cd /home/oracle # 进入到 oracle 用户目录
source .bash_profile # 加载 oracle 环境变量
$PATH # 查看 oracle 环境变量是否生效
sqlplus / as sysdba # 连接 oracle 数据库
alter user system identified by oracle; # 修改 DBA 账号的密码
alter user sys identified by oracle; # 修改 DBA 账号的密码
alter profile default limit password_life_time unlimited; # 设置密码为永不过期
create user test identified by oracle; # 创建一个 test 用户,密码 oracle
select * from dba_users t where t.username = 'TEST'; # 查询用户是否创建成功
grant connect, resource to test; # 给用户授予连接和数据权限
相关配置(可省略)
grant select on V_$session to test;
grant select on V_$sesstat to test;
grant select on V_$statname to test;
show parameter deferred_segment_creation; -- 查看是否启用 true 为启动
alter system set deferred_segment_creation=false; -- 修改为不启用
show parameter deferred_segment_creation; -- 查看是否修改成功 false 未启用
plsql 连接
oracle_11g =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.168.1.***)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = helowin)
)
)
数据持久化
docker cp oracle:/home/oracle/app/oracle/oradata/helowin /usr/local/docker/oracle/helowin
cd /usr/local/docker/oracle
chown -R 500.500 ./helowin # 500 500 是容器内 oracle 组合用户的 id
关闭容器:
docker-compose down
修改docker-compose.yml
version: '3.1'
services:
master:
image: registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
container_name: oracle
privileged: true
ports:
- 1521:1521
# 新增以下信息
volumes:
- ./helowin:/home/oracle/app/oracle/oradata/helowin
查看日志:docker logs -f oracle,我们发现了一个错误,这是由于 oracle 为了数据安全,添加的版本验证
/home/oracle/app/oracle/product/11.2.0/dbhome_2
Processing Database instance "helowin": log file /home/oracle/app/oracle/product/11.2.0/dbhome_2/startup.log
Redo Buffers 24137728 bytes
ORA-00214: control file '/home/oracle/app/oracle/oradata/helowin/control01.ctl'
version 877 inconsistent with file
'/home/oracle/app/oracle/flash_recovery_area/helowin/control02.ctl' version 841
SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
/home/oracle/app/oracle/product/11.2.0/dbhome_2/bin/dbstart: Database instance "helowin" warm started.
进入容器内,解决错误,每次重启都需要此部操作!!!
docker exec -it oracle bash
cd /home/oracle # 进入到 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 # 以 dba 身份连接 oracle 数据库
shutdown immediate # 关闭数据库实例(这里会报错,不用管)
startup # 启动实例
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/278993.html