簡體   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