我正在设置一个新的服务器,但一直遇到这个问题。

当我尝试登录MySQL数据库与根用户,我得到的错误:

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

不管我是通过终端(SSH)连接,还是通过phpMyAdmin或MySQL客户端(例如Navicat)连接。他们都失败了。

我看了mysql。用户表,得到如下:

+------------------+-------------------+
| user             | host              |
+------------------+-------------------+
| root             | %                 |
| root             | 127.0.0.1         |
| amavisd          | localhost         |
| debian-sys-maint | localhost         |
| iredadmin        | localhost         |
| iredapd          | localhost         |
| mysql.sys        | localhost         |
| phpmyadmin       | localhost         |
| root             | localhost         |
| roundcube        | localhost         |
| vmail            | localhost         |
| vmailadmin       | localhost         |
| amavisd          | test4.folkmann.it |
| iredadmin        | test4.folkmann.it |
| iredapd          | test4.folkmann.it |
| roundcube        | test4.folkmann.it |
| vmail            | test4.folkmann.it |
| vmailadmin       | test4.folkmann.it |
+------------------+-------------------+

如您所见,root用户应该具有访问权限。

服务器非常简单,因为我已经尝试了一段时间来解决这个问题。

它运行Ubuntu 16.04.1 LTS (Xenial Xerus)和Apache, MySQL和PHP,这样它就可以托管网站,iRedMail 0.9.5-1,这样它就可以托管邮件。

在安装iRedMail之前,登录MySQL数据库工作正常。我也试过只安装iRedMail,但根也不能用。

如何解决MySQL登录问题,或者如何在现有的MySQL安装上安装iRedMail ?是的,我尝试了安装提示,我在配置文件中找不到这些变量。


当前回答

这适用于我的MySQL版本8.0.26和Ubuntu 20.04 (Focal Fossa)。

sudo mysql -u root

mysql> USE mysql;
mysql> SELECT User, Host, plugin FROM mysql.user;

+------------------+-----------+-----------------------+
| User             | Host      | plugin                |
+------------------+-----------+-----------------------+
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | auth_socket           |
+------------------+-----------+-----------------------+

mysql> UPDATE user SET

plugin='caching_sha2_password' WHERE User='root';

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'you_mysql_password';
mysql> FLUSH PRIVILEGES;
mysql> exit;

sudo service mysql restart

其他回答

这招对我很管用:

mysql --user=root mysql
CREATE USER 'some_user'@'%' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'some_user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

我在Debian 8 (Jessie)虚拟机上遇到了这个问题,我在Windows 10桌面上通过PuTTY进行交互。

我尝试了这里的各种建议,但都不太管用,我正在Debian主机上运行MariaDB。最后,我发现我无法在安全模式下启动数据库服务器,但我不需要这样做,下面的命令实际上对我有用,即允许新创建的MySQL用户登录到MySQL/MariaDB服务器:

sudo service mysql restart
sudo mysql # Logs in automatically into MariaDB
use mysql;
update user set plugin='' where user='your_user_name';
flush privileges;
exit;
sudo service mysql restart # Restarts the MySQL service

如果上面的方法对你不太管用,那就按照zetacu的帖子中列出的步骤来做,然后再按照我的步骤来做。

现在你应该能够使用远程终端客户端,并安全地登录到MySQL使用命令:

mysql -u your_user_name -p

*在提示时输入密码

对于那些在macOS上安装最新MariaDB并遵循MariaDB文档中的本教程的用户,请运行:

sudo mariadb-secure-installation

而不是只给出mariadb-secure-installation命令。否则,运气不好,尽管出现错误提示:

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Enter current password for root (enter for none):
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Enter current password for root (enter for none):
Aborting!

这种情况也发生在我身上。问题出在Linux发行版自带的MySQL存储库上。所以当你简单地这样做时:

sudo apt install mysql-server

它从默认的存储库安装MySQL,这就产生了这个问题。所以为了克服这个问题,你需要卸载已经安装好的MySQL:

sudo apt remove mysql* --purge --auto-remove

请在MySQL官方网站“MySQL APT repository”下载。

请参考他们的文档,了解如何添加存储库和 安装它。这没有问题。

正如zetacu所回答的,您可以验证MySQL根用户现在确实使用mysql_native_password插件。

我建议删除MySQL连接-

这是MySQL 5.5版。如果你的版本不同,请相应地更改第一行。

sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.5 mysql-client-core-5.5
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean

然后再次安装,但是这次你自己设置root密码。 这将节省很多精力。

sudo apt-get update
sudo apt-get install mysql-server