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

Dear monks, a simple but frustrating problem: my cgi script interacts just fine with the database when I run it from the command line, but won't connect when I run it through apache. The /var/log/apache/error_log says:
DBI connect('fab:127.0.0.1:3306','fab',...) failed: Can't create TCP/I +P socket (1) at /var/www/cgi-bin/fab.cgi line 180 Couldn't connect to database: Can't create TCP/IP socket (1) at /var/w +ww/cgi-bin/fab.cgi line 180.
I am not entirely sure where to start solving this. I'm running MySQL Ver 12.22 Distrib 4.0.20a, for Win95/Win98 (i32), Server version: Apache/1.3.29 (Cygwin), I compiled the MySQL client under Cygwin to get the library and header files (as per the recipe at http://search.cpan.org/src/RUDY/DBD-mysql-2.9004/INSTALL.html#windows/cygwin), and perl v5.8.5 built for cygwin-thread-multi-64int. I reinstalled the latest versions for DBI and DBD::mysql today. The incantation:
$dbh = DBI->connect("DBI:mysql:fab:127.0.0.1:3306",$user,$pass);
works fine when I execute the script from the command line, but not when invoked by apache. I hope this is not bad form to ask here, because I'm worried it might be a permissions thing w.r.t. the apache process? Not sure what to do, I didn't change anything in httpd.conf? Thanks, as always.

Edited by Chady -- converted <pre> to <code> tags.

Replies are listed 'Best First'.
Re: DBI, DBD::MySQL, Apache & Cygwin
by reneeb (Chaplain) on Sep 02, 2004 at 10:29 UTC
    Try to connect without the port:
    $dbh = DBI->connect("DBI:mysql:fab:127.0.0.1",$user,$pass);
      Mmmm... if I run that in a script from the command line, it works... but not as a cgi.
Re: DBI, DBD::MySQL, Apache & Cygwin
by Velaki (Chaplain) on Sep 02, 2004 at 08:17 UTC

    You may wish to look into where $user and $pass come from in your script, since it appears that the values being used do not have the correct permissions to access the database.

    Give it a look-see, and determine if your script is running as the correct user (you may have to look at both the script and the httpd.conf file), and/or whether or not your script is pulling the correct values to be used in the connect string. Also, ensure that the user and password values being sent are permitted in the database.

    Hope that helped,
    -v
    "Perl. There is no substitute."
      Hi Velaki, thanks for that swift reply!

      $user and $pass work fine when I run the script from the command line and there is no way they could be changed during the CGI execution (they are, erm, hard coded...).

      I went through mysql.user and the $user and $pass combination definitely are allowed on this database, from localhost and 127.0.0.1.

      I hope you don't mind me asking if you could give me any pointers what to change in httpd.conf? Do I have to allow more in the ScriptAlias <Directory "/blah/blah"></Directory> directive? Thanks!
Re: DBI, DBD::MySQL, Apache & Cygwin
by eclecticIO (Beadle) on Sep 02, 2004 at 12:38 UTC
    Since you've specified connecting over the netowrk with '127.0.0.1:3306' (IP:port) you may need to check and make sure your MySQL config file has networking enabled. On Windows the config file my.conf should be under the conf directory where you installed MySQL. Open the file and look for the line 'skip-networking' and comment it out. Networking is disabled by default for security reasons.

    I'm not sure if this will help, and I'm not sure why it would work from the command line and not through the server (I'm posting this pre-coffee), but your error message looks similar to the one I invariably get every time I set up Apache/MySQL reminding me to enable networking. Apparently I am unable to remember this little fact on my own. :)

    eclecticIO

    "Given the pace of technology, I propose we leave math to the machines and go play outside." Calvin - Homicidal Psycho Jungle Cat
Re: DBI, DBD::MySQL, Apache & Cygwin
by doowah2004 (Monk) on Sep 02, 2004 at 12:43 UTC
    Well, apparently the mysql is on the local host, so you do not need to put in the location. Try this and it should work:

    my $dsn = 'DBI:mysql:fad'; my $db_user_name = '[username]'; my $db_password = '[pass]'; my $dbh = DBI->connect($dsn, $db_user_name, $db_password);


    Cameron
      Thanks for your reply, but no... The setup is like this:
      • Windows runs MySQL
      • Cygwin runs perl and apache
      I can't leave the IP address of the MySQL server unspecified or set to localhost, because then DBD::mysql will try to connect to it through /tmp/mysql.sock, which doesn't exist (remember, it's a windows version of MySQL, so no UNIX sockets...).

      Thanks again though!
Re: DBI, DBD::MySQL, Apache & Cygwin
by CountZero (Bishop) on Sep 11, 2004 at 12:57 UTC
    Totally off-topic, but just being curious: why do you run Apache and Perl in Cygwin? I run a similar set-up all under Windows and it works.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law