Perl, DBI, localhost problem

Discussion in 'Perl Misc' started by Jonas Andersson, Jan 30, 2004.

  1. 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
     
    Jonas Andersson, Jan 30, 2004
    #1
    1. Advertising

  2. Jonas Andersson

    Paul Lalli Guest

    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
     
    Paul Lalli, Jan 30, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiong Feng
    Replies:
    0
    Views:
    940
    Jiong Feng
    Nov 19, 2003
  2. Alex
    Replies:
    4
    Views:
    611
    Hasani Blackwell
    Feb 8, 2005
  3. =?Utf-8?B?S2VuTGVl?=
    Replies:
    5
    Views:
    2,767
  4. EdwardH

    Localhost isn't localhost

    EdwardH, Sep 29, 2005, in forum: Java
    Replies:
    20
    Views:
    6,478
    Roedy Green
    Oct 1, 2005
  5. Ted Byers
    Replies:
    4
    Views:
    1,467
    Rainer Weikusat
    Aug 25, 2011
Loading...

Share This Page