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

604

我正在搭建一个新服务器,但遇到了这个问题。

当我尝试使用root用户登录MySQL数据库时,会出现以下错误:

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

无论是通过终端(SSH)、phpMyAdmin还是MySQL客户端,比如Navicat,都无法连接成功。

我查看了mysql.user表并得到以下结果:

+------------------+-------------------+
| 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.1LTS(Xenial Xerus)与Apache、MySQL和PHP一起使用,以便它可以托管网站,以及iRedMail 0.9.5-1,以便它可以托管邮件。

在安装iRedMail之前,登录MySQL数据库没有问题。我还尝试仅安装iRedMail,但是root也无法工作。

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


4
我点击了这个链接,第一个选项对我有用:https://askubuntu.com/questions/763336/cannot-enter-phpmyadmin-as-root-mysql-5-7 - Mikael Arhelger
21
"sudo mysql_secure_installation" 是最简单的修复方法。 - Sergey Ponomarev
在2022年3月2日,这是“MySQL客户端ERROR 1698 28000用户访问被拒绝”的*头号搜索引擎结果。 - Peter Mortensen
23个回答

6

对于第一次

sudo mysql -u root -p

SHOW VARIABLES LIKE 'validate_password%';

我们将会看到类似于这样的内容:
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| validate_password.dictionary_file    |        |
| validate_password.length             | 8      |
| validate_password.mixed_case_count   | 1      |
| validate_password.number_count       | 1      |
| validate_password.policy             | MEDIUM |
| validate_password.special_char_count | 1      |
+--------------------------------------+--------+

我们需要更改以下行:

  1. validate_password.length(密码长度)
  2. validate_password.number_count(数字数量)
  3. validate_password.policy(密码策略)
  4. validate_password.special_char_count(特殊字符数量)
SET GLOBAL validate_password.policy=LOW;
SET GLOBAL validate_password.length=4;
SET GLOBAL validate_password.number_count=0;
SET GLOBAL validate_password.special_char_count=0;

SHOW VARIABLES LIKE 'validate_password%';

我们将会看到:
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password.check_user_name    | ON    |
| validate_password.dictionary_file    |       |
| validate_password.length             | 4     |
| validate_password.mixed_case_count   | 1     |
| validate_password.number_count       | 0     |
| validate_password.policy             | LOW   |
| validate_password.special_char_count | 0     |
+--------------------------------------+-------+

现在退出MySQL客户端:

exit;
sudo mysql -u root -p

现在你可以输入你的密码,只需四个或更多字母。

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new-password';

exit;

sudo mysql -u root -p

您的新密码已存储在用户“root”的数据库中;


5
在我的情况下,
mysql -u root -p

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

我确定我的密码是正确的。否则,错误代码将会是ERROR 1045 (28000): Access denied for user

所以我使用sudo重新登录,

sudo mysql -u root -p

这次对我有效。请查看该文档
然后更改根密码,
mysql> alter user 'root'@'%' identified with mysql_native_password by 'me123';
Query OK, 0 rows affected (0.14 sec)

mysql>

然后使用sudo /etc/init.d/mysql restart重新启动服务器。


4

这对我来说适用于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

这对我有用 - 我忘记了登录方法需要从auth socket更改为password。我发现它会定期自行更改回去,不确定原因。 - Leon Segal

4

这个对我有效:

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

即使有链接,也需要解释。例如,第一行的想法/要点是什么?为什么之前的答案不起作用?使用了哪个版本的MySQL?在什么系统上?在什么情况下?来自帮助中心“...始终解释为什么您提出的解决方案是适当的以及它如何工作”。请通过编辑(更改)您的答案进行回复,而不是在此处进行评论(不要添加“编辑:”,“更新:”或类似内容 - 答案应该看起来像今天写的)。 - Peter Mortensen
你是对的@PeterMortensen。我不知道为什么我首先发布了它。它似乎与zetacu的第二个选项非常相似。请让我知道您已经阅读了我的评论,然后我将删除我的答案。 - Dorad

3
第一步:进入文件/etc/phpmyadmin/config.inc.php,然后取消注释"AllowNoPassword"所在的行。
第二步:登录您的MySQL默认帐户。
mysql -u root -p

use mysql;
update user set plugin="" where user='root';
flush privileges;

这就是全部内容!


假设是什么?已经安装了 phpMyAdmin 吗?这是在什么上下文中尝试的?操作系统/发行版/版本是什么?云端?VPS?自己的服务器?自己的工作站?安装了什么?例如,在桌面版 Ubuntu 18.04(Bionic Beaver)上,即使已经安装了 Apache、PHP 和 MariaDB,文件夹 /etc/phpmyadmin 默认情况下也不存在。 - Peter Mortensen
请通过编辑(更改)您的问题/答案来回复,而不是在评论中回复(不要包含“编辑:”,“更新:”或类似内容 - 答案应该看起来像今天写的)。 - Peter Mortensen

3

操作系统:Ubuntu 18.04(倍洛克海狸)

MySQL:5.7

  1. Add the skip-grant-tables to the end of file mysqld.cnf

  2. Copy the my.cnf file

    sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/my.cnf
    
  3. Reset the password

    (base) ➜  ~ sudo service mysql stop
    (base) ➜  ~ sudo service mysql start
    (base) ➜  ~ mysql -uroot
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)
    
    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> use mysql
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed, 3 warnings
    mysql> update mysql.user set authentication_string=password('newpass') where user='root' and Host ='localhost';
    Query OK, 1 row affected, 1 warning (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 1
    
    mysql> update user set plugin="mysql_native_password";
    Query OK, 0 rows affected (0.00 sec)
    Rows matched: 4  Changed: 0  Warnings: 0
    
    mysql>  flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> quit
    Bye
    
  4. Remove the skip-grant-tables from my.cnf

    (base) ➜  ~ sudo emacs /etc/mysql/mysql.conf.d/mysqld.cnf
    (base) ➜  ~ sudo emacs /etc/mysql/my.cnf
    (base) ➜  ~ sudo service mysql restart
    
  5. Open the MySQL client

    (base) ➜  ~ mysql -uroot -ppassword
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)
    
    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>
    
  6. Check the password policy

    mysql> select @@validate_password_policy;
    +----------------------------+
    | @@validate_password_policy |
    +----------------------------+
    | MEDIUM                     |
    +----------------------------+
    1 row in set (0.00 sec)
    
    
    mysql> SHOW VARIABLES LIKE 'validate_password%';
    +--------------------------------------+--------+
    | Variable_name                        | Value  |
    +--------------------------------------+--------+
    | validate_password_dictionary_file    |        |
    | validate_password_length             | 8      |
    | validate_password_mixed_case_count   | 1      |
    | validate_password_number_count       | 1      |
    | validate_password_policy             | MEDIUM |
    | validate_password_special_char_count | 1      |
    +--------------------------------------+--------+
    6 rows in set (0.08 sec)!
    
  7. Change the configuration of the validate_password

    mysql> set global validate_password_policy=0;
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> set global validate_password_mixed_case_count=0;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> set global validate_password_number_count=3;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> set global validate_password_special_char_count=0;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> set global validate_password_length=3;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> SHOW VARIABLES LIKE 'validate_password%';
    +--------------------------------------+-------+
    | Variable_name                        | Value |
    +--------------------------------------+-------+
    | validate_password_dictionary_file    |       |
    | validate_password_length             | 3     |
    | validate_password_mixed_case_count   | 0     |
    | validate_password_number_count       | 3     |
    | validate_password_policy             | LOW   |
    | validate_password_special_char_count | 0     |
    +--------------------------------------+-------+
    6 rows in set (0.00 sec)
    

注意

您应该知道造成错误的原因是什么?是由于validate_password_policy吗?

您应该决定重置您的密码以符合策略或更改策略。


3

在mysql Ver 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


2

@zetacu和@peter给出的答案非常准确,但是其中只有部分对我有效。这里提供一下给使用该技术的用户。

mysql  Ver 8.0.30-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))

因此,我的用户表看起来像这样:

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 |
| pk               | localhost | auth_socket           |
| root             | localhost | auth_socket           |
+------------------+-----------+-----------------------+
6 rows in set (0.00 sec)

首先,我按照第二个(推荐的)选项替换了YOUR_SYSTEM_USER为您的用户名。所以,我使用相同的方法创建了一个新用户,但是仍然没有效果。

然后,我尝试了第一种方法,将root用户设置为使用my_native_password插件:

sudo mysql -u root
mysql> USE MySQL;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE 
User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;

sudo service mysql restart

它起作用了!!!因此,只需创建一个新用户并使用my_native_password插件。


1

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

0

我也遇到过这个问题。问题出在Linux发行版自带的MySQL存储库上。因此,当你简单地执行以下操作时:

sudo apt install mysql-server

它从默认存储库安装MySQL,这会导致问题。因此,为了克服这个问题,您需要卸载已安装的MySQL:

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

然后从官方MySQL网站MySQL APT repository下载MySQL存储库。

按照他们的文档添加存储库并安装。这不会出现任何问题。

另外,正如zetacu回答的那样,您可以验证MySQL root用户现在确实使用mysql_native_password插件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接