Docker下安装oracle11g数据库(Win10)

开始拉取镜像

docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g

下载完成后 查看镜像:
在这里插入图片描述

创建容器

docker run -d -p1521:1521 --name oracle11g registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g

当返回容器ID时,表明创建成功,但如果有异常,最大可能是端口被占用。
如果当前机器上本身就存在oracle服务且没有修改过端口,默认1521端口是被占用的,此时需要在映射中修改下端口
比如如下

docker run -d -p 1531:1521 --name oracle11g registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g

此时创建了一个容器名称为 oracle11g 端口为1531的oracle容器。

挂载本地卷

D:/docker/outData为本地机器目录,/home/oracle/app/oracle/oradata为容器内的目录,挂载卷可以把相关文件存储在本地目录,删除容器而不会导致数据被删除。

D:/docker/outData:/home/oracle/app/oracle/oradata

首先创建临时目录

运行oracle11g 形成必要的关键文件;使用命令cp 复制文件到本地目录

docker run --name oracle_temp -p 1500:1521 --privileged=true -d registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g:latest

cp oracle_temp:/home/oracle/app/oracle/oradata D:/docker/outData   #复制

docker rm -f oracle_temp #复制完成后 删除临时容器

挂载本地卷

docker run -d --name oracle11g -p 1531:1521 --privileged=true  -v D:/docker/outData:/home/oracle/app/oracle/oradata registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g:latest

–name:容器名称
-p:端口号 1531:1521 把容器内的端口1521 映射到本地机器1531上
–privileged=true 设置权限
-v:用于挂在本地卷(卷有多种,此处不详述), 本地目录:容器内目录 。

启动容器

启动容器,进入当前容器配置 其中 oracle11g 为容器名称

 docker start oracle11g
 docker exec -it oracle11g bash


切换到root 用户下
su root 密码:helowin

在这里插入图片描述

编辑profile文件配置ORACLE环境变量

网上很多方法都是在docker容器中查找并编辑profile文件 vi /etc/profile
但实际win10 场景下这样是无法进入profile的,可用如下方式进入,直接在登录的root场景下访问/etc/profile

在这里插入图片描述

如果出现如下错误,则表示没有操作当前文件的权限

在这里插入图片描述


可以通过 chmod +x /etc/profile 命令赋权限,此赋权限方法后续单独说明,此处不做详解

在这里插入图片描述


附权之后,再次访问,增加环境变量

1. export ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_2
2. export ORACLE_SID=helowin
3. export PATH=$ORACLE_HOME/bin:$PATH

使得环境变量生效

source /etc/profile

oracle数据库连接

软连接

 ln -s $ORACLE_HOME/bin/sqlplus /usr/bin

连接 并更改密码

sqlplus /nolog
conn /as sysdba
--改密码
alter user system identified by system;

alter user sys identified by sys;

--解锁scott用户(安装时若使用默认情况没有解锁和设置密码进行下列操作,要超级管理员操作)
alter user scott account unlock;

--解锁scott用户的密码【此句也可以用来重置密码】
alter user scott identified by tiger;

在这里插入图片描述

此时 外部就可以登录连接词数据库

在这里插入图片描述

连接成功

在这里插入图片描述

其他问题

挂载本地卷后,数据库启动失败

我挂载的本地目录,已经可以正常连接数据库,但使用 alert database mount; 启动服务时无法启动,出现ORA-00205: error in identifying control file, check alert log for more info 错误

[oracle@614726f2a0f3 /]$ sqlplus /nolog

SQL*Plus: Release 11.2.0.1.0 Production on Wed Aug 17 18:33:50 2022

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

SQL> conn /as sysdba
Connected.
SQL> alert database mount;
SP2-0734: unknown command beginning "alert data..." - rest of line ignored.
SQL> alter database mount;
alter database mount
*
ERROR at line 1:
ORA-00205: error in identifying control file, check alert log for more info

解决办法:
我本地目录挂载的有问题,我复制本地目录后,挂载目录比实际目录多了一个层级,调整了本地目录后问题解决
比如以control01.ctl 文件为例,我本地应该如下访问D:/docker/outData/oradata/helowin/control01.ctl
但我查询时,发现D:/docker/outData/oradata/oradata/helowin/control01.ctl,中间多了一层目录oradata,手工调整后问题解决

启动服务成功,但数据库修改system名称失败

经过如下多次重启服务,发现出现如下错误,ORA-00214: control file ‘/home/oracle/app/oracle/oradata/helowin/control01.ctl’
version 1416 inconsistent with file
‘/home/oracle/app/oracle/flash_recovery_area/helowin/control02.ctl’ version 841
经分析是因为我本地卷用的是其他库的备份,导致控制文件不同

SQL> conn /as sysdba
Connected.
SQL> alter user system indentified by system;
alter user system indentified by system
                  *
ERROR at line 1:
ORA-00922: missing or invalid option


SQL> shutdown abort
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.

Total System Global Area 1603411968 bytes
Fixed Size                  2213776 bytes
Variable Size             402655344 bytes
Database Buffers         1191182336 bytes
Redo Buffers                7360512 bytes
ORA-00205: error in identifying control file, check alert log for more info


SQL> startup mount;
ORA-01081: cannot start already-running ORACLE - shut it down first
SQL> shutdown abort
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.

Total System Global Area 1603411968 bytes
Fixed Size                  2213776 bytes
Variable Size             402655344 bytes
Database Buffers         1191182336 bytes
Redo Buffers                7360512 bytes
ORA-00214: control file '/home/oracle/app/oracle/oradata/helowin/control01.ctl'
version 1416 inconsistent with file
'/home/oracle/app/oracle/flash_recovery_area/helowin/control02.ctl' version 841


解决办法:
首先退出数据库到oracle层
然后进行转义复制,需要和报错中的两个目录匹配
‘/home/oracle/app/oracle/oradata/helowin/control01.ctl’
‘/home/oracle/app/oracle/flash_recovery_area/helowin/control02.ctl’

SQL> exit
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
[oracle@af06e1fdfd1a /]$ cp /home/oracle/app/oracle/flash_recovery_area/helowin/control02.ctl /home/oracle/app/oracle/flash_recovery_area/helowin/control02_bak.ctl
[oracle@af06e1fdfd1a /]$ cp /home/oracle/app/oracle/oradata/helowin/control01.ctl /home/oracle/app/oracle/flash_recovery_area/helowin/control02.ctl

复制后重新连接

启动服务,提示数据库无法连接

此问题是因为我原本本地卷中有这个数据库,但我在备份时没有备份这个库,导致数据库无法启动

SQL> alter user system identified by system;
alter user system identified by system
                                *
ERROR at line 1:
ORA-01109: database not open


SQL> alter database mount;
alter database mount
*
ERROR at line 1:
ORA-01100: database already mounted


SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6:
'/home/oracle/app/oracle/product/11.2.0/dbhome_2/dbs/CESHI_DAT.DBF'

解决办法:
下线此数据库后重新启动

SQL> alter database datafile 6 offline drop;

Database altered.

SQL> alter database open;

Database altered.

怎样迁移数据文件

此操作实际是生成本地数据库后的一个补充,生成数据库后默认在容器内,此操作则可以把容器内的数据库文件迁移到本地

首先找到数据空间对应文件的目录

SELECT*FROM DBA_DATA_FILES;

其中 TABLESPACE_NAME 为名称 file_name 为存储路径
如下
TABLESPACE_NAME:CESHI
FILE_NAME:/home/oracle/app/oracle/product/11.2.0/dbhome_2/dbs/CESHI_DAT_DBF

在这里插入图片描述


可以通过cd ls 找下对应文件如下操作

sh-4.1$ su root
Password:
[root@af06e1fdfd1a /]# cd /home/oracle/app/oracle/product/11.2.0/dbhome_2/dbs/
[root@af06e1fdfd1a helowin]# ls
CESHI_DAT.DBF  control01.ctl.bak  redo01.log  redo03.log    system01.dbf  undotbs01.dbf
control01.ctl       example01.dbf      redo02.log  sysaux01.dbf  temp01.dbf    users01.dbf
[root@af06e1fdfd1a helowin]#

在这里插入图片描述

其中 需要在root下寻找
ls找到的对应文件,绿色表示有全部权限,当为白色时,表示权限不足

下线本数据库

下线本地数据库是为了保证迁移后可以正确启动

alter tablespace CESHI offline;

下线后 再次查找 DBA_DATA_FILES 表,里面对应的字段就会变为 offline,此时程序就访问不到这个数据库了

从容器内复制到本地挂载目录

在root根目录下复制文件
/home/oracle/app/oracle/oradata/helowin/CESHI_DAT.DBF 目录为本地目录,因为之前挂载卷时已经进行了映射,容器内的/home/oracle/app/oracle/oradata,就是本地的D:/docker/outData/oradata 目录

cp /home/oracle/app/oracle/product/11.2.0/dbhome_2/dbs/CESHI_DAT.DBF /home/oracle/app/oracle/oradata/helowin/CESHI_DAT.DBF

修改数据空间对应的数据文件

此操作实际上就是修改DBA_DATA_FILES 对应的FILE_NAME 字段

SQL> alter database rename file '/home/oracle/app/oracle/product/11.2.0/dbhome_2/dbs/CESHI_DTA.DBF' to '/home/oracle/app/oracle/oradata/helowin/CESHI_DAT.DBF';
alter database rename file '/home/oracle/app/oracle/product/11.2.0/dbhome_2/dbs/CESHI_DTA.DBF' to '/home/oracle/app/oracle/oradata/helowin/CESHI_DAT.DBF'
*
ERROR at line 1:
ORA-01511: error in renaming log/data files
ORA-01141: error renaming data file 6 - new file
'/home/oracle/app/oracle/oradata/helowin/CESHI_DAT.DBF' not found
ORA-01110: data file 6:
'/home/oracle/app/oracle/product/11.2.0/dbhome_2/dbs/CESHI_DAT.DBF'
ORA-27041: unable to open file
Linux-x86_64 Error: 13: Permission denied
Additional information: 9

alter database rename file ‘原本目录文件’ to ‘新目录对应文件’
若是出现如上错误ORA-01511: ORA-01141: ORA-01110:ORA-27041: ,经过解读,最终的问题是ORA-01141:找不到这个文件,但我们可以实际看下,当前确实此目录下已经存在此文件

在这里插入图片描述

那出现这个问题最大的可能就是权限不足
可以用 ls -l 方式查看当前文件夹下所有文件的权限,因为我本地已经修改 所以权限一样

在这里插入图片描述

根目录下给此用户赋权限

[root@af06e1fdfd1a /]# chmod g+rwx,o+rwx,u+rwx /home/oracle/app/oracle/oradata/helowin/CESHI_DAT.DBF
SQL> alter TABLESPACE CWBASEOI70 offline;

Tablespace altered.
SQL> alter database rename file '/home/oracle/app/oracle/product/11.2.0/dbhome_2/dbs/CESHI_DAT.DBF' to '/home/oracle/app/oracle/oradata/helowin/CESHI_DAT.DBF';

Database altered.

online数据库

使用 :alter TABLESPACE CESHI ONLINE 可启动数据库

SQL>  alter TABLESPACE CWBASEOI70 ONLINE;
 alter TABLESPACE CWBASEOI70 ONLINE
*
ERROR at line 1:
ORA-01113: file 6 needs media recovery
ORA-01110: data file 6:
'/home/oracle/app/oracle/oradata/helowin/CWBASEOI70_DAT.DBF'

此时如果出现ORA-01113: file 6 needs media recovery 错误,则是迁移过程中还有介质未回复
使用如下恢复介质,然后online数据库

SQL>  recover datafile '/home/oracle/app/oracle/oradata/helowin/CESHI_DAT.DBF';
Media recovery complete.
SQL> alter TABLESPACE CESHI ONLINE;

Tablespace altered.

其他 问题及解决办法

在迁移过程中,会报一些奇怪的错,最终发现都是/home/oracle/app/oracle/oradata/helowin/CESHI_DAT.DBF 文件权限的问题,所以在迁移过程中一定优先处理权限

--问题1 恢复时报错
ORA-00283: recovery session canceled due to errors
ORA-01114: IO error writing block to file 6 (block # 1)
ORA-01110: data file 6:
'/home/oracle/app/oracle/oradata/helowin/CESHI_DAT.DBF'
ORA-27091: unable to queue I/O
ORA-27041: unable to open file
Linux-x86_64 Error: 13: Permission denied
Additional information: 3

--问题2 找不到文件
ERROR at line 1:
ORA-01525: error in renaming data files
ORA-01141: error renaming data file 6 - new file
'/home/oracle/app/oracle/oradata/helowin/CESHI_DAT.DBF' not found
ORA-01110: data file 6:
'/home/oracle/app/oracle/product/11.2.0/dbhome_2/dbs/CESHI_DAT.DBF'
ORA-27041: unable to open file
Linux-x86_64 Error: 13: Permission denied
Additional information: 9


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值