繁体   English   中英

无法打开与远程数据库的连接

[英]Can't open connection to remote database

我在本地PC上的XAMPP上运行了php脚本,我想访问一些数据库,而不是在本地主机上。 即使我使用自己的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新手,所以请不要判断糟糕的代码。

您的数据库未打开以进行外部连接。 登录到数据库所在的服务器,以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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM