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

当我尝试登录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 ?是的,我尝试了安装提示,我在配置文件中找不到这些变量。


当前回答

对于那些在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!

其他回答

不需要sudo

数据库初始化时使用2个全特权帐户:第一个帐户是“root”,无法访问,第二个帐户是您的用户名(使用命令whoami检查)。

要访问root帐户,您需要使用您的用户名登录

mysql -u $(whoami)

并手动修改root用户密码

use mysql;
set password for 'root'@'localhost' = password('YOUR_ROOT_PASSWORD_HERE');
flush privileges;
quit

以“root”身份登录

mysql -u root -p

有一个很好的和定期更新的指南,如何为最新的MySQL设置新密码:

如何在Ubuntu 20.04上安装MySQL

最好从上面的页面中阅读整个主题,但简而言之,这可能会有所帮助。

运行安全脚本:

sudo mysql_secure_installation

mysql_secure_installation的详细信息

之后,您可以按照以下步骤修改密码:

sudo mysql

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH cache_sha2_password BY 'password'

修改root用户密码的详细信息

如果遇到问题,可能需要重新安装MySQL。

这适用于我的mysql版本15.1:

$ sudo mysql

MariaDB [mysql]> use mysql;
MariaDB [mysql]> set password for 'root'@'localhost' = password('YOUR_ROOT_PASSWORD_HERE');
MariaDB [mysql]> flush privileges;
MariaDB [mysql]> quit

以“root”身份登录

mysql -u root -p

归功于Raoul HATTERER

我也是第一次遇到同样的问题。

现在它是固定的:

首先,复制/etc/mysql/mysql.conf.d/mysqld.cnf文件并粘贴到/etc/mysql/my.cnf

你可以通过命令:

sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/my.cnf

现在让我们重置密码:

在终端中使用以下命令:

sudo service mysql stop
sudo service mysql start
sudo mysql -u root

现在您在MySQL控制台中。

然后让我们写一些查询来重置我们的根密码:

USE mysql
update mysql.user set authentication_string=password('newpass') where user='root' and Host ='localhost';
update user set plugin="mysql_native_password";
flush privileges;
quit

现在我们可以清理/etc/mysql/my.cng

在编辑器中打开上面的文件,删除文件中的所有行。

之后,让我们重新启动MySQL:

sudo mysql service restart

现在让我们使用MySQL和新创建的密码:

sudo mysql -u root -p

最后输入新创建的密码。

这种情况也发生在我身上。问题出在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插件。