Linux下MySQL无法在本地以非root用户身份连接数据库

Posted zanyrain

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下MySQL无法在本地以非root用户身份连接数据库相关的知识,希望对你有一定的参考价值。

标题有点罗嗦,目前出现的问题如下:
当以非root身份登录数据库时,会报错

mysql -u root -p
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

而使用root身份访问就没有问题
问题探究
查看root用户使用的认证插件:

mysql> select user,plugin from user where user = 'root';
+------+-------------+
| user | plugin      |
+------+-------------+
| root | auth_socket |
+------+-------------+
1 row in set (0.00 sec)

MySQL使用的auth_socket是通过Unix套接字进行连接认证[1]

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. [2]

因此可以将认证插件改为 mysql_native_password
解决方法

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
FLUSH PRIVILEGES;

另请参阅
[1] https://dev.mysql.com/doc/refman/8.0/en/socket-pluggable-authentication.html
[2] https://mariadb.com/kb/en/authentication-plugin-unix-socket/

以上是关于Linux下MySQL无法在本地以非root用户身份连接数据库的主要内容,如果未能解决你的问题,请参考以下文章

linux系统下使用xampp 丢失mysql root密码 只能远程访问,本地无法连接数据库

解决Linux下普通用户无法使用root连接MySQL服务

无法以 root linux 用户身份运行 mysql 命令

phpMyAdmin : 可以在不输入密码的情况下以非 root 用户身份登录,尽管该帐户有一个

linux学习--文件文本用户

linux学习--文件文本用户练习