mysql_connect() doesn't work when run by apache; works from command line
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 th开发者_如何学Ce server I'm connecting to)
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.
- Check basic network connectivity.
ping
xxx.xxx.xxx.xxx from the webserver. If that doesn't work, check the network configuration on both ends. - 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. - Check that the user is allowed to log on using the user administration feature of mysql.
精彩评论