阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL提示ERROR 1698 (28000): Access denied for user ‘root’@’localhost’错误解决办法

97次阅读
没有评论

共计 2127 个字符,预计需要花费 6 分钟才能阅读完成。

测试的 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 提示 ERROR 1698 (28000): Access denied for user'root'@'localhost'错误解决办法

安装完成后,登录 MySQL 时出现如下错误:

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

MySQL 提示 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 提示 ERROR 1698 (28000): Access denied for user'root'@'localhost'错误解决办法

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

保存:wq,退出。

输入如下命令:

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

重新启动 MySQL。如下图:

MySQL 提示 ERROR 1698 (28000): Access denied for user'root'@'localhost'错误解决办法

第 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;  #然后回车

结果如下图:

MySQL 提示 ERROR 1698 (28000): Access denied for user'root'@'localhost'错误解决办法

然后输入 quit,退出 MySQL

第 3 步:

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

MySQL 提示 ERROR 1698 (28000): Access denied for user'root'@'localhost'错误解决办法

再返回终端输入 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

MySQL 提示 ERROR 1698 (28000): Access denied for user'root'@'localhost'错误解决办法

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

MySQL 提示 ERROR 1698 (28000): Access denied for user'root'@'localhost'错误解决办法

从图中可以看到在执行了 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 用户的字段改成功了。如下图:

MySQL 提示 ERROR 1698 (28000): Access denied for user'root'@'localhost'错误解决办法

最后 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,然后再执行上面修改密码的语句。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2127字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中