• 欢迎访问金笔头博客,这是一个菜鸟(伪)程序员的自留地,欢迎访问我的github:点击进入

docker笔记(5)-实践1.oracle11g数据库配置和运行

Docker eason 700次浏览 0个评论 扫描二维码

环境介绍

  • macOS 10.13.6
  • Docker Community Edition Version 18.06.1-ce-mac73 (26764)
  • 容器内系统CentOS
  • dmp文件导出编码为AMERICAN_AMERICA.ZHS16GBK

实验目标

通过docker运行oracle11g,并将本地dmp文件通过imp导入oracle数据库,容器重启依旧保留数据库中数据

涉及知识点

  • 镜像拉取及查看
  • 容器创建及运行
  • 容器内执行命令
  • 容器端口映射
  • 容器数据持久化

实践记录

1.拉取镜像 为节省时间,这里使用阿里云镜像仓库内的某位大大提供的镜像,镜像描述信息如下:

oracle 11.0.2 64bit 企业版 实例名: helowin
启动容器: docker run -d --name oracle_11g -p 1521:1521 registry.aliyuncs.com/helowin/oracle_11g
(plsql navicat等连接要注意端口的映射 )
1.进入容器 docker exec -it 容器ID /bin/bash
2.加载环境变量 source /home/oracle/.bash_profile
3.登录 sqlplus /nolog
connect /as sysdba 到这里就可以进行您想要的任何操作了
容器系统用户 root:helowin
(如需用plsql工具连接 请设置用户和密码) 举例:
create user test identified by test;
grant connect,resource,dba to test;
(很多留言问oracle 用户密码,想不明白,你们咋想的 )
如需映射oracle的数据文件 把容器内对应的文件,拷贝到宿主机,映射即可(如下)
/home/oracle/app/oracle/oradata/
/home/oracle/app/oracle/flash_recovery_area/helowin/
推荐使用docker-compose管理
重要提示:
1.镜像可以用,确定!
2.镜像比较大.6个多G,下载速度比较快,犹豫就千万别下。
3.oracle没有多余用户,没有多余设置,不要问用户和密码(sysdba直接登录做你想做的)

使用docker pull命令进行镜像拉取

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

2.执行docker images查看已下载的本地镜像

mac$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c82521676580 8 weeks ago 109MB
registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g latest 3fa112fd3642 2 years ago 6.85GB

3.基于刚拉取的镜像启动容器并指定容器名称为oracle11g,映射宿主机1521端口到容器1521端口,挂载宿主机目录/Users/mac/ora_data到容器目录/home/oracle/app/oracle/data/

mac$ docker run --name oracle11g -itd -p 127.0.0.1:1521:1521 -v /Users/mac/ora_data:/home/oracle/app/oracle/data/ registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g /bin/bash

部分参数解释:

  • –name:为容器指定名称
  • -p:把容器端口映射到一个特定地址的特定端口,一般格式为hostip:hostPort:containerPort
  • -v:挂载宿主机目录/文件到容器中作为数据卷,一般格式为hostDirOrFile:/containerDirOrFile,如果宿主机或容器目录不存在,会自动生成

4.查看本地容器情况

mac$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bb30311a9cee registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g "/bin/sh -c '/home/o…" 2 minutes ago Up 2 minutes 127.0.0.1:1521->1521/tcp oracle11g
19d06aea51a0 nginx "nginx -g 'daemon of…" 3 weeks ago Exited (137) 3 weeks ago webserver

5.进入容器oracle11g,加载环境变量

mac$ docker exec -it oracle11g bash
[oracle@bb30311a9cee /]$ source /home/oracle/.bash_profile

6.将要导入的dsms.dmp文件复制到本地目录/Users/mac/ora_data下,在容器内/home/oracle/app/oracle/data/目录下就能看到要导入的文件了

[oracle@bb30311a9cee /]$ cd /home/oracle/app/oracle/data/
[oracle@bb30311a9cee data]$ ll
total 1040
-rw-r--r-- 1 oracle oinstall 1062912 Sep 20 18:41 dsms.dmp

7.创建表空间和数据库用户,为导入dmp文件做准备

--1.以操作系统验证的方式登录
sqlplus / as sysdba

--2.创建表空间
CREATE TABLESPACE DSMS DATAFILE '/home/oracle/app/oracle/data/DSMS.dbf' SIZE 32M AUTOEXTEND ON NEXT 32M MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL;

--3.创建用户
CREATE USER DSMS IDENTIFIED BY "DSMS" ACCOUNT UNLOCK DEFAULT TABLESPACE DSMS;

--4.授权
GRANT DBA TO DSMS;

具体操作如下:

[oracle@91a98478de9b /]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Sun Sep 23 18:19:21 2018

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> CREATE TABLESPACE DSMS DATAFILE '/home/oracle/app/oracle/data/DSMS.dbf' SIZE 32M AUTOEXTEND ON NEXT 32M MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL;

Tablespace created.

SQL> CREATE USER DSMS IDENTIFIED BY "DSMS" ACCOUNT UNLOCK DEFAULT TABLESPACE DSMS;

User created.

SQL> GRANT DBA TO DSMS;

Grant succeeded.

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

8.为了防止出现中文注释乱码,需要先查询容器内数据库编码,如果和要导入的dmp文件编码不一样,需要先更改数据库编码与dmp文件编码一致

SQL> select userenv('language') from dual;

USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.AL32UTF8

可以看到容器内数据库编码为AMERICAN_AMERICA.AL32UTF8,所以需要执行如下操作更改

#关闭数据库
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

#以mount打来数据库
SQL> startup mount;
ORACLE instance started.

Total System Global Area 1603411968 bytes
Fixed Size    2213776 bytes
Variable Size    419432560 bytes
Database Buffers    1174405120 bytes
Redo Buffers    7360512 bytes
Database mounted.

#设置session
SQL> ALTER SYSTEM ENABLE RESTRICTED SESSION;
SQL> ALTER SYSTEM SET JOB_QUEUE_PROCESSES=0;
SQL> ALTER SYSTEM SET AQ_TM_PROCESSES=0;

#启动数据库
SQL> alter database open;

#修改字符集
SQL> ALTER DATABASE character set INTERNAL_USE ZHS16GBK;

#关闭并重新启动
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup;
ORACLE instance started.

Total System Global Area 1603411968 bytes
Fixed Size    2213776 bytes
Variable Size    419432560 bytes
Database Buffers    1174405120 bytes
Redo Buffers    7360512 bytes
Database mounted.
Database opened.

#检查数据库字符集
SQL> select userenv('language') from dual;

USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.ZHS16GBK

可以看到变更过后数据库字符集编码已经为AMERICAN_AMERICA.ZHS16GBK

9.导入dmp文件

#设置导入编码
[oracle@8ee4cf3f4b17 /]$ export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
#开始导入
[oracle@8ee4cf3f4b17 /]$ imp DSMS/DSMS@helowin file = /home/oracle/app/oracle/data/dsms.dmp full = y;

Import: Release 11.2.0.1.0 - Production on Mon Sep 24 14:22:12 2018

Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V11.02.00 via conventional path
import done in ZHS16GBK character set and AL16UTF16 NCHAR character set
. importing DSMS's objects into DSMS
. . importing table "FLAG_HARVEST_STEP" 3 rows imported
...

10.至此dmp文件数据已成功导入数据库,重启容器验证,发现表数据依旧存在,Done!

#停止容器
mac$ docker stop oracle11g
#启动容器
mac$ docker start oracle11g
#进入容器
mac$ docker exec -it oracle11g bash
#加载环境变量
[oracle@fc0dc4aced75 /]$ source /home/oracle/.bash_profile
#sqlplus登录
[oracle@fc0dc4aced75 /]$ sqlplus
Enter user-name: DSMS
Enter password:

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
#表查询
SQL> select * from FLAG_HARVEST_STEP;

ETL LAST_STARTTIME    LAST_ENDTIME    LAST_JOBSTATUS    LAST_TXDA
--- ------------------- ------------------- -------------------- ---------
SVI 2013-12-25    2013-12-25    Done    13-DEC-13
CVI 2013-12-25    2013-12-25    Done    13-DEC-13
TVI 2013-12-25    2013-12-25    Done    13-DEC-13

金笔头博客, 版权所有丨如未注明 , 均为原创, 转载请注明docker笔记(5)-实践1.oracle11g数据库配置和运行
喜欢 (2)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址