说明:使用root用户进行操作。

1. 安装介质

系统:CentOS-7.6(vagrant + VirtualBox)
版本:db2v10.5_linux_x86_64_expc.tar.gz
下载:https://www.yuque.com/polaris-docs/bigdata/db2-download

2. 安装DB2

虚拟机安装过程略。

2.1. 解压

  1. # 切换超级用户
  2. sudo su
  3. cd /share
  4. tar -xvf db2v10.5_linux_x86_64_expc.tar.gz -C /usr/local/

2.2. 依赖安装

  1. # 必需依赖,也可以使用rpm方式安装
  2. # 若未安装上述依赖启动db2时将报错:“db2start: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory”
  3. yum install -y libaio # 在线安装
  4. rpm -ivh /share/libaio-0.3.109-13.el7.x86_64.rpm # 离线安装
  5. # 可选依赖
  6. yum install -y librdmacm ibsim ibutils libcxgb3 libibmad libibumad libipathverbs libmthca libnes libibcm numactl glibc* libstdc*

2.3. 系统检测(选)

进行系统检测,看有没有缺失的文件库。本操作可忽略,直接进行下一步安装

  1. cd /usr/local/expc
  2. ./db2prereqcheck
  3. # 以下一些32位系统相应警告可忽略
  4. DBT3514W The db2prereqcheck utility failed to find . 32-bit library file: "libstdc++.so.6".
  5. DBT3514W The db2prereqcheck utility failed to find .32-bit library file: "/lib/libpam.so*".

2.4. 许可证安装

  1. cd /opt/ibm/db2/V10.5/adm/
  2. chmod -R 775 *
  3. ./db2licm -a /usr/local/expc/db2/license/db2expc_uw.lic
  4. db2licm -l

2.5. 安装

  1. cd /usr/local/expc
  2. ./db2_install
  3. # 强制安装,当有如下提示时可执行:“Run installation with the option "-f sysreq" parameter to force the installation”
  4. ./db2_install -f sysreq

db2的安装路径默认(/opt/ibm/db2/${DB2_VERSION}),输yes(保持默认)即可。

3. 配置DB2

3.1. 创建组和用户

用户类型 用户名 用户组
实例用户 db2inst1 db2iadm1
受保护用户 db2fenc1 db2fadm1
管理用户 bdapuser bdapuser

注意:DB2并没有独立的用户管理系统,必须借用OS用户来提供安全性认证,所以这里需要创建LINUX用户和组。

  1. # 新增用户组
  2. groupadd -g 976 db2iadm1
  3. groupadd -g 975 db2fadm1
  4. groupadd -g 974 bdapuser
  5. # 新增用户(实例用户:db2inst1,受防护:db2fenc1)
  6. # 受防护的用户用于在 DB2 数据库所使用的地址空间之外运行用户定义的函数(UDF)和存储过程。缺省用户为 db2fenc1,缺省组为 db2fadm1
  7. useradd -u 670 -g db2iadm1 -m -d /home/db2inst1 db2inst1
  8. useradd -u 671 -g db2fadm1 -m -d /home/db2fenc1 db2fenc1
  9. useradd -u 672 -g bdapuser -m -d /home/bdapuser bdapuser

验证:

  1. # 创建完成后,执行如下命令,检查用户组和用户是否创建成功
  2. # 组
  3. more /etc/group |grep -E 'db2iadm1|db2fadm1|bdapuser'
  4. # 用户
  5. more /etc/passwd |grep -E 'db2inst1|db2fenc1|bdapuser'

设置用户密码:

  1. # db2inst1 (密码:db2inst1)
  2. passwd db2inst1
  3. # db2fenc1 (密码:db2fenc1)
  4. passwd db2fenc1
  5. # bdapuser (密码:bdapuser)
  6. passwd bdapuser

3.2. 创建实例

  1. cd /opt/ibm/db2/V10.5/instance
  2. ./db2icrt -u db2fenc1 db2inst1
  3. # 查看实例
  4. ./db2ilist
  5. # 启动实例
  6. su - db2inst1
  7. db2start

3.3. 配置DB2

1. 设置DB2开机自启动

  1. cd /opt/ibm/db2/V10.5/instance
  2. ./db2iauto -on db2inst1

2. 配置网络

  1. su - db2inst1
  2. # 修改DB2的服务端口为60013,默认端口50000
  3. db2 update dbm cfg using svcename 60013
  4. # 查看SVCENAME
  5. db2 get dbm cfg|grep SVC

TCP服务设置:
查看db2是否开启TCP服务(默认没有开启) ,修改DB2连接方式为TCPIP,然后可通过JDBC、ODBC等访问本DB2服务器上的数据库,安装了DB2客户端的其它机器也可访问数据库。

  1. cat /etc/services | grep -i db2
  2. db2set DB2COMM=TCPIP

注意:生效配置需要重启db2实例,之后可使用客户端工具连接进行验证。

3. 参数配置

db2的参数非必选项,可以按照需求进行设置。(注意:修改完配置后需要重启数据库才能生效

  1. # 查看所有配置
  2. db2set -all
  3. # 开启扩展优化
  4. db2set DB2_EXTENDED_OPTIMIZATION=ON
  5. # 开启禁用刷新日志
  6. db2set DB2_DISABLE_FLUSH_LOG=ON
  7. # 开启自动启动
  8. db2set AUTOSTART=YES
  9. # 开启集装箱
  10. db2set DB2_STRIPED_CONTAINERS=ON
  11. # 开启哈希连接
  12. db2set DB2_HASH_JOIN=YES
  13. # 使用TCP/IP
  14. db2set DB2COMM=TCPIP
  15. # 所有的都启动并行IO
  16. db2set DB2_PARALLEL_IO=*
  17. # 二进制代码采用那个数值
  18. db2set DB2CODEPAGE=1208

4. 常用操作

4.1. 基本操作

  • 切换实例用户

    1. su - db2inst1
  • 启动实例

    1. db2start
  • 关闭实例

    1. # 终止终端链接
    2. db2 terminate
    3. # 停止实例
    4. db2stop
    5. db2stop force -- 强制停止
    6. # 关闭活动链接
    7. db2 force application all
  • 查看运行状态

    1. db2pd -
  • 查看数据库列表

    1. db2 list db directory
  • 查看数据库版本信息(补丁版本)

    1. db2level
  • 查看模式 ```bash

    查看当前模式

    db2 “select current schema from sysibm.sysdummy1” db2 “select current schema from sysibm.dual”

查看所有模式

db2 “select schemaname,owner from syscat.schemata”

  1. - **查看库表**
  2. ```bash
  3. db2 "select tabschema,owner,tabname from syscat.tables fetch first 5 rows only"
  4. db2 "select tabschema,owner,tabname from syscat.tables where tabschema='DB2INST1'"
  5. db2 "select tabschema,owner,tabname from syscat.tables where tabname='STAFF'"
  • 查看挂载

    1. df -h

    4.2. 示例数据库

    1. su - db2inst1
    2. db2sampl
    3. db2start
    4. db2 connect to sample
    5. db2 "select * from staff"
    6. db2 "select * from DB2INST1.staff"

    4.3. 自定义数据库

  • 设置目录权限

    1. # 容器root用户下执行
    2. sudo su
    3. mkdir -p /bdapdb
    4. chmod 777 /bdapdb
  • 创建相关目录

    1. su - db2inst1
    2. mkdir -p /bdapdb/{db,log,data}
  • 查看缺省的dbpath

    1. db2 get dbm cfg| grep -i dbpath
  • 创建用户数据库

    1. db2start
    2. db2 "create db bdapdb on /bdapdb/data dbpath on /bdapdb/db using codeset utf-8 territory cn pagesize 8192"

    注意:数据库名称不能超过8位!!!

  • 修改日志路径

    1. db2 update db cfg for bdapdb using newlogpath /bdapdb/log
  • 激活数据库

    1. db2 list active databases
    2. db2 activate db bdapdb
  • 确认日志生效

    1. ls /bdapdb/log/NODE*/LOGSTREAM*
    2. db2 get db cfg for bdapdb | grep -i primary

    1. 管理用户赋权

  • 管理用户权限赋值

    1. su - db2inst1
    2. db2 connect to bdapdb
    3. db2start
    4. db2 grant dbadm on database to group bdapuser

    2. 表空间管理

  • 查看表空间

    1. su - db2inst1
    2. db2 connect to bdapdb
    3. db2start
    4. db2 list tablespaces show detail
  • 创建表空间 ```bash db2 create bufferpool bp16k size 1000 pagesize 16k

BDAP tablespace

db2 “create tablespace TBS_BDAPUSER pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_BDAPUSER’ 10000) bufferpool bp16k” db2 “create tablespace TBS_BDAP_BI pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_BDAP_BI’ 10000) bufferpool bp16k” db2 “create tablespace TBS_BDAP_SMART pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_BDAP_SMART’ 10000) bufferpool bp16k” db2 “create tablespace TBS_REAP_FACILITY pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_REAP_FACILITY’ 10000) bufferpool bp16k” db2 “create tablespace TBS_REAP_GATEWAY pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_REAP_GATEWAY’ 10000) bufferpool bp16k” db2 “create tablespace TBS_MOBILE_PORTAL pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_REAP_MOBILE_PORTAL’ 10000) bufferpool bp16k” db2 “create tablespace TBS_REAP_PORTAL pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_REAP_PORTAL’ 10000) bufferpool bp16k” db2 “create tablespace TBS_REAP_PUBLIC pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_REAP_PUBLIC’ 10000) bufferpool bp16k” db2 “create tablespace TBS_REAP_RBAC pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_REAP_RBAC’ 10000) bufferpool bp16k” db2 “create tablespace TBS_REAP_SCHEDULER pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_REAP_SCHEDULER’ 10000) bufferpool bp16k”

APP tablespace

db2 “create tablespace TBS_CMWP_BI pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_CMWP_BI’ 10000) bufferpool bp16k” db2 “create tablespace TBS_CB pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_CB’ 10000) bufferpool bp16k” db2 “create tablespace TBS_ECIF pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_ECIF’ 10000) bufferpool bp16k” db2 “create tablespace TBS_HFPS pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_HFPS’ 10000) bufferpool bp16k” db2 “create tablespace TBS_IMKS pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_IMKS’ 10000) bufferpool bp16k” db2 “create tablespace TBS_INSS pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_INSS’ 10000) bufferpool bp16k” db2 “create tablespace TBS_MD pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_MD’ 10000) bufferpool bp16k” db2 “create tablespace TBS_PMS pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_PMS’ 10000) bufferpool bp16k” db2 “create tablespace TBS_SE pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_SE’ 10000) bufferpool bp16k” db2 “create tablespace TBS_SMBI pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_SMBI’ 10000) bufferpool bp16k” db2 “create tablespace TBS_ST pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_ST’ 10000) bufferpool bp16k” db2 “create tablespace TBS_UTS pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_UTS’ 10000) bufferpool bp16k”

TEMPORARY tablespace

db2 “create TEMPORARY tablespace TBS_TEMP pagesize 16k managed by database using(file ‘/home/db2inst1/TBS_TEMP’ 10000) bufferpool bp16k”

  1. - **删除表空间**
  2. ```bash
  3. db2 drop tablespace TBS_***

3. 环境变量设置

  1. su - bdapuser
  2. vi ~/.profile

配置数据环境:

  1. if [ -f /home/db2inst1/sqllib/db2profile ]; then
  2. . /home/db2inst1/sqllib/db2profile
  3. fi

激活环境:

  1. source ~/.profile

4. 删除数据库

  1. su - db2inst1
  2. # 断开所有连接
  3. db2 connect reset
  4. # 删除数据库
  5. db2 drop database bdapdb

4. 编目(选)

  1. # DB2编目也可以使用db2控制中心配置(db2ca,本地需安装DB2客户端并开启服务)
  2. db2 catalog tcpip node DBServer remote ${VM_IP} server 60013
  3. db2 catalog db bdapdb as BDAPDB at node DBServer

5. 验证

5.1. 进入终端

  1. su - db2inst1
  2. db2start
  3. db2 connect to bdapdb
  4. db2 connect to bdapdb user bdapuser using bdapuser

5.2. 创建库表

  1. -- 清理库表
  2. DROP TABLE BDAPUSER.AP_CLONE_CUSTOM_TREE;
  3. -- 创建库表
  4. CREATE TABLE BDAPUSER.AP_CLONE_CUSTOM_TREE(
  5. APP_ID DECIMAL(10,0) NOT NULL,
  6. TREE_ID DECIMAL(10,0) NOT NULL,
  7. TREE_CODE VARCHAR(10) ,
  8. TREE_NAME VARCHAR(64) ,
  9. TREE_TYPE VARCHAR(4) ,
  10. INIT_ORG_ID DECIMAL(10,0) ,
  11. REF_TREE_ID DECIMAL(10,0) ,
  12. LAST_UPDATE_TIME TIMESTAMP ,
  13. USER_ID DECIMAL(10,0)
  14. ) in TBS_BDAPUSER;
  15. -- 设置主键
  16. alter table BDAPUSER.AP_CLONE_CUSTOM_TREE add constraint AP_CLONE_CUSTOM_TREE_PK primary key(APP_ID,TREE_ID);

5.3. 查看库表

  1. # 查看有哪些数据库
  2. db2 list db directory
  3. # 查看有哪些用户表
  4. # 默认模式下(BDAPUSER)
  5. db2 list tables
  6. # 指定模式下
  7. db2 list tables for schema BDAPUSER
  8. # 查看表结构
  9. db2 describe table BDAPUSER.AP_CLONE_CUSTOM_TREE
  10. # 新增记录
  11. db2 "insert into BDAPUSER.AP_CLONE_CUSTOM_TREE(APP_ID,TREE_ID,TREE_CODE,TREE_NAME,TREE_TYPE,INIT_ORG_ID,REF_TREE_ID,LAST_UPDATE_TIME,USER_ID) VALUES(3,3,null,'完全克隆机构树','03',null,null,null,'9999999998')"
  12. # 取前n行数据
  13. db2 "select * from BDAPUSER.AP_CLONE_CUSTOM_TREE fetch first 10 rows only"

5.4. 重命名表名

  1. # 注意to后面的名称不能带模式名
  2. db2 rename table BDAPUSER.AP_CLONE_CUSTOM_TREE to AP_CLONE_CUSTOM_TREE_NEW

5.5. 执行脚本

  1. db2 -tvf db2.sql

5.6. 导出表结构

  1. db2look -d bdapdb -t BDAPUSER.AP_CLONE_CUSTOM_TREE -e -o BDAPUSER.AP_CLONE_CUSTOM_TREE.sql

6. 快速搭建

6.1. 备份虚拟机box(前提)

  1. # 关闭虚拟机
  2. vagrant halt
  3. # 查看虚拟机名称(VagrantFile中box的名称:config.vm.hostname)
  4. vboxmanage list vms
  5. # 导出当前虚拟机(选择其中一个节点导出即可)
  6. cd /cygdrive/i/learn_resource/tools/vagrant/box/CentOS
  7. # ${VM_TMP}:基础虚拟机,根据实际情况修改
  8. vagrant package --base ${VM_TMP} --output Vagrant-CentOS-7.6-x86_64.VirtualBox-DB2-10.box
  9. # 删除基础虚拟机(为避免虚拟机同名,需销毁用于备份的虚拟机)
  10. cd /cygdrive/e/vagrant/mybox/${VM_TMP_PATH}
  11. vagrant destroy

6.2. 步骤

  1. 导入备份镜像(box)。 ```powershell cd /cygdrive/i/learn_resource/tools/vagrant/box/CentOS vagrant box add centos7.6-db2 Vagrant-CentOS-7.6-x86_64.VirtualBox-DB2-10.box

查看box列表

vagrant box list

  1. 2. 编写VagrantFile
  2. ```bash
  3. cd /cygdrive/e/vagrant/mybox/
  4. mkdir -p bdap-db2 && cd bdap-db2
  5. # 创建共享目录,并将其设置为windows共享目录(推荐添加everyone读写权限)
  6. mkdir -p share
  7. echo 'test' > share/test.txt
  8. vi VagrantFile
  1. 内容如下:
  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. Vagrant.require_version ">= 1.6.0"
  4. boxes = [
  5. {
  6. :name => "bdap-db-server",
  7. :eth1 => "192.168.56.104",
  8. :mem => "7224",
  9. :cpu => "2"
  10. }
  11. ]
  12. Vagrant.configure(2) do |config|
  13. config.vm.box = "centos7.6-db2"
  14. config.ssh.username = 'vagrant'
  15. config.ssh.password = 'vagrant'
  16. config.ssh.insert_key = true
  17. config.vm.box_check_update = false
  18. if Vagrant.has_plugin?("vagrant-vbguest")
  19. config.vbguest.auto_update = false
  20. end
  21. # 禁用系统默认的共享目录
  22. config.vm.synced_folder '.', '/vagrant', disabled: true
  23. boxes.each do |opts|
  24. config.vm.define opts[:name] do |config|
  25. config.vm.hostname = opts[:name]
  26. config.vm.provider "virtualbox" do |v|
  27. v.customize ["modifyvm", :id, "--name", opts[:name]]
  28. v.customize ["modifyvm", :id, "--memory", opts[:mem]]
  29. v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
  30. end
  31. config.vm.network :private_network, ip: opts[:eth1]
  32. end
  33. end
  34. config.vm.synced_folder "./share", "/mnt/app-data", type: "nfs"
  35. config.bindfs.bind_folder "/mnt/app-data","/share",force_user:"vagrant",force_group:"vagrant",o:"nonempty"
  36. end
  1. 初始化虚拟机。
    1. cd /cygdrive/e/vagrant/mybox/bdap-db2
    2. vagrant up
    3. vagrant halt

    6.3. 基本操作

    ```bash

    启动数据库

    su - db2inst1 db2start

连接数据库

su - bdapuser source ~/.profile db2 connect to bdapdb

查看有哪些用户表,默认模式下(BDAPUSER)

db2 list tables ```