在Docker中使用Oracle 18c(12.2.0.2)

文章目录

  • 一、从Docker Hub或阿里云镜像下载小麦苗上传的镜像文件
  • 二、创建容器并启动数据库
  • 三、尽情使用吧
    • 3.1 数据库使用
    • 3.2 创建数据库
    • 3.3 EMDE的使用
    • 3.4 使用ssh连接到容器内
    • 3.5 外部客户端连接容器内的数据库

一、从Docker Hub或阿里云镜像下载小麦苗上传的镜像文件

# 从Docker hub下载,网络不好时,一般比较慢
docker pull lhrbest/oracle18clhr_rpm_db
# 可以选择从阿里云下载
docker pull registry.cn-hangzhou.aliyuncs.com/lhrbest/oracle18clhr_rpm_db
# 从阿里云下载后可以tag成如下形式
docker tag registry.cn-hangzhou.aliyuncs.com/lhrbest/oracle18clhr_rpm_db lhrbest/oracle18clhr_rpm_db

镜像大概5G左右,解压后大约13G左右,所以请保留充足的空间。执行过程如下:

[root@docker35 ~]# docker pull registry.cn-hangzhou.aliyuncs.com/lhrbest/oracle18clhr_rpm_db
Using default tag: latest
latest: Pulling from lhrbest/oracle18clhr_rpm_db
ac9208207ada: Already exists 
5f7f556e707f: Pull complete 
Digest: sha256:e2e90034c232e328441b704a3db2edeb13a83cfe5e5dd9221f6ee954c4efdf1e
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/lhrbest/oracle18clhr_rpm_db:latest
registry.cn-hangzhou.aliyuncs.com/lhrbest/oracle18clhr_rpm_db:latest
[root@docker35 ~]# docker tag registry.cn-hangzhou.aliyuncs.com/lhrbest/oracle18clhr_rpm_db lhrbest/oracle18clhr_rpm_db
[root@docker35 ~]# docker images | grep 18c
lhrbest/oracle18clhr_rpm_db                                     latest              d99be9a3f472        11 days ago         13.1GB
registry.cn-hangzhou.aliyuncs.com/lhrbest/oracle18clhr_rpm_db   latest              d99be9a3f472        11 days ago         13.1GB
[root@docker35 ~]# 

二、创建容器并启动数据库

# 1、创建容器
docker run -itd -h lhr18cocp --name lhr18cocp -p 222:22  -p 1522:1521 -p 5500:5500  --privileged=true lhrbest/oracle18clhr_rpm_db  /usr/sbin/init
# 2、进入容器
docker exec -it lhr18cocp bash
# 3、启动监听和数据库,使用root命令一键启动
/etc/init.d/oracledb_ORCLCDB-18c start

在创建容器时,需要注意端口映射关系。在以上命令中,主机的222、1521和5500这3个端口不能被占用,否则会创建失败。

如果想再创建一个容器,那么只需要将以上命令修改参数name和p不同即可,立马就可以拥有一个新的环境,而且和之前的IP地址不同,用来做测试再方便不过了,如下:

# 1、创建容器2
docker run -itd -h lhr18cocp --name lhr18cocp2 -p 223:22  -p 1523:1521 -p 5501:5500  --privileged=true lhrbest/oracle18clhr_rpm_db  /usr/sbin/init
# 2、进入容器2
docker exec -it lhr18cocp2 bash
# 3、启动监听和数据库,使用root命令一键启动
/etc/init.d/oracledb_ORCLCDB-18c start

执行过程:

[root@docker35 ~]# docker images | grep 18c
lhrbest/oracle18clhr_rpm_db                                     latest              d99be9a3f472        11 days ago         13.1GB
registry.cn-hangzhou.aliyuncs.com/lhrbest/oracle18clhr_rpm_db   latest              d99be9a3f472        11 days ago         13.1GB
[root@docker35 ~]# docker run -itd -h lhr18cocp --name lhr18cocp -p 222:22  -p 1522:1521 -p 5500:5500  --privileged=true lhrbest/oracle18clhr_rpm_db  /usr/sbin/init
4bf134861d2c67faf68b1a8c42beb66821abc0fa2fc49a34beb143d218f00657
[root@docker35 ~]# docker exec -it lhr18cocp bash

[root@lhr18crpm /]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@lhr18cocp /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.8  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:08  txqueuelen 0  (Ethernet)
        RX packets 672  bytes 101230 (98.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 593  bytes 3748736 (3.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@lhr18cocp /]# /etc/init.d/oracledb_ORCLCDB-18c start
Starting Oracle Net Listener.
Oracle Net Listener started.
Starting Oracle Database instance ORCLCDB.
Oracle Database instance ORCLCDB started.

[root@lhr18crpm /]# su - oracle
Last login: Sun Jun 28 14:05:18 CST 2020 on pts/0
[oracle@lhr18crpm ~]$ echo $ORACLE_HOME
/opt/oracle/product/18c/dbhome_1
[oracle@lhr18crpm ~]$ echo $ORACLE_SID
ORCLCDB

再创建一个新的容器,可以看到IP地址和之前的不同,容器和容器之间是互不影响的,用来做cdb和pdb的迁移再好不过了:

[root@docker35 ~]# docker run -itd -h lhr18cocp --name lhr18cocp2 -p 223:22  -p 1523:1521 -p 5501:5500  --privileged=true lhrbest/oracle18clhr_rpm_db  /usr/sbin/init
68a14533f2024f33400c37ba0056456834db17ac1d6af62b05464016b137bdcc
[root@docker35 ~]# docker exec -it lhr18cocp2 bash
[root@lhr18cocp /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.9  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:09  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

三、尽情使用吧

3.1 数据库使用

[root@lhr18cocp /]# su - oracle
Last login: Thu Jul  9 16:57:52 CST 2020 on pts/1

[oracle@lhr18cocp ~]$ lsnrctl status

LSNRCTL for Linux: Version 18.0.0.0.0 - Production on 09-JUL-2020 17:02:23

Copyright (c) 1991, 2018, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=lhr18cocp)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 18.0.0.0.0 - Production
Start Date                09-JUL-2020 16:57:52
Uptime                    0 days 0 hr. 4 min. 31 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /opt/oracle/product/18c/dbhome_1/network/admin/listener.ora
Listener Log File         /opt/oracle/diag/tnslsnr/lhr18cocp/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=lhr18cocp)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=lhr18cocp)(PORT=5500))(Security=(my_wallet_directory=/opt/oracle/product/18c/dbhome_1/admin/ORCLCDB/xdb_wallet))(Presentation=HTTP)(Session=RAW))
Services Summary...
Service "ORCLCDB" has 1 instance(s).
  Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "ORCLCDBXDB" has 1 instance(s).
  Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "a8cb5bdeb697291fe0530f0011acfe5a" has 1 instance(s).
  Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "orclpdb1" has 1 instance(s).
  Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
The command completed successfully

[oracle@lhr18cocp ~]$ sas

SQL*Plus: Release 18.0.0.0.0 - Production on Thu Jul 9 17:01:30 2020
Version 18.3.0.0.0

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


Connected to:
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.3.0.0.0

SYS@ORCLCDB> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 ORCLPDB1                       READ WRITE NO

3.2 创建数据库

我们也可以自己创建自己需要的数据库,如下所示,创建了一个非cdb的数据库,dbname和sid都是lhrdb:

[oracle@lhr18cocp ~]$ dbca -silent -ignorePreReqs  -createDatabase -templateName General_Purpose.dbc -responseFile NO_VALUE \
> -gdbname lhrdb  -sid lhrdb \
> -createAsContainerDatabase FALSE \
> -sysPassword lhr -systemPassword lhr -dbsnmpPassword lhr \
> -datafileDestination '/opt/oracle/oradata' -recoveryAreaDestination '/opt/oracle/flash_recovery_area' \
> -storageType FS \
> -characterset ZHS16GBK \
> -sampleSchema true \
> -totalMemory 1024 \
> -databaseType MULTIPURPOSE \
> -emConfiguration NONE
[WARNING] [DBT-06208] The 'SYS' password entered does not conform to the Oracle recommended standards.
   CAUSE: 
a. Oracle recommends that the password entered should be at least 8 characters in length, contain at least 1 uppercase character, 1 lower case character and 1 digit [0-9].
b.The password entered is a keyword that Oracle does not recommend to be used as password
   ACTION: Specify a strong password. If required refer Oracle documentation for guidelines.
[WARNING] [DBT-06208] The 'SYSTEM' password entered does not conform to the Oracle recommended standards.
   CAUSE: 
a. Oracle recommends that the password entered should be at least 8 characters in length, contain at least 1 uppercase character, 1 lower case character and 1 digit [0-9].
b.The password entered is a keyword that Oracle does not recommend to be used as password
   ACTION: Specify a strong password. If required refer Oracle documentation for guidelines.
Prepare for db operation
10% complete
Copying database files
40% complete
Creating and starting Oracle instance
42% complete
46% complete
50% complete
54% complete
58% complete
60% complete
Completing Database Creation
66% complete
69% complete
70% complete
Executing Post Configuration Actions
100% complete
Database creation complete. For details check the logfiles at:
 /opt/oracle/cfgtoollogs/dbca/lhrdb.
Database Information:
Global Database Name:lhrdb
System Identifier(SID):lhrdb
Look at the log file "/opt/oracle/cfgtoollogs/dbca/lhrdb/lhrdb0.log" for further details.

[oracle@lhr18cocp ~]$ . oraenv
ORACLE_SID = [ORCLCDB] ? lhrdb
The Oracle base remains unchanged with value /opt/oracle
[oracle@lhr18cocp ~]$ sas

SQL*Plus: Release 18.0.0.0.0 - Production on Thu Jul 9 17:20:47 2020
Version 18.3.0.0.0

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


Connected to:
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.3.0.0.0

SYS@lhrdb> show pdbs
SYS@lhrdb> 

3.3 EMDE的使用

这个镜像默认配置了镜像,端口为5500,所以EM的访问地址为:https://192.168.1.35:5500/em

[oracle@lhr18cocp ~]$ lsnrctl status | grep tcps
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=lhr18cocp)(PORT=5500))(Security=(my_wallet_directory=/opt/oracle/product/18c/dbhome_1/admin/ORCLCDB/xdb_wallet))(Presentation=HTTP)(Session=RAW))

3.4 使用ssh连接到容器内

C:\Users\lhrxxt>ssh root@192.168.1.35 -p222
The authenticity of host '[192.168.1.35]:222 ([192.168.1.35]:222)' can't be established.
ECDSA key fingerprint is SHA256:9LX3zOgSJ7HC5MztqfJg7S46TJLZBdwV4/xIdCkExy8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.1.35]:222' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Wed Jun 24 08:53:40 2020 from gateway
[root@lhr18cocp ~]# ps -ef|grep pmon
oracle     244     0  0 16:57 ?        00:00:00 ora_pmon_ORCLCDB
oracle    4143     0  0 17:19 ?        00:00:00 ora_pmon_lhrdb
root      5193  5125  0 17:31 pts/2    00:00:00 grep --color=auto pmon

3.5 外部客户端连接容器内的数据库

我们可以在容器外部通过sqlplus或其他工具连接容器内的数据库,注意此时的端口应该为1522:

C:\Users\lhrxxt>sqlplus sys/lhr@192.168.1.35:1522/ORCLCDB as sysdba

SQL*Plus: Release 12.1.0.2.0 Production on Thu Jul 9 17:39:43 2020

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


Connected to:
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production

SYS@192.168.1.35:1522/ORCLCDB> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 ORCLPDB1                       READ WRITE NO

如果使用PLSQL Developer也是可以连接的,如下:

怎么样,是不是很爽,是不是非常方便呢,文末记得点赞哈。

——————————————————————————————————————————

【干货来了|小麦苗IT资料分享】
★小麦苗DB职场干货:https://mp.weixin.qq.com/s/Vm5PqNcDcITkOr9cQg6T7w
★小麦苗数据库健康检查:https://share.weiyun.com/5lb2U2M
★小麦苗微店:https://weidian.com/s/793741433?wfr=c&ifr=shopdetail
★各种操作系统下的数据库安装文件(Linux、Windows、AIX等):https://pan.baidu.com/s/19yJdUQhGz2hTgozb9ATdAw 提取码:4xpv
★小麦苗分享的资料:https://share.weiyun.com/57HUxNi
★小麦苗课堂资料:https://share.weiyun.com/5fAdN5m
★小麦苗课堂试听资料:https://share.weiyun.com/5HnQEuL
★小麦苗出版的相关书籍:https://share.weiyun.com/5sQBQpY
★小麦苗博客文章:https://share.weiyun.com/5ufi4Dx
★数据库系列(Oracle、MySQL、NoSQL):https://share.weiyun.com/5n1u8gv
★公开课录像文件:https://share.weiyun.com/5yd7ukG
★其它常用软件分享:https://share.weiyun.com/53BlaHX
★其它IT资料(OS、网络、存储等):https://share.weiyun.com/5Mn6ESi
★Python资料:https://share.weiyun.com/5iuQ2Fn
★已安装配置好的虚拟机:https://share.weiyun.com/5E8pxvT
——————————————————————————————————————————

About Me
——————————————————————————————————————————
● 本文作者:小麦苗,部分内容整理自网络,若有侵权请联系小麦苗删除
● 《数据库笔试面试宝典》作者
● OCP、OCM、高可用(RAC+DG+OGG)网络班开讲啦,有需要的小伙伴可以私聊我
● 本文在itpub、博客园、CSDN和个人微信公众号( xiaomaimiaolhr)上有同步更新
● 本文itpub地址: http://blog.itpub.net/26736162
● 本文博客园地址: http://www.cnblogs.com/lhrbest
● 本文CSDN地址: https://blog.csdn.net/lihuarongaini
● 本文pdf版、个人简介及小麦苗云盘地址: http://blog.itpub.net/26736162/viewspace-1624453/
● 数据库笔试面试题库及解答: http://blog.itpub.net/26736162/viewspace-2134706/
● DBA宝典今日头条号地址: http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826
——————————————————————————————————————————
● QQ群号: 230161599 、618766405
● 微信群:可加我微信,我拉大家进群,非诚勿扰
● 联系我请加QQ好友 ( 646634621 ),注明添加缘由
● 于 2019-10-01 06:00 ~ 2019-10-31 24:00 在西安完成
● 最新修改时间:2019-10-01 06:00 ~ 2019-10-31 24:00
● 文章内容来源于小麦苗的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解
● 版权所有,欢迎分享本文,转载请保留出处
——————————————————————————————————————————
小麦苗的微店: https://weidian.com/s/793741433?wfr=c&ifr=shopdetail
小麦苗出版的数据库类丛书: http://blog.itpub.net/26736162/viewspace-2142121/
小麦苗OCP、OCM、高可用网络班: http://blog.itpub.net/26736162/viewspace-2148098/
小麦苗腾讯课堂主页: https://lhr.ke.qq.com/
——————————————————————————————————————————
使用微信客户端扫描下面的二维码来关注小麦苗的微信公众号( xiaomaimiaolhr)
及QQ群(DBA宝典)、添加小麦苗微信, 学习最实用的数据库技术。
小麦苗信息
——————————————————————————————————————————

你可能感兴趣的:(Oracle)