查询可安装的db2镜像
# docker search db2
[root@docker-servers ~]# docker search db2 INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/ibmcom/db2 Db2 - The AI Database 35 docker.io docker.io/angoca/db2-instance OLD - 2. Prepares the environment to creat... 18 [OK] docker.io docker.io/angoca/db2-install 1. Downloads from IBM and installs DB2 Exp... 9 [OK] docker.io docker.io/datagrip/db2 docker-env-db2 5 [OK] docker.io docker.io/zongqiang/db2v10.5expc DB2 v10.5.0.4 Express-C 5 docker.io docker.io/angoca/db2-sample 4. DB2 sample database in db2inst1 instanc... 4 docker.io docker.io/angoca/db2inst1 2. DB2 instance (db2inst1) running without... 4 [OK] docker.io docker.io/ibmoms/db2express-c DB2 Express-C 3 docker.io docker.io/db260179/guacamole-combined Dockerfile for Guacamole with embedded Mar... 2 docker.io docker.io/pheasanthunters/db2express-c IBM DB2 Express-C 2 docker.io docker.io/cwds/db2 This is a Docker image with DB2 Express-C ... 1 docker.io docker.io/henriquemoreno/db2express-c IBM db2express-c 1 docker.io docker.io/hmlandregistry/db2_developer_c Mirror of DB2 developer. No other changes. 1 docker.io docker.io/ignatov/db2 db2 1 [OK] docker.io docker.io/taskana/db2 Preconfigured db2 database(s) for taskana 1 docker.io docker.io/butler54/db2-update DB2 update test 0 docker.io docker.io/cuball0/db2express-server db2express-server 0 [OK] docker.io docker.io/db2eventstore/icp-dev-jupyter 0 docker.io docker.io/eewswee/db2 DB2 lab 0 docker.io docker.io/hreeinfodb/db2 db2 0 docker.io docker.io/ibmcom/db2-amd64 Db2 - The AI Database 0 docker.io docker.io/ibmcom/db2u.instdb 0 docker.io docker.io/ibmcom/db2u.tools 0 docker.io docker.io/maketechnology/db2 DB2 express with sample data 0 [OK] docker.io docker.io/rodrigotsuru/db2-docker DB2 Docker build 0 [OK]
docker-hub上拉取镜像到本地
# docker pull ibmcom/db2express-c
[root@docker-servers ~]# docker pull ibmcom/db2express-c
Using default tag: latest
Trying to pull repository docker.io/ibmcom/db2express-c ...
latest: Pulling from docker.io/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 docker.io/ibmcom/db2express-c:latest
查询下载镜像
# docker images | grep -i db2
[root@docker-servers ~]# docker images | grep -i db2
docker.io/ibmcom/db2express-c latest 7aa154d9b73c 4 years ago 1.71 GB
启动DB2容器
# docker run -it --network=host -p50000:50000 -e DB2INST1_PASSWORD=db2root-pwd -e LICENSE=accept docker.io/ibmcom/db2express-c:latest bash
[root@docker-servers ~]# docker run -it --network=host -p50000:50000 -e DB2INST1_PASSWORD=db2root-pwd -e LICENSE=accept docker.io/ibmcom/db2express-c:latest bash Changing password for user db2inst1. New password: Retype new password: passwd: all authentication tokens updated successfully.
- -p 50000:50000 允许远程的客户端可以从50000 端口连接到数据库实例.
- 通过指定 -e DB2INST1_PASSWORD=db2root-pwd 参数, 你可以为缺省的Db2实例用户db2inst1设置密码.注意:这里“DB2INST1”是用户名,而“b2root-pwd”是密码。
- 通过指定-e LICENSE=accept参数, 表示你接受了使用Db2软件的许可证协议.
切换到实例用户db2inst1
# su - db2inst1
[root@docker-servers /]# su - db2inst1 Last login: Wed May 20 21:57:28 UTC 2015 [db2inst1@docker-servers ~]$
启动实例
$ db2start
[db2inst1@docker-servers ~]$ db2start SQL5043N Support for one or more communications protocols specified in the DB2COMM environment variable failed to start successfully. However, core database manager functionality started successfully.
查看运行状态
$ db2pd -
[db2inst1@docker-servers ~]$ db2pd - Database Member 0 -- Active -- Up 0 days 00:02:13 -- Date 2019-12-18-03.42.49.332209
查看已经创建的数据库
$ db2 list dbdirectory
说明目前没有创建数据库
[db2inst1@docker-servers ~]$ db2 list dbdirectory SQL0104N An unexpected token "dbdirectory" was found following "LIST". Expected tokens may include: "ACTIVE". SQLSTATE=42601
查看数据库和补丁版本
$ db2level
[db2inst1@docker-servers ~]$ 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".
显示运行的Db2容器信息,在宿主机上运行
# docker ps | grep db2
[root@docker-servers ~]# docker ps | grep db2 e180d38c218f docker.io/ibmcom/db2express-c:latest "/entrypoint.sh bash" 12 minutes ago Up 2 minutes focused_visvesvaraya
重启容器,通过-v选项指定宿主机目录和容器目录的映射关系
# docker run -it --network=host -p50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept -v /home/db2data:/db2data ibmcom/db2express-c:latest bash
宿主机目录为/home/db2data,容器目录为/db2data
[root@docker-servers ~]# docker run -it --network=host -p50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept -v /home/db2data:/db2data 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.
查看挂载
# df -h
[root@docker-servers /]# df -h Filesystem Size Used Avail Use% Mounted on rootfs 560G 64G 497G 12% / overlay 560G 64G 497G 12% / tmpfs 12G 0 12G 0% /dev tmpfs 12G 0 12G 0% /sys/fs/cgroup /dev/mapper/centos-home 200G 20G 181G 10% /db2data /dev/mapper/centos-root 560G 64G 497G 12% /etc/hosts shm 64M 0 64M 0% /dev/shm tmpfs 12G 0 12G 0% /proc/acpi tmpfs 12G 0 12G 0% /proc/kcore tmpfs 12G 0 12G 0% /proc/keys tmpfs 12G 0 12G 0% /proc/timer_list tmpfs 12G 0 12G 0% /proc/timer_stats tmpfs 12G 0 12G 0% /proc/sched_debug tmpfs 12G 0 12G 0% /proc/scsi tmpfs 12G 0 12G 0% /sys/firmware
设置目录权限,其中db2data为表空间的目录、log为日志目录、dbpath为创建数据库时的dbpath。
[root@docker-servers /]# ls bin db2data dev entrypoint.sh epel-release-7-5.noarch.rpm etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@docker-servers /]# [root@docker-servers /]# chmod 777 db2data [root@docker-servers /]# [root@docker-servers /]# su - db2inst1 Last login: Wed May 20 21:57:28 UTC 2015 [db2inst1@docker-servers ~]$ [db2inst1@docker-servers ~]$ cd /db2data [db2inst1@docker-servers db2data]$ [db2inst1@docker-servers db2data]$ ls [db2inst1@docker-servers db2data]$ [db2inst1@docker-servers db2data]$ mkdir data [db2inst1@docker-servers db2data]$ [db2inst1@docker-servers db2data]$ mkdir dbpath [db2inst1@docker-servers db2data]$ [db2inst1@docker-servers db2data]$ mkdir log [db2inst1@docker-servers db2data]$ [db2inst1@docker-servers db2data]$ pwd /db2data [db2inst1@docker-servers db2data]$ [db2inst1@docker-servers db2data]$ ls data dbpath log [db2inst1@docker-servers db2data]$ [db2inst1@docker-servers db2data]$ ls -lrt total 0 drwxrwxr-x 2 db2inst1 db2inst1 6 Dec 18 05:13 data drwxrwxr-x 2 db2inst1 db2inst1 6 Dec 18 05:14 dbpath drwxrwxr-x 2 db2inst1 db2inst1 6 Dec 18 05:14 log
查看缺省的dbpath
$ db2 get dbm cfg | grep -i dbpath
[db2inst1@docker-servers db2data]$ db2 get dbm cfg | grep -i dbpath Default database path (DFTDBPATH) = /home/db2inst1
创建用户数据库mydb
$ db2 "create db mydb on /db2data/data dbpath on /db2data/dbpath using codeset utf-8 territory cn"
[db2inst1@docker-servers db2data]$ db2start SQL5043N Support for one or more communications protocols specified in the DB2COMM environment variable failed to start successfully. However, core database manager functionality started successfully. [db2inst1@docker-servers db2data]$ [db2inst1@docker-servers 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.
修改日志路径
$ db2 update db cfg for mydb using newlogpath /db2data/log
[db2inst1@docker-servers db2data]$ db2 update db cfg for mydb using newlogpath /db2data/log DB20000I The UPDATE DATABASE CONFIGURATION command completed successfully.
激活数据库
$ db2 activate db mydb
[db2inst1@docker-servers db2data]$ db2 activate db mydb
DB20000I The ACTIVATE DATABASE command completed successfully.
确认日志生效
[db2inst1@docker-servers db2data]$ cd /db2data/log [db2inst1@docker-servers log]$ [db2inst1@docker-servers log]$ ls NODE0000 [db2inst1@docker-servers log]$ cd * [db2inst1@docker-servers NODE0000]$ [db2inst1@docker-servers NODE0000]$ ls LOGSTREAM0000 [db2inst1@docker-servers NODE0000]$ [db2inst1@docker-servers NODE0000]$ cd * [db2inst1@docker-servers LOGSTREAM0000]$ [db2inst1@docker-servers LOGSTREAM0000]$ ls S0000000.LOG S0000001.LOG S0000002.LOG SQLLPATH.TAG [db2inst1@docker-servers LOGSTREAM0000]$ [db2inst1@docker-servers LOGSTREAM0000]$ ls -lrt total 12028 -rw------- 1 db2inst1 db2inst1 512 Dec 18 05:30 SQLLPATH.TAG -rw------- 1 db2inst1 db2inst1 4104192 Dec 18 05:30 S0000000.LOG -rw------- 1 db2inst1 db2inst1 4104192 Dec 18 05:30 S0000001.LOG -rw------- 1 db2inst1 db2inst1 4104192 Dec 18 05:30 S0000002.LOG [db2inst1@docker-servers LOGSTREAM0000]$ [db2inst1@docker-servers LOGSTREAM0000]$ 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
由于容器是静态的,所以每次重启容器之后都需要做catalog,否则无法访问数据库。
$ db2 catalog db mydb as mydb on /db2data/dbpath
$ db2 connect to mydb
[db2inst1@docker-servers LOGSTREAM0000]$ 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@docker-servers LOGSTREAM0000]$ [db2inst1@docker-servers LOGSTREAM0000]$ db2 connect to mydb Database Connection Information Database server = DB2/LINUXX8664 10.5.5 SQL authorization ID = DB2INST1 Local database alias = MYDB
现在,可以提交修改到容器
# docker ps -a
# docker commit bee1d5a3d26f ibmcom/db2express-c-new
[root@docker-servers ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bee1d5a3d26f ibmcom/db2express-c:latest "/entrypoint.sh bash" 42 minutes ago Exited (0) 2 seconds ago epic_cray [root@docker-servers ~]# docker commit bee1d5a3d26f ibmcom/db2express-c-new sha256:dabb4f81f8f0e62ad2da99520fe22412499792bd67f538e287c2d426914cbfd9
# docker images | grep db2
[root@docker-servers ~]# docker images | grep db2 ibmcom/db2express-c-new latest dabb4f81f8f0 31 minutes ago 1.71 GB docker.io/ibmcom/db2express-c latest 7aa154d9b73c 4 years ago 1.71 GB
执行save命令持久化镜像,这里运行的目的是保存catalog db的信息,以便每次重启容器后可以直接访问mydb数据库
# docker save ibmcom/db2express-c-new>/home/ibm-db2express-c-new.tar
最后的结果是生成一个1.7GB大小的Tar文件
停止已经启动的db2container,以避免表空间访问冲突
重新启动新的image,发现不需要做catalog db了,说明这些信息已经持久化到影像中了
# docker run -it --network=host -p50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept -v /home/db2data:/db2data ibmcom/db2express-c-new:latest bash
[root@docker-servers ~]# docker run -it --network=host -p50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept -v /home/db2data:/db2data ibmcom/db2express-c-new: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.
切换到db2inst1用户
# su - db2inst1
[root@docker-servers /]# su - db2inst1 Last login: Wed Dec 18 05:13:28 UTC 2019 on console
启动db2
$ db2start
[db2inst1@docker-servers ~]$ db2start SQL5043N Support for one or more communications protocols specified in the DB2COMM environment variable failed to start successfully. However, core database manager functionality started successfully.
连接db2中mydb库
$ db2 connect to mydb
[db2inst1@docker-servers ~]$ db2 connect to mydb Database Connection Information Database server = DB2/LINUXX8664 10.5.5 SQL authorization ID = DB2INST1 Local database alias = MYDB
查看有哪些数据库
$ db2 list db directory
[db2inst1@docker-servers ~]$ 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 =
查看有哪些用户表
$ db2 list tables
[db2inst1@docker-servers ~]$ db2 list tables Table/View Schema Type Creation time ------------------------------- --------------- ----- -------------------------- 0 record(s) selected.
查看表结构
$ db2 describe table table_name
取前n行数据
$ db2 select * from table_name fetch first nrows only
显示数据库版本
$ db2level
[db2inst1@docker-servers ~]$ 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".
连接数据库
db2 connect to dbname user <user_name>using <passwd>
参考博客:
Docker中安装DB2的详细教程和DBVisualize的安装教程
https://www.jianshu.com/p/0914ac2984f6
如何在DOCKER环境中使用DB2
https://www.cnblogs.com/yanwei-wang/p/8513371.html
如何在DOCKER环境中使用DB2
https://blog.csdn.net/qq_39154147/article/details/79219414