# IBM DB2 12 學習之旅2: 建立Instance與Sample資料庫 ### 前言: - 安裝請看 上一篇 [IBM DB2 12 學習之旅1: 安裝](https://hackmd.io/@Gx4_ZHfXRFWRxHYxbAANvA/B1bhtTfegg) - 本篇將介紹安裝後如何建立Instance與SAMPLE資料庫 ### 基礎小常識: 1. 什麼是DB2 Instance? - DB2 instance 是 IBM DB2 資料庫管理系統中的一個核心概念,指的是一個獨立的執行環境,用於管理一組資料庫。它是 DB2 軟體安裝後需要建立的一個邏輯層,負責處理資料庫的運作、管理和存取。 - 一個 instance 可以管理多個資料庫,但每個資料庫只能屬於一個 instance。 - instance 控制 CPU、記憶體和磁碟 I/O 等資源的分配。 - instance 定義了用戶和應用程式如何連接到其下的資料庫。 - 在單台伺服器上,可以運行多個 DB2 instance,每個 instance 獨立運作,互不干擾。例如,一個 instance 用於開發環境,另一個用於生產環境。 - instance 的名稱在安裝或建立時指定(如 db2inst1),並與特定的使用者帳戶關聯。 2. 常用的Instance指令 | 說明 | 指令 | | -------- | -------- | | 建立 instance | db2icrt | | 啟動/停止 instance | db2start/db2stop | | 查看 instance | db2ilist | | 切換 instance | export DB2INSTANCE=instance_name | 3. 建立Instance要注意以下幾點 - 不同 instance 之間的資料庫是隔離的,無法直接跨 instance 存取。 - instance 的配置(如緩衝池大小、並行處理等)會影響其下所有資料庫的性能。 - 在多 instance 環境中,需確保資源分配合理,避免競爭。 ### 建立instance: >``` shell! >cd /opt/ibm/db2/V12.1/instance >./db2icrt -u db2fenc1 db2inst1 說明: db2icrt -u [FencedID] [InstName] - FencedID: 代表執行隔離的使用者定義函數 (UDF) 及隔離的儲存程序的使用者名稱。如果在用戶端上建立實例,則不需要此旗標。指定您建立的隔離使用者名稱。 - InstName: 代表實例的名稱。實例的名稱必須與擁有實例的使用者名稱相同。指定您建立之擁有實例的使用者名稱。將會在擁有實例的使用者起始目錄中建立實例。 >``` shell! >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.44593". >DBI1070I Program db2icrt completed successfully. ### 切換至instance: db2inst1 >``` shell! >su - db2inst1 查看建立好的instance >``` shell! >db2level > >DB21085I This instance or install (instance name, where applicable: "db2inst1") uses "64" bits and DB2 code release "SQL12011" with level identifier "02020110". >Informational tokens are "DB2 v12.1.1.0", "s2412161033", "DYN2412161033AMD64", and Fix Pack "0". >Product is installed at "/opt/ibm/db2/V12.1". 啟動db2 instance >``` shell! >db2start >DB2START processing was successful. >SQL1063N DB2START processing was successful. ### 建立SAMPLE資料庫 >``` shell! >db2sampl > > Creating 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: >``` shell! >db2 connect to sample > > Database Connection Information > > Database server = DB2/LINUXX8664 12.1.1.0 > SQL authorization ID = DB2INST1 > Local database alias = SAMPLE 檢查db2使用的port: >``` shell! >cat /etc/services >... >DB2_db2inst1 20016/tcp >DB2_db2inst1_1 20017/tcp >DB2_db2inst1_2 20018/tcp >DB2_db2inst1_3 20019/tcp >DB2_db2inst1_4 20020/tcp >DB2_db2inst1_END 20021/tcp >db2c_db2inst1 25010/tcp <---這個就是port 防火牆設定開通port(用root): >``` shell! >firewall-cmd --zone=public --add-port=25010/tcp --permanent >firewall-cmd --reload 設定開機啟動db2 services(用root): >``` shell! >/opt/ibm/db2/V12.1/instance/db2iauto -on db2inst1