注册

在unbuntu下Docker安装oracle和mysql

大神解说在unbuntu下docker安装oracle和mysql

一、安装docker

更新apt源,安装CA证书,命令如下:

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates

添加GPG密钥:

sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

打开/etc/apt/sources.list.d/docker.list文件(没有就建一个)

添加源deb https://apt.dockerproject.org/repo ubuntu-xenial main然后保存

根据系统版本选择不同的源:

On Ubuntu Precise 12.04 (LTS)
deb https://apt.dockerproject.org/repo ubuntu-precise main

On Ubuntu Trusty 14.04 (LTS)
deb https://apt.dockerproject.org/repo ubuntu-trusty main

Ubuntu Wily 15.10
deb https://apt.dockerproject.org/repo ubuntu-wily main

Ubuntu Xenial 16.04 (LTS)
deb https://apt.dockerproject.org/repo ubuntu-xenial main

这里选择Ubuntu Xenial 16.04 (LTS)

更新apt,并确定Docker 的源正确

sudo apt-get update
apt-cache policy docker-engine

安装:

sudo apt-get install docker-engine

支持普通用户使用docker

ppa安装docker组已经被创建了,不然sudo groupadd docker建一下

sudo gpasswd -a ${USER} docker
sudo service docker restart
sudo chmod a+rw /var/run/docker.sock

docker search : 从Docker Hub查找oracle镜

$ docker search oracle
NAME                                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
oraclelinux                         Oracle Linux is an open-source operating s...   398       [OK]       
frolvlad/alpine-oraclejdk8          The smallest Docker image with OracleJDK 8...   269                  [OK]
alexeiled/docker-oracle-xe-11g      This is a working (hopefully) Oracle XE 11...   221                  [OK]
sath89/oracle-12c                   Oracle Standard Edition 12c Release 1 with...   214                  [OK]
sath89/oracle-xe-11g                Oracle xe 11g with database files mount su...   133                  [OK]
isuper/java-oracle                  This repository contains all java releases...   55                   [OK]
jaspeen/oracle-11g                  Docker image for Oracle 11g database            54                   [OK]
oracle/glassfish                    GlassFish Java EE Application Server on Or...   30                   [OK]
oracle/openjdk                      Docker images containing OpenJDK Oracle Linux   25                   [OK]
airdock/oracle-jdk                  Docker Image for Oracle Java SDK (8 and 7)...   23                   [OK]
ingensi/oracle-jdk                  Official Oracle JDK installed on centos.        21                   [OK]
cogniteev/oracle-java               Oracle JDK 6, 7, 8, and 9 based on Ubuntu ...   20                   [OK]
wnameless/oracle-xe-11g             Dockerfile of Oracle Database Express Edit...   16                   [OK]
n3ziniuka5/ubuntu-oracle-jdk        Ubuntu with Oracle JDK. Check tags for ver...   14                   [OK]
oracle/nosql                        Oracle NoSQL on a Docker Image with Oracle...   13                   [OK]
collinestes/docker-node-oracle      A container with Node.js/Oracle instant cl...   9                    [OK]
andreptb/oracle-java                Debian Jessie based image with Oracle JDK ...   7                    [OK]
sgrio/java-oracle                   Docker images of Java 7/8 provided by Orac...   7                    [OK]
openweb/oracle-tomcat               A fork off of Official tomcat image with O...   7                    [OK]
flurdy/oracle-java7                 Base image containing Oracle's Java 7 JDK       5                    [OK]
davidcaste/debian-oracle-java       Oracle Java 8 (and 7) over Debian Jessie        3                    [OK]
teradatalabs/centos6-java8-oracle   Docker image of CentOS 6 with Oracle JDK 8...   3                    
spansari/nodejs-oracledb            nodejs with oracledb installed globally on...   1                    
publicisworldwide/oracle-core       This is the core image based on Oracle Lin...   1                    [OK]
sigma/nimbus-lock-oracle                                                            0                    [OK]
二、把oracle-oracle-xe-11g pull到本地
docker pull sath89/oracle-xe-11g

pull后显示:

$ docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
hello-world            latest              f2a91732366c        8 days ago          1.85kB
sath89/oracle-xe-11g   latest              04851454491b        3 months ago        792MB

创建并启动oracle实例:

docker run -d  -p 9090:8080 -p 1521:1521 -v /home/${USER}/oracle/data:/u01/app/oracle sath89/oracle-xe-11g

其中 -p 9090:8080是docker镜像中Oracle Application Express web management端口8080映射到本地9090端口:

http://localhost:9090/apex
workspace: INTERNAL
user: ADMIN
password: oracle

-p 1521:1521是oracle服务端口,如果本地端口不想用1521可以修改第一个1521。

三、安装oracle即时客户端instantclient:

具体参考以前的一个博文打造ubuntu下精简版的oracle客户端及pro*c编译环境

11g的instantclient软件oracle官网上找不到了我把它放到天翼云上(访问码:6540)

本地配置oracle的SID:

在/opt/oracle/product/network/admin/tnsnames.ora(和instantclient路径有关)追加:

XE =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = xe)
    )
  )

不配置也可以直接:

sqlplus sys/oracle@127.0.0.1:1521/xe as sysdba
sqlplus system/oracle@//localhost:1521/xe

配置好了就可以这样访问

sqlplus sys/oracle@xe as sysdba
sqlplus system/oracle@xe

其中sys和system是预设的两个用户,一个是sysdba权限一个是system权限。初始密码都为oracle

建议安装成功后立刻修改sys密码

sqlplus sys/oracle@xe as sysdba
SQL>alter user sys identified by your-passwd

创建表空间

SQL> create tablespace myts  datafile  '/u01/app/oracle/oradata/XE/myts.dbf'  size 100M autoextend on next 5M maxsize 400M;

修改系统默认表空间

SQL>  alter database default tablespace MYTS;
SQL>  select a.property_name, a.property_value from database_properties a where a.property_name like '%DEFAULT%';

PROPERTY_NAME
--------------------------------------------------------------------------------
PROPERTY_VALUE
--------------------------------------------------------------------------------
DEFAULT_TEMP_TABLESPACE
TEMP

DEFAULT_PERMANENT_TABLESPACE
MYTS

DEFAULT_EDITION
ORA$BASE

.......

创建用户、授权并制定表空间

SQL> create user scott identified by tiger;
SQL> grant connect, resource to scott;
SQL> alter user scott default tablespace myts;

建表脚本 myscott.sql :

--------------------------------------------------------
--  File created - 星期六-六月-14-2014   
--------------------------------------------------------
--------------------------------------------------------
--  DDL for Table BONUS
--------------------------------------------------------

  CREATE TABLE "BONUS" 
   ( "ENAME" VARCHAR2(10), 
 "JOB" VARCHAR2(9), 
 "SAL" NUMBER, 
 "COMM" NUMBER
   ) ;
--------------------------------------------------------
--  DDL for Table DEPT
--------------------------------------------------------

  CREATE TABLE "DEPT" 
   ( "DEPTNO" NUMBER(2,0), 
 "DNAME" VARCHAR2(14), 
 "LOC" VARCHAR2(13)
   ) ;
--------------------------------------------------------
--  DDL for Table EMP
--------------------------------------------------------

  CREATE TABLE "EMP" 
   ( "EMPNO" NUMBER(4,0), 
 "ENAME" VARCHAR2(10), 
 "JOB" VARCHAR2(9), 
 "MGR" NUMBER(4,0), 
 "HIREDATE" DATE, 
 "SAL" NUMBER(7,2), 
 "COMM" NUMBER(7,2), 
 "DEPTNO" NUMBER(2,0)
   ) ;
--------------------------------------------------------
--  DDL for Table SALGRADE
--------------------------------------------------------

  CREATE TABLE "SALGRADE" 
   ( "GRADE" NUMBER, 
 "LOSAL" NUMBER, 
 "HISAL" NUMBER
   ) ;

---------------------------------------------------
--   DATA FOR TABLE BONUS
--   FILTER = none used
---------------------------------------------------
REM INSERTING into BONUS

---------------------------------------------------
--   END DATA FOR TABLE BONUS
---------------------------------------------------


---------------------------------------------------
--   DATA FOR TABLE DEPT
--   FILTER = none used
---------------------------------------------------
REM INSERTING into DEPT
Insert into DEPT (DEPTNO,DNAME,LOC) values (10,'ACCOUNTING','NEW YORK');
Insert into DEPT (DEPTNO,DNAME,LOC) values (20,'RESEARCH','DALLAS');
Insert into DEPT (DEPTNO,DNAME,LOC) values (30,'SALES','CHICAGO');
Insert into DEPT (DEPTNO,DNAME,LOC) values (40,'OPERATIONS','BOSTON');

---------------------------------------------------
--   END DATA FOR TABLE DEPT
---------------------------------------------------


---------------------------------------------------
--   DATA FOR TABLE EMP
--   FILTER = none used
---------------------------------------------------
REM INSERTING into EMP
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7369,'SMITH','CLERK',7902,to_timestamp('17-12月-80 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),800,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7499,'ALLEN','SALESMAN',7698,to_timestamp('20-2月 -81 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),1600,300,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7521,'WARD','SALESMAN',7698,to_timestamp('22-2月 -81 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),1250,500,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7566,'JONES','MANAGER',7839,to_timestamp('02-4月 -81 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),2975,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7654,'MARTIN','SALESMAN',7698,to_timestamp('28-9月 -81 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),1250,1400,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7698,'BLAKE','MANAGER',7839,to_timestamp('01-5月 -81 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),2850,null,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7782,'CLARK','MANAGER',7839,to_timestamp('09-6月 -81 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),2450,null,10);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7788,'SCOTT','ANALYST',7566,to_timestamp('19-4月 -87 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),3000,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7839,'KING','PRESIDENT',null,to_timestamp('17-11月-81 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),5000,null,10);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7844,'TURNER','SALESMAN',7698,to_timestamp('08-9月 -81 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),1500,0,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7876,'ADAMS','CLERK',7788,to_timestamp('23-5月 -87 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),1100,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7900,'JAMES','CLERK',7698,to_timestamp('03-12月-81 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),950,null,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7902,'FORD','ANALYST',7566,to_timestamp('03-12月-81 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),3000,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7934,'MILLER','CLERK',7782,to_timestamp('23-1月 -82 12.00.00.000000000 上午','DD-MON-RR HH.MI.SS.FF AM'),1300,null,10);

---------------------------------------------------
--   END DATA FOR TABLE EMP
---------------------------------------------------


---------------------------------------------------
--   DATA FOR TABLE SALGRADE
--   FILTER = none used
---------------------------------------------------
REM INSERTING into SALGRADE
Insert into SALGRADE (GRADE,LOSAL,HISAL) values (1,700,1200);
Insert into SALGRADE (GRADE,LOSAL,HISAL) values (2,1201,1400);
Insert into SALGRADE (GRADE,LOSAL,HISAL) values (3,1401,2000);
Insert into SALGRADE (GRADE,LOSAL,HISAL) values (4,2001,3000);
Insert into SALGRADE (GRADE,LOSAL,HISAL) values (5,3001,9999);

---------------------------------------------------
--   END DATA FOR TABLE SALGRADE
---------------------------------------------------


--------------------------------------------------------
--  DDL for Index PK_EMP
--------------------------------------------------------

  CREATE UNIQUE INDEX "PK_EMP" ON "EMP" ("EMPNO") 
  ;
--------------------------------------------------------
--  DDL for Index PK_DEPT
--------------------------------------------------------

  CREATE UNIQUE INDEX "PK_DEPT" ON "DEPT" ("DEPTNO") 
  ;



--------------------------------------------------------
--  Constraints for Table EMP
--------------------------------------------------------

  ALTER TABLE "EMP" ADD CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO") ENABLE;

--------------------------------------------------------
--  Constraints for Table DEPT
--------------------------------------------------------

  ALTER TABLE "DEPT" ADD CONSTRAINT "PK_DEPT" PRIMARY KEY ("DEPTNO") ENABLE;

--------------------------------------------------------
--  Ref Constraints for Table EMP
--------------------------------------------------------

  ALTER TABLE "EMP" ADD CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
   REFERENCES "DEPT" ("DEPTNO") ENABLE;

执行脚本、查询:

SQL> @myscott.sql
SQL> select * from EMP;
四、不安装客户端直接在docker内执行sqlplus:
$ docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS                                            NAMES
3c6a2c2761fb        sath89/oracle-xe-11g   "/entrypoint.sh "   2 minutes ago       Up About a minute   0.0.0.0:1521->1521/tcp, 0.0.0.0:9090->8080/tcp   dreamy_pasteur
$ docker exec -it 3c6a2c2761fb /bin/bash
root@3c6a2c2761fb:/# su oracle
oracle@3c6a2c2761fb:/$ $ORACLE_HOME/bin/sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Production on Wed Nov 29 13:40:15 2017

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> quit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
oracle@8e1a86793036:/$ $ORACLE_HOME/bin/sqlplus scott/tiger

SQL*Plus: Release 11.2.0.2.0 Production on Wed Nov 29 13:40:24 2017

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> select * from emp;

     EMPNO ENAME      JOB        MGR HIREDATE     SAL       COMM
---------- ---------- --------- ---------- --------- ---------- ----------
    DEPTNO
----------
      7369 SMITH      CLERK       7902 17-DEC-80     800
 20

......
五、停止和启动容器中oracle实例
$ docker stop 3c6a2c2761fb
$ docker start 3c6a2c2761fb

为方便后续的管理可在.bashrc中追加alias

alias orastartdock='docker start 3c6a2c2761fb'
alias orastopdock='docker stop 3c6a2c2761fb'

也可以在

docker run -d --name=oracle1 -p 9090:8080 -p 1521:1521 -v /home/${USER}/oracle/data:/u01/app/oracle sath89/oracle-xe-11g

指定容器名oracle1,这样用来以下方式来停止和启动实例:

$ docker stop oracle1
$ docker start oracle1

而不必使用前面由docker随机生成的CONTAINER ID

可用~$ docker rename oracle1 ora1来重命名docker容器名,用~$docker rm 容器名or容器ID来删除,也可用下面的命令全部删除

docker rm $(docker ps -aq)

当然启动状态下是不能删的。

只要改变端口和数据存放路径,一个镜像可以建立多个oracle实例,如:

docker run -d --name=oracle2 -p 9092:8080 -p 1522:1521 -v /home/${USER}/oracle/data2:/u01/app/oracle sath89/oracle-xe-11g


docker run -d --name=ora3 -p 9093:8080 -p 1523:1521 -v /home/${USER}/oracle/data3:/u01/app/oracle sath89/oracle-12c

这样你可以很容易建立多个oracle环境。

六、运行mysql
$docker pull mysql:5.7.20

$docker run -p 3307:3306 --name mysql5.7.20 -v /home/${USER}/mysqldata/mysql5.7.20/conf:/etc/mysql/conf.d -v /home/${USER}/mysqldata/mysql5.7.20/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.20

$ docker exec -it mysql5.7.20 bash

# mysql -p123456

mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH  GRANT OPTION; 
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> FLUSH   PRIVILEGES; 
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
root@657aaa1203f4:/# exit
exit
~$ mysql -h 127.0.0.1   -P3307 -u root -123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement
mysql> quit
 

这里必须加上-h 127.0.0.1,不然哪怕指定了端口-P3307还是连到宿主机的mysql而不是docker容器的那个,这是因为宿主机上在.my.cf指定了3306端口。

以上就是大神解说在unbuntu下Docker安装oracle和mysql的详细内容,更多请关注CTO智库其它相关文章!