How do I connect Oracle to DB2?
If your Oracle database is running on Linux or UNIX, you can use Oracle's Database Gateway for ODBC (DG4ODBC) and the DB2 ODBC driver to connect Oracle to IBM DB2.
DG4ODBC is included with Oracle 11g and later and is compatible with Oracle 10g and later.
DG4ODBC interacts with Heterogeneous Services (an Oracle database component) to allow Oracle client applications to access non-Oracle databases. The non-Oracle data is transparently integrated, and so Oracle client applications are not aware that the data is stored in a remote database from another vendor.
The following instructions show you how to connect Oracle with DB2. For further information about DG4ODBC, refer to our DG4ODBC tutorial.
- Check whether your version of DG4ODBC is 32-bit or 64-bit:
cd $ORACLE_HOME/bin file dg4odbc
If the
filecommands output containsELF 64-bit LSB executable, or something similar, DG4ODBC is 64-bit, and you need to download the 64-bit DB2 ODBC driver for your platform. Otherwise, you need to use the 32-bit DB2 ODBC driver for your platform. - Install and license the DB2 ODBC driver on the machine where DG4ODBC is installed.
For installation instructions, refer to the DB2 ODBC driver documentation. Refer to the documentation to find out which environment variables you need to set (
LD_LIBRARY_PATH,LIBPATH,LD_RUN_PATH, orSHLIB_PATHdepending on the platform and linker). - Test that you can use the DB2 ODBC driver to access your DB2 database. To do this, create an ODBC data source in your
odbc.inifile (which is usually located in/etc). For example:[DB2] Driver = Easysoft ODBC-DB2 Server = machine_name_or_ip_address_of_db2_instance Database = my_db2_database User = my_os_user Password = my_os_password ConvWToUtf = yes
If your DB2 instance is not listening on the default port (50000), you also need to specify the port number. For example:
Port = 50001
Use
isqlto test your new ODBC data source. For example:cd /usr/local/easysoft/unixODBC/bin ./isql.sh -v DB2
At the prompt, enter
helpto display a list of tables. To exit, press Return in an empty prompt line. - Create a DG4ODBC init file.
The
initsid.orafile references the ODBC data source that you added to/etc/odbc.ini. For this example, we will call our init fileinitdb2.ora. This file needs to be written to your$ORACLE_HOME/hs/admindirectory as the Oracle user. For example:cd $ORACLE_HOME/hs/admin cp initdg4odbc.ora initdb2.ora
- Ensure these parameters and values are present in your init file:
HS_FDS_CONNECT_INFO = DB2 HS_FDS_SHAREABLE_NAME = /usr/local/easysoft/unixODBC/lib/libodbc.so HS_LANGUAGE = AMERICAN_AMERICA.US7ASCII #HS_FDS_TRACE_LEVEL = 4 # # ODBC specific environment variables # set ODBCINI=/etc/odbc.ini
The following table describes these parameters:
Parameter Description HS_FDS_CONNECT_INFOThe name of the ODBC data source that you added to /etc/odbc.ini.HS_FDS_SHAREABLE_NAMEThe path to the unixODBC Driver Manager library on your machine. HS_LANGUAGEThis is required to prevent DG4ODBC using the UTF-8 character encoding. HS_FDS_TRACE_LEVELUn-comment this parameter if you need to obtain an initsidlog file. By default, log files are written to your$ORACLE_HOME/hs/logfolder. A new log file is created for each connection. so remember to turn this off by commenting the line out when you no longer require logging.Note On AIX, you need to extract and then rename the unixODBC Driver Manager shared object. To do this:
-
cdto the directory where unixODBC's libraries are installed. For example:cd /usr/local/easysoft/unixODBC/lib
- Extract the shared object from the archive:
ar -X32_64 -xv libodbc.a
- Rename the shared object to
libodbc.so:mv libodbc.so.1 libodbc.so
Then add this entry to your Oracle user's
.profilefile:LIBPATH=$LIBPATH:/usr/local/easysoft/lib:/usr/local/easysoft/unixODBC/lib export LIBPATH
The library path needs to be set before the Oracle listener is started.
Note On HP-UX machines, shared objects have the extension
.sl. Yourinitsidentry needs include:HS_FDS_SHAREABLE_NAME = /usr/local/easysoft/unixODBC/lib/libodbc.sl
-
- For the Oracle Listener to listen for DG4ODBC, information about DG4ODBC must be added to the Oracle Listener configuration file,
listener.ora. By defaultlistener.orais located in$ORACLE_HOME/network/admin.Add an entry to
listener.orathat creates aSID_NAMEfor DG4ODBC. For example:SID_LIST_LISTENER = (SID_LIST = (SID_DESC= (SID_NAME=db2) (ORACLE_HOME=oracle_home_directory) (PROGRAM=dg4odbc) (ENVS=LD_LIBRARY_PATH = /usr/local/easysoft/unixODBC/lib: /usr/local/easysoft/lib) ) )Replace
oracle_home_directorywith the value of$ORACLE_HOME. For example,/u01/app/oracle/product/11.2.0/dbhome_1/.The
SID_NAMEparameter value is theSID_NAMEfor your init file. In our test, we called the init fileinitdb2.ora, so ourSID_NAMEis db2.The
ENVSparameter value is the environment variables that you need to set for the DB2 ODBC driver. On AIX, you need to replaceLD_LIBRARY_PATHwithLIBPATH.On HP-UX you need to replace
LD_LIBRARY_PATHwithSHLIB_PATH.Some versions of DG4ODBC need to locate additional Oracle libraries when run. If this is the case, you need to include the
$ORACLE_HOME/libfolder in theENVSparameter value.Save the file when you have entered this information.
- Add a DG4ODBC entry to
$ORACLE_HOME/network/admin/tnsnames.orathat specifies theSID_NAMEcreated in the previous step. For example:db2con= (DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=oracle_host)(PORT=1521)) (CONNECT_DATA= (SID=db2)) (HS=OK) )
The
tnsnames.oraentry can be given the same name asSID_NAMEvalue (and so in our example,db2concould bedb2.) TheSIDparameter value has to be the same as theSID_NAMEvalue.Save the file when you've entered this information.
- Start (or restart) the Oracle Listener:
cd $ORACLE_HOME/bin ./lsnrctl stop ./lsnrctl start
When you start the listener, you may get information messages similar to:
Service "db2con" has 1 instance(s). Instance "db2con", status UNKNOWN, has 1 handler(s) for this service... The command completed successfully
The status
UNKNOWNis returned by DG4ODBC, as the service is not created until it is used.You can also run
lsnrctland enterSERVICESto display service status information. Don't use thereloadoption within thelnsrctlprogram as this does not always reread your configuration files. - After adding the
tnsnames.oraalias and restarting the listener, usetnsping aliasto check that you can connect to the new service. Iftnspingsucceeds, you get a message similar to:$ tnsping db2con TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on 06-MAY-2008 17:19:20 Copyright (c) 1997, 2007, Oracle. All rights reserved. Used parameter files: /home/oracle/2/product/11.2.0/db_1/network/admin/sqlnet.ora Used TNSNAMES adapter to resolve the alias Attempting to contact (DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=myhost)(PORT=1521)) (CONNECT_DATA= (SID=db2)) (HS=OK)) OK (0 msec)If the
tnspingfails, please check the three Oracle configuration files for errors. - Connect to your Oracle database in SQL*Plus.
- In SQL*Plus, create a database link for the target DB2 database. For example:
CREATE PUBLIC DATABASE LINK db2 CONNECT TO "dbuser" IDENTIFIED BY "dbpassword" using 'db2con';Replace
dbuseranddbpasswordwith a valid user name and password for the target DB2 database.