前言:

本文通过使用docker部署一个可用的db2环境,演示了docker image抽取,保存,container 进入与退出,挂载外部文件系统,并持久化景象的诸多操作。相比阅读枯燥无味的docker参数表格,它助你迅速上手docker。


1.查询docker 官方 repository 中的db2镜像
[root@k8s01 ~]# docker search db2
NAME                                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ibmcom/db2express-c                                IBM DB2 Express-C                              193
.............

2.从docker-hub上拉取镜像到本地 (默认最新版latest)

[root@k8s01 ~]# 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 for ibmcom/db2express-c:latest

3.尝试启动db2容器并初始化密码

[root@k8s01 ~]# docker run -it -p 50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept ibmcom/db2express-c:latest bash
Changing password for user db2inst1.
New password: BAD PASSWORD: The password contains the user name in some form
Retype new password: passwd: all authentication tokens updated successfully.
[root@e20e950fdabc /]#

Note:
    -p 50000:50000 exposes port 50000 to allow connections from the remote client.
    By specifying -e DB2INST1_PASSWORD=db2inst1-pwd parameter, you set a password of your choice for the db2inst1 user for the default DB2 instance.
    By specifying -e LICENSE=accept parameter, you are accepting this License to use the software contained in this image.


 

4.切换到实例用户db2inst1,启动实例并创建sample数据库

[root@e20e950fdabc /]# su - db2inst1
Last login: Wed May 20 21:57:28 UTC 2015

[db2inst1@e20e950fdabc ~]$ db2start
SQL1063N  DB2START processing was successful.
[db2inst1@e20e950fdabc ~]$ db2sampl

  Creating database "SAMPLE"...
  Connecting to database "SAMPLE"...
  Creating tables and data in schema "DB2INST1"...
  Creating tables with XML columns and XML data in schema "DB2INST1"...

  'db2sampl' processing complete.

[db2inst1@e20e950fdabc ~]$

5.查看运行状态与本地db directory信息,版本信息等

[db2inst1@e20e950fdabc ~]$ db2pd -

Database Member 0 -- Active -- Up 0 days 00:01:28 -- Date 2019-01-17-06.05.58.453473

[db2inst1@e20e950fdabc ~]$ ps -elf | grep db2
4 S root        22     1  0  80   0 - 18943 wait   06:04 pts/0    00:00:00 su - db2inst1
4 S db2inst1    23    22  0  80   0 -  3794 wait   06:04 pts/0    00:00:00 -bash
0 S db2inst1   614     1  0  80   0 - 55234 msgrcv 06:06 pts/0    00:00:00 /home/db2inst1/sqllib/bin/db2bp 23A1000 5 A
0 R db2inst1  1157    23  0  80   0 -  5800 -      06:16 pts/0    00:00:00 ps -elf
0 S db2inst1  1158    23  0  80   0 -  3117 pipe_w 06:16 pts/0    00:00:00 grep --color=auto db2


[db2inst1@e20e950fdabc ~]$ db2 list db directory

 System Database Directory

 Number of entries in the directory = 1

Database 1 entry:

 Database alias                       = SAMPLE
 Database name                        = SAMPLE
 Local database directory             = /home/db2inst1
 Database release level               = 10.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

[db2inst1@e20e950fdabc ~]$

[db2inst1@e20e950fdabc ~]$ 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 "DB2 v10.5.0.5", "s141128", "IP23633", and Fix Pack
"5".
Product is installed at "/home/db2inst1/sqllib".

 

6. 按住control 再分别按 P,Q,就能临时退出container的终端。可用exec重新进入该运行中的container
[root@k8s01 ~]# docker container ls | grep db2
e20e950fdabc        ibmcom/db2express-c:latest   "/entrypoint.sh bash"    6 minutes ago       Up 6 minutes        22/tcp, 0.0.0.0:50000->50000/tcp   agitated_almeida
[root@k8s01 ~]# docker exec -it e20e950fdabc /bin/bash
[root@e20e950fdabc /]#
 
 

7.关闭数据库实例,并退出container

[root@e20e950fdabc /]# db2stop
bash: db2stop: command not found
[root@e20e950fdabc /]# su - db2inst1
Last login: Thu Jan 17 06:04:07 UTC 2019 on pts/0
[db2inst1@e20e950fdabc ~]$ db2stop
SQL1064N  DB2STOP processing was successful.
[db2inst1@e20e950fdabc ~]$ exit
logout
[root@e20e950fdabc /]# exit    #会完全退出container,但是后面发现container并没有消亡,仍再运行。因为前面是用exec进入的container,退出的是新创建的session。如果是attach进入的,会自动挂载到主回话,因此exit 会同时关闭container。
exit
[root@k8s01 ~]# docker container ls | grep db2
e20e950fdabc        ibmcom/db2express-c:latest   "/entrypoint.sh bash"    12 minutes ago      Up 12 minutes       22/tcp, 0.0.0.0:50000->50000/tcp   agitated_almeida
[root@k8s01 ~]#

 

8.因为container本身并没有保存“变化”的能力,这对于db2来说是不可接受的,因此通过-v选项指定宿主机目录和容器目录的映射关系,将db2的核心数据保存在container之外:
  #这里多用了一个参数 d,表示直接将container放入后台运行。若要进入容器,需要手动attach或者exec。
  #注意如果直接exec会报 “Error response from daemon: Container xxx is not running”
  #exec进入container后,再exit,不会终止container

    Note:
    The docker exec command runs a new command in a running container.
    The command started using docker exec will only run while the container's primary process (PID 1) is running

[root@k8s01 ~]# docker run -dit -p 50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept -v /db2data:/db2data ibmcom/db2express-c:latest bash
447e9b63ca763604f80d651869dea8a871373961076c8bd85a71e36c19aa1587
[root@k8s01 ~]# docker container ls | grep db2
447e9b63ca76        ibmcom/db2express-c:latest   "/entrypoint.sh bash"    4 seconds ago       Up 4 seconds        22/tcp, 0.0.0.0:50000->50000/tcp   keen_yonath
[root@k8s01 ~]# docker attach 447e9b63ca76
# 退出container终端
[root@k8s01 ~]#
[root@k8s01 ~]# ls -lrt /db2data/
total 0

# 将宿主机/db2data,挂载位容器目录/db2data。 宿主机目录如果不存在,会被自动创建。


[root@k8s01 ~]# docker exec -it 447e9b63ca76 /bin/bash
[root@447e9b63ca76 /]# ls -lrt /db2data
total 0
[root@447e9b63ca76 /]#
[root@447e9b63ca76 /]# df -h
Filesystem               Size  Used Avail Use% Mounted on
rootfs                    38G  4.7G   34G  13% /
overlay                   38G  4.7G   34G  13% /
tmpfs                     64M     0   64M   0% /dev
tmpfs                   1001M     0 1001M   0% /sys/fs/cgroup
/dev/mapper/centos-root   38G  4.7G   34G  13% /db2data    #容器外挂载的目录
shm                       64M     0   64M   0% /dev/shm
tmpfs                   1001M     0 1001M   0% /proc/acpi
tmpfs                     64M     0   64M   0% /proc/kcore
tmpfs                     64M     0   64M   0% /proc/keys
tmpfs                     64M     0   64M   0% /proc/timer_list
tmpfs                     64M     0   64M   0% /proc/timer_stats
tmpfs                     64M     0   64M   0% /proc/sched_debug
tmpfs                   1001M     0 1001M   0% /proc/scsi
tmpfs                   1001M     0 1001M   0% /sys/firmware
 

9.在容器内的db2data为db2创建核心目录:data表空间的目录.log为日志目录.dbpath 为“Default database path”。

[root@447e9b63ca76 /]# chmod 777 /db2data/
[root@447e9b63ca76 /]# su - db2inst1
Last login: Wed May 20 21:57:28 UTC 2015
[db2inst1@447e9b63ca76 ~]$ cd /db2data/
[db2inst1@447e9b63ca76 db2data]$ mkdir data dbpath log
[db2inst1@447e9b63ca76 db2data]$ ls -lrt
total 0
drwxrwxr-x 2 db2inst1 db2inst1 6 Jan 17 06:51 log
drwxrwxr-x 2 db2inst1 db2inst1 6 Jan 17 06:51 dbpath
drwxrwxr-x 2 db2inst1 db2inst1 6 Jan 17 06:51 data
[db2inst1@447e9b63ca76 db2data]$


[db2inst1@447e9b63ca76 db2data]$ db2 get dbm cfg| grep -i dbpath
 Default database path                       (DFTDBPATH) = /home/db2inst1


10.创建用户数据库mydb

[db2inst1@447e9b63ca76 db2data]$ db2start
SQL1063N  DB2START processing was successful.
[db2inst1@447e9b63ca76 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.
[db2inst1@447e9b63ca76 db2data]$

11.修改日志路径并激活数据库

[db2inst1@447e9b63ca76 db2data]$ db2 update db cfg for mydb using newlogpath /db2data/log
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.

[db2inst1@447e9b63ca76 db2data]$ db2 activate db mydb
DB20000I  The ACTIVATE DATABASE command completed successfully.
[db2inst1@447e9b63ca76 db2data]$

#确认日志生效

[db2inst1@447e9b63ca76 db2data]$ ls -lrt /db2data/log/NODE0000/LOGSTREAM0000/
total 12028
-rw------- 1 db2inst1 db2inst1     512 Jan 17 07:01 SQLLPATH.TAG
-rw------- 1 db2inst1 db2inst1 4104192 Jan 17 07:01 S0000002.LOG
-rw------- 1 db2inst1 db2inst1 4104192 Jan 17 07:01 S0000001.LOG
-rw------- 1 db2inst1 db2inst1 4104192 Jan 17 07:02 S0000000.LOG

[db2inst1@447e9b63ca76 db2data]$ db2 get db cfg for mydb | grep -i primary
 Number of primary log files                (LOGPRIMARY) = 3
 Percent max primary log space by transaction  (MAX_LOG) = 0
[db2inst1@447e9b63ca76 db2data]$


12.由于容器是静态的,所以每次重启容器之后都需要做catalog,否则无法访问数据库。

[db2inst1@447e9b63ca76 db2data]$ db2 catalog db mydb as mydb on /db2data/dbpath
SQL1005N  The database alias "mydb" already exists in either the local
database directory or system database directory.

[db2inst1@447e9b63ca76 db2data]$ db2 connect to mydb

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.5
 SQL authorization ID   = DB2INST1
 Local database alias   = MYDB

[db2inst1@447e9b63ca76 db2data]$

 


13.将container的修改进行commit。

[root@k8s01 ~]# docker ps | grep db2
447e9b63ca76        ibmcom/db2express-c:latest   "/entrypoint.sh bash"    26 minutes ago      Up 26 minutes       22/tcp, 0.0.0.0:50000->50000/tcp   hungry_leavitt
[root@k8s01 ~]#

[root@k8s01 ~]# docker commit 447e9b63ca76 ibmcom/db2express-c-bond
sha256:d0364d72abe20fb4623afdfb4518d031161025852b20a1ddf65a00a7bd40661c

[root@k8s01 ~]# docker images | grep db2
ibmcom/db2express-c-bond             latest              d0364d72abe2        22 seconds ago      1.71GB
ibmcom/db2express-c                  latest              7aa154d9b73c        3 years ago         1.71GB

 

14.执行save命令将commit过的镜像导出,保存到宿主机。最后完全关闭container。

[root@k8s01 ~]# docker save ibmcom/db2express-c-bond > /tmp/ibm-db2express-c-bond.tar
[root@k8s01 ~]# ls -lrt /tmp/
total 1697848
-rw-r--r-- 1 root root 1738594816 Jan 17 15:12 ibm-db2express-c-bond.tar

[root@k8s01 ~]# docker container ls | grep db2
447e9b63ca76        ibmcom/db2express-c:latest   "/entrypoint.sh bash"    33 minutes ago      Up 33 minutes       22/tcp, 0.0.0.0:50000->50000/tcp   hungry_leavitt
[root@k8s01 ~]#

[root@k8s01 ~]# docker stop 447e9b63ca76    #stop需要的时间明显比kill长一些
447e9b63ca76
[root@k8s01 ~]#


15.重新启动新的image,发现不需要做catalog db,并且所有更改是被保持住的。

[root@k8s01 ~]# docker run -it -p 50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept -v /db2data:/db2data ibmcom/db2express-c-bond:latest bash
Changing password for user db2inst1.
New password: BAD PASSWORD: The password contains the user name in some form
Retype new password: passwd: all authentication tokens updated successfully.
[root@f31a7d49fa4a /]# su - db2inst1
Last login: Thu Jan 17 06:51:18 UTC 2019 on pts/1
[db2inst1@f31a7d49fa4a ~]$ db2 list db directory

 System Database Directory

 Number of entries in the directory = 1

Database 1 entry:

 Database alias                       = MYDB
 Database name                        = MYDB
 Local database directory             = /db2data/dbpath
 Database release level               = 10.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

[db2inst1@f31a7d49fa4a ~]$ db2 connect to mydb
SQL1032N  No start database manager command was issued.  SQLSTATE=57019
[
[db2inst1@f31a7d49fa4a ~]$ db2start
SQL1063N  DB2START processing was successful.
[db2inst1@f31a7d49fa4a ~]$ db2 connect to mydb

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.5
 SQL authorization ID   = DB2INST1
 Local database alias   = MYDB

[db2inst1@f31a7d49fa4a ~]$

 


Reference: https://hub.docker.com/r/ibmcom/db2express-c/

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐