Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Unable to connect to database

by Shaveta_Chawla (Sexton)
on Aug 07, 2013 at 10:14 UTC ( [id://1048320]=perlquestion: print w/replies, xml ) Need Help??

Shaveta_Chawla has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to connect to remote database. The snippet of the code is:
use strict; use DBI; use Test::More qw( no_plan ) ; use Data::Dumper; my $host = 'example.com'; my $port = '3306'; my $db = 'calls'; my $user = 'username'; my $password = 'password'; my $data_source = "DBI:mysql:database=" . $db . ":host=" . $host . ":p +ort=" . $port; my $dbh = DBI->connect($data_source,$user,$password,{ PrintError => 0} +); if(!$dbh) { print "Connection to Database Failed\n"; exit -1; }

When i execute the script i get the following error: install_driver(mysql) failed: Can't locate DBD/mysql.pm in @INC (@INC contains ........

my mysql and DBI package is up and running.

Manually installed the DBD module and copied it into the perl repository.

From CPAN i get:

cpan> install DBD::mysql DBD::mysql is up to date (4.023).

From PPM i get:

ppm install DBD::mysql No missing packages to install

Even when i execute the command

perl -e "use DBD::mysql";

i get the following error: Can't locate loadable object for module DBD::mysql in @INC......

perl -e "print @INC"; gives me the location of perl library where DBD module is installed. i don't know how to resolve the issue.

Replies are listed 'Best First'.
Re: Unable to connect to database
by Tux (Canon) on Aug 07, 2013 at 11:33 UTC

    Besides that, your code won't work. The driver part of the DSN separates by semi-colons, not by colons:

    my $data_source = "DBI:mysql:database=" . $db . ":host=" . $host . ":p +ort=" . $port; my $dbh = DBI->connect($data_source,$user,$password,{ PrintError => 0} +);

    =>

    my $data_source = "dbi:mysql:database=$db;host=$host;port=$port"; # these colons should be semi-colons ^ ^ my $dbh = DBI->connect ($data_source, $user, $password, { PrintError => 0, });

    Enjoy, Have FUN! H.Merijn
    my $data_source =
Re: Unable to connect to database
by marto (Cardinal) on Aug 07, 2013 at 10:19 UTC

    "Manually installed the DBD module and copied it into the perl repository."

    What does this mean? You copied files around manually? Show us how you installed this module. Do you have more than one Perl installed on this machine?

Re: Unable to connect to database
by rnewsham (Curate) on Aug 07, 2013 at 10:36 UTC

    If you have confirmed mysql.pm should be found in @INC, then it could be a file permissions issue. If you have manually moved files around it is likely that they have the wrong permissions. If a file is unreadable you will get the "Can't locate .." message.

      No, these aren't the only issues involved. Not all modules are pure Perl, many require compilation. Many modules require further cpan based dependancies, external libraries or tools and so on which require their own installtion procedure. In such cases simply copying files and editing their permissions isn't going to work. cpan, cpanp and cpanm take care of this for you. Module documentation should cover the basics of installation.

        Agreed it is far from the only potential issue, I only raised that single suggestion as I thought it was the most likely cause of the error as described. I expect other problems related to incorrect installation will give further errors when that one is resolved.

Re: Unable to connect to database
by Shaveta_Chawla (Sexton) on Aug 08, 2013 at 06:53 UTC

    I removed the modules which i installed manually. Now if i am trying to install using cpan i get the error:

    C:\PROGRA~1\MySQL\MYSQLS~1.6\bin\MYSQLA~1.EXE: connect to server at 'localhost' failed error: 'Access denied for user 'ODBC'@'localhost' (using password: NO)' Problem running C:\PROGRA~1\MySQL\MYSQLS~1.6\bin\MYSQLA~1.EXE - aborting ... Warning: No success on commandC:\Perl\bin\perl.exe Makefile.PL INSTALLDIRS=site

    and if i install the module from ppm then i get the following error:

    Downloading DBD-mysql-4.011...redirect Downloading DBD-mysql-4.011...failed 401 Authorization Required ppm install failed: 401 Authorization Required

      5.8.8 is from 2006, consider upgrading. Either read the documentation regarding manual installation or add more PPM repos via PPM::Repositories.

        I just upgraded my perl from 5.8.8 to 5.16.3, and installed the modules without any error, now i am successfully able to run the script. Thanks!!

        perl -v gives me

        This is perl, v5.8.8 built for MSWin32-x86-multi-thread (with 12 registered patches, see perl -V for more detail) Copyright 1987-2007, Larry Wall Binary build 824 287188 provided by ActiveState http://www.ActiveState.com Built Sep 3 2008 11:14:55 Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1048320]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found