您的位置:首页 > 运维架构 > Linux

Linux mysql 5.6: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

2014-07-22 23:33 771 查看
案例环境:操作系统:RedHatEnterpriseLinuxServerrelease5.7(Tikanga)64bit数据库版本:Mysql5.6.1964bit案例介绍:今天开始学习mysql,遂先安装了Mysql5.6.1964bit版本的数据库,结果安装成功了,但是使用root登录时遇到了ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:NO)错误.如下所示

[root@DB-Servertmp]#rpm-ivhMySQL-server-5.6.19-1.rhel5.x86_64.rpm
Preparing...###########################################[100%]
1:MySQL-server###########################################[100%]
[root@DB-Servertmp]#rmp-ivhMySQL-client-5.6.19-1.rhel5.x86_64.rpm
-bash:rmp:commandnotfound
[root@DB-Servertmp]#rpm-ivhMySQL-client-5.6.19-1.rhel5.x86_64.rpm
Preparing...###########################################[100%]
1:MySQL-client###########################################[100%]
[root@DB-Servertmp]#/etc/rc.d/init.d/mysqlstatus
MySQLisnotrunning[FAILED]
[root@DB-Servertmp]#/etc/rc.d/init.d/mysqlstart
StartingMySQL.[OK]
[root@DB-Servertmp]#mysql-uroot-p
Enterpassword:
ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:NO)
[root@DB-Servertmp]#mysql-urootmysql
ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:NO)




安装过程中没有设置过root密码,不清楚root密码,搜索了网上一些资料,大致有下面一些情况:
1:有些博客说root的随机密码位于/root/.mysql_secret中,但是我这个版本根本没有/root/.mysql_secret文件。搜索到一篇文章,大意如下所示(没有确认)。到现在也没有明白,我这个版本为什么没有生成这个文件?
ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:NO):表示没有生成root的临时密码
ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:YES):表示生成了root的临时密码。
2:有些资料说root的默认密码为空,经过确认那是以前的老版本,Mysql5.6及以后版本出处于安全考虑,root密码已经不为空了。
另外,我在官网论坛居然发现有很多人遇到类似问题ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:NO)
解决方法:
首先停掉mysql服务器

[root@DB-Serverinit.d]#/etc/rc.d/init.d/mysqlstop
ShuttingdownMySQL..[OK]

然后使用mysqld_safe命令在启动mysql,更新root账号的密码
--skip-grant-tables:不启动grant-tables(授权表),跳过权限控制。
--skip-networking:跳过TCP/IP协议,只在本机访问(从网上有些资料看,这个选项不是必须的。可以不用)

[root@DB-Serverinit.d]#mysqld_safe--user=mysql--skip-grant-tables--skip-networking&
[2]9396
[root@DB-Serverinit.d]#14072214:59:46mysqld_safeLoggingto'/var/lib/mysql/DB-Server.err'.
14072214:59:46mysqld_safeStartingmysqlddaemonwithdatabasesfrom/var/lib/mysql

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

tail-200/var/lib/mysql/DB-Server.err|more
2014-07-2214:59:419346[Note]Shuttingdownplugin'binlog'
2014-07-2214:59:419346[Note]/usr/sbin/mysqld:Shutdowncomplete
14072214:59:41mysqld_safemysqldfrompidfile/var/lib/mysql/DB-Server.pidended
14072214:59:46mysqld_safeStartingmysqlddaemonwithdatabasesfrom/var/lib/mysql
2014-07-2214:59:470[Warning]TIMESTAMPwithimplicitDEFAULTvalueisdeprecated.Pleaseuse--explicit_defaults_for_timesta
mpserveroption(seedocumentationformoredetails).
2014-07-2214:59:479516[Note]Plugin'FEDERATED'isdisabled.
2014-07-2214:59:479516[Note]InnoDB:Usingatomicstorefcountbufferpoolpages
2014-07-2214:59:479516[Note]InnoDB:TheInnoDBmemoryheapisdisabled
2014-07-2214:59:479516[Note]InnoDB:Mutexesandrw_locksuseGCCatomicbuiltins
2014-07-2214:59:479516[Note]InnoDB:Compressedtablesusezlib1.2.3
2014-07-2214:59:479516[Note]InnoDB:UsingLinuxnativeAIO
2014-07-2214:59:479516[Note]InnoDB:UsingCPUcrc32instructions
2014-07-2214:59:479516[Note]InnoDB:Initializingbufferpool,size=128.0M
2014-07-2214:59:479516[Note]InnoDB:Completedinitializationofbufferpool
2014-07-2214:59:479516[Note]InnoDB:HighestsupportedfileformatisBarracuda.
2014-07-2214:59:479516[Note]InnoDB:128rollbacksegment(s)areactive.
2014-07-2214:59:479516[Note]InnoDB:Waitingforpurgetostart
2014-07-2214:59:479516[Note]InnoDB:5.6.19started;logsequencenumber1626087
2014-07-2214:59:479516[Note]/usr/sbin/mysqld:readyforconnections.
Version:'5.6.19'socket:'/var/lib/mysql/mysql.sock'port:0MySQLCommunityServer(GPL)





[root@DB-Serverinit.d]#mysql-urootmysql
Readingtableinformationforcompletionoftableandcolumnnames
Youcanturnoffthisfeaturetogetaquickerstartupwith-A
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis1
Serverversion:5.6.19MySQLCommunityServer(GPL)
Copyright(c)2000,2014,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>selectHost,User,Password,password_expired
->fromuser
->whereuser='root'andhost='root'orhost='localhost';
+-----------+------+-------------------------------------------+------------------+
|Host|User|Password|password_expired|
+-----------+------+-------------------------------------------+------------------+
|localhost|root|*A848DE7CCD839E924921BEE41711991DDA0D529E|Y|
+-----------+------+-------------------------------------------+------------------+
1rowinset(0.00sec)
mysql>updateusersetpassword=PASSWORD('p12#456')
->whereuser='root'andhost='root'orhost='localhost';
QueryOK,1rowaffected(0.00sec)
Rowsmatched:1Changed:1Warnings:0

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

mysql>flushprivileges;
QueryOK,0rowsaffected(0.00sec)
mysql>quit
Bye
[root@DB-Serverinit.d]#/etc/rc.d/init.d/mysqlrestart
ShuttingdownMySQL..14072215:02:27mysqld_safemysqldfrompidfile/var/lib/mysql/DB-Server.pidended
[OK]
StartingMySQL.[OK]
[2]-Donemysqld_safe--user=mysql--skip-grant-tables--skip-networking
[root@DB-Serverinit.d]#mysql-urootp
ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:NO)
[root@DB-Serverinit.d]#mysql-uroot-p
Enterpassword:
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis2
Serverversion:5.6.19
Copyright(c)2000,2014,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>

另外,如果登录mysql数据库后执行脚本遭遇ERROR1820(HY000):YoumustSETPASSWORDbeforeexecutingthisstatement,可以使用重新设置一次密码即可解决问题.

mysql>setpassword=password('p12#456');

至此问题解决,但是还有不少地方有些疑惑,为啥出现这个错误?其实这么多资料都只是说了解决方法,但是都回避了问题的原因。
参考资料:http://huangyifa163.blog.163.com/blog/static/262875752011127102215790/http://sundful.iteye.com/blog/704337
http://wenku.baidu.com/view/735ffa41be1e650e52ea995a.html
http://www.cnblogs.com/sunson/articles/2172086.html
http://blog.163.com/eric1945@126/blog/static/164934572201081494343373/http://www.cnblogs.com/likai198981/archive/2013/04/06/3002518.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐