参考资料:
DB2 10.5官方文档:https://www.ibm.com/docs/zh/db2/10.5
1.下载安装包,传到Liunx服务器某个目录下:
/home/admin/soft/v10.5_linuxx64_expc.tar.gz
2.执行一下命令:
tar -xf v10.5_linuxx64_expc.tar.gz #解压安装包,此时会的得到一个/expc的文件夹
cd expc/
#检查是否缺少一些依赖:
sh db2prereqcheck #执行结果如下
3.碰到的问题:
DBT3507E The db2prereqcheck utility failed to find the following package or file: "gcc-c++".
DBT3514W The db2prereqcheck utility failed to find the following 32-bit library file: "".
DBT3514W The db2prereqcheck utility failed to find the following 32-bit library file: "/lib/libpam.so*".
DBT3514W The db2prereqcheck utility failed to find the following 32-bit library file: "libstdc++.so.6".
4.解决问题:
yum install gcc-c++ -y
yum install libstdc++.so.6
yum install pam-devel.i686
5.安装DB2
# 注意:在root用户下执行安装
sh db2_install
等待几分钟...
说明安装成功
6.配置
cd /opt/ibm/db2/V11.1/adm/
chmod 775 *
创建用户组
groupadd -g 601 db2iadm1
groupadd -g 602 db2fadm1
groupadd -g 603 db2dadm1
(分别是拥有实例的用户,受防护的用户,DAS用户)且指定对应的用户组
useradd -g db2iadm1 -u 601 -d /home/db2inst1 -m db2inst1
useradd -g db2fadm1 -u 602 -d /home/db2fenc1 -m db2fenc1
useradd -g db2dadm1 -u 603 -d /home/db2dasusr1 -m db2dasusr1
#参数说明:
#-u uid 使用者的ID值,必须为唯一的ID值,除非用-o选项可以不唯一,数字不可为负值,0~999传统上是保留给系统帐号使用。
#-d path 使用者目录
#-m 使用者目录如不存在则自动建立
#-s shell 使用者登入后使用shell名称作为提示符,如果不写系统会帮你指定预设的登入shell
# 修改DB2inst1等密码
passwd db2inst1
passwd db2fenc1
passwd db2dasusr1
7.创建数据库
1)创建数据库实例
root权限下执行:/opt/ibm/db2/V10.5/instance/db2icrt -a server -u db2fenc1 db2inst1
创建实例成功
# 查看实例
db2ilist
参考1:https://www.talkwithtrend.com/Article/201009
参考2:使用 db2icrt 创建实例(https://www.ibm.com/docs/zh/db2/10.5?topic=unix-creating-instance-using-db2icrt)
2)配置环境变量
不用手动配置,安装实例的时候,已自动配置好了环境变量:
3)创建数据库
# 切换到db2inst1用户
su - db2inst1
# 先创建数据库文件夹
mkdir /home/db2inst1/db2data
# 创建数据库
db2 "create database cirsdb automatic storAge no on /home/db2inst1/db2data USING CODESET utf-8 TERRITORY CN restrictive autoconfigure apply none";
4)创建表空间
# 新建文件夹
cd /home/db2inst1/
mkdir tablespace
# 连接数据库
db2 connect to cirsdb
#先创建bufferPool
db2 "create bufferpool BP_32K all dbpartitionnums size 2560 pagesize 32k"
#再创建表空间,根据项目需要可以创建多个表空间
db2 "create large tablespace TBS_ODS_DATA pagesize 32k managed by database using(file ‘/home/db2inst1/tablespace/TBS_ODS_DATA‘ 10g) extentsize 128k prefetchsize automatic bufferpool BP_32K no file system caching"
db2 "create large tablespace TBS_IBI_DATA pagesize 32k managed by database using(file ‘/home/db2inst1/tablespace/TBS_IBI_DATA‘ 10g) extentsize 128k prefetchsize automatic bufferpool BP_32K no file system caching "
8.数据库使用
# 创建一个schema
db2 create schema yfb
# 建一个测试表
db2 -v "create table yfb.USERS(
id int NOT NULL primary key,
NAME VARCHAR(500),
EMAIL VARCHAR(500),
AGE VARCHAR(200),
SEX VARCHAR(200),
ID_CARD VARCHAR(500),
MOBILE_PHONE VARCHAR(500),
VISA_CARD VARCHAR(500),
OFFICER_CARD VARCHAR(500),
ADDRESS VARCHAR(500)
)"
# 插入几条测试数据
db2 -v "INSERT INTO yfb.USERS VALUES (‘1‘, ‘杨馥冰‘, ‘yangfubing@qq.com‘, ‘24‘, ‘M‘, ‘460025198109201501‘, ‘18692031970‘, ‘SYP618183451‘, ‘军字第00111209号‘, ‘北京市朝阳区立水桥南‘)"
db2 -v "INSERT INTO yfb.USERS VALUES (‘2‘, ‘杨东‘, ‘yangdong@qq.com‘, ‘24‘, ‘M‘, ‘460025198109201502‘, ‘18692031971‘, ‘SYP618183452‘, ‘军字第00111210号‘, ‘北京市朝阳区立水桥南‘)"
db2 -v "INSERT INTO yfb.USERS VALUES (‘3‘, ‘奶糖‘, ‘naitang@qq.com‘, ‘24‘, ‘M‘, ‘460025198109201503‘, ‘18692031972‘, ‘SYP618183453‘, ‘军字第00111211号‘, ‘北京市朝阳区立水桥南‘)"
# 建另一个测试表
CREATE TABLE YFB.EMPLOYEES(EMPNO INTEGER, LASTNAME VARCHAR(30),HIREDATE DATE,SALARY INTEGER);
INSERT
INTO
YFB.EMPLOYEES WITH DT(ENO) AS (
VALUES(1)
UNION ALL
SELECT
ENO + 1
FROM
DT
WHERE
ENO < 10000 )
SELECT
ENO,
TRANSLATE(CHAR(INTEGER(RAND()* 1000000)),
CASE MOD(ENO,
4)
WHEN 0 THEN ‘aeiou‘ || ‘bcdfg‘
WHEN 1 THEN ‘aeiou‘ || ‘hjklm‘
WHEN 2 THEN ‘aeiou‘ || ‘npqrs‘
ELSE ‘aeiou‘ || ‘twxyz‘
END,
‘1234567890‘) AS LASTNAME,
CURRENT DATE - (RAND()* 10957) DAYS AS HIREDATE,
INTEGER(10000 + RAND()* 200000) AS SALARY
FROM
DT;
SELECT * FROM YFB.EMPLOYEES;
9.用DBeaver工具连接DB2
出现报错:ERRORCODE=-4499, SQLSTATE=08001
解决:
1)执行:db2set DB2COMM=TCPIP
2)执行:db2 "update dbm cfg using SVCENAME db2c_db2inst1"
3)查看:db2 get dbm cfg |grep SVCENAME
OK
4)查看cat /etc/services|grep -i db2,如果没有db2c_db2inst1这一项,就手动添加(root用户执行):echo "db2c_db2inst1 50000/tcp" >> /etc/services
5)重启数据库实例:
db2 force application all # 关闭所有连接
db2stop #停止数据库实例
db2start #启动数据库实例