I installed a DB2 instance inside a Docker container. Now I'm trying to connect to it from a simple Golang app, using Windows 11 x64, but i get this error:
Error connecting to database: failed to ping database: SQLDriverConnect: {IM002} [Microsoft][Driver Manager ODBC] Datasource name not found and default driver not specified.
I tried to download a DB2 driver from IBM (https://www.ibm.com/support/pages/db2-odbc-cli-driver-download-and-installation-information), unzipped/installed it in C:/Program Files, added it to my PATH env variable, restarted my pc. After that i still don't see the driver in the "ODBC Data Source Administrator" drivers list.
This is my go code for the connection:
import (
"database/sql"
"fmt"
_ "github.com/alexbrainman/odbc"
)
func ConnectDB() (*sql.DB, error) {
connStr := "DATABASE=TEST1;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=db2inst1;PWD=db2inst1;"
db, err := sql.Open("odbc", connStr)
if err != nil {
return nil, fmt.Errorf("failed to open database: %v", err)
}
if err = db.Ping(); err != nil {
return nil, fmt.Errorf("failed to ping database: %v", err)
}
fmt.Println("Connected to DB2 database!")
return db, nil
}
And this is my main.go:
import (
"database/sql"
"go-db-check/db"
"log"
)
func main() {
database, err := db.ConnectDB()
if err != nil {
log.Fatalf("Error connecting to database: %v", err)
}
defer func(database *sql.DB) {
closeErr := database.Close()
if closeErr != nil {
log.Println("Error during connection shutdown: ", err)
}
}(database)
}
I assume you want to run go on ms-windows, and connect from golang to a Db2-database that happens to be in a docker container.
Use IBM's interface for go, which is available from https://github.com/ibmdb/go_ibm_db
It is worthwhile and essential to fully comprehend all of the details in the readme, and to get IBM's example program working.
This interface will automatically download an odbc driver called "clidriver" (it does this by default). That is a tiny footprint zero-install odbc driver for Db2 that you can operate with almost no external configuration, or alternatively you can configure via an xml file named db2dsdriver.cfg
. It has no GUI but instead offers command-line tools to configure it , to troubleshoot it, and various other functions (like testing connectivity, getting version information etc) . The clidriver tools are all documented on the main Db2 knowledge center online on many different pages accessible indirectly at https://www.ibm.com/docs/en/db2/11.5?topic=dsd-installing-data-server-driver-odbc-cli-software-windows-operating-systems
When programming in go, you choose either to include the full connection string inside your go script, or to only include a DSN (with all other connection details stored externally in db2dsdriver.cfg, which you can either configure with a text editor or alternately via special command-lines described here https://www.ibm.com/docs/en/db2/11.5?topic=commands-db2cli-db2-interactive-cli )
When starting, it is often easier to put the entire connection string inside your go script (as it means no external configuration is required), but that might not be suitable for enterprise usage.
If you need the MS-windows odbcad32 tool to show the DSNs suitable configuration ( db2cli -setup
... and db2cli registerdsn
...) may help. But that is optional.
Separately you have to ensure that your docker configuration exposes a port number on which the Db2-instance is listening, and that (mapped) port-number is opened for inbound/outbound TCP traffic on your MS-windows software firewall, and you reference the correct port number either in your connection string or in external configuration file.