当前位置:首页 > Mysql教程 > 列表

解决MySQL密码正确却无法本地登录

发布:smiling 来源: PHP粉丝网  添加日期:2014-10-03 22:23:58 浏览: 评论:0 

今天在使用phpmyadmin连接数据库时提示ERROR 1045(28000):Access denied for user \'root\'@\'localhost\'(using password: YES),这个提示是密码不正确,但密码是没有问题的.

MySQL root密码正确,却怎么也无法从本地登录MySQL,提示:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

这里后来经高人指点才发现mysql库中的user表缺少一个root指向host:localhost的数据项,只有一个root指向host:主机名的数据项,故怎么也无法利用root账户登录MySQL.

总结一点就是root账户缺失了访问localhost主机的账户信息,导致无法本地登录。

那有什么办法恢复root登录呢?

这里记录一下今天遇到的纠结事情,首先kill掉MySQL进程然后在启动mysql的参数中加入:

--skip-grant-tables

会发现这时无密码就可以登录mysql了.

当然我们还必须修复root账户丢失的数据项.

这里有两种解决方案:

第一种是因为root账户初始的时候有3条记录,包含root对应localhost,hostname,127.0.0.1三条账户数据,我们可以update host为其他两项中一项为localhost即可。

第二种是直接insert一条记录,host为localhost即可.

总结一下:即使root的host包含了主机名,127.0.0.1那么依然是无法正常登录的,这里必须要有localhost的host才行.

如果上面办法还是无法正常登录我们可尝试另一种办法

在本地用mysql命令直接回车可以进入mysql,但是里面只有test和information_schema数据库,没有mysql等数据库,使用use mysql报如下错:

mysql> use mysql

ERROR 1044 (42000):Access denied for user "@'localhost' to database 'mysql'

意思是说没有指定user,没有权限访问数据库mysql,那么用root登录呢,输入正确的密码报如下错:

[root@228827 ~]# mysql -uroot -p123456

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

密码正确的情况下,mysql数据库已经禁止了root用户在本地的登录权限了.

使用root用户通过主机127.0.0.1登录就可以正常进入mysql,127.0.0.1和localhost对mysql数据库来讲是不同的主机.

[root@228827 ~]# mysql -uroot -p123456 -h 127.0.0.1

这让我想起了mysql下的user表,我们要进mysql看user表,一种方法可以通过上面的命令,如果不行,可以用下面的命令启动数据库,缺省密码进入:

  1. [root@228827 ~]# /etc/init.d/mysql stop 
  2. [root@228827 ~]# /usr/local/mysql/bin/mysqld_safe –skip-grant-tables & 
  3. [root@228827 ~]# mysql 
  4. Welcome to the MySQL monitor. Commands end with ; or g. 
  5. Your MySQL connection id is 1 
  6. Server version: 5.1.57 Source distribution 
  7. Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. 
  8. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. 
  9. mysql> use mysql 
  10. Database changed 
  11. mysql> select user,host,password from user where user='root'
  12. +——+——————-+——————————————-+ 
  13. user | host | password |  --phpfensi.com 
  14. +——+——————-+——————————————-+ 
  15. | root | % | *A50E066E106320CF4142 | 
  16. | root | centos | *A50E066E106320CF4142 | 
  17. | root | 127.0.0.1 | *A50E066E1063608320CF4142 | 
  18. +——+——————-+——————————————-+ 
  19. rows in set (0.12 sec) 

发现user表host字段中没有localhost,但是我的理解是%代表所有的主机都能登录的,为什么localhost不能呢,同样的情况我在5.0.45版的mysql上面做实验就不会发生localhost无法登录,我当前用的是5.1.57版的,难道是版本的问题?

接下来的修改很明显了,代码如下:

mysql> update user set host='localhost' where user='root' and host='%';

mysql> flush privileges;

OK,退出mysql,重启mysql就解决问题了.

Tags: MySQL密码 MySQL无法本地登录

分享到: