-- 2014.12.10
昨天根据开发的需要,创建了几个用户,但是在授权的时候报错:
ERROR 1044 (42000): Access denied for user 'root'@'%' to database 'mdm'
在网上搜了一下,大概有以下几种说法:
1、对该数据库权限不足。
2、参数问题。
3、user的user列存在空值记录。
以上的解决方法基本上都是需要重启MySQL、而且都是片面的。
这里解释一下,出现这种情况的原因。
首先这里是用root用户操作,所以对这个数据库的权限绝对是最高级的,但是如果是非root用户操作,就可能是权限的问题,需要root用户授权。
出现上面的报错,很可能,其实应该可以说基本上都是因为my.cnf 文件里面有skip-name-resolve参数,这个参数导致这不能解析hostname或其它方式的登录, 所以登录任何用户,匹配的时候不走root@'localhost',或者127.0.0.1或者::1 ,而是一个劲的走root@'%'.
再来查看一下每种方式下的root用户的具体权限:
mysql> select * from user\\G;
*************************** 1. row ***************************
Host: bidevedw\\_db
User: root
Password: *D013A4E3A5BB01E4239D18D7E93B59B7D2B767AD
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string:
password_expired: N
*************************** 2. row ***************************
Host: ::1
User: root
Password: *D013A4E3A5BB01E4239D18D7E93B59B7D2B767AD
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string:
password_expired: N
*************************** 3. row ***************************
Host: %
User: root
Password: *D013A4E3A5BB01E4239D18D7E93B59B7D2B767AD
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: N
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string:
password_expired: N
*************************** 4. row ***************************
Host: %
User: u_mdm_r2
Password: *CCB51F609423C18692BB961A7A83342B8311B6F4
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: N
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
Create_tablespace_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string:
password_expired: N
ERROR:
No query specified
mysql>
我这里root有三种解析登录方式分别是:Host: bidevedw\_db、Host: ::1、Host: %
一般情况还有Host:127.0.0.1、Host:localhost我的里面把哪两种删了。
请注意 !
注意上面每条记录的的红色字体部分Grant_priv: Y
这个表示,以这种方式解析登录的root用户,是否有grant权限,Y则表示有授权限给其他用户的权限,N表示没有。
这里恰好,就是我们要找的原因,因为我的/my.cnf文件里面有skip-name-resolve参数,所以root都是解析到@'%'方式登录,于是就没有grant_priv权限。
解决方法:
1、在不重启MySQL服务的情况下,只需要在登录的时候加上-h参数。
例如:(a). /usr/local/mysql/bin/mysql -uroot -p123456 -h::1
(b)./usr/local/mysql/bin/mysql -uroot -p123456 -h227.0.0.1
2、需要重启MySQL。把skip-name-resolve参数去掉 ---- 还没验证。不过我记得,如果去掉了,日志里面会有大量的警告信息。我是因为那些警告信息,才添加的这个参数。