On Fri, 30 Jan 2004, Jonas Andersson wrote:
> Hi
>
> I run Apache and collect info from my local MySQL database through a
> Perl-CGI using DBI. Works fine. However, when I try to write a non-CGI
> script (hence not using Apache) to access MySQL, I cannot connect to
> the database.
>
> I use
>
> my $username="user2"; my $password="(not shown)";
> my $data_source="DBI:mysql:MyDatabase:localhost";
> $dbh = DBI->connect($data_source, $username, $password) or die "1";
>
> which works fine when CGI:ed. But when I try this for my non-CGI
> script, all I get is
>
> DBI connect('MyDatabase:localhost','user2',...) failed: Access denied
> for user: 'user2@localhost' to database 'MyDatabase' at newdeal1a.pl
> line 165
> error connecting
>
> The username and password are obviously correct, and I can see that
> this has got something to do with localhost, but I'm afraid I can't
> see what. I have searched FAQs and the net, but I can't find any help.
> I'd be most grateful for whatever input I can get for this - I'm sure
> the problem is trivial.
>
> Thanks a lot, JA
Are you running the non-CGI script on the same server that the CGI script
is running on? If not, then "localhost" refers to two different machines.
One (the CGI's machine) has permissions to the database, the other does
not. Try changing the datasource to
"DBI:mysql:MyDatabase:machine.domain.com" where "machine.domain.com" is
the name of the server that the CGI script works with.
Paul Lalli
|