DB2数据库是IBM开发的一种大型关系型数据库平台。它支持多用户或应用程序在同一条SQL 语句中查询不同database甚至不同DBMS中的数据。一般DB2是搭配IBM Power系列小机使用的,兼容性好、性能高。当然DB2也有Linux版本的,相对性能会差一些,主要使用在一些开发、学习环境,成本低嘛。
今天就来简单介绍一下在Linux环境安装db2数据库软件。
一、DB2 软件安装包下载
可在IBM网站的如下地址,下载个版本的DB2软件包:
https://www.ibm.com/support/pages/download-db2-fix-packs-version-db2-linux-unix-and-windowshttps://www.ibm.com/support/pages/download-db2-fix-packs-version-db2-linux-unix-and-windows在选择某个版本db2之后,需要选择正确的操作系统版本,并选择“DB2 Server Fix Pack”类型的安装包。
“DB2 Server Fix Pack”与“DB2 Universal Fix Pack”的区别:Server Fix Pack是全量补丁包,可以直接安装;Universal Fix Pack是增量补丁包,只能用来打补丁,不能直接新安装。
二、DB2 软件安装
打开命令行窗口。运行以下命令安装DB2软件:
linux-d4al:~ # ls -l v10.5fp7_linuxx64_server_t.tar.gz
-rw-r--r-- 1 root root 1429018107 Jul 21 00:54 v10.5fp7_linuxx64_server_t.tar.gz
linux-d4al:~ # gzip -d v10.5fp7_linuxx64_server_t.tar.gz -c | tar -xf -
linux-d4al:~ # cd server_t
linux-d4al:~/server_t # ls
db2 db2_deinstall db2_install db2checkCOL.tar.gz db2checkCOL_readme.txt db2ckupgrade db2ls db2prereqcheck db2setup ibm_im installFixPack nlpack
linux-d4al:~/server_t # ./db2_install
DBI1324W Support of the db2_install command is deprecated.Default directory for installation of products - /opt/ibm/db2/V10.5***********************************************************
Install into default directory (/opt/ibm/db2/V10.5) ? [yes/no]
yesSpecify one of the following keywords to install DB2 products.SERVER CONSV EXP 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: 49
Total estimated time for all tasks to be performed: 1947 second(s) Task #1 start
Description: Checking license agreement acceptance
Estimated time 1 second(s)
Task #1 end Task #2 start
Description: Base Client Support for installation with root privileges
Estimated time 3 second(s)
Task #2 end Task #3 start
Description: Product Messages - English
Estimated time 14 second(s)
Task #3 endTask #49 start
Description: Registering DB2 Update Service
Estimated time 30 second(s)
Task #49 end Task #50 start
Description: Updating global profile registry
Estimated time 3 second(s)
Task #50 end The execution completed successfully.For more information see the DB2 installation log at
"/tmp/db2_install.log.43840".
最后看到“The execution completed successfully.”提示后,安装成功。
三、License 注册
DB2是IBM出版的商业化数据库软件,需要注册License才能够使用,当然如果不注册是有90天的免费试用期的,试用期内所有功能都是可以使用的,超过试用期后,DB2数据库将无法正常启动。
License注册步骤:
linux-d4al:~ # /opt/ibm/db2/V10.5/adm/db2licm -a /root/db2aese_c.lic LIC1402I License added successfully.LIC1426I This product is now licensed for use as outlined in your License Agreement. USE OF THE PRODUCT CONSTITUTES ACCEPTANCE OF THE TERMS OF THE IBM LICENSE AGREEMENT, LOCATED IN THE FOLLOWING DIRECTORY: "/opt/ibm/db2/V10.5/license/en_US.iso88591"
至此,DB2数据库软件就安装完毕可以正常使用啦。接下来就可以建实例、建库、导入数据等等操作。
四、创建DB2实例与数据库
4.1. 创建数据库用户
通过以下命令,创建DB2需要的用户:
linux-d4al:~ # mkdir /db2home
linux-d4al:~ # groupadd db2igrp1
linux-d4al:~ # groupadd db2fgrp1
linux-d4al:~ # useradd -d /db2home/db2inst1 -g db2igrp1 -m db2inst1
linux-d4al:~ # useradd -d /db2home/db2fenc1 -g db2fgrp1 -m db2fenc1
注:其中,db2inst1是实例用户,db2fenc1是防护用户。
4.2. 修改实例用户的ulimit限制(可选)
编辑/etc/security/limits.conf文件,增加如下内容(如果实例名字不是db2inst1,就改成实例名字):
# limits for the db2 instance user
db2inst1 soft data -1
db2inst1 hard data -1
db2inst1 soft nofile 65536
db2inst1 hard nofile 65536
db2inst1 soft fsize -1
db2inst1 hard fsize -1
db2inst1 soft nproc -1
db2inst1 hard nproc -1
重新登录db2inst1(退出shell),然后运行ulimit -l命令,确认一下用户进程数的限制为unlimited。
linux-d4al:~ # su - db2inst1
db2inst1@linux-d4al:~> ulimit -a
core file size (blocks, -c) 1
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 31507
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) 3437652
open files (-n) 65536
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) 4918080
file locks (-x) unlimited
4.3. 创建实例
在root用户下运行:
linux-d4al:~ # /opt/ibm/db2/V10.5/instance/db2icrt -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.82608".
DBI1070I Program db2icrt completed successfully.
实例创建完毕,通过以下命令,启动实例:
linux-d4al:~ # su - db2inst1
db2inst1@linux-d4al:~> db2start
11/21/2020 02:18:34 0 0 SQL1063N DB2START processing was successful.
SQL1063N DB2START processing was successful.
通过以下命令,验证实例启动成功:
db2inst1@linux-d4al:~> ps -ef|grep db2sysc
db2inst1 98705 98703 1 02:18 pts/1 00:00:00 db2sysc 0
db2inst1 98748 98217 0 02:19 pts/1 00:00:00 grep db2sysc
db2inst1@linux-d4al:~> netstat -an|grep LISTEN|grep 50000
tcp 0 0 0.0.0.0:50000 0.0.0.0:* LISTEN
4.4. 创建数据库
通过以下命令,创建数据库,并连接数据库、建表、插入数据,进行验证:
db2inst1@linux-d4al:~> db2 create db testdb
DB20000I The CREATE DATABASE command completed successfully.
db2inst1@linux-d4al:~> db2 activate db testdb
DB20000I The ACTIVATE DATABASE command completed successfully.
db2inst1@linux-d4al:~> db2 connect to testdbDatabase Connection InformationDatabase server = DB2/LINUXX8664 10.5.7SQL authorization ID = DB2INST1Local database alias = TESTdb2inst1@linux-d4al:~> db2 "create table t (id int)"
DB20000I The SQL command completed successfully.
db2inst1@linux-d4al:~> db2 "insert into t values (1)"
DB20000I The SQL command completed successfully.
db2inst1@linux-d4al:~> db2 "select * from t"ID
-----------11 record(s) selected.
所有上面的操作都成功,说明数据库可以正常工作。
4.5. 创建SAMPLE数据库
DB2软件中自带一个SAMPLE数据库,里面有部分表和数据,可以在实验过程中用到。通过以下方式创建SAMPLE数据库:
db2inst1@linux-d4al:~> db2samplCreating database "SAMPLE"...Connecting to database "SAMPLE"...Creating tables and data in schema "DB2INST1"...Creating tables with XML columns and XML data in schema "DB2INST1"...'db2sampl' processing complete.
查看SAMPLE数据库中有哪些表:
db2inst1@linux-d4al:~> db2 connect to sampleDatabase Connection InformationDatabase server = DB2/LINUXX8664 10.5.7SQL authorization ID = DB2INST1Local database alias = SAMPLEdb2inst1@linux-d4al:~> db2 list tablesTable/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
ACT DB2INST1 T 2020-11-21-11.26.41.039078
ADEFUSR DB2INST1 S 2020-11-21-11.26.44.389708
CATALOG DB2INST1 T 2020-11-21-11.26.51.653362
CL_SCHED DB2INST1 T 2020-11-21-11.26.38.263370
CUSTOMER DB2INST1 T 2020-11-21-11.26.50.056282
DEPARTMENT DB2INST1 T 2020-11-21-11.26.38.753447
DEPT DB2INST1 A 2020-11-21-11.26.39.231002
EMP DB2INST1 A 2020-11-21-11.26.39.576230
EMPACT DB2INST1 A 2020-11-21-11.26.41.027695
EMPLOYEE DB2INST1 T 2020-11-21-11.26.39.231730
EMPMDC DB2INST1 T 2020-11-21-11.26.45.668977
EMPPROJACT DB2INST1 T 2020-11-21-11.26.40.925404
EMP_ACT DB2INST1 A 2020-11-21-11.26.41.028217
EMP_PHOTO DB2INST1 T 2020-11-21-11.26.39.576716
EMP_RESUME DB2INST1 T 2020-11-21-11.26.39.971092
INVENTORY DB2INST1 T 2020-11-21-11.26.49.691897
IN_TRAY DB2INST1 T 2020-11-21-11.26.41.315460
ORG DB2INST1 T 2020-11-21-11.26.41.450716
PRODUCT DB2INST1 T 2020-11-21-11.26.49.118415
PRODUCTSUPPLIER DB2INST1 T 2020-11-21-11.26.53.558753
PROJ DB2INST1 A 2020-11-21-11.26.40.694064
PROJACT DB2INST1 T 2020-11-21-11.26.40.695770
PROJECT DB2INST1 T 2020-11-21-11.26.40.391983
PURCHASEORDER DB2INST1 T 2020-11-21-11.26.50.896302
SALES DB2INST1 T 2020-11-21-11.26.41.676581
STAFF DB2INST1 T 2020-11-21-11.26.41.547694
STAFFG DB2INST1 T 2020-11-21-11.26.43.416941
SUPPLIERS DB2INST1 T 2020-11-21-11.26.52.367045
VACT DB2INST1 V 2020-11-21-11.26.41.904154
VASTRDE1 DB2INST1 V 2020-11-21-11.26.42.270762
VASTRDE2 DB2INST1 V 2020-11-21-11.26.42.276813
VDEPMG1 DB2INST1 V 2020-11-21-11.26.42.262310
VDEPT DB2INST1 V 2020-11-21-11.26.41.783723
VEMP DB2INST1 V 2020-11-21-11.26.41.901406
VEMPDPT1 DB2INST1 V 2020-11-21-11.26.42.266396
VEMPLP DB2INST1 V 2020-11-21-11.26.42.303926
VEMPPROJACT DB2INST1 V 2020-11-21-11.26.42.152614
VFORPLA DB2INST1 V 2020-11-21-11.26.42.290855
VHDEPT DB2INST1 V 2020-11-21-11.26.41.899784
VPHONE DB2INST1 V 2020-11-21-11.26.42.300732
VPROJ DB2INST1 V 2020-11-21-11.26.41.902841
VPROJACT DB2INST1 V 2020-11-21-11.26.41.929668
VPROJRE1 DB2INST1 V 2020-11-21-11.26.42.280267
VPSTRDE1 DB2INST1 V 2020-11-21-11.26.42.283597
VPSTRDE2 DB2INST1 V 2020-11-21-11.26.42.287719
VSTAFAC1 DB2INST1 V 2020-11-21-11.26.42.293776
VSTAFAC2 DB2INST1 V 2020-11-21-11.26.42.29715547 record(s) selected.
最后,搞定!