ibm db2是款商用的db,默認支持的編程語言中沒有Go,也沒有介紹過,今天就演示下Go經過odbc方式鏈接ibm db2的例子。git
DB2 ODBC driver的來源有一下幾種:github
其中,db2 cli(db2 Call Level Interface)是一個和db2交互的SQL接口,基於ODBC規範和SQL / CLI國際標準。
下載macos64_odbc_cli.tar.gz,解壓到任意目錄下;sql
db2 有兩種data source配置文件,具體路徑在<install-odbc-dir>/cfg下面數據庫
db2cli.ini範例macos
; db2cli.ini data source [DB2_SAMPLE] Database=SAMPLE Protocol=TCPIP Port=50000 Hostname=my-db2-machine UID=my-OS-user PWD=my-OS-password
db2dsdriver.cfg範例編程
<parameter name="name" value="value"/> <!-- db2dsdriver.cfg data source --> <configuration> <dsncollection> <dsn alias="DB2_SAMPLE" name="SAMPLE" host="my-db2-machine" port="50000"> <parameter name="UserID" value="my-db2-user"/> <parameter name="Password" value="my-db2-password"/> </dsn> </dsncollection> </configuration>
比較簡單,不作過多介紹,cfg下也有example。注意,配置文件中存在相同的dsn時,優先加載db2cli.ini文件;
$ cd /home/myuser/db2/odbc_cli/clidriver/bin/ $ ./db2cli validate -dsn DB2_SAMPLE db2cli validate -dsn sample =============================================================================== Client information for the current copy: =============================================================================== Client Package Type : IBM Data Server Driver For ODBC and CLI Client Version (level/bit): DB2 v10.5.0.5 (special_35187/64-bit) Client Platform : Darwin Install/Instance Path : .../clidriver DB2DSDRIVER_CFG_PATH value: <not-set> db2dsdriver.cfg Path : .../clidriver/cfg/db2dsdriver.cfg DB2CLIINIPATH value : <not-set> db2cli.ini Path : .../clidriver/cfg/db2cli.ini db2diag.log Path : .../clidriver/db2dump/db2diag.log =============================================================================== db2dsdriver.cfg schema validation for the entire file: =============================================================================== Note: The validation utility could not find the configuration file db2dsdriver.cfg. The file is searched at ".../clidriver/cfg/db2dsdriver.cfg". =============================================================================== db2cli.ini validation for data source name "sample": =============================================================================== [ Keywords used for the connection ] Keyword Value --------------------------------------------------------------------------- DATABASE sample PROTOCOL TCPIP HOSTNAME 127.0.0.1 SERVICENAME 50000 UID db2inst1 PWD ********
$ echo "select count(1) from syscat.tables" |db2cli execsql -dsn sample [ -user *** -passwd *** ] IBM DATABASE 2 Interactive CLI Sample Program (C) COPYRIGHT International Business Machines Corp. 1993,1996 All Rights Reserved Licensed Materials - Property of IBM US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. > select count(1) from syscat.tables FetchAll: Columns: 1 1 643 FetchAll: 1 rows fetched.
其中,用戶名和密碼可省略,配置ini文件中;
首先要安裝unixODBC驅動管理器,該odbc管理器是開源項目,可以幫助非win平臺下的用戶輕鬆使用odbc訪問目標數據庫.bash
與其它鏈接方式不一樣的是,ODBC應用程序app一般加載連接到ODBC驅動程序管理器而不是特定的ODBC驅動程序。
ODBC驅動程序管理器是ODBC應用程序和ODBC驅動程序之間的接口和橋樑。app
在運行時,應用程序提供了一個鏈接字符串,即DSN,該鏈接字符串定義了要鏈接的ODBC數據源,並依次定義將處理鏈接的ODBC驅動程序。
unixODBC加載所請求的ODBC驅動程序並將全部ODBC API調用傳遞給目標驅動程序,也就是db2 odbc driver;
流程以下:
app ---> unixODBC ---> db2 ODBC driver編程語言
對於DB2 ODBC驅動程序來講,ODBC應用程序須要提供一個與DB2 ODBC驅動程序數據源同名的ODBC數據源。
unixODBC加載相應的數據源驅動程序(DB2 ODBC driver),並將數據源配置信息傳遞給加載的DB2 ODBC driver,DB2 ODBC驅動程序檢查它的數據源的配置文件,判斷它的名稱與它傳遞的名稱相同;fetch
brew install unixODBC
其它平臺常見unixODBC官方網站便可;
查看odbc配置文件
$ odbcinst -j unixODBC 2.3.6 DRIVERS............: /usr/local/etc/odbcinst.ini SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources USER DATA SOURCES..: .../.odbc.ini SQLULEN Size.......: 8 SQLLEN Size........: 8 SQLSETPOSIROW Size.: 8
其中,odbcinst.ini配置範例以下:
# Example odbcinst.ini driver definition for DB2 Data Server Driver for ODBC and # CLI [DB2] Description=DB2 ODBC Driver Driver=/usr/lib/libdb2.so # Replace /usr/lib with the directory where your # driver shared object is located. Fileusage=1 # Dontdlclose=1 # IBM recommend setting Dontdlclose to 1, which stops # unixODBC unloading the ODBC Driver on disconnect. # Note that in unixODBC 2.2.11 and later, the Driver # Manager default for Dontdlclose is 1.
上面已經提到過,爲了讓應用程序經過odbc方式鏈接db,unixODBC管理器須要知道要加載的驅動在哪裏,同時須要指定odbc驅動的鏈接參數,如:ip,user等;
要應用這些參數,就要配置相關的DSN 參數鏈接;
~/.odbc.ini配置以下:
[DB2_SAMPLE] Driver=DB2
故此,當用戶程序須要鏈接DB2_SAMPLE時,
- 首先,unixODBC會加載DB2 ODBC driver驅動, - 以後,DB2 ODBC driver會在db2cli.ini/db2dsdriver.cfg找相同名字的dns的相關配置; - 若用戶程序提供的用戶名和密碼,配置文件中的用戶名和密碼會忽略。
以下isql是unixODBC自帶的ODBC程序,經過它能夠驗證dsn的配置是否正確,鏈接是否ok;
$ isql -v DB2_SAMPLE username password 錯誤1:說明~/.odbc.ini配置文件中的Data Source Name(DSN)沒有找到; [IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified [ISQL]ERROR: Could not SQLConnect $ isql -v DB2_SAMPLE username password 錯誤2:說明DB2 ODBC driver配置文件沒有找到匹配的DSN名字的配置信息 [ ][unixODBC][IBM][CLI Driver] SQL1531N The connection failed because the name specified with the DSN connection string keyword could not be found in either the db2dsdriver.cfg configuration file or the db2cli.ini configuration file. Data source name specified in the connection string: "DB2_SAMPLE". $ isql -v DB2_SAMPLE +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL>
PS:對於win下的go,抽空再嘗試。