2010-11-02 26 views
52

我在新安装的服务器有奇怪的情况,似乎Google不能帮助我这次。 我无法连接到从我的php代码(远程)的MySQL。当我尝试从同一台服务器上的命令行进行连接时,连接失败。php无法连接到错误13(但命令行可以)的MySQL

无法连接:无法连接到 MySQL服务器 'mysql.server的'(13)

这里是代码,并通过命令行连接尝试

[[email protected] 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); 
?> 

[[email protected] 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部分。

但结果是一样的,我知道它很简单,但我不能。 请帮助:)

编辑: 问题是SELinux的

setsebool -P httpd_can_network_connect_db=1 

是解决办法。

+0

你正在使用哪个操作系统? – Thariama 2010-11-02 13:44:38

+0

Fedora11远程服务器是Win2k8 – SimSimY 2010-11-02 14:05:33

回答

114
setsebool -P httpd_can_network_connect=1 

也将是一个有益的CLI命令,许多人参观了这个问题,才能让mysql_connet()从HTTP内连接(Apache)的请求到远程MySQL数据库服务器上,确保启用从httpd的网络连接中SElinux通常位于/ etc/selinux/config中(默认情况下禁用,以防止黑客利用您的httpd攻击其他机器)。

+0

正如我在之前的评论中所说 - 这确实与SELinux相关。 – SimSimY 2011-09-01 09:38:03

+0

当它尝试命令行它的工作,但是当Apache执行它的错误是“警告:mysql_connect():[2002]权限被拒绝(尝试连接通过tcp://w.x.y.z:3306)”。是的,它与SELinux有关。真的,我们尝试了很多不同的东西。非常感谢你@thesimon – 2012-12-11 06:44:31

+1

我经常关闭selinux,所以只要看到selinux相关的权限错误就不会识别。感谢您的线索。 – pdwalker 2015-10-25 05:05:20

26

在CentOS 6中,可以使用以下(不含-P

setsebool httpd_can_network_connect=1 
+4

你从地狱救了我,非常感谢你 – 2014-08-05 10:09:13

+1

救了我......男人! – skidadon 2015-06-05 03:07:00

2

在Fedora 21与Apache 2/httpd的版本2.6连接到远程MySQL服务器5.6或MariaDB的版本10时使用PHP 5.6版甚至在php代码中指定服务器的FQDN而不是本地主机时,它似乎是连接到本地服务器的问题。

该命令将解决当前会话的权限问题:

setsebool httpd_can_network_connect_db on 

为了使修复永久在随后重新引导你需要这样做:

setsebool -P httpd_can_network_connect_db on 

感谢所有在这个问题上拯救我从“被拒绝的权限”地狱。 :)

相关问题