前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL提示ERROR 1698 (28000): Access denied for user ‘root’@’localhost’错误解决办法

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

作者头像
星哥玩云
发布2022-08-18 15:21:13
1.6K0
发布2022-08-18 15:21:13
举报
文章被收录于专栏:开源部署开源部署

测试的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
MySQL提示ERROR 1698 (28000): Access denied for user

安装完成后,登录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
MySQL提示ERROR 1698 (28000): Access denied for user

因为安装的过程中没让设置密码,可能密码为空,但无论如何都进不去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
MySQL提示ERROR 1698 (28000): Access denied for user

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

保存:wq,退出。

输入如下命令:

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

重新启动MySQL。如下图:

MySQL提示ERROR 1698 (28000): Access denied for user
MySQL提示ERROR 1698 (28000): Access denied for user

第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
MySQL提示ERROR 1698 (28000): Access denied for user

然后输入quit,退出MySQL

第3步:

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

MySQL提示ERROR 1698 (28000): Access denied for user
MySQL提示ERROR 1698 (28000): Access denied for user

再返回终端输入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
MySQL提示ERROR 1698 (28000): Access denied for user

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

MySQL提示ERROR 1698 (28000): Access denied for user
MySQL提示ERROR 1698 (28000): Access denied for 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用户的字段改成功了。如下图:

MySQL提示ERROR 1698 (28000): Access denied for user
MySQL提示ERROR 1698 (28000): Access denied for user

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第1步:
  • 第2步:
  • 第3步:
  • 第4步:
  • 拓展更新:
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档