docker-compose安装oracle11g并进行数据迁移(保姆级教程)
1. 先不挂载宿主机目录启动oracle实例
编写docker-compose 启动实例
docker-compose.yml
version: '3.1'
services:master:image: registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11gcontainer_name: oracleprivileged: trueports:- 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 连接
修改tnsnames.ora
find / -name tnsnames.ora
# /home/oracle/app/oracle/product/11.2.0/dbhome_2/network/admin/tnsnames.ora
# /home/oracle/app/oracle/product/11.2.0/dbhome_2/network/admin/samples/tnsnames.ora
vi /home/oracle/app/oracle/product/11.2.0/dbhome_2/network/admin/tnsnames.ora
将localhost修改成ip
oracle_11g =(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.168.1.***)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = helowin)))
数据持久化(将docker内容文件拷贝到宿主机中)
mkdir -p /usr/local/docker/oracle/
cd /usr/local/docker/oracle/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
docker-compose.yml在/usr/local/docker/oracle目录下
version: '3.1'
services:master:image: registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11gcontainer_name: oracleprivileged: trueports:- 1521:1521# 新增以下信息volumes:- ./helowin:/home/oracle/app/oracle/oradata/helowin
数据迁移
- 将/usr/local/docker/oracle/helowin目录下的文件全部删除
cd /usr/local/docker/oracle/helowin
rm -rf ./*
- 将原来的Oracle数据helowin下的文件,全部移到/home/oracle/app/oracle/oradata/helowin下
mv data(原来的数据) /usr/local/docker/oracle/helowin
- 给Oracle用户赋予权限
chown -R 500.500 ./helowin # 500 500 是容器内 oracle 组合用户的 id
- 启动docker-compose
docker-compose up -d
- 查看日志: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 841SQL> 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.ctlsqlplus / as sysdba # 以 dba 身份连接 oracle 数据库
shutdown immediate # 关闭数据库实例(这里会报错,不用管)
startup # 启动实例
- 如果出现
ORA-01113: file * needs media recovery
的错误
解决办法:
> sqlplus / as sysdba SQL>startup ERROR at line 1: ORA-01113: file * needs media recovery ORA-01110: data file *: 'xxx.dbf'
- 单机系统
su - oracle sqlplus / as sysdba recover database; shutdown immediate startup exit
- 高可用性系统
- 在root用户下使用命令hares -state Netlsnr -sys $(hostname) 查看执行结果是否为“online”,确保Netlsnr资源在线。
- 执行如下命令登录Oracle
su - oraclesqlplus commonuser/网管数据库密码 as sysdba;
- 登录成功之后,执行如下命令恢复数据文件
recover database; exit
- 在root用户下执行命令hares -offline Oracle -sys $(hostname) 使Oracle资源离线,然后再执行命令hares -online Oracle -sys $(hostname)使Oracle资源在线。