首页 > 解决方案 > 无法连接到“XX.XX.XX.XX”上的 MySQL 服务器 (13)

问题描述

CentOS+MariaDB+PHP+Nginx

在 DB 中有一个测试表,其中包含两个字符串:'qwe','asd''rty','fgh'

有一个测试脚本,如果我从 cmd 启动它就可以了:

$ php test.php 测试页
qwe - asd
rty - fgh

所以,MySQL+PHP 有效!!!

在 nginx 的根目录中有 info.php 文件,它也可以工作(我在浏览器中看到页面)。所以,Nginx+PHP 也可以使用!!!

但是,当我通过 http 请求 test.php 时,浏览器(和 curl 也一样)返回:Can't connect to MySQL server on 'XX.XX.XX.XX' (13)

我使用以下指令安装了 LEMP 两次,结果相同:https ://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos- 7

在互联网上几个小时没有提供任何答案!感谢帮助!

PS如果有人想测试,这里是脚本:

//Table in DB:
create table a_first (request varchar(30), answer varchar(30)) ;
insert into a_first values ('qwe','asd') ;
insert into a_first values ('rty','fgh') ;

//Contains of /usr/share/php/db.php
$servername="ip_address";
$username="mysql_user";
$password="mysql_pass";
$dbname="dbname" ;

//Contains of /usr/share/nginx/html/test.php
<?php
echo "<html><header></header><body>";
require_once 'db.php';

echo "test page" ;
echo "<br>" ;


$conn = mysql_connect($servername, $username, $password);

if (!$conn) {
    die('cani\'t connect: ' . mysql_error());
}

$db_selected = mysql_select_db($dbname, $conn);
if (!$db_selected) {
    die ('can\'t select DB: ' . mysql_error());
}

$sql = "select * from a_first; " ;
$result = mysql_query($sql);

if (!$result) {
    die('SQL error: ' . mysql_error());
}

while ($row = mysql_fetch_assoc($result)) {
    echo $row['request']." - ".$row['answer']." \n";
    echo "<br>\n" ;
}

mysql_free_result($result);

echo "</body></html>";
?>

标签: phpmysqlnginxcentos

解决方案


推荐阅读