1698 database auth_error ai_generated true

错误 1698 (28000): 用户 'root'@'localhost' 访问被拒绝

ERROR 1698 (28000): Access denied for user 'root'@'localhost'

ID: database/mysql-access-denied-for-root

其他格式: JSON · Markdown 中文 · English
85%修复率
90%置信度
1证据数
2023-02-14首次发现

版本兼容性

版本状态引入弃用备注
MySQL 8.0.33 active
MySQL 8.0.35 active
MariaDB 10.11.6 active

根因分析

MySQL 的 'root' 用户配置为通过 'auth_socket' 或 'unix_socket' 插件进行身份验证,这要求客户端通过 Unix 套接字以系统 root 用户身份连接,而不是使用密码。

English

MySQL's 'root' user is configured to authenticate via 'auth_socket' or 'unix_socket' plugin, which requires the client to connect from the system root user via Unix socket, not with a password.

generic

官方文档

https://dev.mysql.com/doc/refman/8.0/en/access-denied.html

解决方案

  1. Connect using sudo: `sudo mysql -u root`. Then change the authentication plugin: `ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password'; FLUSH PRIVILEGES;`
  2. Create a new admin user with mysql_native_password: `CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES;`

无效尝试

常见但无效的做法:

  1. 90% 失败

    The command itself fails with the same error because it cannot authenticate as root without the socket plugin.

  2. 50% 失败

    This disables all authentication, creating a severe security risk. It should only be used temporarily for recovery.