PyCharm - 数据库连接。用户'root'@'localhost'拒绝访问(使用密码:YES)

时间:2013-09-20 18:11:38

标签: mysql intellij-idea pycharm

我正在尝试通过PyCharm为我的本地db(mysql)设置连接。我正在开发macbook pro。

我正在使用此video

但是,当我尝试测试连接时,我收到了这个错误:

    Connection to Data Source failed
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:927)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1709)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1252)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2486)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2519)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2304)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
    in RemoteDriverImpl.connect(RemoteDriverImpl.java:27)
    at com.sun.proxy.$Proxy87.connect(Unknown Source)
    in LocalDataSource.getConnection(LocalDataSource.java:158)

但我没有问题通过终端连接到mysql

mysql -u root --password=123

UPD

mysql -u root -p -h 127.0.0.1 -D lp
Access denied for user 'root'@'localhost' (using password: YES)

mysql -u root -p -h 127.0.0.1 lp
Access denied for user 'root'@'localhost' (using password: YES)

mysql -u root -p -h localhost lp
connected

UPD

mysql> select host, user, password != '' from mysql.user;
+-----------+------+----------------+
| host      | user | password != '' |
+-----------+------+----------------+
| localhost | root |              1 |
| 127.0.0.1 | root |              1 |
| ::1       | root |              1 |
+-----------+------+----------------+
3 rows in set (0.00 sec)

mysql> select user(), current_user();
+----------------+----------------+
| user()         | current_user() |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+
1 row in set (0.00 sec)

以下是我的设置的屏幕截图:

enter image description here

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

也许你没有对架构的许可? 试试这个:

 mysql -u root -p -h 127.0.0.1 -D lp

<强>更新 您可以访问数据库,但您无权访问该模式。

 GRANT ALL ON `lp`.* TO 'root'@'localhost';

答案 1 :(得分:4)

其中一些或两个都可以解决您的问题:

检查select host, user, password, Select_priv from mysql.user;的结果。

如果所有行的password字段不相同,请执行以下操作:

SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('123');
SET PASSWORD FOR 'root'@'::1' = PASSWORD('123');

如果所有行的Select_priv不是'Y',请执行以下操作:

GRANT ALL ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;
GRANT ALL ON *.* TO 'root'@'::1' WITH GRANT OPTION;

修改

由于这些都不是真的,唯一剩下的选择是机器上运行了多个MySQL实例,一个接收通过套接字建立的连接,另一个接收通过TCP建立的连接。