3

I have a strange issue. I'm trying to write a simple php webpage on my server, but mysql_connect() doesn't connect to any server, either local or otherwise. Here's where it gets strange. If I take the same php script and run it from the commandline, the script works. phpinfo() indicates that both the file (being run by apache) and the commandline (run as root) are calling the same version of php, mysql is loaded, and the php.ini is the same.

Furthermore, I'm running a MediaWiki installation on this same server, and it's using the mysqld installed locally and works just fine, so I'm completely at a loss as to why the code isn't working. The error I receive on runtime:

Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (13)

(The IP is x'd out for the privacy of the owner of the server I'm connecting to)

3
  • does the phpinfo() give any clues? Commented Jun 11, 2010 at 5:42
  • try to su apache and then run the script. I'm guessing it's defaulting to connecting as the currently logged in user. Specify a username in your connect options, and make sure the user is configured in mysql. Commented Jun 11, 2010 at 5:51
  • Your issue is that the user that apache runs as does not have the proper permissions on your db. Commented Jun 11, 2010 at 8:37

2 Answers 2

4

What operating system are you trying to connect from? It sounds like it may be an SELinux issue.

With SELinux, you can usually allow apache to make network connections by using

/usr/sbin/setsebool httpd_can_network_connect 1

OR /usr/sbin/setsebool httpd_can_network_connect true

An operating system without SELinux may have a similar protection mechanism.

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

1 Comment

This did the trick! (It's running on Fedora) Thank you so much :)
0
  1. Check basic network connectivity. ping xxx.xxx.xxx.xxx from the webserver. If that doesn't work, check the network configuration on both ends.
  2. Try running mysql from the webserver: mysql -h xxx.xxx.xxx.xxx -u user -p If that can't connect, track the cause: it could be a SELinux issue on either end (check the security log), or maybe there is an alias in /etc/hosts? Also enable verbose messages with -v -v -v on the mysql command line.
  3. Check that the user is allowed to log on using the user administration feature of mysql.

Comments

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.