为什么使用docker安装oracle,因为自己搭建配置的话可能时间太久太繁琐等等原因,也因为docker实在太方便了
本文主要是使用docker-compose安装Oracle 11g,因为使用docker 直接pull启动的话没办法做到数据持久化,重启容器数据就不存在了很蛋疼~
不过有同学可能需要,本文也说一下,啊哈哈~
Docker和Docker-compose安装
详见之前博客: https://www.cnblogs.com/mike666/p/13674180.html
Oracle镜像拉取:
[root@Mike-node1 ~]# docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
镜像是某位大神制作分享在阿里云上的,由于镜像比较大(6.9G),可能拉取时间会很久,需要耐心等待
第一种方式(不持久化)
使用Docker 启动容器:
[root@Mike-node1 ~]# docker run -d -p 1521:1521 --name oracle registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g 1cab53ad073d078736018a8a5906c5670fe0ae90671675f53eb18f18d4216e06 [root@Mike-node1 ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 [::]:1521 [::]:* LISTEN 0 100 [::1]:25 [::]:* [root@Mike-node1 ~]# [root@Mike-node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1cab53ad073d registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g "/bin/sh -c '/home/o…" About a minute ago Up 37 seconds 0.0.0.0:1521->1521/tcp oracle [root@Mike-node1 ~]#
可以看到已经启动一个 叫 oracle 的容器,并且端口映射到宿主机的 1521 端口
进入Oracle容器创建用户:
容器镜像系统用户root密码为 helowin
[root@Mike-node1 ~]# docker exec -it oracle /bin/bash [oracle@1cab53ad073d /]$ cd /home/oracle/ [oracle@1cab53ad073d ~]$ source .bash_profile [oracle@1cab53ad073d ~]$ [oracle@1cab53ad073d ~]$ sqlplus /nolog SQL*Plus: Release 11.2.0.1.0 Production on Wed Nov 18 11:10:07 2020 Copyright (c) 1982, 2009, Oracle. All rights reserved. SQL> conn /as sysdba Connected. SQL> alter user system identified by system; User altered. SQL> alter user sys identified by system; User altered. SQL> create user mike identified by mikeops; User created. SQL> grant connect,resource,dba to mike; Grant succeeded. SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED; Profile altered. SQL> alter system set processes=2000 scope=spfile; System altered. SQL> select * from dba_users t where t.username = 'MIKE'; USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- MIKE 91 USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- OPEN USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- USERS TEMP 18-NOV-20 USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- DEFAULT DEFAULT_CONSUMER_GROUP USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- 10G 11G N PASSWORD USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- SQL>
其中涉及到的命令含义:
cd /home/oracle # 进入到 oracle 用户目录
source .bash_profile # 加载 oracle 环境变量
sqlplus /nolog # 登录oracle数据库
conn /as sysdba # 切换管理用户
alter user system identified by system; # 修改system用户账号密码为system
alter user sys identified by system; # 修改sys用户账号密码为system
create user mike identified by mikeops; # 创建内部管理员账号mike 密码为mikeops
grant connect,resource,dba to mike; # 将dba权限授权给内部管理员账号
ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED; # 修改密码规则策略为密码永不过期
alter system set processes=2000 scope=spfile; # 修改数据库最大连接数据
select * from dba_users t where t.username = 'MIKE'; # 查看mike用户创建信息
使用 Navicat 连接 连接类型: Basic 主机:你的ip 端口: 1521 服务名: helowin SID 用户名:mike 密码:mikeops
关于使用 Navicat 连接oracle
需要到官网下载对应的插件 https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
然后找到
Name |
Download |
Description |
---|---|---|
Instant Client Package - Basic | All files required to run OCI, OCCI, and JDBC-OCI applications
(54,956,947 bytes) |
注册一个用户登录下载这个zip包
在工具>选项>环境>OCI环境,这里点刚刚解压的文件就可以了
第二种方式(持久化)
使用docker-compose安装:
创建docker-compose文件
[root@Mike-node1 ~]# mkdir -p /data/oracle [root@Mike-node1 ~]# cd /data/oracle [root@Mike-node1 oracle]# vim docker-compose.yml version: '3.1' services: master: image: registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g container_name: oracle privileged: true ports: - 1521:1521 [root@Mike-node1 oracle]#
启动容器进去修改配置:
[root@Mike-node1 oracle]# docker-compose up -d Creating network "oracle_default" with the default driver Creating oracle ... done [root@Mike-node1 oracle]# docker exec -it oracle bash [oracle@7b3e628db32d /]$ cd /home/oracle/ [oracle@7b3e628db32d ~]$ source .bash_profile [oracle@7b3e628db32d ~]$
在容器里配置数据库用户:
[oracle@7b3e628db32d ~]$ sqlplus /nolog SQL*Plus: Release 11.2.0.1.0 Production on Wed Nov 18 13:14:15 2020 Copyright (c) 1982, 2009, Oracle. All rights reserved. SQL> conn /as sysdba Connected. SQL> alter user system identified by system; User altered. SQL> alter user sys identified by system; User altered. SQL> create user mike identified by yca1cahk; User created. SQL> grant connect,resource,dba to mike; Grant succeeded. SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED; Profile altered. SQL> alter system set processes=2000 scope=spfile; System altered. SQL> select * from dba_users t where t.username = 'MIKE'; USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- MIKE 91 USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- OPEN USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- USERS TEMP 18-NOV-20 USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- DEFAULT DEFAULT_CONSUMER_GROUP USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- 10G 11G N PASSWORD USERNAME USER_ID PASSWORD ------------------------------ ---------- ------------------------------ ACCOUNT_STATUS LOCK_DATE EXPIRY_DA -------------------------------- --------- --------- DEFAULT_TABLESPACE TEMPORARY_TABLESPACE CREATED ------------------------------ ------------------------------ --------- PROFILE INITIAL_RSRC_CONSUMER_GROUP ------------------------------ ------------------------------ EXTERNAL_NAME -------------------------------------------------------------------------------- PASSWORD E AUTHENTI -------- - -------- SQL>
配置用户 mike
密码为 yca1cahk
使用 Navicat 连接是否可以登录(Navicat)教程上面有
持久化操作,在宿主机上使用命令
[root@Mike-node1 ~]# docker cp oracle:/home/oracle/app/oracle/oradata/helowin/ /data/oracle/ [root@Mike-node1 ~]# cd /data/oracle/ [root@Mike-node1 oracle]# ll -h total 8.0K -rw-r--r-- 1 root root 176 Nov 18 13:07 docker-compose.yml drwxr-x--- 2 root root 4.0K Nov 18 13:10 helowin [root@Mike-node1 oracle]# [root@Mike-node1 oracle]# chown -R 500.500 helowin/ [root@Mike-node1 oracle]# ll total 8 -rw-r--r-- 1 root root 176 Nov 18 13:07 docker-compose.yml drwxr-x--- 2 500 500 4096 Nov 18 13:10 helowin [root@Mike-node1 oracle]# cd helowin/ [root@Mike-node1 helowin]# ll total 1626084 -rw-r----- 1 500 500 10076160 Nov 18 13:21 control01.ctl -rw-r----- 1 500 500 9748480 Jan 4 2016 control01.ctl.bak -rw-r----- 1 500 500 104865792 Nov 18 13:10 example01.dbf -rw-r----- 1 500 500 52429312 Nov 18 13:21 redo01.log -rw-r----- 1 500 500 52429312 Nov 18 13:10 redo02.log -rw-r----- 1 500 500 52429312 Nov 18 13:10 redo03.log -rw-r----- 1 500 500 534781952 Nov 18 13:21 sysaux01.dbf -rw-r----- 1 500 500 713039872 Nov 18 13:21 system01.dbf -rw-r----- 1 500 500 30416896 Jan 4 2016 temp01.dbf -rw-r----- 1 500 500 99622912 Nov 18 13:21 undotbs01.dbf -rw-r----- 1 500 500 5251072 Nov 18 13:10 users01.dbf [root@Mike-node1 helowin]#
并且把 helowin 目录所有者赋予 500,因为500是容器内 oracle 组合用户的 id
关闭容器并且添加配置
[root@Mike-node1 oracle]# docker-compose down Stopping oracle ... done Removing oracle ... done Removing network oracle_default [root@Mike-node1 oracle]# vim docker-compose.yml version: '3.1' services: master: image: registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g container_name: oracle privileged: true ports: - 1521:1521 volumes: - ./helowin/:/home/oracle/app/oracle/oradata/helowin/ [root@Mike-node1 oracle]# [root@Mike-node1 oracle]# docker-compose up -d Creating network "oracle_default" with the default driver Creating oracle ... done [root@Mike-node1 oracle]#
查看容器日志
[root@Mike-node1 oracle]# docker logs -f 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 7360512 bytes ORA-00214: control file '/home/oracle/app/oracle/oradata/helowin/control01.ctl' version 851 inconsistent with file '/home/oracle/app/oracle/flash_recovery_area/helowin/control02.ctl' version 841 SQL> 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. tail: unrecognized file system type 0x794c7630 for `/home/oracle/app/oracle/product/11.2.0/dbhome_2/startup.log'. Reverting to polling.
发现有错误我们进入容器内部解决一下
[root@Mike-node1 oracle]# docker exec -it oracle bash [oracle@1de544fa2642 /]$ cd /home/oracle/ [oracle@1de544fa2642 ~]$ source .bash_profile [oracle@1de544fa2642 ~]$ rm -rf /home/oracle/app/oracle/flash_recovery_area/helowin/control02.ctl [oracle@1de544fa2642 ~]$ [oracle@1de544fa2642 ~]$ cp /home/oracle/app/oracle/oradata/helowin/control01.ctl /home/oracle/app/oracle/flash_recovery_area/helowin/control02.ctl [oracle@1de544fa2642 ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.1.0 Production on Wed Nov 18 13:36:42 2020 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> shutdown immediate ORA-01507: database not mounted ORACLE instance shut down. SQL> startup 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 Database mounted. Database opened. SQL>
里面的操作是删除新生成的版本控制文件,将数据卷中的版本控制文件复制为新生成的版本控制文件
然后关闭数据库实例,再启动数据库实例
再次使用 Navicat 连接 Oracle,使用刚刚的用户看是否可以连接
检查发现可以正常连接之后,我们身边在数据库创建几张表或者用户
然后我们做一波骚操作,重启一下 oracle 容器看是否可以保存数据不丢失!!!
[root@Mike-node1 oracle]# ll total 8 -rw-r--r-- 1 root root 249 Nov 18 13:30 docker-compose.yml drwxr-x--- 2 500 500 4096 Nov 18 13:10 helowin [root@Mike-node1 oracle]# docker-compose stop Stopping oracle ... done [root@Mike-node1 oracle]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@Mike-node1 oracle]# [root@Mike-node1 oracle]# docker-compose start Starting master ... done [root@Mike-node1 oracle]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1de544fa2642 registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g "/bin/sh -c '/home/o…" 16 minutes ago Up 2 seconds 0.0.0.0:1521->1521/tcp oracle [root@Mike-node1 oracle]# [root@Mike-node1 oracle]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 [::]:1521 [::]:* LISTEN 0 100 [::1]:25 [::]:* [root@Mike-node1 oracle]#
然后可以发现数据库里面的用户和表都还在,持久化顺利完成
后期还是建议使用持久化来搭建 oracle,因为不做持久化重启的话数据会丢失哦~
本文分享完毕,感谢支持点赞~~