简体   繁体   中英

Can't open connection to remote database

I have php script running on XAMPP on my local PC and I want to access some databse other than one on my localhost. Even if I call script with my own IP address I can't connect to the database.

PHP script looks like:

<?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);


?>

I call this script as:

http://zavrsni.noip.me/dohvat.php?username=root&pass=ficko1&database=zavrsni&host=zavrsni.noip.me

And then I get error:

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.

I know that this is not the most secure way of connecting the database, but I'm new to PHP so don't judge the terrible code.

Your database is not open for connections from outside. Log in to the server on which the database resides, log in to your database as root and then enter:

USE zavrsni;
GRANT ALL PRIVILEGES ON * to root@% identified by 'ficko1';

or better

GRANT SELECT ON * to root@% identified by 'ficko1';

Please note that it global grants are highly unsure. Learn how to use the privilege system and later grant only those privileges which are actually needed.

If this still does not work, your mysql server is not permitting connections from outside. Have a look at http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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