LoginSignup
0
0

More than 1 year has passed since last update.

Docker: OracleDB を動かす

Last updated at Posted at 2023-05-09

イメージのダウンロード

docker pull container-registry.oracle.com/database/express:latest

確認

$ docker images
REPOSITORY                                       TAG           IMAGE ID       CREATED        SIZE

container-registry.oracle.com/database/express   latest        c273dde6b184   7 months ago   11.2GB

起動

docker run -p 1521:1521 -p 5500:5500 -e ORACLE_PWD=hinomaruc \
        --name oracledb container-registry.oracle.com/database/express:latest

bash で接続

docker exec -it oracledb /bin/bash
$ docker exec -it oracledb /bin/bash
bash-4.2$ cat /etc/os-release 
NAME="Oracle Linux Server"
VERSION="7.9"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="7.9"
PRETTY_NAME="Oracle Linux Server 7.9"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:7:9:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://bugzilla.oracle.com/"

ORACLE_BUGZILLA_PRODUCT="Oracle Linux 7"
ORACLE_BUGZILLA_PRODUCT_VERSION=7.9
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=7.9

DB に接続

sqlplus system/hinomaruc
bash-4.2$ sqlplus system/hinomaruc

SQL*Plus: Release 21.0.0.0.0 - Production on Tue May 9 04:14:28 2023
Version 21.3.0.0.0

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

Last Successful login time: Tue May 09 2023 03:09:04 +00:00

Connected to:
Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0

SQL>

CDB から PDB への切り替え

CDB: container database
PDB: pluggable database

show con_name;
select pdb_name from cdb_pdbs;
alter session set container = XEPDB1;
SQL> show con_name;

CON_NAME
------------------------------
CDB$ROOT
SQL> select pdb_name from cdb_pdbs;

PDB_NAME
--------------------------------------------------------------------------------
XEPDB1
PDB$SEED

SQL> alter session set container = XEPDB1;

Session altered.

SQL>

ユーザーの作成

OS で領域の用意

bash-4.2$ su
bash-4.2# mkdir -p  /usr/lib/oracle/xe/
bash-4.2# chmod 777 /usr/lib/oracle/xe/

sqlplus でユーザーの作成

bash-4.2$ sqlplus system/hinomaruc

SQL> alter session set container = XEPDB1;
SQL> CREATE USER scott identified by tiger123 default tablespace TEST temporary tablespace TESTTEMP;
SQL> grant connect to scott;
SQL> grant resource to scott;
SQL> grant UNLIMITED TABLESPACE TO scott;
SQL> exit

作成したユーザーで接続

export LANG=ja_JP.utf8
export NLS_LANG=JAPANESE_JAPAN.AL32UTF8
sqlplus scott/tiger123@//localhost:1521/XEPDB1
bash-4.2$ export LANG=ja_JP.utf8
bash-4.2$ export NLS_LANG=JAPANESE_JAPAN.AL32UTF8
bash-4.2$ sqlplus scott/tiger123@//localhost:1521/XEPDB1

SQL*Plus: Release 21.0.0.0.0 - Production on 火 5月 9 05:47:26 2023
Version 21.3.0.0.0

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

最終正常ログイン時間: 火 5月  09 2023 05:45:48 +00:00


Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
に接続されました。
SQL>

簡単な SQL を使う

SQL> create table cities (id varchar2 (10), name varchar2 (20), population number (10), date_mod date);

表が作成されました。

SQL> insert into cities values ('t0131','函館',51742,'2001-7-12');

1行が作成されました。

SQL> insert into cities values ('t0132','札幌',45391,'2001-8-5');

1行が作成されました。

SQL> insert into cities values ('t0133','帯広',31548,'2001-10-23');

1行が作成されました。

SQL> select * from cities;

ID	   NAME 		POPULATION DATE_MOD
---------- -------------------- ---------- --------
t0131      函館                      51742 01-07-12
t0132      札幌                      45391 01-08-05
t0133      帯広                      31548 01-10-23

SQL>

外部から接続

docker の IPアドレスを調べる

  1. Container ID を調べる
  2. $ docker ps
    CONTAINER ID   IMAGE                                                   COMMAND                  CREATED       STATUS                 PORTS                                                                                  NAMES
    83cf55c78772   container-registry.oracle.com/database/express:latest   "/bin/sh -c 'exec $O…"   4 hours ago   Up 4 hours (healthy)   0.0.0.0:1521->1521/tcp, :::1521->1521/tcp, 0.0.0.0:5500->5500/tcp, :::5500->5500/tcp   oracledb
    
  3. CONTAINER ID が、83cf55c78772 の IPアドレスを調べる
  4. $ docker inspect  83cf55c78772 | grep IPAddress
                "SecondaryIPAddresses": null,
                "IPAddress": "172.17.0.2",
                        "IPAddress": "172.17.0.2",
    

接続

connect.sh
export LANG=ja_JP.utf8
export NLS_LANG=JAPANESE_JAPAN.AL32UTF8
sqlplus scott/tiger123@//172.17.0.2:1521/XEPDB1

実行結果

$ ./connect.sh 

SQL*Plus: Release 21.0.0.0.0 - Production on 火 5月 9 06:58:29 2023
Version 21.9.0.0.0

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

最終正常ログイン時間: 火 5月  09 2023 06:53:46 +00:00


Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
に接続されました。
SQL>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0