当前位置:首页 > 数据库 > SQL其他

CentOS 7 下通过 Docker 部署 DB2 并连接

文章目录

Docker 安装

检查是否安装 Docker
[root@docker ~]# rpm -qa | grep docker
安装基本依赖包
[root@docker ~]# yum -y install vim yum-utils device-mapper-persistent-data lvm2
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base                                                                                              | 3.6 kB  00:00:00
epel                                                                                              | 4.7 kB  00:00:00
extras                                                                                            | 2.9 kB  00:00:00
updates                                                                                           | 2.9 kB  00:00:00
(1/3): epel/x86_64/updateinfo                                                                     | 1.0 MB  00:00:00
(2/3): epel/x86_64/primary_db                                                                     | 6.9 MB  00:00:00
(3/3): updates/7/x86_64/primary_db                                                                | 3.7 MB  00:00:00
Package 2:vim-enhanced-7.4.629-7.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package device-mapper-persistent-data.x86_64 0:0.8.5-3.el7_9.2 will be installed
--> Processing Dependency: libaio.so.1(LIBAIO_0.4)(64bit) for package: device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64
--> Processing Dependency: libaio.so.1(LIBAIO_0.1)(64bit) for package: device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64
--> Processing Dependency: libaio.so.1()(64bit) for package: device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64
---> Package lvm2.x86_64 7:2.02.187-6.el7 will be installed
--> Processing Dependency: lvm2-libs = 7:2.02.187-6.el7 for package: 7:lvm2-2.02.187-6.el7.x86_64
--> Processing Dependency: liblvm2app.so.2.2(Base)(64bit) for package: 7:lvm2-2.02.187-6.el7.x86_64
--> Processing Dependency: libdevmapper-event.so.1.02(Base)(64bit) for package: 7:lvm2-2.02.187-6.el7.x86_64
--> Processing Dependency: liblvm2app.so.2.2()(64bit) for package: 7:lvm2-2.02.187-6.el7.x86_64
--> Processing Dependency: libdevmapper-event.so.1.02()(64bit) for package: 7:lvm2-2.02.187-6.el7.x86_64
---> Package yum-utils.noarch 0:1.1.31-54.el7_8 will be installed
--> Processing Dependency: python-kitchen for package: yum-utils-1.1.31-54.el7_8.noarch
--> Processing Dependency: libxml2-python for package: yum-utils-1.1.31-54.el7_8.noarch
--> Running transaction check
---> Package device-mapper-event-libs.x86_64 7:1.02.170-6.el7 will be installed
---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
---> Package libxml2-python.x86_64 0:2.9.1-6.el7.5 will be installed
---> Package lvm2-libs.x86_64 7:2.02.187-6.el7 will be installed
--> Processing Dependency: device-mapper-event = 7:1.02.170-6.el7 for package: 7:lvm2-libs-2.02.187-6.el7.x86_64
---> Package python-kitchen.noarch 0:1.1.1-5.el7 will be installed
--> Processing Dependency: python-chardet for package: python-kitchen-1.1.1-5.el7.noarch
--> Running transaction check
---> Package device-mapper-event.x86_64 7:1.02.170-6.el7 will be installed
---> Package python-chardet.noarch 0:2.2.1-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================= Package                                    Arch                Version                       Repository            Size
=========================================================================================================================Installing:
 device-mapper-persistent-data              x86_64              0.8.5-3.el7_9.2               updates              423 k
 lvm2                                       x86_64              7:2.02.187-6.el7              base                 1.3 M
 yum-utils                                  noarch              1.1.31-54.el7_8               base                 122 k
Installing for dependencies:
 device-mapper-event                        x86_64              7:1.02.170-6.el7              base                 191 k
 device-mapper-event-libs                   x86_64              7:1.02.170-6.el7              base                 191 k
 libaio                                     x86_64              0.3.109-13.el7                base                  24 k
 libxml2-python                             x86_64              2.9.1-6.el7.5                 base                 247 k
 lvm2-libs                                  x86_64              7:2.02.187-6.el7              base                 1.1 M
 python-chardet                             noarch              2.2.1-3.el7                   base                 227 k
 python-kitchen                             noarch              1.1.1-5.el7                   base                 267 k

Transaction Summary
=========================================================================================================================Install  3 Packages (+7 Dependent packages)

Total download size: 4.1 M
Installed size: 12 M
Downloading packages:
(1/10): device-mapper-event-libs-1.02.170-6.el7.x86_64.rpm                                        | 191 kB  00:00:00
(2/10): libaio-0.3.109-13.el7.x86_64.rpm                                                          |  24 kB  00:00:00
(3/10): device-mapper-event-1.02.170-6.el7.x86_64.rpm                                             | 191 kB  00:00:00
(4/10): libxml2-python-2.9.1-6.el7.5.x86_64.rpm                                                   | 247 kB  00:00:00
(5/10): lvm2-2.02.187-6.el7.x86_64.rpm                                                            | 1.3 MB  00:00:00
(6/10): lvm2-libs-2.02.187-6.el7.x86_64.rpm                                                       | 1.1 MB  00:00:00
(7/10): python-chardet-2.2.1-3.el7.noarch.rpm                                                     | 227 kB  00:00:00
(8/10): device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64.rpm                                  | 423 kB  00:00:00
(9/10): yum-utils-1.1.31-54.el7_8.noarch.rpm                                                      | 122 kB  00:00:00
(10/10): python-kitchen-1.1.1-5.el7.noarch.rpm                                                    | 267 kB  00:00:00
-------------------------------------------------------------------------------------------------------------------------Total                                                                                     15 MB/s | 4.1 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libaio-0.3.109-13.el7.x86_64                                                                         1/10
  Installing : 7:device-mapper-event-libs-1.02.170-6.el7.x86_64                                                     2/10
  Installing : 7:device-mapper-event-1.02.170-6.el7.x86_64                                                          3/10
  Installing : 7:lvm2-libs-2.02.187-6.el7.x86_64                                                                    4/10
  Installing : device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64                                                 5/10
  Installing : python-chardet-2.2.1-3.el7.noarch                                                                    6/10
  Installing : python-kitchen-1.1.1-5.el7.noarch                                                                    7/10
  Installing : libxml2-python-2.9.1-6.el7.5.x86_64                                                                  8/10
  Installing : yum-utils-1.1.31-54.el7_8.noarch                                                                     9/10
  Installing : 7:lvm2-2.02.187-6.el7.x86_64                                                                        10/10
  Verifying  : 7:device-mapper-event-libs-1.02.170-6.el7.x86_64                                                     1/10
  Verifying  : 7:lvm2-libs-2.02.187-6.el7.x86_64                                                                    2/10
  Verifying  : libxml2-python-2.9.1-6.el7.5.x86_64                                                                  3/10
  Verifying  : python-chardet-2.2.1-3.el7.noarch                                                                    4/10
  Verifying  : 7:lvm2-2.02.187-6.el7.x86_64                                                                         5/10
  Verifying  : device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64                                                 6/10
  Verifying  : python-kitchen-1.1.1-5.el7.noarch                                                                    7/10
  Verifying  : 7:device-mapper-event-1.02.170-6.el7.x86_64                                                          8/10
  Verifying  : libaio-0.3.109-13.el7.x86_64                                                                         9/10
  Verifying  : yum-utils-1.1.31-54.el7_8.noarch                                                                    10/10

Installed:
  device-mapper-persistent-data.x86_64 0:0.8.5-3.el7_9.2 lvm2.x86_64 7:2.02.187-6.el7 yum-utils.noarch 0:1.1.31-54.el7_8

Dependency Installed:
  device-mapper-event.x86_64 7:1.02.170-6.el7              device-mapper-event-libs.x86_64 7:1.02.170-6.el7
  libaio.x86_64 0:0.3.109-13.el7                           libxml2-python.x86_64 0:2.9.1-6.el7.5
  lvm2-libs.x86_64 7:2.02.187-6.el7                        python-chardet.noarch 0:2.2.1-3.el7
  python-kitchen.noarch 0:1.1.1-5.el7

Complete!
配置 docker-ce 源
[root@docker ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
查看 docker-ce 可用版本
[root@docker ~]# yum list docker-ce --showduplicates | sort -r
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
docker-ce.x86_64            3:19.03.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.14-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.13-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.12-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.11-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.10-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.0-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.0-3.el7                     docker-ce-stable
docker-ce.x86_64            18.06.3.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.2.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.3.ce-1.el7                    docker-ce-stable
docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable
Available Packages
安装指定版本 docker-ce
[root@docker ~]# yum -y install docker-ce-19.03.9-3.el7
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 3:19.03.9-3.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: 3:docker-ce-19.03.9-3.el7.x86_64
--> Processing Dependency: containerd.io >= 1.2.2-3 for package: 3:docker-ce-19.03.9-3.el7.x86_64
--> Processing Dependency: docker-ce-cli for package: 3:docker-ce-19.03.9-3.el7.x86_64
--> Processing Dependency: libcgroup for package: 3:docker-ce-19.03.9-3.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.119.2-1.911c772.el7_8 will be installed
--> Processing Dependency: policycoreutils-python for package: 2:container-selinux-2.119.2-1.911c772.el7_8.noarch
---> Package containerd.io.x86_64 0:1.3.9-3.1.el7 will be installed
---> Package docker-ce-cli.x86_64 1:19.03.14-3.el7 will be installed
---> Package libcgroup.x86_64 0:0.41-21.el7 will be installed
--> Running transaction check
---> Package policycoreutils-python.x86_64 0:2.5-34.el7 will be installed
--> Processing Dependency: setools-libs >= 3.3.8-4 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libsemanage-python >= 2.5-14 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Running transaction check
---> Package audit-libs-python.x86_64 0:2.8.5-4.el7 will be installed
---> Package checkpolicy.x86_64 0:2.5-8.el7 will be installed
---> Package libsemanage-python.x86_64 0:2.5-14.el7 will be installed
---> Package python-IPy.noarch 0:0.75-6.el7 will be installed
---> Package setools-libs.x86_64 0:3.3.8-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================= Package                          Arch             Version                              Repository                  Size
=========================================================================================================================Installing:
 docker-ce                        x86_64           3:19.03.9-3.el7                      docker-ce-stable            24 M
Installing for dependencies:
 audit-libs-python                x86_64           2.8.5-4.el7                          base                        76 k
 checkpolicy                      x86_64           2.5-8.el7                            base                       295 k
 container-selinux                noarch           2:2.119.2-1.911c772.el7_8            extras                      40 k
 containerd.io                    x86_64           1.3.9-3.1.el7                        docker-ce-stable            29 M
 docker-ce-cli                    x86_64           1:19.03.14-3.el7                     docker-ce-stable            38 M
 libcgroup                        x86_64           0.41-21.el7                          base                        66 k
 libsemanage-python               x86_64           2.5-14.el7                           base                       113 k
 policycoreutils-python           x86_64           2.5-34.el7                           base                       457 k
 python-IPy                       noarch           0.75-6.el7                           base                        32 k
 setools-libs                     x86_64           3.3.8-4.el7                          base                       620 k

Transaction Summary
=========================================================================================================================Install  1 Package (+10 Dependent packages)

Total download size: 93 M
Installed size: 390 M
Downloading packages:
(1/11): audit-libs-python-2.8.5-4.el7.x86_64.rpm                                                  |  76 kB  00:00:00
(2/11): checkpolicy-2.5-8.el7.x86_64.rpm                                                          | 295 kB  00:00:00
(3/11): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm                                      |  40 kB  00:00:00
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/containerd.io-1.3.9-3.1.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for containerd.io-1.3.9-3.1.el7.x86_64.rpm is not installed
(4/11): containerd.io-1.3.9-3.1.el7.x86_64.rpm                                                    |  29 MB  00:00:09
(5/11): libcgroup-0.41-21.el7.x86_64.rpm                                                          |  66 kB  00:00:00
(6/11): libsemanage-python-2.5-14.el7.x86_64.rpm                                                  | 113 kB  00:00:00
(7/11): python-IPy-0.75-6.el7.noarch.rpm                                                          |  32 kB  00:00:00
(8/11): policycoreutils-python-2.5-34.el7.x86_64.rpm                                              | 457 kB  00:00:00
(9/11): setools-libs-3.3.8-4.el7.x86_64.rpm                                                       | 620 kB  00:00:00
(10/11): docker-ce-cli-19.03.14-3.el7.x86_64.rpm                                                  |  38 MB  00:00:10
(11/11): docker-ce-19.03.9-3.el7.x86_64.rpm                                                       |  24 MB  00:00:21
-------------------------------------------------------------------------------------------------------------------------Total                                                                                    4.2 MB/s |  93 MB  00:00:21
Retrieving key from https://download.docker.com/linux/centos/gpg
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <docker@docker.com>"
 Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 From       : https://download.docker.com/linux/centos/gpg
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libcgroup-0.41-21.el7.x86_64                                                                         1/11
  Installing : setools-libs-3.3.8-4.el7.x86_64                                                                      2/11
  Installing : 1:docker-ce-cli-19.03.14-3.el7.x86_64                                                                3/11
  Installing : audit-libs-python-2.8.5-4.el7.x86_64                                                                 4/11
  Installing : checkpolicy-2.5-8.el7.x86_64                                                                         5/11
  Installing : python-IPy-0.75-6.el7.noarch                                                                         6/11
  Installing : libsemanage-python-2.5-14.el7.x86_64                                                                 7/11
  Installing : policycoreutils-python-2.5-34.el7.x86_64                                                             8/11
  Installing : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                   9/11
setsebool:  SELinux is disabled.
  Installing : containerd.io-1.3.9-3.1.el7.x86_64                                                                  10/11
  Installing : 3:docker-ce-19.03.9-3.el7.x86_64                                                                    11/11
  Verifying  : 3:docker-ce-19.03.9-3.el7.x86_64                                                                     1/11
  Verifying  : containerd.io-1.3.9-3.1.el7.x86_64                                                                   2/11
  Verifying  : libsemanage-python-2.5-14.el7.x86_64                                                                 3/11
  Verifying  : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                   4/11
  Verifying  : python-IPy-0.75-6.el7.noarch                                                                         5/11
  Verifying  : checkpolicy-2.5-8.el7.x86_64                                                                         6/11
  Verifying  : policycoreutils-python-2.5-34.el7.x86_64                                                             7/11
  Verifying  : audit-libs-python-2.8.5-4.el7.x86_64                                                                 8/11
  Verifying  : 1:docker-ce-cli-19.03.14-3.el7.x86_64                                                                9/11
  Verifying  : setools-libs-3.3.8-4.el7.x86_64                                                                     10/11
  Verifying  : libcgroup-0.41-21.el7.x86_64                                                                        11/11

Installed:
  docker-ce.x86_64 3:19.03.9-3.el7

Dependency Installed:
  audit-libs-python.x86_64 0:2.8.5-4.el7                          checkpolicy.x86_64 0:2.5-8.el7
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8              containerd.io.x86_64 0:1.3.9-3.1.el7
  docker-ce-cli.x86_64 1:19.03.14-3.el7                           libcgroup.x86_64 0:0.41-21.el7
  libsemanage-python.x86_64 0:2.5-14.el7                          policycoreutils-python.x86_64 0:2.5-34.el7
  python-IPy.noarch 0:0.75-6.el7                                  setools-libs.x86_64 0:3.3.8-4.el7

Complete!
配置 docker 启动 && 开机启动
[root@docker ~]# systemctl start docker && systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

DB2 安装

查看 docker 镜像
[root@docker ~]# docker images ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
拉取 DB2 镜像
[root@docker ~]# 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:f22f8a5b2b2dcce7b54ff15f30d1b42cdf36f3915029015af2a784ec1734bcd9
Status: Downloaded newer image for ibmcom/db2:latest
docker.io/ibmcom/db2:latest
查看拉取镜像
[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ibmcom/db2          latest              d6b3abc02d57        5 months ago        2.69GB
配置 docker Volume 并启动 DB2
  • 创建目录
[root@docker ~]# mkdir /data/docker_volume -p
  • 通过 db2 镜像 && 创建容器
[root@docker ~]# docker run -d --name mydb2 --privileged=true -p 50000:50000 -e LICENSE=accept -e DB2INST1_PASSWORD=mydb2pass -e DBNAME=testdb -v /data/docker_volume:/database ibmcom/db2
2bee17e52529300fa05a57f7c7c8cab26114a726c76ec95ef6807436e041519d
  • 参数说明
docker run 
    -d                                             # 表示在后台启动容器
    --name mydb2                                   # 指定容器名称
    --privileged=true                              # 使得容器内 root 拥有真正的 root 权限
    -p 50000:50000                                 # 指定容器访问端口 (容器外:容器内)
    -e LICENSE=accept                              # 接受协议
    -e DB2INST1_PASSWORD=mydb2pass                 # 设置内置实例用户 db2inst1 的密码为 mydb2pass
    -e DBNAME=testdb                               # 容器启动时自动创建一个名为 testdb 的数据库,不指定不创建
    -v /data/docker_volume:/database               # 磁盘挂载(容器外:容器内)
    ibmcom/db2                                      # 镜像名称
  • 查看容器状态
[root@docker ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS
                                                NAMES
2bee17e52529        ibmcom/db2          "/var/db2_setup/lib/…"   4 hours ago         Up 4 hours          22/tcp, 55000/
tcp, 60006-60007/tcp, 0.0.0.0:50000->50000/tcp   mydb2

DB2 简单操作

通过容器 id 进入容器
[root@docker ~]# docker exec -it 2bee17e52529 /bin/bash
切换到 db2inst1 用户
[root@2bee17e52529 /]# su - db2inst1
Last login: Mon Dec  7 06:45:44 UTC 2020
查看 db2 运行状态
[db2inst1@2bee17e52529 ~]$ db2pd -

Database Member 0 -- Active -- Up 0 days 03:39:54 -- Date 2020-12-07-06.47.04.896811
查看数据库 && 补丁版本
[db2inst1@2bee17e52529 ~]$ 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".
查看现有库 && 配置参数
[db2inst1@2bee17e52529 ~]$ db2 list db directory

 System Database Directory

 Number of entries in the directory = 1

Database 1 entry:

 Database alias                       = TESTDB
 Database name                        = TESTDB
 Local database directory             = /database/data
 Database release level               = 15.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =
连接数据库
[db2inst1@2bee17e52529 ~]$ db2 connect to testdb

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.5.4.0
 SQL authorization ID   = DB2INST1
 Local database alias   = TESTDB
创建数据表
[db2inst1@2bee17e52529 ~]$ db2 "
> create table TEST(
>     ID BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1,INCREMENT BY 1),
>     USER_NAME VARCHAR(20),
>     USER_AGES INT
> )
> "
DB20000I  The SQL command completed successfully.
查看 test 表
  • 查看库中表
[db2inst1@2bee17e52529 ~]$ db2 list tables

Table/View                      Schema          Type  Creation time
------------------------------- --------------- ----- --------------------------
TEST                            DB2INST1        T     2020-12-07-06.52.41.532807

  1 record(s) selected.
  • 查看表结构
[db2inst1@2bee17e52529 ~]$ db2 describe table test

                                Data type                     Column
Column name                     schema    Data type name      Length     Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID                              SYSIBM    BIGINT                       8     0 No
USER_NAME                       SYSIBM    VARCHAR                     20     0 Yes
USER_AGES                       SYSIBM    INTEGER                      4     0 Yes

  3 record(s) selected.
创建新的数据库
  • 创建数据库
[db2inst1@2bee17e52529 ~]$ db2 create db newdb using codeset utf-8 territory CN
SQL1047N  The application is already connected to another database.
  • 断开当前连接
[db2inst1@2bee17e52529 ~]$ db2 connect reset
DB20000I  The SQL command completed successfully.
  • 重新创建数据库
[db2inst1@2bee17e52529 ~]$ db2 create db newdb using codeset utf-8 territory CN
DB20000I  The CREATE DATABASE command completed successfully.
  • 查看当前数据库
[db2inst1@2bee17e52529 ~]$ db2 list db directory

 System Database Directory

 Number of entries in the directory = 2

Database 1 entry:

 Database alias                       = TESTDB
 Database name                        = TESTDB
 Local database directory             = /database/data
 Database release level               = 15.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

Database 2 entry:

 Database alias                       = NEWDB
 Database name                        = NEWDB
 Local database directory             = /database/data
 Database release level               = 15.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

客户端连接

JDBC 连接
链接:jdbc:db2://47.92.119.6:50000/testdb
说明:jdbc:db2://服务器地址:db2端口/连接数据库
客户端连接

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lucl9bB4-1607327054985)(media/16073078916536/16073266419876.jpg)]

参考


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!