【问题标题】:SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES) (SQL: select * from `table`)SQLSTATE[HY000] [1045] 用户'homestead'@'localhost'的访问被拒绝(使用密码:YES)(SQL:select * from `table`)
【发布时间】:2017-07-25 08:49:37
【问题描述】:

我正在使用 Laravel,并且我已经配置了 .env 文件。当我进行迁移并迁移时,它会影响数据库,但是当我尝试从同一个数据库中读取时,会出现此异常。谢谢

SQLSTATE[HY000] [1045] 用户 'homestead'@'localhost' 的访问被拒绝 (使用密码:YES) (SQL: select * from table)

【问题讨论】:

  • 我认为你没有桌子的权限。

标签: php sql laravel


【解决方案1】:

您必须设置正确的权限:

$ mysql -u root -p
$ <your-password>
$ use mysql
$ GRANT ALL ON *.* to 'homestead'@'localhost' IDENTIFIED BY '<your-password>';
$ FLUSH PRIVILEGES;

【讨论】:

  • @sanduniYW 你必须在 MySQL 中设置权限,你可以使用上面列出的命令。
【解决方案2】:

如果是 wampserver 或 xampp 的新安装或新升级,您需要将端口包含在连接中,

因为默认的 MySQL 端口是 3306,

但是 mariadb 在默认 (3306) 端口上默认出现,所以当您尝试连接 MySQL 时,它会重定向到 mariadb。

您需要更正您的端口,它可能是 3306 或 3307 或 3308 并使用 ip 而不是 localhost。你的最终代码:

$host = '127.0.0.1';
$db   = 'test';
$user = 'root';
$pass = '';
$port = '3308';
$charset = 'utf8mb4';

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
    $mysqli = mysqli_connect($host, $user, $pass, $port, $db);
    mysqli_set_charset($mysqli, $charset);
} catch (\mysqli_sql_exception $e) {
     throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}

Solved question here

对于 pdo 和 docker :

See these answers

【讨论】:

    猜你喜欢
    • 2018-07-03
    • 2017-08-10
    • 2019-07-23
    • 2023-03-08
    • 2017-08-08
    • 2020-11-16
    • 2015-06-04
    • 2016-12-16
    • 2017-07-18
    相关资源
    最近更新 更多