0

I have a Perl script which retrieves data from MySQL Database. this is the code:

sub startSession{
    my $self = shift;

    my $dsn = "dbi:".$self{platform}.":".$self{database}.":".$self{host}.":".$self{port};
    print "$dsn\n" ;
    $self{dbHandle} = DBI->connect($dsn,$user,$password);   
}

I have provided every information from an external file. I get the error message

DBI connect('dbname:**.**.**.**:3306','',...) failed: Access denied for user 'root'@'dbserver' (using password: NO) at line 89

Can't call method "prepare" on an undefined value at at line 97

I am very sure the root can connect from any host and the password is also correct.

1
  • 5
    One problem is that $self{...} has nothing to do with my $self = shift. You'd need to use $self->{...}. Commented May 3, 2012 at 18:30

3 Answers 3

3

First, your immediate problem, is as @Sinan Ünür says, that you need to change $self{platform} to $self->{platform}, etc.

Your second immediate problem is that it appears you're getting $user and $password from nowhere (they are not passed to the function, so they are undefined unless they are global variables), which would explain the using password: NO part of the error. Maybe those should be $self->{user} and $self->{password}?

You should considering put this at the top of your module, at least during development, to automatically catch errors like these:

use warnings qw(all);
use strict;

But I'd also comment, that from a design perspective, you really ought to treat DSNs as opaque strings. Each database has its own DSN format. So if you ever want to target a different database, you'll need a different DSN format. Or, possibly, someday MySQL will use a different format (it already has two). Either way, it'll be much easier to change it one place, in a configuration file, than to track down each place you concatenate the various pieces together.

Sign up to request clarification or add additional context in comments.

3 Comments

Like I have said in the problem statement, user and password are taken from an external file.
@user1304082: That's what you think anyway. It's quite evident that $user and $password aren't even declared in the scope you're using them, and that you need to provide more context if you want help solving your problem.
@user1304082 I have clarified my answer. You haven't passed $user and $password to the function (or, if you have, you're missing the my $user = shift;, my $password = shift; lines), so unless those are global variables, they're undefined. use strict will catch this.
3

The key part of the warning that I see is "using password: NO". Check that the password is being set properly.

6 Comments

How do I do that ? and one more thing, I dont even have a host named 'dbserver'
If you turn on use strict; and use warnings; for this, I'd bet that it would give you more information on how to fix this than SO users.
It gave me this warnings Global symbol "$user" requires explicit package name at dbAdmin.pm line 27. Global symbol "$password" requires explicit package name at dbAdmin.pm line 27
Somewhere in your script, you need to have my $username = 'my_db_user'; and my $password = 'my_db_password';.
I did that, the machine is a new clean installation, do you think i need to install any mysql drivers on it before i use this script ?
|
2

Presumably, $self is a hashref, not a plain hash, and you don't have warnings on. So, turn them on, and use $self->{platform} etc.

3 Comments

these are the warnings I get Global symbol "$user" requires explicit package name at dbAdmin.pm line 27. Global symbol "$password" requires explicit package name at dbAdmin.pm line 27
Quite right, but the OP's immediate problem is that the call to DBI->connect is failing.
Then clearly your variables $user and $password aren't getting set from the external file you mentioned. How are you doing that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.