温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL 'root'@'localhost'无法登录

发布时间:2020-09-22 19:17:18 来源:网络 阅读:2120 作者:Mr_sheng 栏目:MySQL数据库

今天早上同事说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 'root'@'localhost'无法登录

然后#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



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI