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

I have several perl scripts that connect to mysql databases. The only way I've gotten them to work is by hard coding the password in the DBI->connect statement. I'm running:

RHEL Servers 5.10
mysql 5.5.32
perl 5.8.8
DBI 1.52

If I use the following command the connect is successful:

my $dbh = DBI->connect('DBI:mysql:dns', 'userid', 'password' ) || die "Could not connect to database: $DBI::errstr";

This is a bad idea for obvious reasons! If I use:

my $dbh = DBI->connect('DBI:mysql:dns', $userid, $passwd ) || die "Could not connect to database: $DBI::errstr"

It fails with:

Usage: $class->connect([$dsn [,$user [,$passwd ,\%attr]]]).

I have tried it with $dsn both hard coded as above and with the variable $dsn set to the string as well as with $userid both hardcoded and a variable. I've tried both connects with and without the attribute field. The only format I have working is with the dsn, userid and password all hard coded. What am I overlooking or doing incorrectly?

Thanks for any help. <\p>

  • Comment on DBI->connect fails when password variable used

Replies are listed 'Best First'.
Re: DBI->connect fails when password variable used
by NetWallah (Canon) on May 30, 2014 at 22:48 UTC
    The code in DBI.pm that spits that error message is :
    sub connect { ... Carp::croak('Usage: $class->connect([$dsn [,$user [,$passwd [,\%at +tr]]]])') if (ref $old_driver or ($attr and not ref $attr) or ref $pass);
    In your case, when you use $passwd, is that a scalar or ref ?

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

      Thanks for the reply, Larry and I apologize for the delay getting back to you. That $passwd is a scalar. I've tried using, "connect($dsn,$user,$passwd)" as well as "connect("DBI:mysql:servers", "my uid", "mypasswd", {'RaiseError' => 1})" and the only thing that works is the hardcoded data as in the latter example. I've tried some of the other suggestions submitted without any luck so far. I would be happy to have the script prompt me for my password and be able to pass it in $passwd but haven't gotten that to work either. Thanks again for your suggestions. Fred

        How do you set $passwd?

        If you're reading the string from the user, most likely, that string still has a newline at its end, which you might want to remove:

        $passwd =~ s/\s*$//; # Remove all whitespace from the end of the passw +ord
Re: DBI->connect fails when password variable used
by RedElk (Hermit) on May 31, 2014 at 02:05 UTC

    Not sure of the details myself but FWIW check the DBI docs for particulars. It ("\%attr") is mentioned under bind_params.

    Also, the synopsis has the following. Might be worth a try:

    $dbh = DBI->connect($data_source, $username, $auth, \%attr)

Re: DBI->connect fails when password variable used
by erix (Prior) on May 31, 2014 at 07:07 UTC

    I think it's possible to set a password in a .cnf file: see option-files.

    (of course, access to .cnf files should be restricted).

Re: DBI->connect fails when password variable used
by Anonymous Monk on May 31, 2014 at 01:13 UTC
    Are you using  $dsn = 'dsn';?