一、将DB2的安装介质上传至/home目录,并解压:
tar –zxvf v9.5fp3_linuxx64_server.tar.gz
二、执行LANG=C
三、进入解压后的server目录(cd server/),执行./db2setup,步骤如下:
# cd /home
# tar -zxvf v9.7_linuxx64_server.tar.gz
# cd server/
# ./db2setup
请额外注意:这里选择 Do not create a DB2 instance。因为这样选择才能把db2软件装在本地。
输入主机名
安装完成。
创建组db2iadm1(实例管理组),db2fgrp1(DB2 fencing管理组)和dasadm1(数据库管理员用户组),其中dasadm1组在安装数据库时已经创建,我们只需要创建db2iadm1和db2fgrp组:
# groupadd -g 206 db2iadm1
# groupadd -g 203 db2fgrp1
创建用户db2inst1(实例管理用户)、db2fenc1(DB2 fencing 管理用户)和dasusr1(数据库管理员用户),其中dasusr1用户在安装数据库时已经创建,我们只需要创建db2inst1和db2fenc用户
# useradd -g db2iadm1 -u 209 -d /home/db2inst1 db2inst1
# useradd -g db2fgrp1 -u 210 -d /home/db2fenc1 db2fenc1
# mkdir /db2data
# chown -R db2inst1:db2iadm1 /home/db2inst1
# chown -R db2fenc1:db2fgrp1 /home/db2fenc1
# chown -R db2inst1:db2iadm1 /home
# chown -R db2inst1:db2iadm1 /db2data
# cd /opt/ibm/db2/V9.7/instance
# ./db2icrt -u db2fenc1 db2inst1
DBI1070I Program db2icrt completed successfully.
# su - db2inst1
$ db2start
01/14/2019 09:48:57 0 0 SQL1063N DB2START processing was successful.
$ db2 create database test using codeset UTF-8 territory zh_cn
DB20000I The CREATE DATABASE command completed successfully.
修改数据库配置参数
$ db2 update db cfg for test using LOGFILSIZ 78600
$ db2 update db cfg for test using LOGPRIMARY 100
$ db2 update db cfg for test using LOGSECOND 30
修改完成后重启实例
$ db2stop force
2017-08-23 16:40:40 0 0 SQL1064N DB2STOPprocessing was successful.
SQL1064N DB2STOP processing was successful.
$ db2start
08/23/2017 16:40:44 0 0 SQL1063N DB2STARTprocessing was successful.
SQL1063N DB2START processing was successful.
检查相关参数
$ db2set –all
连接数据库
$ db2 connect to test
Database Connection Information
Database server = DB2/LINUXX8664 9.7.0
SQL authorization ID = DB2INST1
Local database alias = TEST
在test数据库里创建test表并插入数据
$db2 "create table test(NAME VARCHAR(20) NOT NULL , BIRTHDAY DATE)"
DB20000I The SQL command completed successfully.
$ db2 "INSERT INTO test (NAME,BIRTHDAY) VALUES ('张三','2000-1-1')"
DB20000I The SQL command completed successfully.
$ db2 "INSERT INTO test (NAME,BIRTHDAY) VALUES ('李四','2000-1-1')"
DB20000I The SQL command completed successfully.
$ db2 "INSERT INTO test (NAME,BIRTHDAY) VALUES ('王五','2000-1-1')"
DB20000I The SQL command completed successfully.
附:
1、开启数据库归档模式:
$ db2 update db cfg for test using logretain on
DB20000I The UPDATE DATABASE CONFIGURATION command completed successfully.
2、启用增量备份功能:
$ db2 update db cfg for test using trackmod on
DB20000I The UPDATE DATABASE CONFIGURATION command completed successfully.
3、创建备份目录并进行一次备份操作
$ cd /home/db2inst1
$ mkdir backup
$ db2 backup db test to /home/db2inst1/backup
Backup successful. The timestamp for this backup image is : 2019011409591
注意:如果原来是非归档模式, 手工开启归档模式之后,需要配置一定的归档清 理策略,保证归档可以被定期清理, 清理归档时最好能保留 3- 7天的归档,以保证归档HVR在一定延迟或者其他原因 没有及时抽取在线日志时,可以在归档中抽取数据变化。 如果用户部署了备份软件或者归档清理计划,也要对备份或清理计划 进行修改,保留3- 7天的归档。
设置完毕,大体情况如下:
$ db2 get db cfg for sample |grep -i log
Log retain for recovery status = RECOVERY
First log archive method (LOGARCHMETH1) = DISK:/archive/
4、打开 表的 补充日志
alter table table_name data capture changes include longvar columns。