简体   繁体   English

当apache运行时,mysql_connect()不起作用; 从命令行工作

[英]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. 我正在尝试在我的服务器上编写一个简单的php网页,但mysql_connect()不会连接到本地或其他任何服务器。 Here's where it gets strange. 这是奇怪的地方。 If I take the same php script and run it from the commandline, the script works. 如果我使用相同的PHP脚本并从命令行运行它,脚本可以工作。 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. phpinfo()表示文件(由apache运行)和命令行(以root身份运行)都调用相同版本的php,加载mysql,php.ini是相同的。

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. 此外,我在同一台服务器上运行MediaWiki安装,它使用本地安装的mysqld,工作得很好,所以我完全不知道为什么代码不能正常工作。 The error I receive on runtime: 我在运行时收到的错误:

Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (13) 无法连接到'xxx.xxx.xxx.xxx'上的MySQL服务器(13)

(The IP is x'd out for the privacy of the owner of the server I'm connecting to) (为了我所连接的服务器的所有者的隐私,IP是x'd)

What operating system are you trying to connect from? 您尝试连接的操作系统是什么? It sounds like it may be an SELinux issue. 听起来这可能是一个SELinux问题。

With SELinux, you can usually allow apache to make network connections by using 使用SELinux,您通常可以允许apache通过使用来建立网络连接

/usr/sbin/setsebool httpd_can_network_connect 1

OR /usr/sbin/setsebool httpd_can_network_connect true 或/ usr / sbin / setsebool httpd_can_network_connect是的

An operating system without SELinux may have a similar protection mechanism. 没有SELinux的操作系统可能具有类似的保护机制。

  1. Check basic network connectivity. 检查基本网络连接。 ping xxx.xxx.xxx.xxx from the webserver. 从网络服务器ping xxx.xxx.xxx.xxx。 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 ? 尝试从网络服务器运行mysql: mysql -h xxx.xxx.xxx.xxx -u user -p如果无法连接,请跟踪原因:它可能是两端的SELinux问题(检查安全日志),或者也许在/etc/hosts有一个别名? Also enable verbose messages with -v -v -v on the mysql command line. 还要在mysql命令行上使用-v -v -v启用详细消息。
  3. Check that the user is allowed to log on using the user administration feature of mysql. 检查是否允许用户使用mysql的用户管理功能登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM