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

当我尝试登录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!

其他回答

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

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

现在它是固定的:

首先,复制/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

最后输入新创建的密码。

操作系统:Ubuntu 18.04(仿生海狸)

MySQL: 5.7

Add the skip-grant-tables to the end of file mysqld.cnf Copy the my.cnf file sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/my.cnf 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 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 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> 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)! 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)

Note

你应该知道你的错误是由什么引起的?validate_password_policy吗?

您应该决定重置密码以填写策略或更改策略。

首先

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      |
+--------------------------------------+--------+

我们需要改变这些行:

validate_password.length validate_password.number_count validate_password.policy 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用户的新密码;

你想用root用户访问MySQL,但是你没有提供root用户的正确密码。

如果你需要为root用户设置一个新密码,MySQL的网站上有很多关于如何做的文档:B.3.3.2如何重置root用户密码

我不会在这里展示这个过程,因为上面链接的MySQL文档是清晰而简洁的。