docker拉取oracle11g鏡像並配置數據持久化


一、拉取oracle11g鏡像

下載過程稍長,鏡像大小6.8G(之前拉取過了,所以就不截圖了)

#docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g

鏡像詳情:https://dev.aliyun.com/detail.html?spm=5176.1972343.2.8.E6Cbr1&repoId=1969

下載完成后 查看鏡像

# docker images

二、創建容器初步了解如何使用鏡像

1、創建容器

# docker run -v /opt:/data -p 1521:1521 --name oracle11g -h demo --restart=always -d registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g

說明

-v(映射主機目錄到容器內,把opt目錄映射到容器根目錄data下)

--name(容器名)

--restart=always(設置容器開機自啟動)

-d(創建容器之后不自動進入容器)

2、進入容器

# docker exec -it oracle11g bash

3、切換到root 用戶下

$ su root

密碼:helowin

4、編輯profile文件配置ORACLE環境變量

 # vi /etc/profile

在最后面添加如下內容

export ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_2           #oracle路徑
export ORACLE_SID=helowin                                                    #oracle啟動數據庫實例名
export PATH=$ORACLE_HOME/bin:$PATH                                           #添加系統環境變量

5、立即生效

# source /etc/profile

6、創建軟連接

ln -s $ORACLE_HOME/bin/sqlplus /usr/bin

7、切換到oracle 用戶

這里還要說一下,一定要寫中間的內條 -   必須要,否則軟連接無效

 # su - oracle

8、登錄oracle數據庫

$ sqlplus /nolog

SQL> conn /as sysdba

9、修改sys、system密碼

SQL> alter user system identified by system;

SQL> alter user sys identified by sys;

也可以創建用戶  create user test identified by test;

並給用戶賦予權限  grant connect,resource,dba to test;

修改oracle默認用戶密碼永不過期

SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

SQL> exit

exit  是退休sql 軟連接

10、查看監聽是否成功啟動

$ lsnrctl status

11、查看監聽配置文件

注意:oracle監聽文件不要隨意更改,如若更改需要先備份一份

$ cat /home/oracle/app/oracle/product/11.2.0/dbhome_2/network/admin/tnsnames.ora 

# tnsnames.ora Network Configuration File: /home/oracle/app/oracle/product/11.2.0/dbhome_2/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.

LISTENER_HELOWIN =
  (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))


HELOWIN =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = helowin)
    )
  )

$ cd /home/oracle/app/oracle/product/11.2.0/dbhome_2/network/admin/

$ vi listener.ora

# listener.ora Network Configuration File: /home/oracle/app/oracle/product/11.2.0/dbhome_2/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = )(PORT = 1521))
    )
  )

ADR_BASE_LISTENER = /home/oracle/app/oracle

連接oracle數據庫時,先檢查端口映射是否成功

打開Windows的doc窗口輸入一下內容

telnet ip地址 端口號

三、制作屬於自己定制的oracle11g鏡像

兩種方法

方法一:編寫Dockerfile文件

方法二:容器打包成鏡像

方法一:過程如下

有時間再寫

方法二:過程如下

1、創建容器

# docker run --name oracle11g -d oracle_11g

[root@docker-servers ~]# docker run --name oracle11g -d oracle_11g
4826bc11291ebe022a159544d7ad233896723ede260b913c38d2fc101112b04f
[root@docker-servers ~]#
[root@docker-servers ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
4826bc11291e        oracle_11g          "/bin/sh -c '/home..."   44 seconds ago      Up 19 seconds       1521/tcp                 oracle11g

2、進入容器

# docker exec -it oracle11g bash

[root@docker-servers ~]# docker exec -it oracle11g bash
[oracle@4826bc11291e /]$
[oracle@4826bc11291e /]$

3、切換到root 用戶下

root密碼為:helowin

[oracle@4826bc11291e /]$ su root
Password:
[root@4826bc11291e /]#

4、編輯profile文件配置ORACLE環境變量

# 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

5、立即生效

# source /etc/profile

6、創建軟連接

# ln -s $ORACLE_HOME/bin/sqlplus /usr/bin

7、切換到oracle 用戶

# su - oracle

8、登錄oracle數據庫

$ sqlplus / as sysdba

9、修改sys、system密碼

SQL> alter user system identified by system;

SQL> alter user sys identified by sys;

SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

[root@4826bc11291e /]# vi /etc/profile
[root@4826bc11291e /]# source /etc/profile
[root@4826bc11291e /]# ln -s $ORACLE_HOME/bin/sqlplus /usr/bin
[root@4826bc11291e /]#
[root@4826bc11291e /]# su - oracle
[oracle@4826bc11291e ~]$
[oracle@4826bc11291e ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Wed Aug 14 18:09:43 2019

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>
SQL> alter user system identified by system;

User altered.

SQL> alter user sys identified by sys;

User altered.

SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

Profile altered.

SQL>

10、退出容器打包成鏡像

退出容器

# exit

根據某個“容器 ID”來創建一個新的“鏡像”

# docker commit oracle11g oracle_11g:v1

[root@docker-servers ~]# docker commit oracle11g oracle_11g:v1
sha256:cbe72388da002ee16fa37a448221ec64f1bb5ab534d6904076f1276c3a7a4b72
[root@docker-servers ~]#
[root@docker-servers ~]# docker images
REPOSITORY                            TAG                 IMAGE ID            CREATED             SIZE
oracle_11g                            v1                  cbe72388da00        6 seconds ago       6.86 GB
oracle_11g                            latest              3fa112fd3642        3 years ago         6.85 GB

可以看到,和之前的容器相比,鏡像文件大了一點點

11、根據新鏡像運行一個容器

# docker run -p 1523:1521 --name oracle11g_test -d oracle_11g:v1

[root@docker-servers ~]# docker run -p 1523:1521 --name oracle11g_test -d oracle_11g:v1
2963da8bbe11d451c727d27a501af647d4655106e62fe97b509fd6f7590e860a
[root@docker-servers ~]#
[root@docker-servers ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
2963da8bbe11        oracle_11g:v1       "/bin/sh -c '/home..."   31 seconds ago      Up 9 seconds        0.0.0.0:1523->1521/tcp   oracle11g_test

12、外部測試連接

先監聽一下端口

# netstat -tunlp | grep 1523

[root@docker-servers ~]# netstat -tunlp | grep 1523
tcp        0      0 :::1523                 :::*                    LISTEN      23654/docker-proxy-

未能連接成功,實驗失敗

四、oracle容器數據持久化

具體方法說明:

如需映射oracle的數據文件 把容器內對應的文件,拷貝到宿主機,映射即可(如下)

/home/oracle/app/oracle/oradata/

/home/oracle/app/oracle/flash_recovery_area/helowin/

我映射的命令

# docker run -v /home/oracle11g-dev-pub/oradata:/home/oracle/app/oracle/oradata -v /home/oracle11g-dev-pub/flash_recovery_area/helowin/:/home/oracle/app/oracle/flash_recovery_area/helowin/ -p 1522:1521 --name oracle11g-dev -h dev -d oracle_11g

# docker run -v /opt/oracle11g-demo-pub/oradata:/home/oracle/app/oracle/oradata -v /opt/oracle11g-demo-pub/flash_recovery_area/helowin/:/home/oracle/app/oracle/flash_recovery_area/helowin/ -p 1521:1521 --name oracle11g-demo -h demo -d oracle_11g

具體操作如下:

1、先建一個測試容器

# docker run --name oracle11g_test -d oracle_11g

[root@localhost ~]# docker run --name oracle11g_test -d oracle_11g
0237fc1d788290398508749846bcce02640d17cfc588c460e2d46df31ebcf325

2、新建目錄

# mkdir -p /opt/oracle11g-demo-pub/flash_recovery_area

# mkdir -p /opt/oracle11g-demo-pub/oradata

[root@localhost ~]# mkdir -p /opt/oracle11g-demo-pub/flash_recovery_area
[root@localhost ~]# mkdir -p /opt/oracle11g-demo-pub/oradata

3、拷貝數據

# docker cp oracle11g_test:/home/oracle/app/oracle/oradata/helowin /opt/oracle11g-demo-pub/oradata

# docker cp oracle11g_test:/home/oracle/app/oracle/flash_recovery_area/helowin/ /opt/oracle11g-demo-pub/flash_recovery_area

[root@localhost ~]# docker cp oracle11g_test:/home/oracle/app/oracle/oradata/helowin /opt/oracle11g-demo-pub/oradata
[root@localhost ~]# docker cp oracle11g_test:/home/oracle/app/oracle/flash_recovery_area/helowin/ /opt/oracle11g-demo-pub/flash_recovery_area

4、創建容器

# docker run -v /opt/oracle11g-demo-pub/oradata:/home/oracle/app/oracle/oradata -v /opt/oracle11g-demo-pub/flash_recovery_area/helowin/:/home/oracle/app/oracle/flash_recovery_area/helowin/ -p 1521:1521 --name oracle11g-demo_new -h demo -d oracle_11g

[root@localhost ~]# docker run -v /opt/oracle11g-demo-pub/oradata:/home/oracle/app/oracle/oradata -v /opt/oracle11g-demo-pub/flash_recovery_area/helowin/:/home/oracle/app/oracle/flash_recovery_area/helowin/ -p 1521:1521 --name oracle11g-demo -h demo -d oracle_11g
416f7073da96fb4a1bd1fc5bde05ba29007dce0ba68b717f50101527cc39f441
[root@localhost ~]#

查看容器

[root@localhost flash_recovery_area]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
416f7073da96        oracle_11g          "/bin/sh -c '/home..."   About a minute ago   Up 59 seconds       0.0.0.0:1521->1521/tcp   oracle11g-demo
0237fc1d7882        oracle_11g          "/bin/sh -c '/home..."   4 minutes ago        Up 4 minutes        1521/tcp                 oracle11g_test

5、進入容器root授權

# docker exec -it oracle11g-demo bash

[root@localhost flash_recovery_area]# docker exec -it oracle11g-demo bash
[oracle@demo /]$
[oracle@demo /]$ cd /home/oracle/app/oracle/flash_recovery_area/helowin/
bash: cd: /home/oracle/app/oracle/flash_recovery_area/helowin/: Permission denied

目錄授權

$ su root

密碼:helowin

[oracle@demo /]$ su root
Password:
[root@demo /]# 

# cd /home/oracle/app/oracle/

# chown -R oracle.oinstall oradata

[root@demo oradata]# cd /home/oracle/app/oracle/
[root@demo oracle]#
[root@demo oracle]# ll
total 0
drwxr-x--- 1 oracle oinstall 21 Jan  4  2016 admin
drwxr-xr-x 5 oracle oinstall 43 Aug 23  2014 cfgtoollogs
drwxr-xr-x 3 oracle oinstall 17 Aug 23  2014 checkpoints
drwxrwxr-x 1 oracle oinstall 34 Aug 23  2014 diag
drwxr-x--- 4 oracle oinstall 36 Jan  4  2016 flash_recovery_area
drwxr-xr-x 3 root   root     21 Aug 14 19:58 oradata
drwxr-xr-x 3 oracle oinstall 18 Aug 26  2014 oradiag_oracle
drwxr-xr-x 1 oracle oinstall 20 Aug 27  2014 product
[root@demo oracle]#
[root@demo oracle]# chown -R oracle.oinstall oradata
[root@demo oracle]#
[root@demo oracle]# ll
total 0
drwxr-x--- 1 oracle oinstall 21 Jan  4  2016 admin
drwxr-xr-x 5 oracle oinstall 43 Aug 23  2014 cfgtoollogs
drwxr-xr-x 3 oracle oinstall 17 Aug 23  2014 checkpoints
drwxrwxr-x 1 oracle oinstall 34 Aug 23  2014 diag
drwxr-x--- 4 oracle oinstall 36 Jan  4  2016 flash_recovery_area
drwxr-xr-x 3 oracle oinstall 21 Aug 14 19:58 oradata
drwxr-xr-x 3 oracle oinstall 18 Aug 26  2014 oradiag_oracle
drwxr-xr-x 1 oracle oinstall 20 Aug 27  2014 product

# cd /home/oracle/app/oracle/flash_recovery_area

# chown -R oracle.oinstall helowin

[root@demo oracle]# cd /home/oracle/app/oracle/flash_recovery_area
[root@demo flash_recovery_area]#
[root@demo flash_recovery_area]# ll
total 0
drwxr-x--- 4 oracle oinstall 41 Jan  4  2016 HELOWIN
drwxr-x--- 2 root   root     52 Jan  4  2016 helowin
[root@demo flash_recovery_area]#
[root@demo flash_recovery_area]# chown -R oracle.oinstall helowin
[root@demo flash_recovery_area]#
[root@demo flash_recovery_area]# ll
total 0
drwxr-x--- 4 oracle oinstall 41 Jan  4  2016 HELOWIN
drwxr-x--- 2 oracle oinstall 52 Jan  4  2016 helowin

6、修改環境變量創建軟連接

# 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

# source /etc/profile

# ln -s $ORACLE_HOME/bin/sqlplus /usr/bin

[root@demo oracle]# vi /etc/profile
[root@demo oracle]# source /etc/profile
[root@demo oracle]# ln -s $ORACLE_HOME/bin/sqlplus /usr/bin
[root@demo oracle]#

7、重啟數據庫

# su - oracle

$ sqlplus / as sysdba

SQL> shutdown immediate;

SQL> startup

[oracle@demo ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Thu Aug 15 17:47:24 2019

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>
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>

8、修改sys、system密碼

SQL> alter user system identified by system;

SQL> alter user sys identified by sys;

設置密碼永不過期

SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

SQL> alter user system identified by system;

User altered.

SQL> alter user sys identified by sys;

User altered.

SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

Profile altered.

SQL>

 9、創建邏輯備份目錄和普通用戶存放的表空間的目錄

$ cd /home/oracle/app/oracle/oradata

$ mkdir oracle_imp_exp

$ mkdir tablespace

SQL> exit
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
[oracle@demo ~]$
[oracle@demo ~]$ cd /home/oracle/app/oracle/oradata
[oracle@demo oradata]$
[oracle@demo oradata]$ ls
helowin
[oracle@demo oradata]$
[oracle@demo oradata]$ mkdir oracle_imp_exp
[oracle@demo oradata]$ mkdir tablespace
[oracle@demo oradata]$ ll
total 0
drwxr-x--- 2 oracle oinstall 225 Jan  4  2016 helowin
drwxr-xr-x 2 oracle oinstall   6 Aug 15 19:11 oracle_imp_exp
drwxr-xr-x 2 oracle oinstall   6 Aug 15 19:12 tablespace

10、外部就可以測試連接了

已經成功連接上數據庫

五、持久化數據異機遷移

1、關閉容器數據打包

# docker stop oracle11g-demo

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
416f7073da96        oracle_11g          "/bin/sh -c '/home..."   32 minutes ago      Up 32 minutes       0.0.0.0:1521->1521/tcp   oracle11g-demo
0237fc1d7882        oracle_11g          "/bin/sh -c '/home..."   36 minutes ago      Up 36 minutes       1521/tcp                 oracle11g_test
[root@localhost ~]#
[root@localhost ~]# docker stop oracle11g-demo
oracle11g-demo
[root@localhost ~]#

數據打包

# tar zcvf oracle11g-demo-pub.tar.gz oracle11g-demo-pub/

[root@localhost ~]# cd /opt/
[root@localhost opt]#
[root@localhost opt]# ls
oracle11g-demo-pub
[root@localhost opt]#
[root@localhost opt]# tar zcvf oracle11g-demo-pub.tar.gz oracle11g-demo-pub/

2、遷移數據到別的Linux機器且具有docker環境

[root@localhost opt]# ls
ip.sh  oracle11g-demo-pub  oracle11g-demo-pub.tar.gz
[root@localhost opt]#
[root@localhost opt]# du -sh oracle11g-demo-pub.tar.gz
276M    oracle11g-demo-pub.tar.gz
[root@localhost opt]#
[root@localhost opt]# du -sh oracle11g-demo-pub
1.6G    oracle11g-demo-pub

# scp oracle11g-demo-pub.tar.gz root@192.168.2.207:/opt/

[root@localhost opt]# scp oracle11g-demo-pub.tar.gz root@192.168.2.207:/opt/
The authenticity of host '192.168.2.207 (192.168.2.207)' can't be established.
ECDSA key fingerprint is SHA256:1Pr9kAu2Qnph9tGbSOiW0hshVdzR0NAwknNOX5KwQII.
ECDSA key fingerprint is MD5:df:5d:6a:b9:a9:80:76:37:b6:90:61:a2:f3:c3:c1:7c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.207' (ECDSA) to the list of known hosts.
root@192.168.2.207's password:
oracle11g-demo-pub.tar.gz                                                                                                                                                100%  275MB  34.4MB/s   00:08
[root@localhost opt]#

3、另一台機器數據解壓

# tar zxvf oracle11g-demo-pub.tar.gz

[root@docker-servers ~]# cd /opt/
[root@docker-servers opt]#
[root@docker-servers opt]# ls
oracle11g-demo-pub.tar.gz
[root@docker-servers opt]#
[root@docker-servers opt]# tar zxvf oracle11g-demo-pub.tar.gz

解壓后

[root@docker-servers opt]# ls
oracle11g-demo-pub  oracle11g-demo-pub.tar.gz
[root@docker-servers opt]#
[root@docker-servers opt]# du -sh oracle11g-demo-pub
1.6G    oracle11g-demo-pub
[root@docker-servers opt]#
[root@docker-servers opt]# du -sh oracle11g-demo-pub.tar.gz
276M    oracle11g-demo-pub.tar.gz
[root@docker-servers opt]#

4、創建容器

# docker run -v /opt/oracle11g-demo-pub/oradata:/home/oracle/app/oracle/oradata -v /opt/oracle11g-demo-pub/flash_recovery_area/helowin/:/home/oracle/app/oracle/flash_recovery_area/helowin/ -p 1521:1521 --name oracle11g-demo_new -h demo -d oracle_11g

5、重啟數據庫

# docker exec -it oracle11g-demo bash

$ su root

# su - oracle

$ sqlplus / as sysdba

SQL> shutdown immediate;

SQL> startup

[root@localhost opt]# docker exec -it oracle11g-demo bash
[oracle@demo /]$
[oracle@demo /]$ su root
Password:
[root@demo /]#
[root@demo /]# su - oracle
[oracle@demo ~]$
[oracle@demo ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Sat Aug 17 22:11:22 2019

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>
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL>
SQL>
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>

 

6、外部連接,測試數據是否存在

經實驗,可以連接,並數據存在

參考博客

Docker拉取oracle11g鏡像配置

https://blog.csdn.net/qq_38380025/article/details/80647620

Docker 下拉取oracle 11g鏡像配置

https://blog.csdn.net/zwx521515/article/details/77982884


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM