![PHP 和 MySQL 远程主机](https://linux22.com/image/493511/PHP%20%E5%92%8C%20MySQL%20%E8%BF%9C%E7%A8%8B%E4%B8%BB%E6%9C%BA.png)
如何运行需要连接到远程主机的 MySQL 的 PHP 脚本?
当我运行这个时:
$dbHost = "example.com";
$dbUsername = "root";
$dbPassword = "pass";
$dbDatabase = "some_db";
$dbi = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $dbi) or die ("Could not select database.");
...我得到:
Warning: mysql_connect(): Access denied for user 'root'@'10.1.1.1' (using password: YES) in /home/foo/bar.php on line 9
我应该更改哪些设置?
答案1
默认情况下,MySQL 用户仅允许从本地主机访问数据库。您需要使用以下命令添加从您的 IP 或所有 IP 的访问权限授予。
GRANT USAGE ON *.* to [email protected] IDENTIFIED BY 'password for user';
GRANT ALL PRIVILEGES ON foodatabasename.* TO [email protected];
FLUSH PRIVILEGES;