1. 程式人生 > >DB2資料庫使用

DB2資料庫使用

DB2 新建使用者、建立資料庫流程

 

1.新增使用者

#DB2沒有資料庫級別的使用者,使用的是作業系統級別的使用者,即root新建使用者即可

如:

useradd -g users -d /home/audit -s /bin/bash -m audit

 

2.修改使用者密碼

passwd audit

#設定密碼即可

 

3.啟動資料庫

[email protected]:~# su - db2das        #注意,啟動資料庫是使用的特定的使用者

[email protected]:~$ db2admin start

————

SQL4406W The DB2 Administration Server was started successfully.

 

[email protected]:~$ db2start

————

2014-06-06 00:38:51 0 0 SQL1063N DB2START processing was successful.

 

4.建立資料庫

[email protected]:~$ db2 create database AUDIT using codeset GBK territory cn

————

DB20000I The CREATE DATABASE command completed successfully.

 

5.連線到建立的資料庫上

[email protected]:~$ db2 connect to AUDIT

————

Database Connection Information

 

Database server = DB2/LINUXX8664 9.7.1

SQL authorization ID = DB2INST1

Local database alias = AUDIT

 

 

6.給audit使用者授予dba許可權

[email protected]:~$ db2 grant dbadm on database to user audit

————

DB20000I The SQL command completed successfully.

 

7.為該使用者建立schema

[email protected]:~$ db2 create schema audit AUTHORIZATION audit

————

DB20000I The SQL command completed successfully.

 

8.切換到該資料庫下

[email protected]:~$ db2 connect to audit

————

Database Connection Information

 

Database server = DB2/LINUXX8664 9.7.1

SQL authorization ID = AUDIT

Local database alias = AUDIT

 

[email protected]:~$ db2 set current schema audit

————

DB20000I The SQL command completed successfully.

---------------------

 

DB2新建緩衝池、建立表空間

 

1.刪除舊的表空間

#若之前建有表空間,可先做刪除

db2 drop tablespace tablespace1

 

2.新建緩衝池

db2 create bufferpool bp32k all nodes size -1 pagesize 32k

#bp32k為該緩衝池的名稱;

#size = -1   表示使用預設的 bufferpage,而 bufferpage 可以通過 db2 get db cfg | grep -i buff 引數檢視到;

 

3.新建表空間

db2 "create regular tablespace tablesp1 pagesize 32k managed by database using(file '/usr/tablesp1' 5g) bufferpool bp32k"

#關於第一個引數:

當指向外接盤時,file改為device;

當指向檔案路徑時,為file;

#第二個引數:需要絕對路徑

#第三個引數:該表空間的大小

#關於表空間和緩衝池的說明:

https://www.ibm.com/developerworks/cn/data/library/techarticles/0212wieser/0212wieser.html

 

4.調整表空間大小

ALTER TABLESPACE <tablesp1> RESIZE (FILE '/cstp/usr/db2ad/db2ad/ <tablesp1>' 5g)

 

5.調整緩衝池大小

db2 alter bufferpool bp32k size 2g

 

6.遠端客戶端的配置網路:

1.db2ad賬戶下:

1)首先檢視/etc/services中db2各個服務的埠號

more /etc/services

 

2)

db2 update dbm cfg using SVCENAME DB2_db2ad

#這樣在服務端例項對應的埠號是60040;

 

2.db2db賬戶下:

1)

db2 catalog tcpip node <node_name> remote 172.***.***.*** server 60040

 

2) db2 list node directory  可以看到如下資訊:

 

Node Directory

Number of entries in the directory = 1

Node 1 entry:

Node name = <node_name>

Comment =

Directory entry type = LOCAL

Protocol = TCPIP

Hostname = 172.17.252.214

Service name = 60040

 

3)

db2 catalog db <dbname> at node <node_name>

 

4)db2 list db directory   可以看到如下資訊:

 

System Database Directory

Number of entries in the directory = 1

Database 1 entry:

Database alias = <dbname>

Database name = <dbname>

Node name = <node_name>

Database release level = c.00

Comment =

Directory entry type = Remote

Catalog database partition number = -1

Alternate server hostname =

Alternate server port number =

--編目系統資料庫目錄

 

授權:

在db2ad賬戶下,給db2db授權使用表空間:

db2 "grant use of tablespace <tablesp1> to user db2db"

 

7.建表

切換到db2db使用者,執行建表的sql語句:

db2 -vf tmp.sql

 

8.刪除資料庫

檢視所有資料庫:db2 list db directory

斷開資料庫連線:db2 connect reset

刪除已存在的資料庫:db2 drop database <DBname>

#如果執行失敗可能是有資料庫連線的存在,執行 db2 stop force 或者 db2 force application all,再啟動 db2 start ,刪除資料庫時,所有表空間會自動刪除。

 

9.備份、恢復資料庫

1)備份

db2 backup database <DBname> to D:\資料夾

#備份之前需檢視TESTDB使用的表空間的檔案路徑。恢復時需在機器上建立同樣路徑的資料夾,否則恢復會導致表空間掛起,恢復不成功。

 

檢視資料庫表空間

db2 connect to TESTDB; 

db2 list tablespaces show detail;

db2pd -tablespaces -db TESTDB;

 

檢視資料庫配置 

db2 get db cfg

 

2)恢復

a.先建立表空間需使用的資料夾(與原庫一樣);

b.執行恢復命令:

db2 resotre db TESTDB from "D:\BACKUP" taken at  20180720.... into TESTDB;

 

db2 rollforward  db TESTDB to end of log and stop