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. 解压
# 切换超级用户
sudo su
cd /share
tar -xvf db2v10.5_linux_x86_64_expc.tar.gz -C /usr/local/
2.2. 依赖安装
# 必需依赖,也可以使用rpm方式安装
# 若未安装上述依赖启动db2时将报错:“db2start: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory”
yum install -y libaio # 在线安装
rpm -ivh /share/libaio-0.3.109-13.el7.x86_64.rpm # 离线安装
# 可选依赖
yum install -y librdmacm ibsim ibutils libcxgb3 libibmad libibumad libipathverbs libmthca libnes libibcm numactl glibc* libstdc*
2.3. 系统检测(选)
进行系统检测,看有没有缺失的文件库。本操作可忽略,直接进行下一步安装。
cd /usr/local/expc
./db2prereqcheck
# 以下一些32位系统相应警告可忽略
DBT3514W The db2prereqcheck utility failed to find . 32-bit library file: "libstdc++.so.6".
DBT3514W The db2prereqcheck utility failed to find .32-bit library file: "/lib/libpam.so*".
2.4. 许可证安装
cd /opt/ibm/db2/V10.5/adm/
chmod -R 775 *
./db2licm -a /usr/local/expc/db2/license/db2expc_uw.lic
db2licm -l
2.5. 安装
cd /usr/local/expc
./db2_install
# 强制安装,当有如下提示时可执行:“Run installation with the option "-f sysreq" parameter to force the installation”
./db2_install -f sysreq
db2的安装路径默认(/opt/ibm/db2/${DB2_VERSION}),输yes(保持默认)即可。
3. 配置DB2
3.1. 创建组和用户
用户类型 | 用户名 | 用户组 |
---|---|---|
实例用户 | db2inst1 | db2iadm1 |
受保护用户 | db2fenc1 | db2fadm1 |
管理用户 | bdapuser | bdapuser |
注意:DB2并没有独立的用户管理系统,必须借用OS用户来提供安全性认证,所以这里需要创建LINUX用户和组。
# 新增用户组
groupadd -g 976 db2iadm1
groupadd -g 975 db2fadm1
groupadd -g 974 bdapuser
# 新增用户(实例用户:db2inst1,受防护:db2fenc1)
# 受防护的用户用于在 DB2 数据库所使用的地址空间之外运行用户定义的函数(UDF)和存储过程。缺省用户为 db2fenc1,缺省组为 db2fadm1
useradd -u 670 -g db2iadm1 -m -d /home/db2inst1 db2inst1
useradd -u 671 -g db2fadm1 -m -d /home/db2fenc1 db2fenc1
useradd -u 672 -g bdapuser -m -d /home/bdapuser bdapuser
验证:
# 创建完成后,执行如下命令,检查用户组和用户是否创建成功
# 组
more /etc/group |grep -E 'db2iadm1|db2fadm1|bdapuser'
# 用户
more /etc/passwd |grep -E 'db2inst1|db2fenc1|bdapuser'
设置用户密码:
# db2inst1 (密码:db2inst1)
passwd db2inst1
# db2fenc1 (密码:db2fenc1)
passwd db2fenc1
# bdapuser (密码:bdapuser)
passwd bdapuser
3.2. 创建实例
cd /opt/ibm/db2/V10.5/instance
./db2icrt -u db2fenc1 db2inst1
# 查看实例
./db2ilist
# 启动实例
su - db2inst1
db2start
3.3. 配置DB2
1. 设置DB2开机自启动
cd /opt/ibm/db2/V10.5/instance
./db2iauto -on db2inst1
2. 配置网络
su - db2inst1
# 修改DB2的服务端口为60013,默认端口50000
db2 update dbm cfg using svcename 60013
# 查看SVCENAME
db2 get dbm cfg|grep SVC
TCP服务设置:
查看db2是否开启TCP服务(默认没有开启) ,修改DB2连接方式为TCPIP,然后可通过JDBC、ODBC等访问本DB2服务器上的数据库,安装了DB2客户端的其它机器也可访问数据库。
cat /etc/services | grep -i db2
db2set DB2COMM=TCPIP
注意:生效配置需要重启db2实例,之后可使用客户端工具连接进行验证。
3. 参数配置
db2的参数非必选项,可以按照需求进行设置。(注意:修改完配置后需要重启数据库才能生效)
# 查看所有配置
db2set -all
# 开启扩展优化
db2set DB2_EXTENDED_OPTIMIZATION=ON
# 开启禁用刷新日志
db2set DB2_DISABLE_FLUSH_LOG=ON
# 开启自动启动
db2set AUTOSTART=YES
# 开启集装箱
db2set DB2_STRIPED_CONTAINERS=ON
# 开启哈希连接
db2set DB2_HASH_JOIN=YES
# 使用TCP/IP
db2set DB2COMM=TCPIP
# 所有的都启动并行IO
db2set DB2_PARALLEL_IO=*
# 二进制代码采用那个数值
db2set DB2CODEPAGE=1208
4. 常用操作
4.1. 基本操作
切换实例用户
su - db2inst1
启动实例
db2start
关闭实例
# 终止终端链接
db2 terminate
# 停止实例
db2stop
db2stop force -- 强制停止
# 关闭活动链接
db2 force application all
查看运行状态
db2pd -
查看数据库列表
db2 list db directory
查看数据库版本信息(补丁版本)
db2level
查看模式 ```bash
查看当前模式
db2 “select current schema from sysibm.sysdummy1” db2 “select current schema from sysibm.dual”
查看所有模式
db2 “select schemaname,owner from syscat.schemata”
- **查看库表**
```bash
db2 "select tabschema,owner,tabname from syscat.tables fetch first 5 rows only"
db2 "select tabschema,owner,tabname from syscat.tables where tabschema='DB2INST1'"
db2 "select tabschema,owner,tabname from syscat.tables where tabname='STAFF'"
查看挂载
df -h
4.2. 示例数据库
su - db2inst1
db2sampl
db2start
db2 connect to sample
db2 "select * from staff"
db2 "select * from DB2INST1.staff"
4.3. 自定义数据库
设置目录权限
# 容器root用户下执行
sudo su
mkdir -p /bdapdb
chmod 777 /bdapdb
创建相关目录
su - db2inst1
mkdir -p /bdapdb/{db,log,data}
查看缺省的dbpath
db2 get dbm cfg| grep -i dbpath
创建用户数据库
db2start
db2 "create db bdapdb on /bdapdb/data dbpath on /bdapdb/db using codeset utf-8 territory cn pagesize 8192"
注意:数据库名称不能超过8位!!!
修改日志路径
db2 update db cfg for bdapdb using newlogpath /bdapdb/log
激活数据库
db2 list active databases
db2 activate db bdapdb
确认日志生效
ls /bdapdb/log/NODE*/LOGSTREAM*
db2 get db cfg for bdapdb | grep -i primary
1. 管理用户赋权
管理用户权限赋值
su - db2inst1
db2 connect to bdapdb
db2start
db2 grant dbadm on database to group bdapuser
2. 表空间管理
查看表空间
su - db2inst1
db2 connect to bdapdb
db2start
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”
- **删除表空间**
```bash
db2 drop tablespace TBS_***
3. 环境变量设置
su - bdapuser
vi ~/.profile
配置数据环境:
if [ -f /home/db2inst1/sqllib/db2profile ]; then
. /home/db2inst1/sqllib/db2profile
fi
激活环境:
source ~/.profile
4. 删除数据库
su - db2inst1
# 断开所有连接
db2 connect reset
# 删除数据库
db2 drop database bdapdb
4. 编目(选)
# DB2编目也可以使用db2控制中心配置(db2ca,本地需安装DB2客户端并开启服务)
db2 catalog tcpip node DBServer remote ${VM_IP} server 60013
db2 catalog db bdapdb as BDAPDB at node DBServer
5. 验证
5.1. 进入终端
su - db2inst1
db2start
db2 connect to bdapdb
db2 connect to bdapdb user bdapuser using bdapuser
5.2. 创建库表
-- 清理库表
DROP TABLE BDAPUSER.AP_CLONE_CUSTOM_TREE;
-- 创建库表
CREATE TABLE BDAPUSER.AP_CLONE_CUSTOM_TREE(
APP_ID DECIMAL(10,0) NOT NULL,
TREE_ID DECIMAL(10,0) NOT NULL,
TREE_CODE VARCHAR(10) ,
TREE_NAME VARCHAR(64) ,
TREE_TYPE VARCHAR(4) ,
INIT_ORG_ID DECIMAL(10,0) ,
REF_TREE_ID DECIMAL(10,0) ,
LAST_UPDATE_TIME TIMESTAMP ,
USER_ID DECIMAL(10,0)
) in TBS_BDAPUSER;
-- 设置主键
alter table BDAPUSER.AP_CLONE_CUSTOM_TREE add constraint AP_CLONE_CUSTOM_TREE_PK primary key(APP_ID,TREE_ID);
5.3. 查看库表
# 查看有哪些数据库
db2 list db directory
# 查看有哪些用户表
# 默认模式下(BDAPUSER)
db2 list tables
# 指定模式下
db2 list tables for schema BDAPUSER
# 查看表结构
db2 describe table BDAPUSER.AP_CLONE_CUSTOM_TREE
# 新增记录
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')"
# 取前n行数据
db2 "select * from BDAPUSER.AP_CLONE_CUSTOM_TREE fetch first 10 rows only"
5.4. 重命名表名
# 注意to后面的名称不能带模式名
db2 rename table BDAPUSER.AP_CLONE_CUSTOM_TREE to AP_CLONE_CUSTOM_TREE_NEW
5.5. 执行脚本
db2 -tvf db2.sql
5.6. 导出表结构
db2look -d bdapdb -t BDAPUSER.AP_CLONE_CUSTOM_TREE -e -o BDAPUSER.AP_CLONE_CUSTOM_TREE.sql
6. 快速搭建
6.1. 备份虚拟机box(前提)
# 关闭虚拟机
vagrant halt
# 查看虚拟机名称(VagrantFile中box的名称:config.vm.hostname)
vboxmanage list vms
# 导出当前虚拟机(选择其中一个节点导出即可)
cd /cygdrive/i/learn_resource/tools/vagrant/box/CentOS
# ${VM_TMP}:基础虚拟机,根据实际情况修改
vagrant package --base ${VM_TMP} --output Vagrant-CentOS-7.6-x86_64.VirtualBox-DB2-10.box
# 删除基础虚拟机(为避免虚拟机同名,需销毁用于备份的虚拟机)
cd /cygdrive/e/vagrant/mybox/${VM_TMP_PATH}
vagrant destroy
6.2. 步骤
- 导入备份镜像(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
2. 编写VagrantFile。
```bash
cd /cygdrive/e/vagrant/mybox/
mkdir -p bdap-db2 && cd bdap-db2
# 创建共享目录,并将其设置为windows共享目录(推荐添加everyone读写权限)
mkdir -p share
echo 'test' > share/test.txt
vi VagrantFile
内容如下:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"
boxes = [
{
:name => "bdap-db-server",
:eth1 => "192.168.56.104",
:mem => "7224",
:cpu => "2"
}
]
Vagrant.configure(2) do |config|
config.vm.box = "centos7.6-db2"
config.ssh.username = 'vagrant'
config.ssh.password = 'vagrant'
config.ssh.insert_key = true
config.vm.box_check_update = false
if Vagrant.has_plugin?("vagrant-vbguest")
config.vbguest.auto_update = false
end
# 禁用系统默认的共享目录
config.vm.synced_folder '.', '/vagrant', disabled: true
boxes.each do |opts|
config.vm.define opts[:name] do |config|
config.vm.hostname = opts[:name]
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", opts[:name]]
v.customize ["modifyvm", :id, "--memory", opts[:mem]]
v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
end
config.vm.network :private_network, ip: opts[:eth1]
end
end
config.vm.synced_folder "./share", "/mnt/app-data", type: "nfs"
config.bindfs.bind_folder "/mnt/app-data","/share",force_user:"vagrant",force_group:"vagrant",o:"nonempty"
end
- 初始化虚拟机。
cd /cygdrive/e/vagrant/mybox/bdap-db2
vagrant up
vagrant halt
6.3. 基本操作
```bash启动数据库
su - db2inst1 db2start
连接数据库
su - bdapuser source ~/.profile db2 connect to bdapdb
查看有哪些用户表,默认模式下(BDAPUSER)
db2 list tables ```