SlideShare a Scribd company logo
1 of 5
CONNECT TO SQL USING PERL FROM LINUX
Introduction:
I recently came across a situation where I need to connect SQL using Perl from Linux
box. As I have not done that I was like, ā€˜Is it possible to connect MSSQL from Linux?ā€ but after
googling I came to know Microsoft has a driver for Linux which can be used to connect via
Linux. I had tried it and it worked like a charm. I was able to connect to SQL server using
Microsoft binary. The real problem is that it cannot be used as Perl module to extend the
usability via scripting.
Again I started my search and reading multiple articles, websites and I was able to
successfully connect to SQL using Perl from Linux. I will briefly describe how to achieve it and
where to get the packages in this article.
MSSQL Driver for Linux:
It was always tough to connect to MSSQL from Linux but surprising Microsoft has
provided an ODBC driver which can be used to have easy access to SQL server via cmd. The
details can be gathered from the below screenshot.
Packages needs to be installed:
o Unix-ODBC
o Unix-ODBC-devel
o FreeTDS
o DBI::Sybase http://search.cpan.org/~mewp/DBD-Sybase-1.15/
Unix-ODBC:
This can be installed via the default Linux distro package manager.
RHEL:
yum install unixODBC
It resolves and auto install unixODBC-Devel and freeTDS as dependencies. The distro which
does not have FreeTDS and UNIX-ODBC, please download the latest stable packages form the
respective website and you have to install it manually using below steps.
Building FreeTDS:
$ ./configure --prefix=/usr/local
$ make
$ su root
Password:
$ make install
Source: http://www.freetds.org/userguide/config.htm
Configuration:
Once the installation gets completed. The main part is to configure the freetds and
odbc.ini, odbcinst.ini files. This plays a crucial part in connecting to SQL server.
FreeTDS:
Edit the freetds.conf from the location you had installed earlier. If you have installed
via package manager it can be found at /etc/freetds.conf.
The below is the template for adding DB details.
[DBName]
host = <serverURL where DB is hosted>
port = 1433
tds version = 8.0
For some unknown reason, I had to change the version of TDS to 8.0 from 7.0 to get it
connect to SQL server 2012.
Odbc.ini
This file passes the details such as DataSource and other driver details to establish the
connection.
The below is the template for editing odbc.ini.
[ODBC Data Sources]
DBName = MSSQL Server
[DBName]
Driver = freetds
Description = Microsoft SQL
Trace = No
Server = <serverURL where DB is hosted>
port = 1433
Database = DBName
TDS_Version = 7.1
Odbcinst.ini
This file passes which DB we are connecting and the ODBC driver details.
The below is the template for editing odbcinst.ini.
[freetds]
Description = MS SQL database access with FreeTDS
Driver = /usr/lib64/libtdsodbc.so.0
Setup = /usr/lib64/libtdsodbc.so.0
UsageCount = 1
Testing:
Once everything is in place, itā€™s time for us to test the freetds configuration. Itā€™s
important that freetds connection should work to move further.
Execute the below command to verify whether freetds has configured properly.
Execute the below command to test the connection gets established or not.
TDSERVER=7.0tsql -H <Server name> -U <username> -p <port> -D <DBName to Connect>
If itā€™s successful you can see the output as below.
Installing Perl Module:
As we had completed most of the integral configuration, itā€™s time to install Perl
module which we can use later to connect to SQL DB via scripting. There are few ways to use
Perl to connect to a SQL Server using FreeTDS. All of them can be installed via CPAN but I
have tried installing configuring the module.
o DBD::Sybase
o DBD::ODBC
o Sybperl
DBD::Sybase:
Despite the name it works for any Sybase or Microsoft SQL Server. DBD::Sybase uses
the CT-Library API and works well.
Building DBD::Sybase
Download the latest Sybase module from cpan.org and follow the below steps.
export SYBASE=/usr/local/freetds
perl Makefile.PL
Use 'CHAINED' mode by default (Y/N) [Y]: Y (if this Y, no need to use $dbh-
>commit() in the script. Autocommit does the job).
Make
make install (as root)
Connect to Server using DBD::Sybase
#!/usr/bin/perl
#
use DBI;
my $user = "<username>";
my $passwd = ' <password';
my $server = "<server url";
my $dbh = DBI->connect("DBI:Sybase:server=$server", $user, $passwd,
{PrintError => 0});
unless ($dbh) {
die "ERROR: Failed to connectnError Message: $DBI::errstr";
}else {
print "n";
print "Successful Connection.";
print "nn";
}
$dbh->disconnect;

More Related Content

What's hot

Oracle autovue
Oracle autovueOracle autovue
Oracle autovueOsama Mustafa
Ā 
Getting Started with CloudScript
Getting Started with CloudScriptGetting Started with CloudScript
Getting Started with CloudScriptNephoScale
Ā 
Upgrade EBS DB from 11g to 12c.
Upgrade EBS DB from 11g to 12c.Upgrade EBS DB from 11g to 12c.
Upgrade EBS DB from 11g to 12c.Osama Mustafa
Ā 
Installing & Setting Up WordPress
Installing & Setting Up WordPressInstalling & Setting Up WordPress
Installing & Setting Up WordPressGravitational FX
Ā 
Squid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
Squid for Load-Balancing & Cache-Proxy ~ A techXpress GuideSquid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
Squid for Load-Balancing & Cache-Proxy ~ A techXpress GuideAbhishek Kumar
Ā 
Mule esb add logger to existing flow
Mule esb add logger to existing flowMule esb add logger to existing flow
Mule esb add logger to existing flowGermano Barba
Ā 
Mule esb - How to make CRUD operations to a MySql Database in 5 minutes
Mule esb - How to make CRUD operations to a MySql Database in 5 minutesMule esb - How to make CRUD operations to a MySql Database in 5 minutes
Mule esb - How to make CRUD operations to a MySql Database in 5 minutesGennaro Spagnoli
Ā 
Idempotent filter in Mule
Idempotent filter in MuleIdempotent filter in Mule
Idempotent filter in MuleF K
Ā 
Deploy agent in em12c
Deploy agent in em12cDeploy agent in em12c
Deploy agent in em12cOsama Mustafa
Ā 
Activemq installation and master slave setup using shared broker data
Activemq installation and master slave setup using shared broker dataActivemq installation and master slave setup using shared broker data
Activemq installation and master slave setup using shared broker dataRamakrishna Narkedamilli
Ā 
APACHE WEB SERVER FOR LINUX
APACHE WEB SERVER FOR LINUXAPACHE WEB SERVER FOR LINUX
APACHE WEB SERVER FOR LINUXwebhostingguy
Ā 
Oracle virtual server-2-t0-3-upgrade
Oracle virtual server-2-t0-3-upgradeOracle virtual server-2-t0-3-upgrade
Oracle virtual server-2-t0-3-upgradeRavi Kumar Lanke
Ā 
Mule security - ldap for spring security
Mule  security - ldap for spring securityMule  security - ldap for spring security
Mule security - ldap for spring securityD.Rajesh Kumar
Ā 
Install oracle siebel on windows 2008 r2
Install oracle siebel on windows 2008 r2Install oracle siebel on windows 2008 r2
Install oracle siebel on windows 2008 r2Osama Mustafa
Ā 
Scatter and gather in mule
Scatter and gather in muleScatter and gather in mule
Scatter and gather in muleRajkattamuri
Ā 
Refresh development from productions
Refresh development from productionsRefresh development from productions
Refresh development from productionsOsama Mustafa
Ā 

What's hot (18)

Oracle autovue
Oracle autovueOracle autovue
Oracle autovue
Ā 
Getting Started with CloudScript
Getting Started with CloudScriptGetting Started with CloudScript
Getting Started with CloudScript
Ā 
Upgrade EBS DB from 11g to 12c.
Upgrade EBS DB from 11g to 12c.Upgrade EBS DB from 11g to 12c.
Upgrade EBS DB from 11g to 12c.
Ā 
Installing & Setting Up WordPress
Installing & Setting Up WordPressInstalling & Setting Up WordPress
Installing & Setting Up WordPress
Ā 
Squid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
Squid for Load-Balancing & Cache-Proxy ~ A techXpress GuideSquid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
Squid for Load-Balancing & Cache-Proxy ~ A techXpress Guide
Ā 
Mule esb add logger to existing flow
Mule esb add logger to existing flowMule esb add logger to existing flow
Mule esb add logger to existing flow
Ā 
Mule esb - How to make CRUD operations to a MySql Database in 5 minutes
Mule esb - How to make CRUD operations to a MySql Database in 5 minutesMule esb - How to make CRUD operations to a MySql Database in 5 minutes
Mule esb - How to make CRUD operations to a MySql Database in 5 minutes
Ā 
Idempotent filter in Mule
Idempotent filter in MuleIdempotent filter in Mule
Idempotent filter in Mule
Ā 
Identity service keystone ppt
Identity service keystone pptIdentity service keystone ppt
Identity service keystone ppt
Ā 
Deploy agent in em12c
Deploy agent in em12cDeploy agent in em12c
Deploy agent in em12c
Ā 
Activemq installation and master slave setup using shared broker data
Activemq installation and master slave setup using shared broker dataActivemq installation and master slave setup using shared broker data
Activemq installation and master slave setup using shared broker data
Ā 
Configurare https mule
Configurare https muleConfigurare https mule
Configurare https mule
Ā 
APACHE WEB SERVER FOR LINUX
APACHE WEB SERVER FOR LINUXAPACHE WEB SERVER FOR LINUX
APACHE WEB SERVER FOR LINUX
Ā 
Oracle virtual server-2-t0-3-upgrade
Oracle virtual server-2-t0-3-upgradeOracle virtual server-2-t0-3-upgrade
Oracle virtual server-2-t0-3-upgrade
Ā 
Mule security - ldap for spring security
Mule  security - ldap for spring securityMule  security - ldap for spring security
Mule security - ldap for spring security
Ā 
Install oracle siebel on windows 2008 r2
Install oracle siebel on windows 2008 r2Install oracle siebel on windows 2008 r2
Install oracle siebel on windows 2008 r2
Ā 
Scatter and gather in mule
Scatter and gather in muleScatter and gather in mule
Scatter and gather in mule
Ā 
Refresh development from productions
Refresh development from productionsRefresh development from productions
Refresh development from productions
Ā 

Viewers also liked

Road to database automation: database source control
Road to database automation: database source controlRoad to database automation: database source control
Road to database automation: database source controlEduardo Piairo
Ā 
DbOps, DevOps and Ops
DbOps, DevOps and OpsDbOps, DevOps and Ops
DbOps, DevOps and OpsEduardo Piairo
Ā 
Road to database automation: database source control
Road to database automation: database source controlRoad to database automation: database source control
Road to database automation: database source controlEduardo Piairo
Ā 
Why Microsoft Dynamics AX
Why Microsoft Dynamics AXWhy Microsoft Dynamics AX
Why Microsoft Dynamics AXMicrosoft
Ā 
Database Source Control: Migrations vs State
Database Source Control: Migrations vs StateDatabase Source Control: Migrations vs State
Database Source Control: Migrations vs StateEduardo Piairo
Ā 
Get the Most Out of Management Reporter for Microsoft Dynamics AX | Laura Cooper
Get the Most Out of Management Reporter for Microsoft Dynamics AX | Laura CooperGet the Most Out of Management Reporter for Microsoft Dynamics AX | Laura Cooper
Get the Most Out of Management Reporter for Microsoft Dynamics AX | Laura CooperKatie Elliott
Ā 
Microsoft Dynamics AX (Microsoft Dynamics Axapta)
Microsoft Dynamics AX (Microsoft Dynamics Axapta)Microsoft Dynamics AX (Microsoft Dynamics Axapta)
Microsoft Dynamics AX (Microsoft Dynamics Axapta)Logic Technology
Ā 
Microsoft Dynamics Retail e-Book
Microsoft Dynamics Retail e-Book   Microsoft Dynamics Retail e-Book
Microsoft Dynamics Retail e-Book Microsoft India
Ā 
Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3Ali Raza Zaidi
Ā 
Microsoft Dynamics AX 2012 - Development Introduction Training - Part 1/3
Microsoft Dynamics AX 2012 - Development Introduction Training - Part 1/3Microsoft Dynamics AX 2012 - Development Introduction Training - Part 1/3
Microsoft Dynamics AX 2012 - Development Introduction Training - Part 1/3Fabio Filardi
Ā 
Microsoft dynamics ax 2012 development introduction part 1/3
Microsoft dynamics ax 2012 development introduction part 1/3Microsoft dynamics ax 2012 development introduction part 1/3
Microsoft dynamics ax 2012 development introduction part 1/3Ali Raza Zaidi
Ā 
Data Source API in Spark
Data Source API in SparkData Source API in Spark
Data Source API in SparkDatabricks
Ā 

Viewers also liked (13)

Road to database automation: database source control
Road to database automation: database source controlRoad to database automation: database source control
Road to database automation: database source control
Ā 
DbOps, DevOps and Ops
DbOps, DevOps and OpsDbOps, DevOps and Ops
DbOps, DevOps and Ops
Ā 
Road to database automation: database source control
Road to database automation: database source controlRoad to database automation: database source control
Road to database automation: database source control
Ā 
Why Microsoft Dynamics AX
Why Microsoft Dynamics AXWhy Microsoft Dynamics AX
Why Microsoft Dynamics AX
Ā 
Database Source Control: Migrations vs State
Database Source Control: Migrations vs StateDatabase Source Control: Migrations vs State
Database Source Control: Migrations vs State
Ā 
Get the Most Out of Management Reporter for Microsoft Dynamics AX | Laura Cooper
Get the Most Out of Management Reporter for Microsoft Dynamics AX | Laura CooperGet the Most Out of Management Reporter for Microsoft Dynamics AX | Laura Cooper
Get the Most Out of Management Reporter for Microsoft Dynamics AX | Laura Cooper
Ā 
Microsoft Dynamics AX (Microsoft Dynamics Axapta)
Microsoft Dynamics AX (Microsoft Dynamics Axapta)Microsoft Dynamics AX (Microsoft Dynamics Axapta)
Microsoft Dynamics AX (Microsoft Dynamics Axapta)
Ā 
Microsoft Dynamics Retail e-Book
Microsoft Dynamics Retail e-Book   Microsoft Dynamics Retail e-Book
Microsoft Dynamics Retail e-Book
Ā 
Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3
Ā 
Microsoft Dynamics AX 2012 - Development Introduction Training - Part 1/3
Microsoft Dynamics AX 2012 - Development Introduction Training - Part 1/3Microsoft Dynamics AX 2012 - Development Introduction Training - Part 1/3
Microsoft Dynamics AX 2012 - Development Introduction Training - Part 1/3
Ā 
Microsoft dynamics ax 2012 development introduction part 1/3
Microsoft dynamics ax 2012 development introduction part 1/3Microsoft dynamics ax 2012 development introduction part 1/3
Microsoft dynamics ax 2012 development introduction part 1/3
Ā 
Data Source API in Spark
Data Source API in SparkData Source API in Spark
Data Source API in Spark
Ā 
Ax Presentation
Ax PresentationAx Presentation
Ax Presentation
Ā 

Similar to Connect sql-using-perl-from-linux

Using microsoft odbc and oracle gateway
Using microsoft odbc and oracle gatewayUsing microsoft odbc and oracle gateway
Using microsoft odbc and oracle gatewayAndy Liu
Ā 
Introducing with MongoDB
Introducing with MongoDBIntroducing with MongoDB
Introducing with MongoDBMahbub Tito
Ā 
The best Teradata RDBMS introduction a quick refresher
The best Teradata RDBMS introduction a quick refresherThe best Teradata RDBMS introduction a quick refresher
The best Teradata RDBMS introduction a quick refresherSrinimf-Slides
Ā 
3 Easy Steps to Install Docker PostgreSQL Environment.pdf
3 Easy Steps to Install Docker PostgreSQL Environment.pdf3 Easy Steps to Install Docker PostgreSQL Environment.pdf
3 Easy Steps to Install Docker PostgreSQL Environment.pdfcalfonzodaly
Ā 
Node.js with MySQL.pdf
Node.js with MySQL.pdfNode.js with MySQL.pdf
Node.js with MySQL.pdfSudhanshiBakre1
Ā 
Mysql tutorial 5257
Mysql tutorial 5257Mysql tutorial 5257
Mysql tutorial 5257Phuong Do Anh
Ā 
How to deploy laravel application on aws ec2
How to deploy laravel application on aws ec2How to deploy laravel application on aws ec2
How to deploy laravel application on aws ec2Katy Slemon
Ā 
Teradata client4
Teradata client4Teradata client4
Teradata client4Madhu Bandi
Ā 
Docker for Web Developers: A Sneak Peek
Docker for Web Developers: A Sneak PeekDocker for Web Developers: A Sneak Peek
Docker for Web Developers: A Sneak Peekmsyukor
Ā 
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql  - how do pdo, mysq-li, and x devapi do what they doPhp &amp; my sql  - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they doDave Stokes
Ā 
Eyeball XMPP Server Administrator Guide
Eyeball XMPP Server Administrator GuideEyeball XMPP Server Administrator Guide
Eyeball XMPP Server Administrator GuideEyeball Networks
Ā 
configuring+oracle+rds+with+glasfish+server
configuring+oracle+rds+with+glasfish+serverconfiguring+oracle+rds+with+glasfish+server
configuring+oracle+rds+with+glasfish+serverhunghtc83
Ā 
R hive tutorial supplement 1 - Installing Hadoop
R hive tutorial supplement 1 - Installing HadoopR hive tutorial supplement 1 - Installing Hadoop
R hive tutorial supplement 1 - Installing HadoopAiden Seonghak Hong
Ā 
Ms Sql Business Inteligence With My Sql
Ms Sql Business Inteligence With My SqlMs Sql Business Inteligence With My Sql
Ms Sql Business Inteligence With My Sqlrsnarayanan
Ā 

Similar to Connect sql-using-perl-from-linux (20)

Using microsoft odbc and oracle gateway
Using microsoft odbc and oracle gatewayUsing microsoft odbc and oracle gateway
Using microsoft odbc and oracle gateway
Ā 
Introducing with MongoDB
Introducing with MongoDBIntroducing with MongoDB
Introducing with MongoDB
Ā 
Lovely
LovelyLovely
Lovely
Ā 
The best Teradata RDBMS introduction a quick refresher
The best Teradata RDBMS introduction a quick refresherThe best Teradata RDBMS introduction a quick refresher
The best Teradata RDBMS introduction a quick refresher
Ā 
3 Easy Steps to Install Docker PostgreSQL Environment.pdf
3 Easy Steps to Install Docker PostgreSQL Environment.pdf3 Easy Steps to Install Docker PostgreSQL Environment.pdf
3 Easy Steps to Install Docker PostgreSQL Environment.pdf
Ā 
Node.js with MySQL.pdf
Node.js with MySQL.pdfNode.js with MySQL.pdf
Node.js with MySQL.pdf
Ā 
Mysql tutorial 5257
Mysql tutorial 5257Mysql tutorial 5257
Mysql tutorial 5257
Ā 
How to deploy laravel application on aws ec2
How to deploy laravel application on aws ec2How to deploy laravel application on aws ec2
How to deploy laravel application on aws ec2
Ā 
Its3 Drupal
Its3 DrupalIts3 Drupal
Its3 Drupal
Ā 
Teradata client4
Teradata client4Teradata client4
Teradata client4
Ā 
Docker for Web Developers: A Sneak Peek
Docker for Web Developers: A Sneak PeekDocker for Web Developers: A Sneak Peek
Docker for Web Developers: A Sneak Peek
Ā 
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql  - how do pdo, mysq-li, and x devapi do what they doPhp &amp; my sql  - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Ā 
Eyeball XMPP Server Administrator Guide
Eyeball XMPP Server Administrator GuideEyeball XMPP Server Administrator Guide
Eyeball XMPP Server Administrator Guide
Ā 
configuring+oracle+rds+with+glasfish+server
configuring+oracle+rds+with+glasfish+serverconfiguring+oracle+rds+with+glasfish+server
configuring+oracle+rds+with+glasfish+server
Ā 
instaling
instalinginstaling
instaling
Ā 
instaling
instalinginstaling
instaling
Ā 
instaling
instalinginstaling
instaling
Ā 
instaling
instalinginstaling
instaling
Ā 
R hive tutorial supplement 1 - Installing Hadoop
R hive tutorial supplement 1 - Installing HadoopR hive tutorial supplement 1 - Installing Hadoop
R hive tutorial supplement 1 - Installing Hadoop
Ā 
Ms Sql Business Inteligence With My Sql
Ms Sql Business Inteligence With My SqlMs Sql Business Inteligence With My Sql
Ms Sql Business Inteligence With My Sql
Ā 

Recently uploaded

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
Ā 
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhisoniya singh
Ā 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
Ā 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
Ā 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
Ā 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
Ā 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
Ā 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
Ā 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
Ā 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
Ā 
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...Alan Dix
Ā 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
Ā 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
Ā 
Integration and Automation in Practice: CI/CD in MuleĀ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleĀ Integration and Automat...Integration and Automation in Practice: CI/CD in MuleĀ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleĀ Integration and Automat...Patryk Bandurski
Ā 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
Ā 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
Ā 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
Ā 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
Ā 

Recently uploaded (20)

The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
Ā 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
Ā 
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
Ā 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
Ā 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
Ā 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
Ā 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
Ā 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Ā 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
Ā 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Ā 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
Ā 
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...
Swan(sea) Song ā€“ personal research during my six years at Swansea ... and bey...
Ā 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Ā 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
Ā 
Integration and Automation in Practice: CI/CD in MuleĀ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleĀ Integration and Automat...Integration and Automation in Practice: CI/CD in MuleĀ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleĀ Integration and Automat...
Ā 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
Ā 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
Ā 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
Ā 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Ā 

Connect sql-using-perl-from-linux

  • 1. CONNECT TO SQL USING PERL FROM LINUX Introduction: I recently came across a situation where I need to connect SQL using Perl from Linux box. As I have not done that I was like, ā€˜Is it possible to connect MSSQL from Linux?ā€ but after googling I came to know Microsoft has a driver for Linux which can be used to connect via Linux. I had tried it and it worked like a charm. I was able to connect to SQL server using Microsoft binary. The real problem is that it cannot be used as Perl module to extend the usability via scripting. Again I started my search and reading multiple articles, websites and I was able to successfully connect to SQL using Perl from Linux. I will briefly describe how to achieve it and where to get the packages in this article. MSSQL Driver for Linux: It was always tough to connect to MSSQL from Linux but surprising Microsoft has provided an ODBC driver which can be used to have easy access to SQL server via cmd. The details can be gathered from the below screenshot.
  • 2. Packages needs to be installed: o Unix-ODBC o Unix-ODBC-devel o FreeTDS o DBI::Sybase http://search.cpan.org/~mewp/DBD-Sybase-1.15/ Unix-ODBC: This can be installed via the default Linux distro package manager. RHEL: yum install unixODBC It resolves and auto install unixODBC-Devel and freeTDS as dependencies. The distro which does not have FreeTDS and UNIX-ODBC, please download the latest stable packages form the respective website and you have to install it manually using below steps. Building FreeTDS: $ ./configure --prefix=/usr/local $ make $ su root Password: $ make install Source: http://www.freetds.org/userguide/config.htm Configuration: Once the installation gets completed. The main part is to configure the freetds and odbc.ini, odbcinst.ini files. This plays a crucial part in connecting to SQL server. FreeTDS: Edit the freetds.conf from the location you had installed earlier. If you have installed via package manager it can be found at /etc/freetds.conf. The below is the template for adding DB details. [DBName] host = <serverURL where DB is hosted> port = 1433 tds version = 8.0 For some unknown reason, I had to change the version of TDS to 8.0 from 7.0 to get it connect to SQL server 2012.
  • 3. Odbc.ini This file passes the details such as DataSource and other driver details to establish the connection. The below is the template for editing odbc.ini. [ODBC Data Sources] DBName = MSSQL Server [DBName] Driver = freetds Description = Microsoft SQL Trace = No Server = <serverURL where DB is hosted> port = 1433 Database = DBName TDS_Version = 7.1 Odbcinst.ini This file passes which DB we are connecting and the ODBC driver details. The below is the template for editing odbcinst.ini. [freetds] Description = MS SQL database access with FreeTDS Driver = /usr/lib64/libtdsodbc.so.0 Setup = /usr/lib64/libtdsodbc.so.0 UsageCount = 1 Testing: Once everything is in place, itā€™s time for us to test the freetds configuration. Itā€™s important that freetds connection should work to move further. Execute the below command to verify whether freetds has configured properly.
  • 4. Execute the below command to test the connection gets established or not. TDSERVER=7.0tsql -H <Server name> -U <username> -p <port> -D <DBName to Connect> If itā€™s successful you can see the output as below. Installing Perl Module: As we had completed most of the integral configuration, itā€™s time to install Perl module which we can use later to connect to SQL DB via scripting. There are few ways to use Perl to connect to a SQL Server using FreeTDS. All of them can be installed via CPAN but I have tried installing configuring the module. o DBD::Sybase o DBD::ODBC o Sybperl DBD::Sybase: Despite the name it works for any Sybase or Microsoft SQL Server. DBD::Sybase uses the CT-Library API and works well. Building DBD::Sybase Download the latest Sybase module from cpan.org and follow the below steps. export SYBASE=/usr/local/freetds perl Makefile.PL Use 'CHAINED' mode by default (Y/N) [Y]: Y (if this Y, no need to use $dbh- >commit() in the script. Autocommit does the job). Make make install (as root)
  • 5. Connect to Server using DBD::Sybase #!/usr/bin/perl # use DBI; my $user = "<username>"; my $passwd = ' <password'; my $server = "<server url"; my $dbh = DBI->connect("DBI:Sybase:server=$server", $user, $passwd, {PrintError => 0}); unless ($dbh) { die "ERROR: Failed to connectnError Message: $DBI::errstr"; }else { print "n"; print "Successful Connection."; print "nn"; } $dbh->disconnect;