SQL 权限行为异常

SQL permissions behaving unexpectedly

最近开始学习php和sql,因为对前端有一定了解,想加强对后端系统的理解。我买了一个 raspberry pi,并设置了一个简单的 LAMP 服务器,效果很好(差不多)。我可以 运行 php 形成它,这是一个好的开始。

但是,我安装了 MariaDB v10.3.22,但遇到了很多问题。安装成功后,我去登录,并期望一个空白密码。那没用。

mysql -u root -p

我收到错误 ERROR 1698 (28000): Access denied for user 'root'@'localhost' 因为是全新安装,所以我尝试了:

mysql_secure_installation

无论如何,我最终发现输入 sudo mysql -u root -p 会让我进入,根本不需要密码。事实上,即使 运行 使用 sudo 进行安全安装也能正常工作,这意味着我不需要输入正确的密码。当 运行 宁 sudo 时,我可以输入任何我想进入 MariaDB shell 和安全脚本的密码,并且它只适用于 root db 用户。为什么是这样?为什么 sudo 可以绕过所有这些,特别是因为我在文档中没有看到 sudo 的任何用法。

我创建了一个具有完全权限的新用户,它工作正常,我不需要 sudo,密码确实有效。

抱歉,如果这让您感到困惑,但这是我所能解释的最好的解释,因为我也很困惑。

谢谢, 安格斯

这可能是因为 root 用户正在使用 Unix 套接字身份验证插件。

作为插件的 documentation 详述:

The unix_socket authentication plugin allows the user to use operating system credentials when connecting to MariaDB via the local Unix socket file.

The unix_socket authentication plugin works by calling the getsockopt system call with the SO_PEERCRED socket option, which allows it to retrieve the uid of the process that is connected to the socket. It is then able to get the user name associated with that uid. Once it has the user name, it will authenticate the connecting user as the MariaDB account that has the same user name.

假设您在 shell 会话中未以 root 身份登录,通过 运行 sudo mysql -u root -p 您以 root 身份执行命令,并且这就是为什么你没有绕过身份验证,它只是按预期使用套接字身份验证。它不需要密码,因为 OS 用户与 MySQL 用户匹配。

您可以通过执行以下操作来检查 root 是否使用 Unix Socket 身份验证:

MariaDB [(none)]> SELECT User, Host, plugin FROM mysql.user;
+------+-----------+-------------+
| User | Host      | plugin      |
+------+-----------+-------------+
| root | localhost | unix_socket |
+------+-----------+-------------+

我还建议您查看 其他关于 MySQL 相同情况的问题。