无法使用密码连接到mysql

时间:2013-04-30 05:29:08

标签: java mysql jdbc

您好我刚刚创建了一个mysql用户sukant1和密码也sukant1但是我无法连接它。请看下面的

mysql> CREATE USER 'sukant1'@'%' IDENTIFIED BY 'sukant1';
Query OK, 0 rows affected (0.07 sec)

mysql> exit
Bye


[sukanth@SWD11 ~]$ mysql -u sukant1 -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'sukant1'@'localhost' (using password: YES)

3 个答案:

答案 0 :(得分:1)

您可能正在通过套接字连接。好吧,试试

$ mysql -u sukant1 -p

当shell询问你密码时,请不要输入任何密码然后你应该进入。

但是,我建议您使用以下代码创建用户:

GRANT ALL PRIVILEGES ON *.* TO 'MY_USER'@'localhost' IDENTIFIED BY 'MY_PASS';

[编辑]

您正在尝试连接的表中,您的表mysql.user中有多个匹配项。您可以阅读有关mysql访问control here的更多信息。

要暂时解决您的问题,请使用其他用途登录您的数据库并输入:

SELECT user, host FROM mysql.user order by user;

您可能会看到类似的内容:

+------------------+-------------+
| user             | host        |
+------------------+-------------+
|                  | localhost   |
| sukant1          | %           |
| sukant1          | localhost   |
| root             | localhost   |
+------------------+-------------+

现在,删除用户:

delete from mysql.user where user = '' and host = 'localhost';
delete from mysql.user where user = 'sukant1' and host = 'localhost';

现在您应该可以根据需要访问数据库了。但是,再说一遍,这不是最好的方法。准备好我发送给您的链接并使用更具体的主机名,例如本地主机。

答案 1 :(得分:0)

尝试以root身份访问mysql ....有时不向所有用户提供mysql访问权。

su root

[root@SWD11 ~]$ mysql -u sukant1 -p

答案 2 :(得分:0)

Reset your root password,虽然不可取。