The 'G' Moment: A Simple Guide to Connecting GBase 8a via Perl DBD::ODBC
DBD::ODBC is a database driver for Perl that allows Perl programs to interact with various databases through the ODBC (Open Database Connectivity) interface. ODBC is a standard database access interface that enables applications to connect to multiple data sources through a common set of APIs, without writing dedicated code for each database. This article describes how to configure the GBase 8a ODBC driver on a Linux system and connect to GBase 8a using the Perl DBD::ODBC package.
Environment Details
This document describes setting up the Perl development environment and ODBC driver on Linux. The versions of various installation packages related to development and debugging are listed below. If your environment or driver package differs, adjust accordingly.
Operating System:
Perl Version:
CPAN offline package version: CPAN-2.38.tar.gz
GBase 8a ODBC driver package version: GBaseODBC-8.3.81.53-build53.17-redhat7-x86_64.rpm
ODBC Driver Environment Setup
(1) Install unixODBC and unixODBC-devel
The Perl DBD::ODBC package depends on the operating system's ODBC driver and the database ODBC driver. Therefore, install the operating system's ODBC driver package first. This article uses yum repository installation, assuming the yum source for the Perl development environment is already configured.
(2) Install the GBase 8a ODBC Driver
The GBase 8a ODBC driver version used here is GBaseODBC-8.3.81.53-build53.17-redhat7-x86_64.rpm. Upload the driver to the Perl development server and install it using the rpm command:
After installation, the following information is automatically added to /etc/odbcinst.ini:
Configure ODBC DSN:
Verify the ODBC configuration:
Confirm that the system ODBC DSN configuration file is /etc/odbc.ini
Edit /etc/odbc.ini to add the GBase 8a ODBC connection information:
[gbase8a] -- System DSN name
Driver = GBase 8a ODBC 8.3 Driver -- Driver name. Note: This must match the driver name in /etc/odbcinst.ini for GBase 8a; otherwise, an error will occur when connecting through ODBC.
DATABASE = test -- Database name
DESCRIPTION = GBase 8a ODBC 8.3 Driver -- Driver description. It is recommended to keep this the same as the driver name in /etc/odbcinst.ini for GBase 8a.
SERVER = 192.168.56.162 -- IP address of the GBase 8a management node
PORT = 5258 -- Service port of the GBase 8a management node
UID = root -- User for connecting to the GBase 8a database
PASSWORD = Admin2O11O531 -- Password
Note: For more connection parameters, refer to the "GBase 8a Programmer's Manual: ODBC Section", which includes load balancing and high availability connection configurations.
(3) Test the GBase 8a ODBC Driver Installation
Run isql gbase8a -v to connect to GBase 8a using isql and verify the connection:
Perl Development Environment Setup
(1) Install CPAN
CPAN (Comprehensive Perl Archive Network) is the official module repository for the Perl programming language, offering over 200,000 open-source modules to rapidly extend Perl's capabilities. It integrates tools for module distribution, download, installation, and management, serving as the core ecosystem that Perl developers rely on.
This document uses CPAN to install the DBD::ODBC module, resolving module dependencies. Therefore, we will first install CPAN.
Install CPAN by downloading the source package and performing an offline compilation. First, visit https://metacpan.org/pod/CPAN, select the CPAN package, click Download, and download CPAN-2.38.tar.gz
Upload the package to the Perl development server and run the following commands:
Compile and install CPAN:
(2) Install DBD::ODBC
To install DBD::ODBC using CPAN, the development server needs internet access. CPAN will automatically connect to the repository, configure the environment, and download dependencies.
Writing Perl Sample Code to Connect to GBase 8a
Prepare a test table in the GBase 8a database and log in:
Write the Perl sample test code:
Execute the sample code: