Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Setup:

Two separate boxes:

  1. Fedora 15, Apache, PHP and PHP modules
  2. Fedora 15, MySQL

I am unable to make a connection to mysql from Apache web server.

<?php

  echo "<br>begin test<br>";

  mysql_connect('hostname[FQDN]','username','password') or ("Error" . mysql_error());

  echo "<br>end test<br>";

?>

the 'begin test' text is being displayed but no mysql error or the 'end test' text when Apache serves the page.

I am able to connect to the MySQL DB using the MySQL workbench on port 3306 from a remote machine. Privileges and access to the user and IP address have been left completely open (%) to avoid any issues in those respects.

share|improve this question

migrated from stackoverflow.com Jul 28 '11 at 21:05

This question came from our site for professional and enthusiast programmers.

2  
what? ......... –  dynamic Jul 28 '11 at 21:04
3  
Sounds like you have a fatal error because the mysql extension isn't installed. yum install php-mysql –  Wrikken Jul 28 '11 at 21:07
    
Can you post the contents of your /var/log/httpd/error_log –  Zypher Jul 28 '11 at 21:11

3 Answers 3

Don't forget that, mysql_connect, mysql_error, etc are not part of PHP. They are an extension.

If you want to test if php has those functions you could do a little test like:

if(!function_exists('mysql_connect'))
    echo 'MySQL extension not installed';

You should have mysql and mysql-server installed on your MySQL Box

You should have php and php-mysql installed on your Apache box. If you haven't installed php-mysql install it: yum -y install php-mysql and then restart Apache.

share|improve this answer
    
php and php-mysql are installed on the apache box and mysql and mysql-server on the mysql box –  hecrodjr Jul 28 '11 at 21:17
    
@hecro What's the output from those two lines of code? If it outputs that you don't have the MySQL extension you may need to configure your php.ini. Can you edit your question with anything related to mysql output by phpinfo();? –  Paulpro Jul 28 '11 at 21:17
1  
The issue was resolved. The change I made was with SElinux and running setsebool -P httpd_can_network_connect=1 or running setenforce=0 which is permissive mode –  hecrodjr Jul 28 '11 at 22:18
    
@hecrodjr: Please add your solution as an answer and accept it, so that others may benefit from your knowledge. –  womble Jul 29 '11 at 0:08

Run

$ netstat -l

You should see the mysql server listening on port 3306. If it's 127.0.0.1, the server listens on the loopback interface only. Fix your my.cnf in that case.

share|improve this answer
    
yes it is listening on port 3306. code Local Address :mysql and Foreign Address *: State LISTEN code –  hecrodjr Jul 28 '11 at 21:16
up vote 0 down vote accepted

The issue was resolved. The change I made was with SElinux and running setsebool -P httpd_can_network_connect=1 or running setenforce=0 which is permissive mode

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.