Can't open connection to remote database - php

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

Related

Remote MySQL in Disk station connection refused

I am trying to log-in remotely to MySQL server which is running on my Disk station, over the network. I confirm that MySQL is running on the disk station, I have php running on the disk station and I can check via remote connection to disk station phpMyAdmin and MySQL queries are answered.
On my laptop, I have created the following PHP script and when I try to run this script on my laptop, I get the following error message
<?php // login.php
$hn = 'bio12' ; //disk station name
$db = 'mysql' ; //database I want to connect to
$un = 'mysqluser' ; // user name
$pwd = 'abctest123' ; // password for the user
$conn = mysql_connect($hn, $un, $pwd);
if(! $conn){
die('Could not connect') ;
}
echo 'Successfully Connected';
?>
Error message: Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\login.php on line 15
Could not connect
I have tried using the IP address instead of 'bio12' but without success.
Interesting enough, when I use the same code and try to connect to local mysql (I am running XAMP on my laptop) and using the 'localhost' instead of 'bio12' it works just fine.
What am I doing wrong?
You need to allow remote connections. Find line in your mysql config (my.cnf file)
bind-address = 127.0.0.1
and comment it with "#" at the beginning, please also see this thread: How to allow remote connection to mysql

MYSQL Connection not allowing IP Address

When I run the following from my webserver it runs fine:
$ip = "localhost";
$uname = user
$pw = user password
$tb = table name
$dbconn = mysqli_connect($ip, $uname, $pw, $tb) or die("Couldn't connect");
However, when I make the following change, I get the "Couldn't connect" error:
$ip = "X.X.X.X";
Where X is the Public IP of my web server. Even when I change it to:
$ip = "127.0.0.1";
I get the couldn't connect error.
Can anybody think of a reason this would be refusing the connection?
Thanks
EDIT:
I have looked on the server logs and get the following (when I do 127.0.0.1):
[26-Nov-2015 23:51:38 Europe/Moscow] PHP Warning: mysqli_connect(): (28000/1045): Access denied for user '*USERNAME*'#'127.0.0.1' (using password: YES) in /filepath/
Where 'USERNAME' is my db username in the correct format, (cpname_dbuser)
If you are trying to access Mysql from remote location then you have to enable Remote Mysql or Whiltelist your ip
Use mysqli_connect_error() to return last connection error
You need to grant privileges from the IP address of your webserver, open port 3306 on the firewall to allow the IP you are connecting from access to the database and you will need to bind MySQL to the IP address you are connecting to. You can do this in the MySQL configuration file (search for bind-address).
To grant privileges you can log in as MySQL root and type the following...
grant all privileges on <database>.* to <username>#<ip address> identified by 'yourpassword'
Where ip address is the address of the machine you are accessing the database from.

Access denied for user ''#'localhost' to database in php

I'm trying to see all the tables from an external MySQL server. I've its IP address, username, password to connect & query their database. But when I try to query to show all the tables I get error message like this,
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 G:\XAMPP\htdocs\test.php on line 2
Failed to connect to MySQL: Access denied for user ''#'localhost' to database 'thedb'
Here is my code,
<?php
$conn = mysql_connect("202.82.31.11", "abc_user", "abc123");
$select = mysql_select_db("thedb");
if(!$conn || !$select){
echo "Failed to connect to MySQL: " . mysql_error();
}
$sql = "SHOW TABLES";
$result=mysql_query($sql);
echo $result;
?>
I was told to always connect from real IP 52.247.181.57. So what does it mean? Do I've to connect from my real IP? If so, how can I do that? I need to know the procedures. Your help would be much appreciated. Tnx. FYI, the IP here are all dummy/random IP for example purpose.
On the external server, you need to allow (whitelist) your IP address. If you have Cpanel on the remote server, go to Remote MySQL and allow your IP.
Also, on the remote server, the port 3306 must allow incoming connections.
First go on http://whatismyipaddress.com/ and check your IP. If its not 52.247.181.57 then you don't have access to the mySQL server. You may request access to the server administrator via cPanel / Remote MySQL and give him your IP. However if you don't own a personal public IP I don't recommend this version.

Connection to MySQL server lost (error:111)

I have a php file to fetch data from a server
When I try to execute it, I get a message saying:-
Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /home/-/-/public_html/GetData.php on line 3
And line 3 is
$con = mysql_connect("cs.neiu.edu","myusername","mypassword");
Also I've tried by giving the hostname as localhost,but its not working.
You need to configure binding address bind-address in my.cnf and grand access to the database eg. GRANT ALL ON foo.* TO bar#'162.54.10.20' IDENTIFIED BY 'PASSWORD';
That error means connection refused. I am in the same class that you are and obviously I am using the same server, and I would guess that you are entering the wrong password. It may not be the same password that you connect to the server with if you changed your password from what was given in class. I use the following and it works fine....
<?php
$con = mysql_connect('localhost','username','password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databaseName", $con);
..
..
..
mysql_close($con);
?>
Actually i realized that my password was wrong.

how to connect to mysql on host on another computer on network

For
$conn = mysql_connect("192.168.0.235", "root", "") or
die("Could not connect: " . mysql_error());
mysql_select_db("crossdomaintest");
$result = mysql_query("SELECT * FROM testing");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo $row[1].$row[2].$row[3]."<br/>";
}
mysql_free_result($result);
I am getting
Warning: mysql_connect() [function.mysql-connect]: Host '192.168.0.167' is not allowed to connect to this MySQL server in /var/www/test.php on line 3
Could not connect: Host '192.168.0.167' is not allowed to connect to this MySQL server
And my IP is 192.168.0.168 and I am using LAMP on Ubuntu 10.10 and 192.168.0.235 is using XAMPP on windows 7 i can access all sites on ...235 but not access database from my local-server.
Any ideas?
it's not about "how to connect". connecting would be all the same.
it's about how to setup remote server to allow connections from your IP.
do you have root privileges on the db server on 192.168.0.235? if so, you have to run appropriate GRANT PRIVILEGES query on it.
If not - ask a db admin to do that
I'd also advise not to use root user to access remote database. Better create another user and give him appropriate privileges
Did you change to root user access the host "%"?
Sorry for my english i'm french.
Cosmik.

Categories