[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

Problems with executing CGI programs


I have Linux fedora 3 installed with apache webserver and mysql database .I wrote the following script to connect to teh mysql database :


use DBI;

#### main program
   $dbpath = "dbi:mysql:database=maxsponder;host=localhost";
   $dbh = DBI->connect($dbpath, "user", "pass")
     or die "Can't open database: $DBI::errstr";
print "Content-type:text/html", "\n\n";
print "Successful MySQL DB Connection";
# Create the statement.
$stmt = "SELECT * FROM adfarmerusage";
print "OK";
# Prepare and execute the SQL query
$sth = $dbh->prepare($stmt);
print "OK";
# Get the first record
# If more than one record will be returned put
# the fetchrow in a while loop
@record = $sth->fetchrow();
# Get the value of the first field returned.
$telephone = $record[0];
print $telephone;
# Clean up the record set and the database connection    
exit 0;

I changed teh permissions to this file and ran under my user account (./dbconn.cgi) and gave results  but when I copied over the file to /var/www/cgi-bin , and ran it there it did not work and in the browser it gave an error message "Internal Server Error" . I am doing something wrong here , why doesnt it work ?Any clues
  • 2
  • 2
  • 2
  • +2
1 Solution
Remember that when it is running as a CGI process, it will be running as the 'nobody' or 'apache' user.

If you add a:

use CGI::Carp qw(fatalsToBrowser);

in your script and run it again from your browser, I'll bet you'll see the error message

Can't open database: ........
janu_sAuthor Commented:
Yes , now I get the Cant open .... error , but how come under my account , that is when I just type ./dbconn.cgi in my local directory not under /var/www/cgi-bin  I get the connection passed through and the same file when tried with ./dbconn.pl when in teh directory /var/www/cgi-bin does not work , why?
janu_sAuthor Commented:
And this the error message Can't open database: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13) .
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

the user running httpd has another .my.cnf as you when logged in
check your ~/.my.cnf what you find as socket there, and the httpd's user aslo (which seems to be /var/lib/mysql/mysql.sock)

If you cannot change the system (httpd.conf or .my.cnf) then you can try following in your code:

$dbh->sock('/tmp/mysql.sock'); # <== write the proper path to the socket here
change this lines

$dbpath = "dbi:mysql:database=maxsponder;host=localhost";
   $dbh = DBI->connect($dbpath, "user", "pass")
   or die "Can't open database: $DBI::errstr";


$dbh = DBI->connect('DBI:mysql:maxsponder','user','pass') || die "Connect Error: $DBI::errstr";

I think the problem is with the 'localhost' definition. Your name to your client is not 'localhost'. So don't use it.
disagreed, sorry.
http:#13882056 does not give a solution, just a hint how you can *probably* get more diagnostic messages easyly
http:#14059283 also gives no solition, the assumption about localhost is very vage 'cause localhost is most likely defined in any /etc/hosts file and the initial question does not tell us that this is a *special* installation (for example a virtual server like some web hosters use)

according the information given by the questioner, I still believe that my suggestion http:#13887225 is most closest to a solution

If the questioner does not return, I'd suggest a PAQ with 0 points and no refund.
Here is another solution i got

Go to System settings
On the SELinux tab choose Transition at the list box
Select "Disable SELinux protection for httpd daemon"
Save/Close the configuration

Go to /var/lib/mysql and delete the mysql.sock file.

From the Services panel, stop the httpd and mysqld daemons.
Start the mysqld daemon
Start the httpd daemon

All should be working fine.
PAQed with no points refunded (of 50)

Community Support Moderator

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now