ERROR 1698 (28000): Access denied for user 'root'@'localhost' (后面没有多余的东西) -------解决方法

这个问题很多安装配置教程里都没有提到,找了好久好久。。。
转载一下。

转载:https://www.linuxidc.com/Linux/2019-08/159900.htm


测试的Linux操作系统是Ubuntu 18.04 LTS,MySQL版本如下:

linuxidc@linuxidc:~/www.linuxidc.com$ mysql --version
mysql Ver 14.14 Distrib 5.7.27, for Linux (x86_64) using EditLine wrapper

在这里插入图片描述
安装完成后,登录MySQL时出现如下错误:

linuxidc@linuxidc:~/www.linuxidc.com$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user ‘root’@‘localhost’

在这里插入图片描述

因为安装的过程中没让设置密码,可能密码为空,但无论如何都进不去MySQL。

那么该怎么做呢,接下来就将这个解决方法总结记录一下。

第1步:
在Ubuntu终端输入如下命令

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

进入到mysqld.cnf配置文件,然后在这个配置文件中的[mysqld]这一块中加入skip-grant-tables这句话。

[mysqld]

# * Basic Settings

user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
skip-grant-tables

在这里插入图片描述

作用:就是让你可以不用密码登录进去MySQL。

保存:wq,退出。

输入如下命令:

linuxidc@linuxidc:~/www.linuxidc.com$ service mysql restart

重新启动MySQL。如下图:

在这里插入图片描述

第2步:
在Ubuntu终端上输入

linuxidc@linuxidc:~/www.linuxidc.com$ mysql -u root -p

遇见输入密码的提示直接回车即可,进入MySQL后,分别执行下面三句话:

linuxidc@linuxidc:~/www.linuxidc.com$ use mysql; #然后回车

linuxidc@linuxidc:~/www.linuxidc.com$ update user set authentication_string=password(“linuxidc”) where user=“root”; #然后回

说明:本例的密码是linuxidc

linuxidc@linuxidc:~/www.linuxidc.com$ flush privileges; #然后回车

结果如下图:

在这里插入图片描述

然后输入quit,退出MySQL

第3步:
重新进入到mysqld.cnf文件中去把刚开始加的skip-grant-tables这条语句给注释掉。如下图:

在这里插入图片描述
再返回终端输入mysql -u root -p,应该就可以进入数据库了。

第4步:
如果此时还是报出错误如下

linuxidc@linuxidc:~/www.linuxidc.com$ mysql -u root -p
Enter password:
ERROR 1524 (HY000): Plugin ‘auth_socket’ is not loaded

在这里插入图片描述

那么就需要返回第3步中,把注释掉的那条语句重新生效(就是删除#符号),重新进入mysql中,先选择一个数据库(use mysql;),然后输入select user,plugin from user;,看下图:

在这里插入图片描述

从图中可以看到在执行了select user,plugin from user;后,错误原因是因为plugin root的字段是auth_socket,那我们改掉它为下面的mysql_native_password就行了。输入:

update user set authentication_string=password(“linuxidc”),plugin=‘mysql_native_password’ where user=‘root’;

然后回车执行以下,再输入select user,plugin from user;回车,我们能看到root用户的字段改成功了。如下图:

在这里插入图片描述

最后quit退出。返回执行第3步。

那么这个问题就完全解决了。

拓展更新:
在MySQL 8版本中,上面更新代码的语句似乎有所变化,那个句法会被告知是错误的,这里我贴一下没有语法错误的:

ALTER user ‘root’@‘localhost’ IDENTIFIED BY ‘newpassward’; //newpassward 新密码

将这句话对应到上面第二步即可。

如果执行本语句出现The MySQL server is running with the --skip-grant-tables option so it cannot execute this statemen这个错误,解决如下:

先flush privileges,然后再执行上面修改密码的语句。


刚好下面的错误我也遇到了。完美。

发布了75 篇原创文章 · 获赞 26 · 访问量 7663

猜你喜欢

转载自blog.csdn.net/qq_40962234/article/details/104596860