简体   繁体   中英

How to connect to database on another server?

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

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.

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