gpt4 book ai didi

php - 将 MySQL 数据库移动到远程服务器后出现 "access denied for user"

转载 作者:行者123 更新时间:2023-11-29 18:31:50 25 4
gpt4 key购买 nike

我在访问数据库时遇到一些问题。

该脚本之前在我的本地主机上运行过。我将其导入到另一台服务器中,另一台服务器给我一条访问被拒绝的消息。

给出的消息是:用户'root'@'10.4.1.163'的访问被拒绝(使用密码:YES)

我使用的脚本是:

<?php
// Connect to database server
mysql_connect("localhost", "root", "password") or die (mysql_error ());

// Select database
mysql_select_db("database") or die(mysql_error());

// SQL query
$strSQL = "SELECT * FROM users WHERE user_id='". $_SESSION['USER_ID'] ."'";

// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);

// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {

// Write the value of the column FirstName (which is now in the array $row)
echo $row['Name'] . " ";
}

// Close the database connection
mysql_close();
?>

我还尝试将 localhost 更改为 IP 地址 10.4.1.163。

我的脚本出了什么问题。我确信我使用的密码是正确的。

有人知道我该如何解决这个问题吗?

最佳答案

MySQL 权限基于它们所连接的地址以及用户。因此 root@localhost 和 root@10.4.1.163 将拥有两组独立的权限。如果您的代码和数据库位于同一服务器上,则将 localhost 更改为 num8er 提到的 127.0.0.1 可能会起作用。

如果您可以终端访问 php 所在的框,您可以尝试直接连接,使用以下命令排除与 php 相关的任何内容:

mysql -h 10.4.1.163 -u root -p[pass] database -e "SHOW TABLES"

请注意,-p 和密码之间没有空格。如果成功,您将获得数据库中的表列表。

要向其他用户或其他主机名/IP 授予访问权限,您将需要运行以下内容:(尽管您实际上应该根据您的要求创建一个具有更严格权限的单独用户)。

GRANT ALL PRIVILEGES ON `database`.* TO 'root'@'10.4.1.163';

在此处查看有关 MySQL GRANT 的文档 - http://dev.mysql.com/doc/refman/5.7/en/grant.html

顺便说一句 - 拜托,拜托,请不要在没有事先使用 mysql_real_escape_string ( http://php.net/manual/en/function.mysql-real-escape-string.php ) 的情况下将任何旧数据注入(inject)到查询中。您还可以研究 PDO ( http://php.net/manual/en/book.pdo.php ),它通常优于现已弃用的 mysql_ 函数

关于php - 将 MySQL 数据库移动到远程服务器后出现 "access denied for user",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45615367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com