0
<?php 
$dbh=mysql_connect('187.0.0.0','abcd','1234') or die(mysql_error());
if($dbh)
{
echo "server connected";
}
$db=mysql_select_db('demo') or die(mysql_error());
if($db)
{
echo "database connected";
}
?>

I used this code on another server to check the connectivity but I'm getting an error

Host 'mail.hosting1001.in' is not allowed to connect to this MySQL server

elixenide
  • 44,308
  • 16
  • 74
  • 100
vipin
  • 1

1 Answers1

2

The server has to allow remote connections by the user in question. Use the GRANT syntax to give your user remote permissions (but limit those permissions to the IP address from which you are making the remote connection, assuming it's a fixed address).

Even better, as feeela suggested in the comments: open a secure tunnel between the machines so that you can connect as if you were connecting to the local machine.

Also, please don't use mysql_*; the mysql_* functions are outdated, deprecated, and insecure. Use MySQLi or PDO instead.

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100
  • 1
    Nice problem description, but opening the SQL server sounds like a Bad Idea™. A better idea is to create an SSH tunnel from one server to the other and use the DB as if it were a local DB. – feeela Sep 30 '15 at 13:03
  • I don't know about the permissions. How can i give this? Will you please explain me in detail? – vipin Sep 30 '15 at 13:30
  • @vipin Please see [the documentation for `GRANT` syntax](https://dev.mysql.com/doc/refman/5.5/en/grant.html). For example, you might run the command `GRANT SELECT ON demo.* TO 'abc'@'123.45.67.89' IDENTIFIED BY 'mypass';`, where `demo` is your database, `abc` is your user, `123.45.67.89` is the IP address of the remote machine, and `mypass` is your password. Again, be careful with this. feeela's point is a good one, and that is the better route. – elixenide Sep 30 '15 at 13:50