2

I can easily connect to a remote MySQL server using the DBI module in my Perl scripts. However, when I try to use the same connection settings/properties from within a CGI script, the connection fails.

There are no helpful errors/warnings being logged either in the apache error log, or the browser, in spite of using

use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

Strangely, the exact same script works fine when executed from the terminal. I also tried connecting the CGI script to the MySQL server on localhost, but without any success.

On the other hand, phpMyAdmin works great on the machine.

I'm using CentOS Release 5.8. I'm suspecting it is a system/priveleges issue with the apache user or anywhere else on the OS, but have hit a dead end.

Any pointers would be much appreciated!

Thanks a lot.

Edit: I'm using the following connection string

$dbh = DBI->connect($ds, $uname, $pwd,{RaiseError => 1 }) or die "$DBI::errstr Could not connect: $!<br>";
6
  • There are always error messages. You're either not accessing them correctly, or ignoring them. Commented Sep 13, 2012 at 19:35
  • Try executing the same script on the terminal, but first change to the Apache user. sudo -u <apache user> <your script>. If it fails, that will be much easier to debug. Also, if there are no errors or warnings, how do you know the connection failed? Commented Sep 13, 2012 at 19:37
  • @LenJaffe - Well, I'm getting the following error message: Software error: DBI connect('$dbname:$host:$port','$username',...) failed: Can't connect to MySQL server on '$host' (13) at /var/www/cgi-bin/crm/$scriptname.cgi line 24 Not sure what to make out of that. Thanks Commented Sep 14, 2012 at 7:14
  • @Schwern - Thanks for the hint! I ran the script from terminal as apache user, and it executes without any error! Even more confusion now. What could be wrong? Commented Sep 14, 2012 at 7:17
  • Did it really have $dbname:$host:$port and $username in the error string? Try printing out $ds, $uname and $pwd. I think they don't contain what you think they contain. I think you have a single quote somewhere you should have a double. Commented Sep 14, 2012 at 7:42

2 Answers 2

3

Fixed it. In case anyone not too familiar with Linux administration faces similar issue, here's what had happened. The security system 'selinux' on my CentOS machine was prohibiting the httpd to establish network connection to the remote MySQL server. It had to be explicitly enabled with the following command

setsebool -P httpd_can_network_connect=1

One can read more about selinux over here:

http://wiki.centos.org/HowTos/SELinux

Sign up to request clarification or add additional context in comments.

Comments

0

Check what users you have set up in MySQL. If you're trying to connect across a network connection, you could have a problem with the host association for the user.

Try: select host,user from mysql.user

1 Comment

Thanks Nicole. The MySQL user I'm using to connect seems to be having the appropriate privileges. Value for Select_Priv column is 'Y' for 2 entries for the username for Host 'localhost' and '%' respectively. In any case, as I mentioned before, I can connect to the MySQL from the same host, but from terminal. Problem occurs only while calling the script from browser. Executing the script from terminal with apache user works fine too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.