ERROR 1045 (28000): Access denied for user \'\'@\'localhost\' (using password: NO)

来源:互联网 发布:股市趋势技术分析知乎 编辑:程序博客网 时间:2024/06/05 23:51

mysql使用grant命令时报该错误,原因是:未用root用户及密码登陆。

C:\Program Files\MySQL\MySQL Server 5.1\bin>mysql -uroot -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23626
Server version: 5.1.62-community MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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> GRANT ALL PRIVILEGES
-> ON *.*
-> TO 'lumeng'@'localhost' IDENTIFIED BY 'lumeng1'
-> WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)



红字部分要注意,如果不加符合和主机名(@'localhost'),是无效的。默认会是(@'%')






Grant可以把指定的权限分配给特定的用户,如果这个用户不存在,则会创建一个用户 

命令格式 
grant 权限 on 数据库名.表名 to 用户名@登陆方式 identified by 'password1'

grant select,insert,update,delete on auth.* to user1@localhost identified by 'password'; 
权    限:select,insert,update,delete,drop,index,all,privileges(表示赋予用户全部权限跟all一样) 
数据库  :当数据库名称.表名称被*.*代替,表示用户拥有操作mysql上所有数据库所有表的权限          
登陆方式:即用户地址,可以是localhost,也可以是ip地址、机器名字、域名.也可以用'%'表示从任何地址连接,默认为% 
'password':可以为空,但是为空这表示只能从本地登陆,建议不能为空 
原创粉丝点击