Search code examples
linuxdatabasedb2database-administration

Doubts to create connection string with DB2


I would like to know if anyone has a connection string model with the DB2 database?

The database is located on a Linux Centos 7 server.

I tried something like this:

db2 Database=SI;Hostname=VMCENTDB2;Protocol=TCPIP;Port=3700;Uid=db2inst1;Pwd=password;

But it didn't work and the following message returned:

DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL1024N A database connection does not exist. SQLSTATE=08003

Thanks in advance.


Solution

  • Your command is not valid for the db2 command, because it does not accept connection strings. Other tools do.

    If you want to connect to a Db2-database, from the shell command line you have different options provided by different tools:

    • use the db2 command (requires previous catalog actions to be completed)
    • use the CLPplus command (accepts a connection string)
    • use an odbc iterface like isql
    • use the db2cli tool (more suitable for experts and also requires pre-configuration of db2dsdriver.cfg and/or db2cli.ini).

    Different options are suitable for different purposes, and different skill sets etc.

    You can use the Db2 command line processor = the db2command, via use of db2 connect to $DATABASENAME user $USER using $PASSWD (you provide your own values for the variables). This does not accept connection strings. But before that connect command can succeed from remote, you must catalog the node on which the database lives (using the db2 catalog tcpip node .... remote ... server ... command), and then catalog the database on that node using the db2 catalog database $DBNAME as $DBALIAS at node $NODENAME command. Refer to the online Db2 Knowledge centre for details of these commands. This is the oldest form of shell interface to Db2 from MS-Windows, Linux or Unix and is very script friendly for cmd.exe or bash or ksh etc. But many people do not like the catalog actions that are pre-requisites for remote working, although are easily scriptable.

    Note that if you ssh to the centos server and get a shell, then you do not need to catalog local databases, you can use connect to them with the db2 command as long as your login shell dots in the correct db2profile file.

    You cannot use a connection string for the Db2-CLP (command line processor), but you can use a connection string in the java-based CLPPlus tool and thereby avoid the need to catalog. CLPlus is useful for people who are familiar with Oracle SQLPlus syntax and does not need any catalog actions.

    The CLPPlus command comes with the Db2-server, and with the Db2 runtime client and with the Db2 data server client, but it does not come with the tiny footprint Db2 clidriver. Refer to the documentation for usage details.