解决方案:
- 打开MySQL目录下的my.ini文件,在文件的最后添加一行“skip-grant-tables”,保存并关闭文件。(WIN7默认安装,my.ini在C:\ProgramData\MySQL\MySQL Server 5.6)
- 重启MySQL服务。
- 通过命令行进入MySQL的BIN目录,输入“mysql -u root -p”(不输入密码),回车即可进入数据库。(WIN7默认安装,BIN目录为:C:\Program Files\MySQL\MySQL Server 5.6\bin)
- 执行“use mysql;”,使用mysql数据库。
- 执行“update user set password=PASSWORD("123456") where user='root';”(修改root的密码)
- 打开MySQL目录下的my.ini文件,删除最后一行的“skip-grant-tables”,保存并关闭文件。
- 重启MySQL服务。
- 在命令行中输入“mysql -u root -p 123456”,即可成功连接数据库。
完成以上步骤,MyEclipse也可成功连接MySQL了。
相信这个问题大部分人都遇到过,至少我遇到过三次了,而且每次原因都不一样,前段时间同学也遇到这个问题,问我怎么解决,我把我的解决思路都说了一遍,发现还不行,最后居然是另外一个原因。。。哎,说多了都是泪,总结如下:
(注:本文都建立在数据库能正常使用,但是程序无法连接数据库的基础上,如果在doc窗口或者用其他mysql图形界面都连接不上,请自补mysql的基础知识吧
)
1.基于官方说法(http://dev.mysql.com/doc/refman/5.5/en/access-denied.html)
- If you have access problems with a Perl, PHP, Python, or ODBC program,
- try to connect to the server withmysql -u user_namedb_nameormysql
- -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.
- There is no space between-pand the password
大概意思就是,如果能用mysql命令行进入mysql,就说明mysql安装没问题,是你程序的问题,没有连接权限(这里后面细说)。这里要说明一点的就是,非常重要,就是最后一句话,不要有空格!我就是吃了这个亏啊,在jabc.properties里面配置username和password时,因为换行的原因,里面有空格,说白了还是用户名和密码出错。如下
- jdbc.username=root
- jdbc.password=123456
root和123456后面都有空格,这个真是肉眼发现不了啊。。。。所以删掉空格就好了
2.用户没有权限(骗自己?参考)
其实,这一点也是网上看别人这么说的,自己没用过。但是我想,如果像我前面第一点提到的,你能在命令行模式下用这个用户名和密码进入mysql的话,那肯定有权限了啊,干嘛还要赋予权限?。。。所以仅供参考。。。
- CREATE USER 'root'@'localhost' IDENTIFIED BY '你的密码';
- 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(没有权限)。![](https://img-blog.csdn.net/20160706165725474?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
继续按下面的流程走:
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
![](https://img-blog.csdn.net/20160706165920424)
改好之后,再修改一下my.ini这个文件,把我们刚才加入的 "skip-grant-tables"这行删除,保存退出再重启mysql就可以了。
![](https://img-blog.csdn.net/20160706165954018)
忘记mysql密码,使用不对的服务器密码,在连接服务器的时候,常常会提示 Access denied 错误,如下图所示:
- [root@localhost bin]# ./mysql -uroot -p12345
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
遇到上面情况怎么办,请按如下步骤操作:
1、停止mysql服务
- [root@localhost bin]# chkconfig --list | grep -i mysql
- mysql 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
- [root@localhost bin]# service mysql stop
- Shutting down MySQL [确定]
2、用mysqld_safe重启服务
- [root@localhost bin]# ./mysqld_safe --user=root --skip-grant-tables --skip-networking &
- [1] 3818
- [root@localhost bin]# 111105 07:30:32 mysqld_safe Logging to '/usr/local/mysql/var/localhost.localdomain.err'.
- 111105 07:30:32 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
- ./mysql -uroot mysql
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 1
- Server version: 5.1.48-log Source distribution
-
-
-
-
- Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
- This software comes with ABSOLUTELY NO WARRANTY. This is free software,
- and you are welcome to modify and redistribute it under the GPL v2 license
-
-
-
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
3、重设密码
- mysql> update user set password=password('12345') where user='root' ;
- Query OK, 3 rows affected (0.05 sec)
- Rows matched: 3 Changed: 3 Warnings: 0
-
-
-
-
- mysql> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
-
-
-
-
- mysql> quit
- Bye
4、重启服务
- [root@localhost bin]#service mysql stop
-
-
- [root@localhost bin]# service mysql start
5、再次连接数据库
- [root@localhost bin]# ./mysql -uroot -p12345
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 1
- Server version: 5.1.48-log Source distribution
-
-
-
-
- Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
- This software comes with ABSOLUTELY NO WARRANTY. This is free software,
- and you are welcome to modify and redistribute it under the GPL v2 license
-
-
-
-
- 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