今天开始学习mysql,遂先安装了Mysql 5.6.19 64bit 版本的数据库,结果安装成功了,但是使用root登录时遇到了ERROR 1045 (28000): Access denied f

Linux MysqL 5.6: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) - 潇湘隐者 - 博客园  https://www.cnblogs.com/kerrycode/p/3861719.html

案例环境:

             操作系统 :Red Hat Enterprise Linux Server release 5.7 (Tikanga) 64 bit

          数据库版本 : MysqL 5.6.19 64 bit

案例介绍:

今天开始学习MysqL,遂先安装了MysqL 5.6.19 64bit 版本的数据库,结果安装成功了,但是使用root登录时遇到了ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)错误. 如下所示

[root@DB-Server tmp]# rpm -ivh MysqL-server-5.6.19-1.rhel5.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:MysqL-server           ########################################### [100%]
[root@DB-Server tmp]# rmp -ivh MysqL-client-5.6.19-1.rhel5.x86_64.rpm 
-bash: rmp: command not found
[root@DB-Server tmp]# rpm -ivh MysqL-client-5.6.19-1.rhel5.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:MysqL-client           ########################################### [100%]
[root@DB-Server tmp]# /etc/rc.d/init.d/MysqL status
MysqL is not running[Failed]
[root@DB-Server tmp]# /etc/rc.d/init.d/MysqL start
Starting MysqL.[  OK  ]
[root@DB-Server tmp]# MysqL -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@DB-Server tmp]# MysqL -u root MysqL
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

clip_image001

 

安装过程中没有设置过root密码,不清楚root密码,搜索了网上一些资料,大致有下面一些情况:

1:有些博客说root的随机密码位于/root/.MysqL_secret中,但是我这个版本根本没有/root/.MysqL_secret文件搜索到一篇文章,大意如下所示(没有确认)。到现在也没有明白,我这个版本为什么没有生成这个文件

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) :表示没有生成root的临时密码

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) :表示生成了root的临时密码。

2:有些资料说root的认密码为空,经过确认那是以前的老版本,MysqL 5.6及以后版本出处于安全考虑,root密码已经不为空了。

另外,我在官网论坛居然发现有很多人遇到类似问题ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

解决方法

首先停掉MysqL服务器

[root@DB-Server init.d]# /etc/rc.d/init.d/MysqL stop
 
 Shutting down MysqL..[ OK ]
 

然后使用MysqLd_safe命令在启动MysqL,更新root账号的密码

--skip-grant-tables:不启动grant-tables(授权表),跳过权限控制。

--skip-networking :跳过TCP/IP协议,只在本机访问(从网上有些资料看,这个选项不是必须的。可以不用)

[root@DB-Server init.d]# MysqLd_safe --user=MysqL --skip-grant-tables --skip-networking &
[2] 9396
[root@DB-Server init.d]# 140722 14:59:46 MysqLd_safe Logging to '/var/lib/MysqL/DB-Server.err'.
140722 14:59:46 MysqLd_safe Starting MysqLd daemon with databases from /var/lib/MysqL

执行上面命令后,此会话窗口会出现无反应的状态,需要使用CTRL+C中断会话,检查/var/lib/MysqL/DB-Server.err日志,发现也无其它异常信息。没有弄明白该会话窗口为啥会出现无响应状态。

tail -200 /var/lib/MysqL/DB-Server.err | more
 
2014-07-22 14:59:41 9346 [Note] Shutting down plugin 'binlog'
2014-07-22 14:59:41 9346 [Note] /usr/sbin/MysqLd: Shutdown complete
 
140722 14:59:41 MysqLd_safe MysqLd from pid file /var/lib/MysqL/DB-Server.pid ended
140722 14:59:46 MysqLd_safe Starting MysqLd daemon with databases from /var/lib/MysqL
2014-07-22 14:59:47 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timesta
mp server option (see documentation for more details).
2014-07-22 14:59:47 9516 [Note] Plugin 'FEDERATED' is disabled.
2014-07-22 14:59:47 9516 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-07-22 14:59:47 9516 [Note] InnoDB: The InnoDB memory heap is disabled
2014-07-22 14:59:47 9516 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-07-22 14:59:47 9516 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-07-22 14:59:47 9516 [Note] InnoDB: Using Linux native AIO
2014-07-22 14:59:47 9516 [Note] InnoDB: Using cpu crc32 instructions
2014-07-22 14:59:47 9516 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-07-22 14:59:47 9516 [Note] InnoDB: Completed initialization of buffer pool
2014-07-22 14:59:47 9516 [Note] InnoDB: Highest supported file format is Barracuda.
2014-07-22 14:59:47 9516 [Note] InnoDB: 128 rollback segment(s) are active.
2014-07-22 14:59:47 9516 [Note] InnoDB: Waiting for purge to start
2014-07-22 14:59:47 9516 [Note] InnoDB: 5.6.19 started; log sequence number 1626087
2014-07-22 14:59:47 9516 [Note] /usr/sbin/MysqLd: ready for connections.
Version: '5.6.19'  socket: '/var/lib/MysqL/MysqL.sock'  port: 0  MysqL Community Server (GPL)

clip_image002

[root@DB-Server init.d]# MysqL -u root MysqL
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Welcome to the MysqL monitor.  Commands end with ; or \g.
Your MysqL connection id is 1
Server version: 5.6.19 MysqL Community Server (GPL)
 
copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered Trademark of Oracle Corporation and/or its
affiliates. Other names may be Trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MysqL> select Host, User, Password,password_expired
    -> from user
    -> where user='root' and host='root' or host='localhost';
+-----------+------+-------------------------------------------+------------------+
| Host      | User | Password                                  | password_expired |
+-----------+------+-------------------------------------------+------------------+
| localhost | root | *A848DE7CCD839E924921BEE41711991DDA0D529E | Y                |
+-----------+------+-------------------------------------------+------------------+
1 row in set (0.00 sec)
 
MysqL> update user set password=PASSWORD('p12#456')
    -> where user='root' and host='root' or host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

新设置用户或更改密码后需用flush privileges刷新MysqL的系统权限相关表,否则会出现拒绝访问,还有一种方法,就是重新启动MysqL服务器,来使新设置生效。

MysqL> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
MysqL> quit
Bye
[root@DB-Server init.d]# /etc/rc.d/init.d/MysqL restart
Shutting down MysqL..140722 15:02:27 MysqLd_safe MysqLd from pid file /var/lib/MysqL/DB-Server.pid ended
[  OK  ]
Starting MysqL.[  OK  ]
[2]-  Done                    MysqLd_safe --user=MysqL --skip-grant-tables --skip-networking
[root@DB-Server init.d]# MysqL -u root p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@DB-Server init.d]# MysqL -u root -p
Enter password: 
Welcome to the MysqL monitor.  Commands end with ; or \g.
Your MysqL connection id is 2
Server version: 5.6.19
 
copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered Trademark of Oracle Corporation and/or its
affiliates. Other names may be Trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MysqL> 

另外,如果登录MysqL数据库后执行脚本遭遇 ERROR 1820 (HY000): You must SET PASSWORD before executing this statement,可以使用重新设置一次密码即可解决问题.

MysqL>set password = password('p12#456'); 

 

至此问题解决,但是还有不少地方有些疑惑,为啥出现这个错误? 其实这么多资料都只是说了解决方法,但是都回避了问题的原因。

相关文章

优化MySQL数据库发布系统存储的方法有:1.mysql库主从读写分...
使用mysql的方法:在“我的电脑”→右键→“管理”→“服务”...
在mysql中查看root用户权限的方法:1.命令行启动mysql服务;...
MySQL主从复制是用来备份一个与主数据库一样环境的从数据库,...
运行mysql的方法1.启动mysql服务,在“我的电脑”→右键→“...
开启mysql的方法1.可以通过快捷键win+r,输入cmd,打开窗口,...