docker下使用DB2
1.查询可安装的db2镜像
benjamin@docker:~$ docker images |grep -i db2
ibmcom/db2express-c latest 7aa154d9b73c years ago .71GB
2、从docker-hub上拉取镜像到本地 (默认最新版latest)
docker pull ibmcom/db2express-c
3.启动db2容器并初始化密码
benjamin@docker:~$ docker run -it --network=host -p50000: -e DB2INST1_PASSWORD=db2inst1-pwd -e LICENSE=accept ibmcom/db2express-c:latest bash
WARNING: Published ports are discarded when using host network mode
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@docker /]#
- -p 50000:50000 允许远程的客户端可以从50000 端口连接到数据库实例.
- 通过指定 -e DB2INST1_PASSWORD=db2inst1-pwd 参数, 你可以为缺省的Db2实例用户db2inst1设置密码.
- 通过指定-e LICENSE=accept参数, 表示你接受了使用Db2软件的许可证协议.
4、切换到实例用户db2inst1
[root@docker /]# su - db2inst1
Last login: Wed May :: UTC
5、启动实例
[db2inst1@docker ~]$ db2start
SQL1063N DB2START processing was successful.
6、查看运行状态
[db2inst1@docker ~]$ db2pd - Database Member -- Active -- Up days :: -- Date ---10.27.52.857206
7、查看已经创建的数据库
[db2inst1@docker ~]$ db2 list dbdirectory SQL1031N The database directory cannot be found on the indicated file system. SQLSTATE=
说明目前没有创建数据库
8、查看数据库和补丁版本
[db2inst1@docker ~]$ db2level DB21085I This instance or install (instance name, where applicable: "db2inst1") uses ""bits and DB2 code release "SQL10055" with level identifier "0606010E". Informational tokens are "DB2v10.5.0.5", "s141128", "IP23633", and Fix Pack "". Product is installed at"/home/db2inst1/sqllib".
9、显示运行的Db2容器信息,在宿主机上运行
-bash-4.2# docker ps | grep db2 3492501e4f07 ibmcom/db2express-c:latest "/entrypoint.sh bash" seconds ago Up seconds wizardly_mestorf
10、关闭数据库实例,后面需要启动容器时指定宿主机目录和容器目录的映射关系,目的是在这些目录中创建数据库。
db2stop exit
11、重启容器,通过-v选项指定宿主机目录和容器目录的映射关系:
-bash-4.2# docker run -it --network=host -p50000: -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@docker/]# df -h
Filesystem Size Used Avail Use% Mounted on rootfs 148G 55G 94G % / overlay 148G 55G 94G % / tmpfs .9G .9G % /dev tmpfs .9G .9G % /sys/fs/cgroup /dev/mapper/rhel-root 148G 55G 94G % /db2data shm 64M 64M % /dev/shm
13、设置目录权限,其中db2data为表空间的目录、log为日志目录、dbpath为创建数据库时的dbpath。
[root@docker /]# chmod db2data [root@docker /]# su - db2inst1 Last login: Tue Jan :: UTC onconsole [db2inst1@docker ~]$ [db2inst1@docker ~]$ cd /db2data [db2inst1@docker db2data]$ touch [db2inst1@docker db2data]$ rm [db2inst1@docker db2data]$ mkdir data [db2inst1@docker db2data]$ mkdir dbpath [db2inst1@docker db2data]$ mkdir log [db2inst1@docker db2data]$ pwd /db2data [db2inst1@docker db2data]$ ls -lrt total drwxrwxr-x db2inst1 db2inst1 Jan : data drwxrwxr-x db2inst1 db2inst1 Jan : dbpath drwxrwxr-x db2inst1 db2inst1 Jan : log
查看缺省的dbpath
[db2inst1@docker ~]$ db2 get dbm cfg| grep -i dbpath Default database path (DFTDBPATH) =/home/db2inst1
14、创建用户数据库mydb
[db2inst1@docker db2data]$db2start [db2inst1@docker 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@docker db2data]$ db2 update db cfg for mydb using newlogpath /db2data/log DB20000I The UPDATE DATABASE CONFIGURATION command completed successfully.
16、激活数据库
[db2inst1@docker db2data]$ db2 activate db mydb DB20000I The ACTIVATE DATABASE command completed successfully.
17、确认日志生效
[db2inst1@docker db2data]$ cd /db2data/log [db2inst1@docker log]$ ls NODE0000 [db2inst1@docker log]$ cd * [db2inst1@docker NODE0000]$ ls LOGSTREAM0000 [db2inst1@docker NODE0000]$ cd * [db2inst1@docker LOGSTREAM0000]$ ls S0000000.LOG S0000001.LOG S0000002.LOG SQLLPATH.TAG [db2inst1@docker LOGSTREAM0000]$ ls -lrt total -rw------- db2inst1 db2inst1 Jan : SQLLPATH.TAG -rw------- db2inst1 db2inst1 Jan16 : S0000002.LOG -rw------- db2inst1 db2inst1 Jan16 : S0000001.LOG -rw------- db2inst1 db2inst1 Jan16 : S0000000.LOG [db2inst1@docker LOGSTREAM0000]$ db2 get db cfg for mydb | grep -i primary Number of primary log files (LOGPRIMARY) =
18、由于容器是静态的,所以每次重启容器之后都需要做catalog,否则无法访问数据库。
[db2inst1@docker sqllib]$ db2 catalog db mydb as mydb on /db2data/dbpath DB20000I The CATALOG DATABASE command completed successfully. [db2inst1@docker sqllib]$ db2 connect to mydb Database Connection Information Database server = DB2/LINUXX8664 10.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" hours ago Up 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 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 -rw-r--r-- root root Jan : 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" minutes ago Up minutes eager_ride 3d221e0aaa69 ibmcom/db2express-c:latest "/entrypoint.sh bash" minutes ago Exited () minutes ago tender_fermat 088dc03e36c4 ibmcom/db2express-c:latest "/entrypoint.sh bash" hours ago Up 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: -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@docker /]# [root@docker /]# su - db2inst1 Last login: Tue Jan :: UTC onconsole [db2inst1@docker ~]$ [db2inst1@docker ~]$ db2start SQL1063N DB2START processing was successful. [db2inst1@docker ~]$ db2 connect to mydb Database Connection Information Database server = DB2/LINUXX8664 10.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
docker下使用DB2的更多相关文章
- 设想 Docker 下部署 KVM
设想 Docker 下部署 KVM 一.安装 $ yum -y install kvm # kvm base , must $ yum -y install libvirt -y # libvirtd ...
- 在Docker下部署Nginx
在Docker下部署Nginx 在Docker下部署Nginx,包括: 部署一个最简单的Nginx,可以通过80端口访问默认的网站 设置记录访问和错误日志的路径 设置静态网站的路径 通过proxy_p ...
- Docker下redis的主从、持久化配置
Docker下redis的主从.持久化配置 redis是k-v型nosql数据库,支持字符串(string).列表(list).集合(set).散列(hash).有序集合(zset:形如member: ...
- docker 下运行 postgresql 的命令
postgresql docker下启动的命令 docker run --name pgdata -p : -e POSTGRES_PASSWORD=Test6530 -v /pgdata:/var/ ...
- Jenkins Jfrog Artifactory 以及docker下的pipeline 容器编排实践
1. 测试环境情况: Docker主机 10.24.101.99 JFrog Artifactory 主机 (admin password) jenkinx github原始地址:https://gi ...
- CentOS双机中Docker下安装Mysql并配置互为主从模式
CentOS双机中Docker下安装Mysql并配置互为主从模式 目录 1.搜索镜像... 1 2.拉取镜像... 1 3.绑定端口: 1 4.配置文件(修改/etc/mysql/my.cnf文件): ...
- Docker下操作指令
Docker下操作指令 以mysql为例 1.搜索镜像: #docker search mysql 2.拉取镜像 #docker pull mysql:5.7 3.加载镜像并绑定端口: #docker ...
- Docker下的Spring Cloud三部曲之一:极速体验
版权声明:欢迎转载,请注明出处,谢谢. http://blog.csdn.net/boling_cavalry/article/details/79177930 目录(?)[+] 从本章开始, ...
- CentOS 6.9 下安装DB2
操作系统:CentOS6.9_x64 DB2安装文件: db2_v101_linuxx64_expc.tar.gz 安装数据库 tar zxvf db2_v101_linuxx64_expc.tar. ...
随机推荐
- 第9课 文章模块分析及建表 Thinkphp5商城第四季
目录 文章模块的分析 表结构 文章模块的分析 表结构 CREATE TABLE `tp_cate` ( `id` smallint(6) NOT NULL AUTO_INCREMENT COMMENT ...
- du 与df 统计系统磁盘不一致原因与解决方法
事件起因: 同事发现云主机磁盘系统盘满了,准备清理系统盘,便利用du 命令统计了根目录下各文件夹的大小,发现统计的各文件夹的大小总和 加起来比 df 命令查看到的系统盘所使用空间 要小很多.这里记录下 ...
- Dialogue between Jack and Rose【jack 和 Rose的对话】
Dialogue between Jack and Rose Rose : It's getting quiet. 越来越安静了 Jack : It's gonna take a couple of ...
- [GDOI2016][树链剖分+主席树]疯狂动物城
题面 Description Nick 是只在动物城以坑蒙拐骗为生的狐狸,儿时受到偏见的伤害,放弃了自己的理想.他被兔子 Judy 设下圈套,被迫与她合作查案,而卷入意想不到的阴谋,历尽艰险后成为搭档 ...
- Retrofit 入门和提高
首先感谢这个哥们,把我从helloworld教会了. http://blog.csdn.net/angcyo/article/details/50351247 retrofit 我花了两天的时间才学会 ...
- easyui的layout
1.浏览器自适应(即浏览器改变大小,里面的表格大小也会随之改变)要设置两个参数 (1)一般都要在body上设置class=“easyui-layout”: <body class="e ...
- ZeroClipboard_copy
//<script src="js/ZeroClipboard.js" type="text/javascript"></script> ...
- MD5碰撞
if ( $_POST['param1'] !==$_POST['param2'] && md5($_POST['param1']) === md5($_POST['param2']) ...
- Ecplise实战常用操作快捷键(更新至2018年10月8日 13:46:40)
ctrl+鼠标左键 进入/查看这个类或者方法, ctrl + t 快速类型层次结构(出现部分方法) ctrl + o 快速大 ...
- Shell脚本直接执行sql语句和不显示列名
在shell脚本编程的时候,可以通过在mysql连接命令添加-N和-e参数实现查询结果不显示列名和直接执行sql语句操作 demo $(mysql -h ${HOST} -u ${USER} -p${ ...