LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2011, 06:29 AM   #1
mansingh2009
LQ Newbie
 
Registered: Jul 2010
Posts: 4

Rep: Reputation: 0
Cool Problem conecting to Database using perl....Guys do help me out .....


Hi

I have written a script to connect to database using mysql and get the table. I am pasting the script here. But its giving me an error that Access Denied, though if I log in to mysql using same user , i m able to log in. But not using script.

The script is

#!/usr/bin/perl

use DBI;
$dbh = DBI->connect('dbi:mysql:mysql','root','redhat') or die "Connection Error: $DBI::errstr \n";
$sql="select * from SUNNY";
$sth=$dbh->prepare($sql);
$sth->execute or die " SQL Error: $DBI::errstr\n";
while (@row = $sth->fetchrow_array){
print "@row\n";
}

its giving an error that ...

DBI connect('mysql','root',...) failed: Access denied for user 'root'@'localhost' (using password: YES) at ./test line 5
Connection Error: Access denied for user 'root'@'localhost' (using password: YES)

if I log in directly , I m able to log in and even have full grants.

mysql> show GRANTS;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)


guys do help me out ......
 
Old 11-18-2011, 08:54 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,635

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by mansingh2009 View Post
Hi
I have written a script to connect to database using mysql and get the table. I am pasting the script here. But its giving me an error that Access Denied, though if I log in to mysql using same user , i m able to log in. But not using script.

The script is
Code:
#!/usr/bin/perl

use DBI;
$dbh = DBI->connect('dbi:mysql:mysql','root','redhat') or die "Connection Error: $DBI::errstr \n";
$sql="select * from SUNNY";
$sth=$dbh->prepare($sql);
$sth->execute or die " SQL Error: $DBI::errstr\n";
while (@row = $sth->fetchrow_array){
print "@row\n";
}
its giving an error that ...

DBI connect('mysql','root',...) failed: Access denied for user 'root'@'localhost' (using password: YES) at ./test line 5
Connection Error: Access denied for user 'root'@'localhost' (using password: YES)

if I log in directly , I m able to log in and even have full grants.
Please use CODE tags in the future..makes things easier to read. And if that's the EXACT code you're using above, the problem is pretty clear. I'd suggest you reference the docs on the Perl:BI module. You're not calling it correctly...try this:
Code:
$dbh = DBI->connect('DBI:mysql:DATABASE NAME GOES HERE', USER, PASSWORD,{ RaiseError => 1}) or die "Could not connect to database! $DBI::errstr";
You missed the DBI being upper-case (things are case sensitive), and specified your database name as 'mysql', which is reserved.
 
Old 11-21-2011, 03:09 AM   #3
mansingh2009
LQ Newbie
 
Registered: Jul 2010
Posts: 4

Original Poster
Rep: Reputation: 0
I corrected the script as below ..but still same error.

$dbh = DBI->connect('DBI:mysql:MANMEET','root','redhat') or die "Connection Error: $DBI::errstr \n";
 
Old 11-21-2011, 08:36 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,635

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by mansingh2009 View Post
I corrected the script as below ..but still same error.

$dbh = DBI->connect('DBI:mysql:MANMEET','root','redhat') or die "Connection Error: $DBI::errstr \n";
Ok...does the root user actually HAVE a password set, because by default the MySQL root user doesn't. And where is the RaisError string in your corrected version? Have you tried a different MySQL user ID?
 
Old 11-21-2011, 10:30 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl database blank server-solution Linux - Software 6 04-28-2006 04:11 AM
problem conecting to my web server juanb Linux - General 3 10-13-2005 09:21 AM
Need help with database perl silhoutte Linux - Newbie 4 05-23-2005 11:12 PM
Perl guys! Time delay 4 Eliza bot Gaim response - want to have fun :) lrt2003 Programming 1 10-03-2004 12:04 PM
problem conecting 2 hardisk with linux OS prai Linux - Hardware 2 07-18-2003 02:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration