php - 无法连接到“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>";
?>
解决方案
推荐阅读
- python - Python 3 - 使用 BS4 刮取内部 Div 的信息
- vba - 如果不同单元格中的某些内容发生变化,如何将单元格更新为今天的日期?
- apache-spark - Google Cloud Dataproc 上的 Pyspark 作业失败
- php - 是否可以按列绑定订单?
- jquery - 堆叠的 div 背景 img 是黑色的
- java - 如何使用java从多个pdf文件中提取信息到文本
- excel - 我不想为已填充数据的单元格运行 VBA 代码
- android - 如何修复约束布局中不必要的空间?
- browser - 无法以两种实现方式之一从我们的网站下载文件
- angular - Angular CLI 6,通用:从 ExpressJS 请求对象获取 cookie 的正确方法是什么?