如何使用 CLI 连接到远程 DB2 LUW 数据库?

How to connect to a remote DB2 LUW database with CLI?

我正在 Linux 上编写一个 C 程序,它使用 CLI 驱动程序 11.5 连接到 DB2 LUW 数据库。

我通过以下 SQLDriverConnect 调用成功连接到端口 50000 上的本地数据库:

SQLDriverConnect(
    hdbc,                /* a valid connection handle */
    NULL,                /* no Window handle */
    (SQLCHAR *)"DATABASE=testdb;UID=username;PWD=password",
    SQL_NTS,             /* the connect string is NUL-terminated */
    NULL,                /* don't care about the completed connect string... */
    0,
    NULL,                /* ...or its length */
    SQL_DRIVER_NOPROMPT  /* don't prompt me */
);

但是如果我指定主机名和 TCP 端口,如我链接到的文档页面中所述:

"DATABASE=testdb:localhost:50000;UID=username;PWD=password"

我收到错误消息

[IBM][CLI Driver] SQL30061N  The database alias or database name "TESTDB:LOCALHOST:50000" was not found at the remote node.  SQLSTATE=08004

有人知道如何连接到远程 DB2 数据库吗?

db2level 给我

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.4.0", "s2006161200", "DYN2006161200AMD64", 
and Fix Pack "0".
Product is installed at "/usr/db2".

查看 .\samples\cli\dbconn.c 示例源代码。

它有3种不同的连接方式,涵盖了C部分...

但您的问题似乎是您的连接字符串格式不正确

DATABASE=XXX:localhost:5000 不是有效的 db2 cli 连接字符串。根据错误消息,它将 database= 之后的所有内容解释为数据库名称。 TESTDB:LOCALHOST:50000.

根据 documentation:

To make a connection to the database in a CLI application, you can perform one of the listed actions:
* Call SQLDriverConnect with a connection string that contains: 
Database=db1; Protocol=tcpip; Hostname=11.22.33.44; Servicename=56789;