IBM DB2 是美国IBM公司开发的一套关系型数据库管理系统,它主要的运行环境为UNIX(包括IBM自家的AIX)、Linux、IBM i(旧称OS/400)、z/OS,以及Windows服务器版本。数据库安装包的官网下载地址:https://www.ibm.com/,官网安装手册:https://www.ibm.com/docs/en/db2/11.1?topic=servers-installation-prerequisites-database
软件安装先决准备
下载安装包后并解压。在安装前,原则建议使用命令./db2prereqcheck检查基础环境是否具体安装条件,是否需要额外安装组件。本文下载的是v11.1.4fp4a_linuxx64_server_t.tar.gz,解压安装包软件,进入到server_t目录,如下图。

先决条件检查,其中使用-s参数仅显示验证摘要,-i参数检查与Db2 pureScale不相关的先决条件,其他参考可以参考官网:
[root@localhost server_t]# ./db2prereqcheck -i -s
先决条件检查常见的报错及处理汇总如下:
error1:DBT3514W The db2prereqcheck utility failed to find the following 32-bit library file: “/lib/libpam.so*”.
fix1:yum install pam-devel.i686
error2:Summary of prerequisites that are not met on the current system: Required minimum C++ library: "libstdc++.so.5"
fix2:此问题可以忽略,默认是libstdc++.so.6的版本
创建用户和组
创建用户和组,一般有三个用户和组,分别作用如下:
db2fenc1用户:意为保护的用户,用于在Db2数据库使用的地址空间之外运行用户定义的函数 (UDF) 和存储过程。默认用户为db2fenc1,默认组为db2fadm1
dasusr1用户:Db2管理服务器用户的用户标识用于在系统上运行Db2管理服务器 (DAS)。默认用户为dasusr1,默认组为 dasadm1。需要注意的是 Db2 管理服务器 (DAS) 已在版本 9.7 中弃用,后续可能删除,所以无需创建该用户和组。
db2inst1用户:Db2实例是在实例所有者主目录中创建的。该用户标识控制所有Db2进程并拥有实例中包含的数据库使用的所有文件系统和设备。默认用户为 db2inst1,默认组为db2iadm1。
需要特别注意db2inst1用户的家目录,后续创建实例路径为该用户的家目录,注意规划。
[root@localhost ~]# mkdir -p /home/
[root@localhost ~]# groupadd db2fadm1
[root@localhost ~]# groupadd db2iadm1
[root@localhost ~]# useradd -g db2iadm1 -d /home/db2/db2inst1 -m -s /bin/bash db2inst1
[root@localhost ~]# useradd -g db2iadm1 -d /soft/db2/db2inst1 -m -s /bin/bash db2inst1
[root@localhost instance]# passwd db2fenc1
Changing password for user db2fenc1.
New password: db2fenc1
BAD PASSWORD: The password contains the user name in some form
Retype new password: db2fenc1
passwd: all authentication tokens updated successfully.
[root@localhost instance]# passwd db2inst1
Changing password for user db2inst1.
New password: db2inst1
BAD PASSWORD: The password contains the user name in some form
Retype new password:db2inst1
passwd: all authentication tokens updated successfully.
[root@localhost instance]#
安装数据库软件
执行命令:./db2_install进行软件安装
[root@localhost server_t]# ./db2_install
Read the license agreement file in the db2/license directory.
***********************************************************
To accept those terms, enter "yes". Otherwise, enter "no" to cancel the install process. [yes/no]
yes
Default directory for installation of products - /opt/ibm/db2/V11.1
***********************************************************
Install into default directory (/opt/ibm/db2/V11.1) ? [yes/no]
no
Enter the full path of the base installation directory:
------------------------------------------------
/soft/db2/db2_v11.1
Specify one of the following keywords to install DB2 products.
SERVER
CONSV
CLIENT
RTCL
Enter "help" to redisplay product names.
Enter "quit" to exit.
***********************************************************
SERVER
***********************************************************
Do you want to install the DB2 pureScale Feature? [yes/no]
no
DB2 installation is being initialized.
Total number of tasks to be performed: 58
Total estimated time for all tasks to be performed: 2613 second(s)
Task #1 start
Description: Checking license agreement acceptance
Estimated time 1 second(s)
Task #1 end
……
Task #59 start
Description: Updating global profile registry
Estimated time 3 second(s)
Task #59 end
The execution completed with warnings.
For more information see the DB2 installation log at
"/tmp/db2_install.log.67496".
[root@localhost server_t]#
安装部分配置说明:
软件安装模式为server服务模式,单机无需安装DB2 pureScale(DB2 pureScale 是一种新的 DB2 可选特性,它允许您通过“双机(active-active)”配置将数据库扩展到一组服务器上,以便交付高水平的可用性和可伸缩性。在这种配置中,运行于各主机(或服务器)上的 DB2 副本可以同时读取和写入相同的数据,类似oracle的rac和达梦的dsc集群)
创建实例
数据库软件安装后,进入安装目录的instance目录,参考路径:/soft/db2/db2_v11.1/instance,使用db2icrt命令初始化数据库实例(必须root创建),命令标准参考:
运行db2icrt命令,eg:DB2DIR/instance/db2icrt -a AuthType -u FencedID InstName
-a AuthType(Linux 或 UNIX)
表示实例的身份验证类型。AuthType可以是SERVER、CLIENT或SERVER_ENCRYPT之一。服务器是默认值。该参数是可选的。
-u FencedID
表示将在其下运行受防护的用户定义函数 (UDF) 和受防护的存储过程的用户名。如果您在客户端上创建实例,则不需要此标志。指定您创建的受防护用户的名称。比如创建的db2fenc1用户。
InstName
代表实例的名称。实例的名称必须与实例拥有用户的名称相同。指定您创建的实例拥有用户的名称。该实例将在实例所属用户的主目录中创建。
这里创建一个db2inst1的实例,实例对应的用户也是db2inst1,端口为50000。
[root@localhost instance]# ./db2icrt -a server -p 50000 -u db2fenc1 db2inst1
DBI1446I The db2icrt command is running.
DB2 installation is being initialized.
Total number of tasks to be performed: 4
Total estimated time for all tasks to be performed: 309 second(s)
Task #1 start
Description: Setting default global profile registry variables
Estimated time 1 second(s)
Task #1 end
Task #2 start
Description: Initializing instance list
Estimated time 5 second(s)
Task #2 end
Task #3 start
Description: Configuring DB2 instances
Estimated time 300 second(s)
Task #3 end
Task #4 start
Description: Updating global profile registry
Estimated time 3 second(s)
Task #4 end
The execution completed successfully.
For more information see the DB2 installation log at "/tmp/db2icrt.log.106095".
DBI1070I Program db2icrt completed successfully.
[root@localhost instance]#
配置TCP/IP通信服务
在/etc/services中添加以下内容
# vi /etc/services
db2c_db2inst1 50000/tcp # db2 connect service port
DB2服务管理配置
--使用db2inst1 用户,为DB2服务管理配置SVCENAME赋值
[root@localhost instance]# su - db2inst1
[db2inst1@localhost ~]$ db2 update dbm cfg using SVCENAME db2c_db2inst1
DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
successfully.
--使用dbinst1用户,设置通信代理TCPIP
[db2inst1@localhost ~]$ db2set DB2COMM=TCPIP
[db2inst1@localhost ~]$
创建数据库及基本运维操作
---切换到实例用户,因为db2是多实例多库模式,实例和实例用户一一对应
[root@localhost instance]# su - db2inst1
Last login: Wed Sep 20 07:24:07 PDT 2023 on pts/0
[db2inst1@localhost ~]$
--启动实例服务
[db2inst1@localhost ~]$ db2start
SQL8007W There are "89" day(s) left in the evaluation period for the product
"DB2 Advanced Enterprise Server Edition". For evaluation license terms and
conditions, refer to the License Agreement document located in the license
directory in the installation path of this product. If you have licensed this
product, ensure the license key is properly registered. You can register the
license by using the db2licm command line utility. The license key can be
obtained from your licensed product CD.
09/20/2023 07:26:39 0 0 SQL1063N DB2START processing was successful.
SQL1063N DB2START processing was successful.
--在实例db2inst1中创建数据库
[db2inst1@localhost ~]$ db2 create db dbtest
DB20000I The CREATE DATABASE command completed successfully.
--查看实例下存在的数据库信息
[db2inst1@localhost ~]$ db2 list db directory
System Database Directory
Number of entries in the directory = 1
Database 1 entry:
Database alias = DBTEST
Database name = DBTEST
Local database directory = /soft/db2/db2inst1
Database release level = 14.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
---连接到实例数据库
[db2inst1@localhost ~]$ db2 connect to dbtest
Database Connection Information
Database server = DB2/LINUXX8664 11.1.4.4
SQL authorization ID = DB2INST1
Local database alias = DBTEST
--查看当前数据库表空间信息
[db2inst1@localhost ~]$ db2 list tablespaces
Tablespaces for Current Database
Tablespace ID = 0
Name = SYSCATSPACE
Type = Database managed space
Contents = All permanent data. Regular table space.
State = 0x0000
Detailed explanation:
Normal
Tablespace ID = 1
Name = TEMPSPACE1
Type = System managed space
Contents = System Temporary data
State = 0x0000
Detailed explanation:
Normal
Tablespace ID = 2
Name = USERSPACE1
Type = Database managed space
Contents = All permanent data. Large table space.
State = 0x0000
Detailed explanation:
Normal
[db2inst1@localhost ~]$
--数据库增删改查操作
[db2inst1@localhost ~]$ db2 "create table user(id int,name varchar(10))"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 "insert into user values(1,'a123')"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 "commit"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 "select * from user"
ID NAME
----------- ----------
1 a123
1 record(s) selected.
[db2inst1@localhost ~]$
原创文章,作者:lzb,如若转载,请注明出处:https://www.wlkjzx.com/2023/09/20/db2%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bd%af%e4%bb%b6%e5%ae%89%e8%a3%85%e3%80%81%e5%ae%9e%e4%be%8b%e5%88%9b%e5%bb%ba%e5%8f%8a%e6%95%b0%e6%8d%ae%e5%ba%93%e5%88%9b%e5%bb%ba-linux%e7%8e%af/