13
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【備忘録】Docker版 Db2環境 取得手順

Last updated at Posted at 2020-09-16

1. はじめに

以前にLinux環境にDb2を導入する手順をまとめました。

【備忘録】Db2 インストール手順まとめ(Linux)
https://qiita.com/Haruka-Ogawa/items/9576399209b32f3016ea

Db2は 2017年から Docker版が提供されおり、
Docker storeからpullすることで より手軽にDb2環境を使用することが可能です。

今回は、DockerのDb2コンテナを取得する手順をまとめます。

2. 準備

PC に Docker を導入しておきます。
今回は Mac PC に Docker v19.03.12 を導入しています。

$ docker -v
Docker version 19.03.12, build 48a66213fe

3. 作業

3-1. イメージ取得

まず、Db2イメージを取得します。

イメージの取得 には docker pullコマンドを使用します。
以下リンク先を開き、画面右側にある Db2イメージ取得のための docker pull コマンド をコピーします。

image.png

コピーした docker pull コマンドを実行します。

$ docker pull ibmcom/db2
Using default tag: latest
latest: Pulling from ibmcom/db2
524b0c1e57f8: Pull complete 
e7a9171c839a: Pull complete 
f4447742b873: Pull complete 
db90696d3502: Pull complete 
6acc1312ab24: Pull complete 
67e9a72bc07a: Pull complete 
2b9e6b7678c7: Pull complete 
Digest: sha256:cf95dd272a4f99d7f9119945ecad04b64ad3a2badc8a6682f1105aa87279da60
Status: Downloaded newer image for ibmcom/db2:latest
docker.io/ibmcom/db2:latest

[参考] Docker ドキュメント:pull
https://docs.docker.jp/engine/reference/commandline/pull.html

3-2. イメージ確認

docker images コマンドで 取得したイメージ一覧を表示し、
db2イメージ が取得されたことを確認します。

$ docker images
REPOSITORY               TAG                  IMAGE ID            CREATED             SIZE
ibmcom/db2               latest               d6b3abc02d57        2 months ago        2.69GB

[参考] Docker ドキュメント: images
https://docs.docker.jp/engine/reference/commandline/images.html

3-3. コンテナ作成・起動

docker runコマンドを実行し、Db2 コンテナ を作成・起動します。
今回実行したコマンドは、 Docker Hub:ibmcom/db2 のガイド を参考にしています。

$ docker run -itd --name mydb --privileged=true -p 50000:50000 -e LICENSE=accept -e DB2INST1_PASSWORD=passw0rd -e DBNAME=testdb -v /Users/ogawa/db2fs:/database ibmcom/db2
67070db8cf20d82a9be8b45454eb1d5239c6c4d33b685c8ab9503a3bcc84e68b

[参考] Docker Hub:ibmcom/db2
https://hub.docker.com/r/ibmcom/db2

[参考] Docker ドキュメント: run
https://docs.docker.jp/engine/reference/commandline/run.html

3-4. コンテナ ログ確認

docker logsコマンドで コンテナのログを確認し、
コンテナが作成・起動されたことを確認します。

$ docker logs -f mydb
(*) Previous setup has not been detected. Creating the users... 
(*) Creating users ...
(*) Creating instance ... 
DBI1446I  The db2icrt command is running.


DB2 installation is being initialized.

 Total number of tasks to be performed: 4 
Total estimated time for all tasks to be performed: 309 second(s) 

Task #1 start
Description: Setting default global profile registry variables 
Estimated time 1 second(s) 
Task #1 end 

Task #2 start
Description: Initializing instance list 
Estimated time 5 second(s) 
Task #2 end 

Task #3 start
Description: Configuring DB2 instances 
Estimated time 300 second(s) 
Task #3 end 

Task #4 start
Description: Updating global profile registry 
Estimated time 3 second(s) 
Task #4 end 

The execution completed successfully.

For more information see the DB2 installation log at "/tmp/db2icrt.log.77".
DBI1070I  Program db2icrt completed successfully.


09/09/2020 03:16:06     0   0   SQL1032N  No start database manager command was issued.
SQL1032N  No start database manager command was issued.  SQLSTATE=57019
(*) Cataloging existing databases
ls: cannot access /database/data/db2inst1/NODE0000: No such file or directory
(*) Applying Db2 license ...

LIC1402I  License added successfully.


LIC1426I  This product is now licensed for use as outlined in your License Agreement.  USE OF THE PRODUCT CONSTITUTES ACCEPTANCE OF THE TERMS OF THE IBM LICENSE AGREEMENT, LOCATED IN THE FOLLOWING DIRECTORY: "/opt/ibm/db2/V11.5/license/en_US.iso88591"
(*) Saving the checksum of the current nodelock file ...
(*) Updating DBM CFG parameters ... 
DB20000I  The UPDATE DATABASE MANAGER CONFIGURATION command completed 
successfully.
DB20000I  The UPDATE DATABASE MANAGER CONFIGURATION command completed 
successfully.
DB20000I  The UPDATE DATABASE MANAGER CONFIGURATION command completed 
successfully.
No Cgroup memory limit detected, instance memory will follow automatic tuning
(*) Remounting /database with suid... 
(*) Nothing appears in the Db2 directory. will skip update/upgrade.
(*) Code level is the same. No update/upgrade needed.

DB2 State : Operable
DB2 has not been started
Starting DB2...

09/09/2020 03:16:35     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
(*) User chose to create testdb database
(*) Creating database testdb ... 
DB20000I  The CREATE DATABASE command completed successfully.
DB20000I  The ACTIVATE DATABASE command completed successfully.
09/09/2020 03:18:38     0   0   SQL1026N  The database manager is already active.
SQL1026N  The database manager is already active.
### Enabling LOGARCHMETH1

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.5.4.0
 SQL authorization ID   = DB2INST1
 Local database alias   = TESTDB

DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification 
were not changed dynamically. For these configuration parameters, the database 
must be shutdown and reactivated before the configuration parameter changes 
become effective.
### Restarting DB2
09/09/2020 03:18:46     0   0   SQL1064N  DB2STOP processing was successful.
SQL1064N  DB2STOP processing was successful.
09/09/2020 03:18:51     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
### Making backup directory and performing backup

Backup successful. The timestamp for this backup image is : 20200909031855

(*) Applying autoconfiguration for instance ... 

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.5.4.0
 SQL authorization ID   = DB2INST1
 Local database alias   = TESTDB

DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification 
were not changed dynamically. For these configuration parameters, the database 
must be shutdown and reactivated before the configuration parameter changes 
become effective.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  One or more of the parameters submitted for immediate modification 
were not changed dynamically. For these configuration parameters, the database 
must be shutdown and reactivated before the configuration parameter changes 
become effective.
DB20000I  The SQL command completed successfully.
09/09/2020 03:19:15     0   0   SQL1064N  DB2STOP processing was successful.
SQL1064N  DB2STOP processing was successful.
09/09/2020 03:19:20     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
(*) Skipping TEXT_SEARCH setup for database testdb because TEXT_SEARCH is not configured for the instance ...
ssh-keygen: generating new host keys: RSA1 RSA DSA ECDSA ED25519 
(*) All databases are now active. 
(*) Setup has completed.
FALSE

2020-09-09-03.19.20.117938+000 I223959E393           LEVEL: Warning
PID     : 14939                TID : 139881437169536 PROC : db2start
INSTANCE: db2inst1             NODE : 000
HOSTNAME: 67070db8cf20
FUNCTION: DB2 UDB, base sys utilities, sqleReleaseStStLockFile, probe:16076
MESSAGE : Released lock on the file:
DATA #1 : String, 50 bytes
/database/config/db2inst1/sqllib/ctrl/db2strst.lck

2020-09-09-03.19.37.389298+000 I224353E420           LEVEL: Warning
PID     : 14947                TID : 140530994374400 PROC : db2sysc 0
INSTANCE: db2inst1             NODE : 000
HOSTNAME: 67070db8cf20
EDUID   : 15                   EDUNAME: db2wlmtm 0
FUNCTION: DB2 UDB, base sys utilities, sqleTimedSleep, probe:1182
DATA #1 : unsigned integer, 8 bytes
1.59962E+12
DATA #2 : unsigned integer, 8 bytes
1.59962E+12

2020-09-09-03.20.07.369457+000 I224774E420           LEVEL: Warning
PID     : 14947                TID : 140530994374400 PROC : db2sysc 0
INSTANCE: db2inst1             NODE : 000
HOSTNAME: 67070db8cf20
EDUID   : 15                   EDUNAME: db2wlmtm 0
FUNCTION: DB2 UDB, base sys utilities, sqleTimedSleep, probe:1182
DATA #1 : unsigned integer, 8 bytes
1.59962E+12
DATA #2 : unsigned integer, 8 bytes
1.59962E+12

2020-09-09-03.20.37.353793+000 I225195E420           LEVEL: Warning
PID     : 14947                TID : 140530994374400 PROC : db2sysc 0
INSTANCE: db2inst1             NODE : 000
HOSTNAME: 67070db8cf20
EDUID   : 15                   EDUNAME: db2wlmtm 0
FUNCTION: DB2 UDB, base sys utilities, sqleTimedSleep, probe:1182
DATA #1 : unsigned integer, 8 bytes
1.59962E+12
DATA #2 : unsigned integer, 8 bytes
1.59962E+12

2020-09-09-03.21.07.337071+000 I225616E420           LEVEL: Warning
PID     : 14947                TID : 140530994374400 PROC : db2sysc 0
INSTANCE: db2inst1             NODE : 000
HOSTNAME: 67070db8cf20
EDUID   : 15                   EDUNAME: db2wlmtm 0
FUNCTION: DB2 UDB, base sys utilities, sqleTimedSleep, probe:1182
DATA #1 : unsigned integer, 8 bytes
1.59962E+12
DATA #2 : unsigned integer, 8 bytes
1.59962E+12

2020-09-09-03.24.02.151703+000 I226037E420           LEVEL: Warning
PID     : 14947                TID : 140530994374400 PROC : db2sysc 0
INSTANCE: db2inst1             NODE : 000
HOSTNAME: 67070db8cf20
EDUID   : 15                   EDUNAME: db2wlmtm 0
FUNCTION: DB2 UDB, base sys utilities, sqleTimedSleep, probe:1182
DATA #1 : unsigned integer, 8 bytes
1.59962E+12
DATA #2 : unsigned integer, 8 bytes
1.59962E+12

2020-09-09-03.24.02.031095+000 I226458E419           LEVEL: Warning
PID     : 14947                TID : 140530998568704 PROC : db2sysc 0
INSTANCE: db2inst1             NODE : 000
HOSTNAME: 67070db8cf20
EDUID   : 14                   EDUNAME: db2wlmt 0
FUNCTION: DB2 UDB, base sys utilities, sqleTimedSleep, probe:1182
DATA #1 : unsigned integer, 8 bytes
1.59962E+12
DATA #2 : unsigned integer, 8 bytes
1.59962E+12

[参考] Docker ドキュメント:logs
https://docs.docker.jp/engine/reference/commandline/logs.html

3-5. コンテナ確認

docker psコマンドで コンテナの一覧を表示し、
Db2コンテナの状態を確認します。

$ docker ps -a
CONTAINER ID        IMAGE                                       COMMAND                  CREATED             STATUS                       PORTS                                                          NAMES
67070db8cf20        ibmcom/db2                                  "/var/db2_setup/lib/…"   About an hour ago   Up About an hour             22/tcp, 55000/tcp, 60006-60007/tcp, 0.0.0.0:50000->50000/tcp   mydb

[参考] Docker ドキュメント: ps
https://docs.docker.jp/engine/reference/commandline/ps.html

3-6. コンテナ内でコマンド実行

docker execコマンドは、コンテナ内で任意のコマンドを実行させることができます。
bash を起動し、db2inst1にスイッチすることで、db2環境に入ります。

$ docker exec -ti mydb bash -c "su - db2inst1"
Last login: Wed Sep  9 04:48:57 UTC 2020

[参考] Docker ドキュメント: exec
https://docs.docker.jp/engine/reference/commandline/exec.html

3-7. Db2環境 確認

Db2コマンドが実行できることを確認します。

  • db2のバージョン確認

db2levelコマンドを実行し、Db2のバージョンを確認します。

[db2inst1@67070db8cf20 ~]$ db2level
DB21085I  This instance or install (instance name, where applicable: 
"db2inst1") uses "64" bits and DB2 code release "SQL11054" with level 
identifier "0605010F".
Informational tokens are "DB2 v11.5.4.0", "s2006161200", "DYN2006161200AMD64", 
and Fix Pack "0".
Product is installed at "/opt/ibm/db2/V11.5".
  • データベース接続

docker runコマンド実行時点で、データベースtestdbが作成されています。
connectコマンドで、データベースtestdbに接続します。

[db2inst1@67070db8cf20 ~]$ db2 connect to testdb

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.5.4.0
 SQL authorization ID   = DB2INST1
 Local database alias   = TESTDB
  • テーブル作成

データベースtestdbに、テーブルTBL1を作成してみます。

[db2inst1@67070db8cf20 ~]$ db2 "CREATE TABLE TBL1(COL1 INTEGER, COL2 VARCHAR(50))"
DB20000I  The SQL command completed successfully.
  • データベース切断

terminateコマンドで、データベースtestdbから切断します。

[db2inst1@67070db8cf20 ~]$ db2 terminate
DB20000I  The TERMINATE command completed successfully.

Db2コマンドを実行し、操作ができることを確認しました。

コンテナから出る時はexitコマンドを使用します。

[db2inst1@67070db8cf20 ~]$ exit
logout

4. コンテナ 起動・停止

作成したコンテナの起動・停止には、
docker startコマンド、docker stopコマンドを使用します。

4-1. 起動

docker startコマンドで、Db2コンテナ(mydb)を起動します。

$ docker start mydb
mydb

[参考] Docker ドキュメント: start
https://docs.docker.jp/engine/reference/commandline/start.html

4-2. 停止

docker stopコマンドで、Db2コンテナ(mydb)を停止します。

$ docker stop mydb
mydb

[参考] Docker ドキュメント: stop
https://docs.docker.jp/engine/reference/commandline/stop.html

5. おわりに

今回は、Dockerコンテナとして提供されているDb2をpullしてみました。

Dockerが導入されたPCさえあれば とても手軽にDb2環境を使用することができ、
作業自体も10分ちょっとくらいでできたので、ちょっとDb2の動作確認などしたい場合などに便利だと感じました。

参考情報

Db2とDocker
https://www.ibm.com/blogs/solutions/jp-ja/db2-docker/

Docker Hub:ibmcom/db2
https://hub.docker.com/r/ibmcom/db2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?