数据库权限的顺序:user -> db -> tables_priv -> columns_priv 。
新建用户,并授予所有权限:
grant all privileges on *.* to 'username'@'localhost' identified by 'password' with grant option;
进入mysql数据库,查询user表和db表可见权限设置
user mysql;
select * from user where user='username' and host='localhost' \G;
select * from db where user='username' and host='localhost' \G;
host值可以为主机名或IP号或localhost,也可为%匹配任何主机名,空host值等价于%,若host值有多个匹配,则优先以最具体的Host值排序(类似于CSS样式的3种排序规则一样)
若mysql库的user表中host值为*或为空,表示所有外部IP都可连接,但不包括本机localhost,若要包含,必须单独为localhost赋予权限。
grant super,process,file on *.* to 'z1'@'%'; // 管理权限的on后面必须跟*.*
grant usage on *.* to 'z2'@'localhost'; // 只授予登录权限给z4@localhost
下面两种创建用户权限的方法等价:(需要先用前者创建用户和密码)
一、grant select,insert,update,delete on dbname.* to 'username'@'%' identified by 'password';
二、insert into db(host,db,user,select_priv,insert_priv,update_priv,delete_priv) values('%','dbname','username','Y','Y','Y','Y');
flush privileges;
查看账号权限:
一、show grants for user@host;
二、use information_schema;
select * from schema_privileges where grantee="'username'@'hostname'";
更改权限:
一、grant执行的时候,不存在则创建账号,若存在,则执行权限的增加与合并
二、revoke可回收已经赋予的权限
revoke all privileges on *.* from 'username'@'hostname';但不能回收登录权限,即不能删除
修改账号密码:
一、mysqladmin -uroot -p123456 password 'newpwd';
二、set password for 'username'@'hostname'=password('newpwd');
若更改当前数据库密码,可省略FOR语句:set password=password('newpwd');
三、grant usage on *.* to 'username'@'hostname' identified by 'newpwd';
grant usage on *.* to 'username'@'hostname' identified by password '...md5...';
四、update user set password=password('newpwd') where host='hostname' and user='username';
flush privileges;
删除账号:
drop user 'username'@'hostname';
本文出自 “ThinkPHP学习笔记” 博客,谢绝转载!