MySQL登陆小问题

时间:2023-03-09 05:40:36
MySQL登陆小问题

root用户创建用户

CREATE USER gechong
IDENTIFIED BY 'gechong';

登录数据库时提示错误:

C:\Users\gechong>mysql -u gechong -p
Enter password: *******
ERROR (): Access denied for user 'gechong'@'localhost' (using password: YES)

另外一种方式创建用户

CREATE USER gechong@localhost
IDENTIFIED BY 'gechong';

登录数据库

C:\Users\gechong>mysql -u gechong -p
Enter password: *******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.6. MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

查找原因:

查看mysql.user

第一种方式创建的用户:

SELECT Host,User,Password
FROM user
WHERE user='gechong'; +------+---------+-------------------------------------------+
| Host | User | Password |
+------+---------+-------------------------------------------+
| % | gechong | *11B9ACA21786F766739D0EB1483C5F64212B81AC |
+------+---------+-------------------------------------------+

第二种方式创建的用户:

SELECT Host,User,Password
FROM user
WHERE user='gechong'; +-----------+---------+-------------------------------------------+
| Host | User | Password |
+-----------+---------+-------------------------------------------+
| localhost | gechong | *11B9ACA21786F766739D0EB1483C5F64212B81AC |
+-----------+---------+-------------------------------------------+

原因找到了,原来是user表中没有相应的localhost字段

总结:在创建或者删除用户时,如果创建语句能在Host字段中添加内容,则在创建或者删除时必须加上@"该内容"(%除外)。