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
网上很多方法都是在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
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,手工调整后问题解决
经过如下多次重启服务,发现出现如下错误,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.
使用 :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