Docker安裝Oracle11g

背鍋的Mike發表於2020-11-18

為什麼使用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,因為不做持久化重啟的話資料會丟失哦~

 

本文分享完畢,感謝支援點贊~~

相關文章