ibmdb / go_ibm_db

GoLang Driver for Db2 family of database servers
BSD 3-Clause "New" or "Revised" License
107 stars 37 forks source link

Error Executing Go Program: SQLDriverConnect: {HY000} [IBM][CLI Driver] SQL1042C An unexpected system error occurred. #243

Open mathiasyeremiaaryadi opened 1 month ago

mathiasyeremiaaryadi commented 1 month ago

Hi, I am running Go program with the library on my local Windows and I have followed all the setup instruction with detail below:

Here is the go code for the connection:

package main

import (
    "database/sql"
    "fmt"

    _ "github.com/ibmdb/go_ibm_db"
    "github.com/spf13/viper"
)

func DB2() (*sql.DB, error) {
    dataSourceName := fmt.Sprintf(
        "Hostname=%s;Database=%s;Port=%s;UID=%s;PWD=%s",
        viper.GetString("DB2_HOSTNAME"),
        viper.GetString("DB2_DATABASE"),
        viper.GetString("DB2_PORT"),
        viper.GetString("DB2_USERNAME"),
        viper.GetString("DB2_PASSWORD"),
    )

    db2Connection, err := sql.Open("go_ibm_db", dataSourceName)
    if err != nil {
        return db2Connection, err
    }

    err = db2Connection.Ping()
    if err != nil {
        return db2Connection, err
    }

    return db2Connection, nil
}

$ go env

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\mathi\AppData\Local\go-build
set GOENV=C:\Users\mathi\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\mathi\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\mathi\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.7
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\2024\MS MVP Delta 2 Robuts H-1 Project BRINETS CARDLINK\ms-mdm-automation-update-tp-sl\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\mathi\AppData\Local\Temp\go-build62941853=/tmp/go-build -gno-record-gcc-switches  

echo %IBM_DB_HOME% C:\Users\mathi\go\pkg\mod\github.com\ibmdb\clidriver

echo %PATH%

C:\geckodriver;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\nodejs\;C:\ProgramData\chocolatey\bin;C:\Program Files\Java\jdk-1.8\bin;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files\Git\cmd;C:\Program Files\Git\usr\bin;C:\Program Files\Go\bin;C:\Program Files\PuTTY\;C:\TDM-GCC-64\bin;C:\Program Files (x86)\Microsoft SQL Server\160\Tools\Binn\;C:\Program Files\Microsoft SQL Server\160\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\Microsoft SQL Server\160\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\160\DTS\Binn\;C:\Program Files\Azure Data Studio\bin;C:\sqlite;C:\Program Files\Git LFS;C:\Users\mathi\AppData\Local\Programs\Python\Launcher\;C:\Users\mathi\AppData\Local\Microsoft\WindowsApps;;C:\Users\mathi\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\mathi\AppData\Roaming\npm;C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\bin;C:\Users\mathi\go\bin;C:\Program Files\Azure Data Studio\bin

go run .

INFO[2024-07-10 21:44:57.856] Initialize configuration environment: success 
 ATA[2024-07-10 21:44:57.889] Error:  SQLDriverConnect: {HY000} [IBM][CLI Driver] SQL1042C  An unexpected system error occurred.  SQLSTATE=58004
exit status 1
vmathur12 commented 1 month ago

@mathiasyeremiaaryadi
Please share the output of "db2level" command. Also check the connection to database server and access to the server. If you are using environment variables then add "viper.AutomaticEnv()" in your go program. Thanks

mathiasyeremiaaryadi commented 1 month ago

@vmathur12 Hi, thank you for the feedback, here is the db2level command that you asked:

DB21085I  This instance or install (instance name, where applicable: "*") uses
"64" bits and DB2 code release "SQL11054" with level identifier "0605010F".
Informational tokens are "DB2 v11.5.4000.1449", "s2006161200",
"DYN2006161200WIN64", and Fix Pack "0".
Product is installed at "C:\Users\mathi\go\pkg\mod\github.com\ibmdb\clidriver"
with DB2 Copy Name "IBM Data Server Driver For ODBC and CLI".

And I did use the viper.AutomaticEnv() as you suggested. Its finally worked on the external CMD windows, but the error still persist if I run the program in the VS Code CMD.

vmathur12 commented 1 month ago

@mathiasyeremiaaryadi Reading the environment variables from JSON or YAML config files. The following is the sample of JSON config file.

config.json file:

{ "DB2_DATABASE" : "database", "DB2_USERNAME" : "usr", "DB2_PASSWORD" : "password", "DB2_HOSTNAME" : "hostname", "DB2_PORT" : "10000", "DB2_PROTOCOL" : "TCPIP" }

go file:

func DB2() (*sql.DB, error) {

viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AddConfigPath(".")
viper.AutomaticEnv()
    viper.ReadInConfig()

dataSourceName := fmt.Sprintf(
    "Hostname=%s;Database=%s;Port=%s;UID=%s;PWD=%s",
    viper.GetString("DB2_HOSTNAME"),
    viper.GetString("DB2_DATABASE"),
    viper.GetString("DB2_PORT"),
    viper.GetString("DB2_USERNAME"),
    viper.GetString("DB2_PASSWORD"),
)

db2Connection, err := sql.Open("go_ibm_db", dataSourceName)
if err != nil {
    return db2Connection, err
}

err = db2Connection.Ping()
if err != nil {
    return db2Connection, err
}
return db2Connection, nil

}