MySQL用户

1、创建用户

create user 'firstuser'@'localhost' identified by '2wsx@WSX';

2、查看登录数据库的当前用户:

select current_user();

3、授权

grant privileges on database_name.table_name to 'user_name'@'host';

#privileges  用户操作权限,privileges可省略

     有select、update、insert、delete等

     如果要授权全部权限可以使用 ALL

#database_name.table_name   指定数据库和相应的表,如果需要授权所有数据库和所有的表使用通配符 * 

       #所有的数据库和所有的表  *.*
  eg:授予用户所有数据的所有权限:grant all on *.* to 'usr_name'@'localhost';

   eg:授予用户所有数据的查权限:grant select on *.* to 'user_name'@'localhost';

  (1)未授权时,firstuser只能看到自己信息的数据库

   

  (2)使用root用户对firstuser授权test数据库的所有权限

grant all on test.* to 'firstuser'@'localhost';

  (3)再次使用firstuser用户登录数据库查看  

   

   (4)删除其中age=15的数据  

   

4、撤销用户权限

#使用 revoke

revoke 权限 on database_name.teble_name from 'user'@'localhost'

  eg:使用root用户撤销firstuser用户对test数据库的数据删除权限,撤销delete

revoke delete on test.* from 'firstuser'@'localhost';

  此时已经无法删除数据

  

5、修改用户名

#rename user 'old_name'@'localhost' to 'new_name'@'localhost';

将firstuser修改成seconduser
rename user 'firstuser'@'localhost' to 'seconduser'@'localhost';

  

6、修改密码

mysql5.7.5版本之前,可以使用

    set password for 'user'@'localhost' = password('new_password');

mysql5.7.6版本以上,需要使用

    alter user 'user_name'@'localhost' identified by 'new_password';

#修改seconduser的密码
alter user 'seconduser'@'localhost' identified by '3edc#EDC'

7、删除用户

drop user 'user_name'@'host'

#删除用户seconduser
drop user 'seconduser'@'localhost';

  

8、授权其他用户也可以授权

grant all *.* to user@ip with grant option; #需要在命令语句后面加上‘with grant option’

授权注意

         授权时报错  

Access denied for user 'root'@'%' (using password: YES)

需要查看user表中的grant_priv中的权限是否开启

select user,host,grant_priv from user;

然后修改grant_priv字段为Y

update user set grant_priv='Y' where user='user' and `Host`='%';

flush privileges; #修改结束之后需要刷新权限

退出数据库重新登录即可。
原文地址:https://www.cnblogs.com/wqs-Time/p/14610749.html