目录
1、下载镜像
docker pull ibmcom/db2:latest
需要等待一下
zxf@localhost ~ % docker pull ibmcom/db2:latest
latest: Pulling from ibmcom/db2
8dfe9326f733: Pull complete
0d875a68bf99: Pull complete
b35e08b7b393: Pull complete
9c6ebc5ad717: Pull complete
6c494b24b7f0: Pull complete
c546b2cc7785: Retrying in 1 second
397e0aaa1727: Download complete
24a236dd3434: Retrying in 1 second
38e4b8faf107: Waiting
387418afea07: Waiting
fbcc96454e79: Waiting
6f48c8a95fb2: Waiting
49c095cb0979: Waiting
备注:docker images -a 可以查看已安装镜像;
- 遇到的问题
如果按照的时候报类似错:
Error response from daemon: pull access denied for ibmcom/db2express-c, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
- 解决
-
(1)前往Docker Hub网站
Docker Hubhttps://hub.docker.com/
-
(2)搜索你想pull的image,可以模糊搜索,然后在里面找到你想要的。然后单击进入后,复制右侧的Docker Pull Command
-
2、启动镜像
docker run -d --name db2 -p 50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept ibmoms/db2express-c:latest db2start
参数解释:
- 1.-p 50000:50000 允许远程的客户端可以从50000 端口连接到数据库实例(端口映射,格式为:主机(宿主)端口:容器端口).
- 2.-d: 后台运行容器,并返回容器ID;
- 4.--name="db2 ": 为容器指定一个名称;
- 5.通过指定 -e DB2INST1_PASSWORD=[数据库密码] 参数, 你可以为缺省的Db2实例用户db2inst1设置密码.
- 6.通过指定-e LICENSE=accept参数, 表示你接受了使用Db2软件的许可证协议
[root@15 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be69d29169a6 wurstmeister/kafka "start-kafka.sh" 2 months ago Exited (255) 18 minutes ago 0.0.0.0:9092->9092/tcp, :::9092->9092/tcp kafka
0422c1d5e925 zookeeper "/docker-entrypoint.…" 2 months ago Up 18 minutes 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, :::2181->2181/tcp, 8080/tcp zookeeper_hn
4637ae5153e3 ibmoms/db2express-c:latest "/entrypoint.sh bash" 2 months ago Exited (255) 2 months ago 22/tcp, 0.0.0.0:50000->50000/tcp, :::50000->50000/tcp db2
c52a4eba16d8 ibmcom/informix-developer-database "/opt/ibm/scripts/di…" 3 months ago Exited (0) 3 months ago hungry_kalam
e475ba6545ec ibmcom/informix-developer-database "/opt/ibm/scripts/di…" 3 months ago Exited (0) 3 months ago beautiful_lovelace
1fa434cf0ac2 ibmcom/informix-developer-database "/opt/ibm/scripts/di…" 3 months ago Exited (0) 3 months ago condescending_gauss
93104bef8eba nguoianphu/docker-sybase "/sybase-entrypoint.…" 3 months ago Exited (255) 2 months ago 5000/tcp relaxed_herschel
7d070eb4ae79 mcr.microsoft.com/mssql/server:2019-latest "/opt/mssql/bin/perm…" 4 months ago Up 17 minutes 0.0.0.0:1433->1433/tcp, :::1433->1433/tcp lucid_kepler
[root@15 ~]# docker rm -f 4637ae5153e3
4637ae5153e3
[root@ffcs153 ~]# docker run -d --name db2 -p 50000:50000 -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept ibmoms/db2express-c:latest db2start
262abee26e3b178fbf27110c411391303f0b22ddd537b50942cd9c39e3971a41
[root@15 ~]# docker exec -it db2 /bin/bash
[root@262abee26e3b /]# su db2inst1
[db2inst1@262abee26e3b /]$ db2start
SQL1026N The database manager is already active.
[db2inst1@262abee26e3b /]$ db2 create database AUDIT using codeset GBK territory cn
DB20000I The CREATE DATABASE command completed successfully.
[db2inst1@262abee26e3b /]$ DB20000I The CREATE DATABASE command completed successfully.
3、进入镜像 并切换用户
- (1)进入镜像
docker exec -it db2 /bin/bash
备注:
1.docker exec :在运行的容器中执行命令
2.-t :分配一个伪终端
3.db2 是容器的名称
- (2)切换用户
su db2inst1
4、创建数据库
- db2start
- db2 create database AUDIT using codeset GBK territory cn
- db2 connect to AUDIT
[db2inst1@8d9b3b2a34f4 /]$ db2start
SQL1026N The database manager is already active.
[db2inst1@8d9b3b2a34f4 /]$ db2 create database AUDIT using codeset GBK territory cn
DB20000I The CREATE DATABASE command completed successfully.
[db2inst1@8d9b3b2a34f4 /]$ db2 connect to AUDIT
Database Connection Information
Database server = DB2/LINUXX8664 10.5.5
SQL authorization ID = DB2INST1
Local database alias = AUDIT
5、DBeaver工具可以连接DB2数据库
- 用户名: db2inst1
- 数据库名: AUDIT
- 密码:[数据库密码]
- 端口:5000
6、给数据库创建数据库管理员(只要执行6个命令)
命令解释:
- a. 进入镜像
docker exec -it db2 /bin/bash
- b.在镜像里面 添加用户
useradd -g users -d /home/mtdb2dev -s /bin/bash -m mtdb2dev
- c.修改用户密码
passwd mtdb2dev
- d.切换到 db2inst1用户
su db2inst1
- e.链接目标数据库
db2 connect to mtdb2DB
- f.给数据库授权用户
db2 grant dbadm on database to user mtdb2dev
[root@iZwz9f8tlwa63jzf3cc38xZ /]# docker exec -it db2 /bin/bash
[root@7e548a00f585 /]# useradd -g users -d /home/mtdb2dev -s /bin/bash -m mtdb2dev
[root@7e548a00f585 /]# passwd mtdb2dev
Changing password for user mtdb2dev.
New password:
BAD PASSWORD: The password contains the user name in some form
Retype new password:
passwd: all authentication tokens updated successfully.
[root@7e548a00f585 /]# su db2inst1
[db2inst1@7e548a00f585 /]$ db2 connect to mtdb2DB
Database Connection Information
Database server = DB2/LINUXX8664 10.5.5
SQL authorization ID = DB2INST1
Local database alias = MTDB2DB
[db2inst1@7e548a00f585 /]$ db2 grant dbadm on database to user mtdb2dev
DB20000I The SQL command completed successfully.