Docker安装

使用docker安装oracle,因为自己搭建配置的话可能时间太久太繁琐等等原因,也因为docker实在太方便了

使用docker-compose安装Oracle 11g,因为使用docker 直接pull启动的话没办法做到数据持久化,重启容器数据就不存在了很蛋疼

Oracle镜像拉取

  • 镜像是制作分享在阿里云上的,由于镜像比较大(6.9G),可能拉取时间会很久,需要耐心等待

    1
    [root@Mike-node1 ~]#  docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
  • 下载完成后,使用下列命令查看镜像

    1
    docker images

创建容器

  • 启动

    1
    docker run -d -p 1521:1521 --name oracle11g registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
  • 启动容器

    1
    docker start oracle11g

配置Oracle环境变量

  • 进入镜像配置

    1
    docker exec -it oracle11g bash
  • 切换到root用户 root/helowin

    1
    2
    3
    4
    5
    6
    [root@a8a161b66e1d /]# vi /etc/profile
    在文件末未添加

    export ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_2
    export ORACLE_SID=helowin
    export PATH=$ORACLE_HOME/bin:$PATH
  • 创建软链接

    1
    ln -s $ORACLE_HOME/bin/sqlplus /usr/bin
  • 切换到iracle用户下(注意中间有-)

    1
    su – oracle

连接数据库

  • 创建用户

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    [root@Mike-node1 ~]# docker exec -it oracle /bin/bash
    [oracle@1cab53ad073d /]$ cd /home/oracle/
    # 加载 oracle 环境变量
    [oracle@1cab53ad073d ~]$ source .bash_profile
    [oracle@1cab53ad073d ~]$
    # 登录oracle数据库
    [oracle@1cab53ad073d ~]$ sqlplus /nolog

    SQL*Plus: Release 11.2.0.1.0 Production on Wed Oct 10 12:54:06 2018

    Copyright (c) 1982, 2009, Oracle. All rights reserved.
    # 切换管理用户
    SQL> conn / as sysdba
    Connected.
    # 修改system用户账号密码为system
    SQL> alter user system identified by system;

    User altered.
    # 修改sys用户账号密码为system
    SQL> alter user sys identified by sys;

    User altered.
    # 创建内部管理员账号 sun 密码为 sun
    SQL> create user sun identified by sun ;

    User created.
    # 将dba权限授权给内部管理员账号
    SQL> grant connect,resource,dba to sun ;

    Grant succeeded.
  • 使用 Navicat 连接

    1
    2
    3
    4
    5
    6
    7
    连接类型: Basic
    主机:你的ip
    端口: 1521
    服务名: helowin
    SID
    用户名:sun
    密码:sun

持久化

  • 使用docker-compose安装,创建docker-compose文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [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]#
  • 启动容器进去修改配置:

    1
    2
    3
    4
    5
    6
    7
    [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 ~]$
  • 在容器里配置数据库用户:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    [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 sun identified by sun;

    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 = 'sun';


    SQL>
  • 持久化操作,在宿主机上使用命令

    • 并且把 helowin 目录所有者赋予 500,因为500是容器内 oracle 组合用户的 id
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    [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
    [root@Mike-node1 oracle]# chown -R 500.500 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]#
  • 关闭容器并且添加配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    [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]#
  • 查看容器日志

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [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.
  • 发现有错误我们进入容器内部解决一下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    [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 容器看是否可以保存数据不丢失!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@Mike-node1 oracle]# docker-compose stop
    Stopping oracle ... done
    [root@Mike-node1 oracle]# docker ps
    [root@Mike-node1 oracle]# docker-compose start
    Starting master ... done
    [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]#
  • 然后可以发现数据库里面的用户和表都还在,持久化顺利完成

参考