linux-mysql Access denied for user 'root'@'localhost'

  这两天在MyEclipse中开发Web项目时,连接MYSQL数据库,出现问题:Access denied for user 'root'@'localhost'(using password:YES)。

        经查找资料发现是root帐户默认不开放远程访问权限,所以需要修改一下相关权限。

      解决方案:
  1. 打开MySQL目录下的my.ini文件,在文件的最后添加一行“skip-grant-tables”,保存并关闭文件。(WIN7默认安装,my.ini在C:\ProgramData\MySQL\MySQL Server 5.6)
  2. 重启MySQL服务。
  3. 通过命令行进入MySQL的BIN目录,输入“mysql -u root -p”(不输入密码),回车即可进入数据库。(WIN7默认安装,BIN目录为:C:\Program Files\MySQL\MySQL Server 5.6\bin)
  4. 执行“use mysql;”,使用mysql数据库。
  5. 执行“update user set password=PASSWORD("123456") where user='root';”(修改root的密码)
  6. 打开MySQL目录下的my.ini文件,删除最后一行的“skip-grant-tables”,保存并关闭文件。
  7. 重启MySQL服务。
  8. 在命令行中输入“mysql -u root -p 123456”,即可成功连接数据库。

      完成以上步骤,MyEclipse也可成功连接MySQL了。


相信这个问题大部分人都遇到过,至少我遇到过三次了,而且每次原因都不一样,前段时间同学也遇到这个问题,问我怎么解决,我把我的解决思路都说了一遍,发现还不行,最后居然是另外一个原因。。。哎,说多了都是泪,总结如下:

(注:本文都建立在数据库能正常使用,但是程序无法连接数据库的基础上,如果在doc窗口或者用其他mysql图形界面都连接不上,请自补mysql的基础知识吧尴尬

1.基于官方说法(http://dev.mysql.com/doc/refman/5.5/en/access-denied.html)

[html]  view plain  copy
  1. If you have access problems with a Perl, PHP, Python, or ODBC program,  
  2. try to connect to the server withmysql -u user_namedb_nameormysql   
  3. -u user_name-pyour_passdb_name. If you are able to connect using the mysql client, the problem lies with your program, not with the access privileges.  
  4. There is no space between-pand the password  
大概意思就是,如果能用mysql命令行进入mysql,就说明mysql安装没问题,是你程序的问题,没有连接权限(这里后面细说)。这里要说明一点的就是,非常重要,就是最后一句话,不要有空格!我就是吃了这个亏啊,在jabc.properties里面配置username和password时,因为换行的原因,里面有空格,说白了还是用户名和密码出错。如下

[html]  view plain  copy
  1. jdbc.username=root    
  2. jdbc.password=123456    

root和123456后面都有空格,这个真是肉眼发现不了啊。。。。所以删掉空格就好了

2.用户没有权限(骗自己?参考)

其实,这一点也是网上看别人这么说的,自己没用过。但是我想,如果像我前面第一点提到的,你能在命令行模式下用这个用户名和密码进入mysql的话,那肯定有权限了啊,干嘛还要赋予权限?。。。所以仅供参考。。。

[html]  view plain  copy
  1. CREATE USER 'root'@'localhost' IDENTIFIED BY '你的密码';  
  2. GRANT ALL PRIVILEGES ON *.* TO root@'localhost';  

3.mysql和驱动jar版本冲突

这个是我同学出现的问题,他前面的问题都没有,后来我们发现,他安装的mysql是5.7,但是用的驱动jar(mysql-connector)是5.1.3(是我发给他的,怪我咯),好像程序里面一直连不上mysql,然后就没有然后了,就卸载了5.7的mysql,重新安装了5.0的mysql...(安装了还是在重启后才起效的),所以这个是个特例。。。



问题重现(以下讨论范围仅限Windows环境):
  
C:\AppServ\MySQL> mysql -u root -p 
Enter password:  
ERROR 1045 (28000): Access denied for user 
'root'@'localhost' (using password: YES)

 

解决方法:

编辑mysql配置文件my.ini(在mysql的安装目录下,我的在D:\Program Files\MySQL\MySQL Server 5.0\my.ini),在[mysqld]这个条目下加入 skip-grant-tables 保存退出后重启mysql

1.点击“开始”->“运行”(快捷键Win+R)。

2.停止:输入 net stop mysql

3.启动:输入 net start mysql  

这时候在cmd里面输入mysql -u root -p就可以不用密码登录了,出现 password:的时候直接回车可以进入,不会出现ERROR 1045 (28000),但很多操作都会受限制,因为我们不能grant(没有权限)。

继续按下面的流程走:

1.进入mysql数据库:
mysql> use mysql; 
Database changed

2.给root用户设置新密码:  
mysql> update user set password=password("新密码") where user="root"; 
Query OK, 1 rows affected (0.01 sec) 
Rows matched: 1 Changed: 1 Warnings: 0

3.刷新数据库
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)  

4.退出mysql:
mysql> quit; 
Bye

改好之后,再修改一下my.ini这个文件,把我们刚才加入的 "skip-grant-tables"这行删除,保存退出再重启mysql就可以了。

  


忘记mysql密码,使用不对的服务器密码,在连接服务器的时候,常常会提示 Access denied 错误,如下图所示:

[plain]  view plain  copy
  1. [root@localhost bin]# ./mysql -uroot -p12345  
  2. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)  

遇到上面情况怎么办,请按如下步骤操作:

1、停止mysql服务

[plain]  view plain  copy
  1. [root@localhost bin]# chkconfig --list | grep -i mysql  
  2. mysql           0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭  
  3. [root@localhost bin]# service mysql stop  
  4. Shutting down MySQL                                        [确定]  
2、用mysqld_safe重启服务

[plain]  view plain  copy
  1. [root@localhost bin]# ./mysqld_safe --user=root --skip-grant-tables --skip-networking &  
  2. [1] 3818  
  3. [root@localhost bin]# 111105 07:30:32 mysqld_safe Logging to '/usr/local/mysql/var/localhost.localdomain.err'.  
  4. 111105 07:30:32 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var  
  5. ./mysql -uroot mysql  
  6. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  7. Your MySQL connection id is 1  
  8. Server version: 5.1.48-log Source distribution  
  9.   
  10.   
  11.   
  12.   
  13. Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.  
  14. This software comes with ABSOLUTELY NO WARRANTY. This is free software,  
  15. and you are welcome to modify and redistribute it under the GPL v2 license  
  16.   
  17.   
  18.   
  19.   
  20. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  

3、重设密码

[plain]  view plain  copy
  1. mysql> update user set password=password('12345') where user='root' ;  
  2. Query OK, 3 rows affected (0.05 sec)  
  3. Rows matched: 3  Changed: 3  Warnings: 0  
  4.   
  5.   
  6.   
  7.   
  8. mysql> flush privileges;  
  9. Query OK, 0 rows affected (0.00 sec)  
  10.   
  11.   
  12.   
  13.   
  14. mysql> quit  
  15. Bye  

4、重启服务

[plain]  view plain  copy
  1. [root@localhost bin]#service mysql stop  
  2.   
  3.   
  4. [root@localhost bin]# service mysql start  

5、再次连接数据库

[plain]  view plain  copy
  1. [root@localhost bin]# ./mysql -uroot -p12345  
  2. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  3. Your MySQL connection id is 1  
  4. Server version: 5.1.48-log Source distribution  
  5.   
  6.   
  7.   
  8.   
  9. Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.  
  10. This software comes with ABSOLUTELY NO WARRANTY. This is free software,  
  11. and you are welcome to modify and redistribute it under the GPL v2 license  
  12.   
  13.   
  14.   
  15.   
  16. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  

解决linux-mysql 登录时,报异常:Access denied for user 'root'@'localhost'

第一步:停服务

/etc/init.d/mysql stop

或者

service mysqld stop


第二步:跳过密码验证
执行命令行:
# /usr/bin/mysqld_safe --skip-grant-tables
报:
151104 09:07:56 mysqld_safe Logging to '/var/lib/mysql/iZ23dq2wm0jZ.err'.
151104 09:07:56 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

第三步:无密码登录
执行命令行:
mysql -u root 

第四步:授权

mysql>

grant all privileges on *.* to 'root'@'localhost' identified by 'root' with grant option;
关键词解释:
root'@'localhost:是用户
root:是密码

问题一:发现无密码条件下,没有授权的写权限

The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
解决方法:

mysql> set global read_only=0;//(关掉新主库的只读属性)
mysql>flush privileges;

再次执行第四步授权语句:grant all privileges on *.* to 'root'@'localhost' identified by 'root' with grant option;

mysql>set global read_only=1;//(读写属性)
mysql>flush privileges;

(注意刷新是必须项)

第五步:重启数据库
/etc/init.d/mysql restart

或者
service mysql restart




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值