在Docker中只需2步即可拥有Oracle 18c环境

举报
小麦苗DB宝 发表于 2022/02/21 12:50:26 2022/02/21
【摘要】 一、从Docker Hub或阿里云镜像下载小麦苗上传的镜像文件 二、创建容器并启动数据库 三、尽情使用吧 3.1 数据库使用 3.2 创建数据库 3.3 EMDE的使用 3.4 使用ssh连接到容器内 3.5 外部客户端连接容器内的数据库 一、从Docker Hub或阿里云镜像下载小麦苗上传的镜像文件小麦苗的Docker Hub的地址:https://hub.docker.com/u/lh...

一、从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.
root@192.168.1.35'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也是可以连接的,如下:

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

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。