前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL ‘root’@’localhost’无法登录解决

MySQL ‘root’@’localhost’无法登录解决

作者头像
星哥玩云
发布2022-08-16 15:17:52
4.5K0
发布2022-08-16 15:17:52
举报
文章被收录于专栏:开源部署

今天早上同事说MySQL root账号登录不上了。我试了一下 

#mysql -u root -p 

提示”Access denied for user ‘root’@’localhost’ (using password: YES)”

因为年后有同事离职,我第一反应是谁修改了root密码?按照忘记root密码来重置一下密码: 

#/etc/init.d/mysql stop 

#mysqld_safe –skip-grant-tables & 

#mysql -uroot -p 

mysql>update mysql.user set password=password(‘mypassword’) where user=’root’; 

mysql>flush privileges; 

mysql>quit

用新密码还是无法登录,提示跟上面一样。换一个非root账号登录,查看一下user表: 

mysql> select user,host from user; 

+———–+———+ 

| user   | host | 

+———–+———+ 

| root  | 127.0.0.1 | 

| night | % | 

+———–+———+

怀疑默认的localhost没有映射到127.0.0.1?试试#mysql -u root -p xxxx -h 127.0.0.1,果然可以登录。 

之前配置数据库的同学没有给’root’@’localhost’和’root’@’ip’授权。 

grant all privileges on . to ‘root’@’localhost’ identified by ‘mypassword’ with grant option; 

grant all privileges on . to ‘root’@’118.192.91.xxx’ identified by ‘mypassword’ with grant option;

再查询一下用户表: 

然后#mysql -u root -p xxxx,登录成功!

查了一下mysql -h localhost和mysql -h 127.0.0.1的区别,通过localhost连接到mysql是使用UNIX socket,而通过127.0.0.1连接到mysql是使用TCP/IP。看看状态: 

mysql -h localhost > status 

Connection id:     639 

Current database: mysql 

Current user:   root@localhost 

SSL:           Not in use 

Current pager: stdout 

Using outfile:        ” 

Using delimiter:    ; 

Server version:     5.6.15-log Source distribution 

Protocol version: 10 

Connection:    Localhost via UNIX socket

mysql -h 127.0.0.1 > status 

Connection id:     640 

Current database: mysql 

Current user:   root@localhost 

SSL:           Not in use 

Current pager: stdout 

Using outfile:        ” 

Using delimiter:    ; 

Server version:     5.6.15-log Source distribution 

Protocol version: 10 

Connection:   127.0.0.1 via TCP/IP

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

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

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

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

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