Perl, DBI, localhost problem

J

Jonas Andersson

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
 
P

Paul Lalli

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top