php 无法连接到 mysql,错误 13(但命令行可以) [英] php can't connect to mysql with error 13 (but command line can)

查看:22
本文介绍了php 无法连接到 mysql,错误 13(但命令行可以)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在新安装的服务器上出现了奇怪的情况,这次谷歌似乎帮不了我了.我无法从我的 php 代码连接到(远程)mysql.当我尝试从同一台服务器上的命令行连接时,连接成功.

I have weird situation in newly installed server, and it seems that Google can't help me this time. I can't connect to (remote) mysql from my php-code. When I try to connect from command line on the same server the connection succseds.

无法连接:无法连接到'MYSQL.SERVER' 上的 MySQL 服务器 (13)

Could not connect: Can't connect to MySQL server on 'MYSQL.SERVER' (13)

这是来自命令行的代码和连接尝试

Here is the code and the connect attempt from the command line

[u1@bosko httpdocs]$ cat  test.php

<?
$link = mysql_connect('MYSQL.SERVER', 'testusersimon', '123456');
if (!$link) {
    die('Could not connect: ' .  mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

[u1@bosko httpdocs]$ mysql -h MYSQL.SERVER -utestusersimon --password=123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 352108
Server version: 5.0.45-community-nt-log MySQL Community Edition (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye

我尝试在 mod_php 模式和 FastCGI 中运行 php 脚本,检查/etc/php.d/mysql.ini"是否出现在 phpinfo() 以及 mysql、mysqli 和 pdo_mysql 部分.

I tried running the php script both in mod_php mode and in FastCGI, check that "/etc/php.d/mysql.ini" shows up in the phpinfo() as well as mysql,mysqli and pdo_mysql sections.

但结果是一样的,我知道它很简单,但我就是做不到.请帮忙:)

but the result was the same, I know its something simple but I just can't . Please help :)

问题出在 SElinux

The problem was with SElinux

setsebool -P httpd_can_network_connect_db=1

是解决方案.

推荐答案

setsebool -P httpd_can_network_connect=1

对于许多访问此问题的人来说也是一个有用的 CLI 命令,为了允许从 HTTP (Apache) 请求中的 mysql_connet() 连接到远程 MySQL 数据库服务器,请确保在 SElinux 中启用来自 httpd 的网络连接,通常位于/etc/selinux/config(默认禁用以防止黑客使用您的 httpd 攻击其他机器).

will also be a helpful CLI command to many people visiting this question, as to allow mysql_connet() connections from within HTTP (Apache) requests to a remote MySQL database server, ensure to enable Network Connections from httpd in SElinux usually located in /etc/selinux/config (disabled by default to prevent hackers from attacking other machines using your httpd).

这篇关于php 无法连接到 mysql,错误 13(但命令行可以)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆