一、拉取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