如何在DOCKER環境中使用DB2


 

如何在DOCKER環境中使用Db2

前言

     此問的目的是幫助用戶能夠在docker的環境中學習Db2,前提條件的你的服務器可以訪問互聯網,否則無法下載Db2鏡像。Docker環境中學習Db2的優勢是省去了下載產品、安裝產品、初始化數據庫實例這部分工作,更適合於開發人員學習Db2。

     另外本文也介紹了Db2和MYSQL的常用命令區別,以便於大家同時學習兩個產品。

1、列出已有的Db2鏡像

-bash-4.2# docker images | grep -i db2

ibmcom/db2express-c                      latest              7aa154d9b73c        2 years ago         1.71 GB

2、拉Db2鏡像

-bash-4.2# docker pull ibmcom/db2express-c

Using default tag: latest

latest: Pulling from ibmcom/db2express-c

a3ed95caeb02: Pull complete

aeb4552c46f2: Pull complete

67f42aa337c8: Pull complete

36add9e32b7b: Pull complete

cca79fe4efc9: Pull complete

47257c26990b: Pull complete

ffaad03725df: Pull complete

12bf772a460f: Pull complete

d7696ccb610d: Pull complete

6cf81245c65d: Pull complete

5dab63ce98e0: Pull complete

91dd2d768d97: Pull complete

e3aca141cf3e: Pull complete

Digest:sha256:6217d60b20b58e08e5189f467cfb9a08f3472cf6c3a5e5d5d91900791c709a86

Status: Downloaded newer image foribmcom/db2express-c:latest

 

3、啟動容器:

-bash-4.2# docker run -it --network=host -p50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept ibmcom/db2express-c:latest bash

Changing password for user db2inst1.

New password: BAD PASSWORD: The passwordcontains the user name in some form

Retype new password: passwd: allauthentication tokens updated successfully.

  • -p 50000:50000 允許遠程的客戶端可以從50000 端口連接到數據庫實例.
  • 通過指定 -e DB2INST1_PASSWORD=db2inst1-pwd 參數, 你可以為缺省的Db2實例用戶db2inst1設置密碼.
  • 通過指定-e LICENSE=accept參數, 表示你接受了使用Db2軟件的許可證協議.

4、切換到實例用戶db2inst1

[root@shcallisto-25 /]# su - db2inst1

Last login: Wed May 20 21:57:28 UTC 2015

5、啟動實例

[db2inst1@shcallisto-25 ~]$ db2start

SQL1063N DB2START processing was successful.

6、查看運行狀態

[db2inst1@shcallisto-25 ~]$ db2pd -

 

Database Member 0 -- Active -- Up 0 days00:00:06 -- Date 2018-01-16-03.30.51.059168

7、查看已經創建的數據庫

[db2inst1@shcallisto-25 ~]$ db2 list dbdirectory

SQL1031N The database directory cannot be found on the indicated file system.

SQLSTATE=58031

說明目前沒有創建數據庫

8、查看數據庫和補丁版本

[db2inst1@shcallisto-25 ~]$ db2level

DB21085I This instance or install (instance name, where applicable:

"db2inst1") uses "64"bits and DB2 code release "SQL10055" with level

identifier "0606010E".

Informational tokens are "DB2v10.5.0.5", "s141128", "IP23633", and Fix Pack

"5".

Product is installed at"/home/db2inst1/sqllib".

9、顯示運行的Db2容器信息,在宿主機上運行

-bash-4.2# docker ps | grep db2

3492501e4f07        ibmcom/db2express-c:latest   "/entrypoint.sh bash"    16 seconds ago      Up 15 seconds                           wizardly_mestorf

10、關閉數據庫實例,后面需要啟動容器時指定宿主機目錄和容器目錄的映射關系,目的是在這些目錄中創建數據庫。

db2stop

exit

11、重啟容器,通過-v選項指定宿主機目錄和容器目錄的映射關系:

-bash-4.2# docker run -it --network=host -p50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept -v /db2data:/db2data ibmcom/db2express-c:latest bash

Changing password for user db2inst1.

New password: BAD PASSWORD: The passwordcontains the user name in some form

Retype new password: passwd: allauthentication tokens updated successfully.

宿主機目錄為/db2data,容器目錄為/db2data

12、[root@shcallisto-25/]# df -h

Filesystem             Size  Used Avail Use% Mounted on

rootfs                 148G   55G  94G  37% /

overlay                148G   55G  94G  37% /

tmpfs                  7.9G     0 7.9G   0% /dev

tmpfs                  7.9G     0 7.9G   0% /sys/fs/cgroup

/dev/mapper/rhel-root  148G  55G   94G  37% /db2data

shm                     64M     0  64M   0% /dev/shm

13、設置目錄權限,其中db2data為表空間的目錄、log為日志目錄、dbpath為創建數據庫時的dbpath。

[root@shcallisto-25 /]# chmod 777 db2data

[root@shcallisto-25 /]# su - db2inst1

Last login: Tue Jan 16 05:15:10 UTC 2018 onconsole

[db2inst1@shcallisto-25 ~]$

[db2inst1@shcallisto-25 ~]$ cd /db2data

[db2inst1@shcallisto-25 db2data]$ touch 1

[db2inst1@shcallisto-25 db2data]$ rm 1

[db2inst1@shcallisto-25 db2data]$ mkdirdata

[db2inst1@shcallisto-25 db2data]$ mkdir dbpath

[db2inst1@shcallisto-25 db2data]$ mkdir log

[db2inst1@shcallisto-25 db2data]$ pwd

/db2data

[db2inst1@shcallisto-25 db2data]$ ls -lrt

total 0

drwxrwxr-x 2 db2inst1 db2inst1 6 Jan 1605:16 data

drwxrwxr-x 2 db2inst1 db2inst1 6 Jan 1605:16 dbpath

drwxrwxr-x 2 db2inst1 db2inst1 6 Jan 1605:16 log

 

查看缺省的dbpath

[db2inst1@shcallisto-25 ~]$ db2 get dbm cfg| grep -i dbpath

 Default database path                       (DFTDBPATH) =/home/db2inst1

14、創建用戶數據庫mydb

[db2inst1@shcallisto-25 db2data]$db2start

[db2inst1@shcallisto-25 db2data]$  db2 "create db mydb on /db2data/data dbpath on /db2data/dbpath using codeset utf-8 territory cn"

DB20000I The CREATE DATABASE command completed successfully.

15、修改日志路徑

[db2inst1@shcallisto-25 db2data]$ db2 update db cfg for mydb using newlogpath /db2data/log

DB20000I The UPDATE DATABASE CONFIGURATION command completed successfully.

16、激活數據庫

[db2inst1@shcallisto-25 db2data]$ db2activate db mydb

DB20000I The ACTIVATE DATABASE command completed successfully.

17、確認日志生效

[db2inst1@shcallisto-25 db2data]$ cd /db2data/log

[db2inst1@shcallisto-25 log]$ ls

NODE0000

[db2inst1@shcallisto-25 log]$ cd *

[db2inst1@shcallisto-25 NODE0000]$ ls

LOGSTREAM0000

[db2inst1@shcallisto-25 NODE0000]$ cd *

[db2inst1@shcallisto-25 LOGSTREAM0000]$ ls

S0000000.LOG  S0000001.LOG S0000002.LOG  SQLLPATH.TAG

[db2inst1@shcallisto-25 LOGSTREAM0000]$ ls-lrt

total 12028

-rw------- 1 db2inst1 db2inst1     512 Jan 16 05:22 SQLLPATH.TAG

-rw------- 1 db2inst1 db2inst1 4104192 Jan16 05:22 S0000002.LOG

-rw------- 1 db2inst1 db2inst1 4104192 Jan16 05:22 S0000001.LOG

-rw------- 1 db2inst1 db2inst1 4104192 Jan16 05:22 S0000000.LOG

[db2inst1@shcallisto-25 LOGSTREAM0000]$ db2get db cfg for mydb | grep -i primary

 Number of primary log files                (LOGPRIMARY) = 3

18、由於容器是靜態的,所以每次重啟容器之后都需要做catalog,否則無法訪問數據庫。

[db2inst1@shcallisto-25 sqllib]$ db2 catalog db mydb as mydb on /db2data/dbpath

DB20000I The CATALOG DATABASE command completed successfully.

[db2inst1@shcallisto-25 sqllib]$ db2connect to mydb

 

  Database Connection Information

 

 Database server        = DB2/LINUXX8664 10.5.5

 SQLauthorization ID   = DB2INST1

 Local database alias   = MYDB

 

此時我們運行docker ps -a將看到有兩個Db2容器在運行:

CONTAINER ID        IMAGE                          COMMAND                  CREATED              STATUS                     PORTS               NAMES

3d221e0aaa69       ibmcom/db2express-c:latest    "/entrypoint.sh bash"   About a minute ago   Up About aminute                             tender_fermat

088dc03e36c4       ibmcom/db2express-c:latest     "/entrypoint.sh bash"    2 hours ago          Up 2 hours                                    kind_dijkstra

19、 現在,可以提交修改到容器。

docker commit 3d221e0aaa69 ibmcom/db2express-c-new

sha256:93ab907a65196dfbeba0b376b0f3bd61d8bf07e9020b80f2c72fefb80a143028

 

-bash-4.2# docker images | grep db2

REPOSITORY                               TAG                 IMAGE ID            CREATED              SIZE

ibmcom/db2express-c-new                  latest              93ab907a6519        About a minute ago   1.71 GB

ibmcom/db2express-c                      latest              7aa154d9b73c        2 years ago          1.71 GB

 

20、執行save命令持久化鏡像,這里運行的目的是保存catalog db的信息,以便每次重啟容器后可以直接訪問mydb數據庫

# docker save ibmcom/db2express-c-new>/home/ibm-db2express-c-new.tar

最后的結果是生成一個1.7GB大小的Tar文件

-bash-4.2# ls -l

total 1694560

-rw-r--r-- 1 root     root    1735228928 Jan 16 08:02 ibm-db2express-c-new.tar

 

21、停止已經啟動的db2container,以避免表空間訪問沖突;

-bash-4.2# dockerps -a | more

CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS                      PORTS               NAMES

c33d4f38ac17        ibmcom/db2express-c-new:latest   "/entrypoint.sh bash"    16 minutes ago      Up 16 minutes                                   eager_ride

3d221e0aaa69        ibmcom/db2express-c:latest       "/entrypoint.sh bash"    40 minutes ago      Exited (0) 16 minutes ago                       tender_fermat

088dc03e36c4        ibmcom/db2express-c:latest       "/entrypoint.sh bash"    2 hours ago         Up 2 hours                                      kind_dijkst

-bash-4.2# docker stop 088dc03e36c4

088dc03e36c4

-bash-4.2# docker stop c33d4f38ac17

c33d4f38ac17

22、重新啟動新的image,發現不需要做catalog db了,說明這些信息已經持久化到影像中了。

-bash-4.2# docker run -it --network=host -p50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept -v/db2data:/db2data ibmcom/db2express-c-new:latest bash

Changing password for user db2inst1.

New password: BAD PASSWORD: The passwordcontains the user name in some form

Retype new password: passwd: all authenticationtokens updated successfully.

[root@shcallisto-25 /]#

[root@shcallisto-25 /]# su - db2inst1

Last login: Tue Jan 16 07:42:24 UTC 2018 onconsole

[db2inst1@shcallisto-25 ~]$

[db2inst1@shcallisto-25 ~]$ db2start

SQL1063N DB2START processing was successful.

[db2inst1@shcallisto-25 ~]$ db2 connect tomydb

 

  Database Connection Information

 

 Database server        = DB2/LINUXX8664 10.5.5

 SQLauthorization ID   = DB2INST1

 Local database alias   = MYDB

23、Db2和mysql常用SQL對比

1)查看有哪些數據庫:

 Db2: list db directory

 mysql: show databases

2)查看有哪些用戶表

 Db2:list tables 后者list tables for schema devuser

 mysql: show tables

3)查看表結構

Db2:describe table table_name
mysql:describe table_name

4)取前n行數據

Db2:select * from table_name fetch first nrows only 
mysql:select * from table_name limit n

5) 顯示數據庫版本

Db2: db2level 顯示db2的版本號
mysql:select version()

6)連接數據庫

Db2:db2 connect to dbname user <user_name>using <passwd>

mysql:use database

7)顯示表中列的信息

Db2: describe table <table-name>

mysql: describle <table-name>

8)創建表

Db2 :create table t1(c1 int);

mysql: create table t1(c1 int);

9)創建數據庫

Db2:create db mydb using codeset utf-8territory cn

mysql:create db mydb

10)加載數據

Db2: load client from db2.txt of del insertinto table t1;

mysql: load data local infile “mysql.txt”into table t1;

11)執行文件中的sql

Db2:db2 -tvf db2.sql

mysql: source mysql.sql;

12)重命名表

Db2: rename table t1 to t2

mysql: alter table t1 rename t2

13)創建索引

Db2: create index index_name on table_name(column_name)

mysql: create index index_name ontable_name (column_name)

14)刪除索引

Db2 :drop index index_name;

mysql: alter table table_name drop indexindex_name

15)查看當前時間

Db2: values (current timestamp)

mysql: select now()

16)導出表結構

Db2: db2look -d mydb -t test -e -o test.sql

mysql:mysqldump -uroot -pdbpasswd -d dbnametest>db.sql

作者:陸川

 

轉自:http://blog.csdn.net/qq_39154147/article/details/79219414


免責聲明!

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



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