• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Mister PKI

SSL Certificates * SSL Tools * Certificate Decoder

  • Buy SSL Certificates
  • Blog
  • OpenSSL
  • Keytool
  • SSL Tools
  • Donate

Perl DBI – Database connections with perl

February 7, 2022 by Mister PKI Leave a Comment

What is Perl DBI? It is the Perl Database Interface which supports database programming with perl. As any good interface does, it provides a user facing set of methods and variables to make database connections and commands with. The interface hides the underlying details of the database being used, whether it be Oracle, MySQL, PostgreSQL, etc.

Perl DBI connect

How do I make a database connection in perl?

As described above, the DBI exposes methods for which you can make a database connection using perl. The method used to make the connection is simply, you guessed it, connect. The parameters required are the data source, username, and password.


For example, to connect to a database with perl using DBI, run the following command:

$dbh = DBI→connect($data_source, $username, $password) or die $DBI::errstr;


To disconnect from the database with DBI, run the following command:

$dbh→disconnect

Perl DBI install

How do I install DBI for Perl? The installation is rather complicated, in our humble opinion. There are many depending factors including the underlying OS (Operating System) it is installed on and the environment in which it is running. For the sake of this article, we will be using a Perl docker image and using an Oracle database.

  1. Download an Oracle Instant Client from https://www.oracle.com. 
  2. Extract the Oracle client to /opt/instantclient_11_2 or the version of the instant client you are using.
  3. Download the SDK (Software Development Kit) for the Oracle client you downloaded.
  4. Extract the SDK to /opt/instantclient_11_2/sdk.
  5. Download the DBD package to install. The Oracle download is located at  https://metacpan.org/pod/DBD::Oracle.
  6. Update your OS and make sure the following dependencies are installed:
    1. libaio1
  7. Install Bundle::DBI in with the following command: perl -MCPAN -e 'install Bundle::DBI'
  8. Change to the directory of the previously extracted DBD package. In this case, /opt/DBD-Oracle-1.83
  9. Run the following commands:
    1. perl Makefile.PL -V 11.2.0
    2. make install
  10. Set the following environment variables:
    1. ORACLE_HOME=/opt/instantclient_11_2/
    2. LD_LIBRARY_PATH=/opt/instantclient_11_2/
    3. C_INCLUDE_PATH=/opt/instantclient_11_2/sdk/include/

After following the previous instructions, you should have a working installation of DBI for perl. The installation can be challenging and you may run into different issues depending on which OS you are installing on. If you run across any issues in the process, feel free to leave us a comment below and we will attempt to help troubleshoot with you.

Perl DBI example

To query a database table using DBI, you must first prepare a select statement using the exposed prepare method in the interface. For example,

 $sth = $dbh→prepare("select * from table1"); 

Where $dbh is the connection made above in a previous section. A select statement may be executed in other ways than using prepare, but this is just one way. After creating the prepared statement, you must then execute that statement with:

$sth→execute();

After execution, assuming an array of rows was returned by your select statement, run the following command to fetch the rows:

$sth→fetchrow_array();

Perl DBI module

There is a DBI module in perl for each database type you are using, including Oracle, PostgreSQL, MySQL, and others. They can be found here: https://metacpan.org/search?q=DBD%3A%3A

Dockerfile for DBI with Perl

Finally, we have made it to the part that puts everything we’ve discussed together. We previously mentioned that installing and configuring DBI can be a complicated process, so doing it repeatedly on multiple servers or having to rebuild a server at a later date may cause us to have to relearn everything about DBI.


Creating your own Dockerfile to take care of the DBI installation will save you time and effort, and is self documenting. Your final version will likely be different than this one, but this will at least get you started.

The source can be found in our GitHub repo at: https://github.com/misterpki/docker-perl-dbi

FROM perl:latest

ENV ORACLE_HOME=/opt/instantclient_11_2/ENV LD_LIBRARY_PATH=/opt/instantclient_11_2/ENV C_INCLUDE_PATH=/opt/instantclient_11_2/sdk/include/
COPY dependencies/* /opt/
WORKDIR /opt
RUN apt-get update && \    
apt-get install -y libaio1 && \    
unzip instantclient-basic-linux.x64-11.2.0.4.0.zip && \    
unzip instantclient-sdk-linux.x64-11.2.0.4.0.zip -d /opt/instantclient_11_2 && \
mv /opt/instantclient_11_2/instantclient_11_2/sdk /opt/instantclient_11_2 && \    rm -r /opt/instantclient_11_2/instantclient_11_2/ && \    
tar -xzvf DBD-Oracle-1.83.tar.gz && \    
perl -MCPAN -e 'install Bundle::DBI' && \    
cd DBD-Oracle-1.83 && \    
perl Makefile.PL -V 11.2.0 && \    
make install

Conclusion

This post demonstrated how to use DBI to make database connections, queries, and other examples including installation. The Dockerfile provides a self documented way of installing DBI in perl. Leave a comment if you have any questions and check out our other articles.

docker,  linux,  perl

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Popular Posts

PKCS12

openssl s_client

Keytool

Keytool list

ECDSA vs RSA

OpenSSL

PKCS7

Certificate Decoder

Training Courses

Top online courses in IT & Software

Cyber Security Training

Udemy - The Complete Internet Security Privacy Course icon

Buy SSL Certificates

The SSL Store

Comodo Store

Sectigo Store

RapidSSL

Recent Posts

  • pfx password
  • pkcs12
  • Sendmail vs Postfix – Mail Transfer Agent Comparison
  • Python mock datetime now
  • Python get SSL Certificate

Footer

  • Twitter
  • YouTube

Pages

  • About Mister PKI
  • Blog
  • Compare and Buy Affordable PKI Certificates
  • Contact Us
  • Full Disclosure
  • Privacy Policy
  • SSL Tools – Certificate Decoder and Certificate Checker

Copyright © 2022