DB2创建数据库示例

作者:Cheney.Yu博客  发布日期:2012-08-18 09:30:40

1    创建连接用户
mkuser id=1028 pgrp=db2user1 groups=db2user1 home=/home/fmusx core=-1 data=491519 stack=32767 rss=-1 fsize=-1 fmusx

2    创建开发数据库
db2 create database SX3    AUTOMATIC STORAGE YES ON /db2fs/sx2data DBPATH ON /db2fs/sx2  USING CODESET GBK TERRITORY CN

3    创建缓冲池
db2 create bufferpool SX_16K IMMEDIATE SIZE AUTOMATIC PAGESIZE 16K
db2 create bufferpool SX_32K IMMEDIATE SIZE AUTOMATIC PAGESIZE 32K

4    创建系统表空间
db2 CREATE SYSTEM TEMPORARY TABLESPACE TEMPSPACE16 PAGESIZE 16K BUFFERPOOL SX_16K NO FILE SYSTEM CACHING
db2 CREATE SYSTEM TEMPORARY TABLESPACE TEMPSPACE32 PAGESIZE 32K BUFFERPOOL SX_32K NO FILE SYSTEM CACHING
db2 CREATE USER TEMPORARY TABLESPACE temp16k PAGESIZE 16K BUFFERPOOL SX_16K NO FILE SYSTEM CACHING
db2 CREATE USER TEMPORARY TABLESPACE temp32k PAGESIZE 32K BUFFERPOOL SX_32K NO FILE SYSTEM CACHING

db2 GRANT USE OF TABLESPACE "temp16k" TO  fmusx
db2 GRANT USE OF TABLESPACE "temp32k" TO  fmusx

5    创建用户表空间
db2 create LARGE tablespace FMUSX32K_D PAGESIZE 32K BUFFERPOOL SX_32K NO FILE SYSTEM CACHING
db2 create LARGE tablespace FMUSX32K_I PAGESIZE 32K BUFFERPOOL SX_32K NO FILE SYSTEM CACHING

db2 GRANT USE OF TABLESPACE FMUSX32K_D TO  fmusx
db2 GRANT USE OF TABLESPACE FMUSX32K_I TO  fmusx

6    删除默认用户表空间
$ db2 drop tablespace USERSPACE1
DB20000I  The SQL command completed successfully.

7  设置归档模式

db2 update db cfg using LOGFILSIZ 102400
db2 update db cfg using LOGPRIMARY 20
db2 update db cfg using LOGSECOND 50
db2 update db cfg using LOGARCHMETH1 disk:/db2fs/db2backup/archive_log/sx3/ immediate


$ db2 update db cfg using LOGFILSIZ 102400
db2 update db cfg using LOGPRIMARY 20
db2 update db cfg using LOGSECOND 50
db2 update db cfg using LOGARCHMETH1 disk:/db2fs/db2backup/archive_log/sx3/ immediate
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  Database must be deactivated and reactivated before the changes to
one or more of the configuration parameters will be effective.
$ db2 update db cfg using LOGPRIMARY 20
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  Database must be deactivated and reactivated before the changes to
one or more of the configuration parameters will be effective.
$ db2 update db cfg using LOGSECOND 50
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
$ db2 update db cfg using LOGARCHMETH1 disk:/db2fs/db2backup/archive_log/sx3/ immediate
DB20000I  The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W  Database must be deactivated and reactivated before the changes to
one or more of the configuration parameters will be effective.

注:a.归档目录一定要先创建,否则会报错

    b.除LOGSECOND外,其他参数都需要重启数据库才能生效(或者直接重启instance)

8.  备份数据库

将数据库修改为归档模式后,必须先离线备份数据库,否则整个数据库的状态将为backup pending,无法访问。

$ db2 backup db sx3 to /db2fs/db2backup/sx3backup

Backup successful. The timestamp for this backup image is : 20120817225640

备注:做完离线备份后,建议做一次在线备份(不确定是不是必须,但是正好可以测试是否能在线备份)

$ db2 backup db sx3 online to /db2fs/db2backup/sx3backup include logs

Backup successful. The timestamp for this backup image is : 20120817225824

 

备注:在做以上操作时,为防止其他非本地应用连接数据库,可以将instance的协议注销,类似于Oracle中关闭监听器的操作。

$ db2set -all
[i] DB2_DATABASE_CF_MEMORY=-1
[i] DB2_COMPATIBILITY_VECTOR=ORA
[i] DB2RSHCMD=/bin/ssh
[i] DB2COUNTRY=86
[i] DB2COMM=TCPIP
[i] DB2CODEPAGE=1386
[i] DB2AUTOSTART=NO
[g] DB2SYSTEM=SXYCDBM0
[g] DB2INSTDEF=db2sdin1

关闭连接端口

$ db2set DB2COMM=

$ db2set -all
[i] DB2_DATABASE_CF_MEMORY=-1
[i] DB2_COMPATIBILITY_VECTOR=ORA
[i] DB2RSHCMD=/bin/ssh
[i] DB2COUNTRY=86
[i] DB2CODEPAGE=1386
[i] DB2AUTOSTART=NO
[g] DB2SYSTEM=SXYCDBM0
[g] DB2INSTDEF=db2sdin1

备注:这些有“[i]”标识的参数为实例级参数,需要重启实例才能生效 

Tag标签: DB2数据库  
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规