我在我的本地PC上的XAMPP上运行了php脚本,我想访问除localhost之外的一些数据库。即使我用自己的IP地址调用脚本,我也无法连接到数据库。
PHP脚本如下所示:
<?php
$host = $_GET['host'];
$username = $_GET['username'];
$pass = $_GET['pass'];
$database = $_GET['database'];
$con = mysql_connect($host,$username,$pass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("performance_schema", $con);
$zavrsni = "zavrsni";
$result = mysql_query("SELECT `OBJECT_NAME`,`COUNT_INSERT`,`AVG_TIMER_INSERT`,`COUNT_UPDATE`,`AVG_TIMER_UPDATE`,`COUNT_DELETE`,`AVG_TIMER_DELETE` FROM `table_io_waits_summary_by_table` where `OBJECT_SCHEMA` =\"".$database."\"");
while($row = mysql_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
mysql_close($con);
?>
我将此脚本称为:
http://zavrsni.noip.me/dohvat.php?username=root&pass=ficko1&database=zavrsni&host=zavrsni.noip.me
然后我收到错误:
Warning: mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\dohvat.php on line 8
Could not connect: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
我知道这不是连接数据库最安全的方式,但我是PHP的新手,所以不要判断可怕的代码。
答案 0 :(得分:0)
您的数据库无法从外部进行连接。 登录数据库所在的服务器,以root身份登录数据库,然后输入:
USE zavrsni;
GRANT ALL PRIVILEGES ON * to root@% identified by 'ficko1';
或更好
GRANT SELECT ON * to root@% identified by 'ficko1';
请注意,全球拨款非常不确定。了解如何使用权限系统,稍后仅授予实际需要的权限。
如果仍然无效,则您的mysql服务器不允许来自外部的连接。看看http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html