This documentation describes the integration of MindsDB with IBM Db2, the cloud-native database built to power low-latency transactions, real-time analytics and AI applications at scale. The integration allows MindsDB to access data stored in the IBM Db2 database and enhance it with AI capabilities.

Prerequisites

Before proceeding, ensure the following prerequisites are met:

  1. Install MindsDB locally via Docker or Docker Desktop.
  2. To connect IBM Db2 to MindsDB, install the required dependencies following this instruction.

Connection

Establish a connection to your IBM Db2 database from MindsDB by executing the following SQL command:

CREATE DATABASE db2_datasource
WITH
    engine = 'db2',
    parameters = {
        "host": "127.0.0.1",
        "user": "db2inst1",
        "password": "password",
        "database": "example_db"
    };

Required connection parameters include the following:

  • host: The hostname, IP address, or URL of the IBM Db2 database.
  • user: The username for the IBM Db2 database.
  • password: The password for the IBM Db2 database.
  • database: The name of the IBM Db2 database to connect to.

Optional connection parameters include the following:

  • port: The port number for connecting to the IBM Db2 database. Default is 50000.
  • schema: The database schema to use within the IBM Db2 database.

Usage

Retrieve data from a specified table by providing the integration name, schema, and table name:

SELECT *
FROM db2_datasource.schema_name.table_name
LIMIT 10;

Run IBM Db2 native queries directly on the connected database:

SELECT * FROM db2_datasource (

    --Native Query Goes Here
    WITH
    DINFO (DEPTNO, AVGSALARY, EMPCOUNT) AS
        (SELECT OTHERS.WORKDEPT, AVG(OTHERS.SALARY), COUNT(*)
            FROM EMPLOYEE OTHERS
            GROUP BY OTHERS.WORKDEPT
        ),
    DINFOMAX AS
        (SELECT MAX(AVGSALARY) AS AVGMAX FROM DINFO)
    SELECT THIS_EMP.EMPNO, THIS_EMP.SALARY,
        DINFO.AVGSALARY, DINFO.EMPCOUNT, DINFOMAX.AVGMAX
    FROM EMPLOYEE THIS_EMP, DINFO, DINFOMAX
    WHERE THIS_EMP.JOB = 'SALESREP'
    AND THIS_EMP.WORKDEPT = DINFO.DEPTNO

);

The above examples utilize db2_datasource as the datasource name, which is defined in the CREATE DATABASE command.

Troubleshooting Guide

Database Connection Error

  • Symptoms: Failure to connect MindsDB with the IBM Db2 database.
  • Checklist:
    1. Make sure the IBM Db2 database is active.
    2. Confirm that host, user, password and database are correct. Try a direct connection using a client like DBeaver.
    3. Ensure a stable network between MindsDB and the IBM Db2 database.

SQL statement cannot be parsed by mindsdb_sql

  • Symptoms: SQL queries failing or not recognizing table names containing spaces or special characters.
  • Checklist:
    1. Ensure table names with spaces or special characters are enclosed in backticks.
    2. Examples:
      • Incorrect: SELECT * FROM integration.travel-data
      • Incorrect: SELECT * FROM integration.‘travel-data’
      • Correct: SELECT * FROM integration.`travel-data`

This guide of common connection Db2 connection issues provided by IBM might also be helpful.