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

Oh monks, who are wise in the ways of Perl, I need your advice. I am trying to connect to MySQL using DBI but things are going awry. I have this in my script:
my $dsn = 'DBI:mysql:ciprestest:localhost'; my $dbuser = 'cipresquery'; my $dbpass = 'iamstupid'; my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or die "Couldn't connect to database: " . DBI->errstr;
But all I get is:
DBI connect('database=ciprestest;host=127.0.0.1;port=3306','',...) fai +led: Access denied for user: 'rvosa@localhost' (Using password: NO)
In other words, the user name ($dbuser) and password ($dbpass) I define seem to be ignored when DBI tries to connect, and it uses my cygwin user name (rvosa) instead. Everything works fine when I connect to mysql from the terminal: mysql -u cipresquery -p'iamstupid' ciprestest and the script does connect when I change the mysql privileges so that user name and password aren't required but that's obviously not what I want. What could be going on? I tried different permutations for quotes around the DBI->connect() arguments, concatenated them all separated by colons, tried "user=cipresquery" for $dbuser and so on but to no avail.

Replies are listed 'Best First'.
Re: DBI confusion
by jaco (Pilgrim) on Apr 13, 2004 at 23:44 UTC
    $dbh = DBI->connect("DBI:mysql:database=$database;host=$dbhost",$dbuse +r,$dbpassword);
Re: DBI confusion
by blue_cowdawg (Monsignor) on Apr 14, 2004 at 00:53 UTC

        $dsn = 'DBI:mysql:ciprestest:localhost';

    As jaco already pointed out you need to spruce up that DSN a bit.

    my $database="ciprestest"; my $host="localhost"; my $DSN=sprintf("DBI:mysql:database=%s:host=%s", $database,$host; my $dby = DBI->connect($DSN,$dbuser,$dbpass, {RaiseErrors => 1 ) or die $DBI->errstr;

    Also you might want to make sure your permissions within MySQL are correct. If the user@host combination is not set up with permissions to access the database you'll get bounced out. Check tables mysql.Db, mysql.User and mysql.Host and make sure they are set up correctly.

Re: DBI confusion
by tantarbobus (Hermit) on Apr 13, 2004 at 23:45 UTC
    Try a DBI->trace(9). That should help you see what is going on.