https://blog.csdn.net/u010735147/article/details/81743853

用户:

  1. '''
  2. 用户管理
  3. '''
  4. mysql> use mysql;
  5. '''
  6. 查看
  7. '''
  8. mysql> select host,user,password from user ;
  9. '''
  10. 创建
  11. '''
  12. mysql> create user zx_root IDENTIFIED by 'xxxxx'; //identified by 会将纯文本密码加密作为散列值存储
  13. '''
  14. 修改
  15. '''
  16. mysql>rename user feng to newuser//mysql 5之后可以使用,之前需要使用update 更新user表
  17. '''
  18. 删除
  19. '''
  20. mysql>drop user newuser; //mysql5之前删除用户时必须先使用revoke 删除用户权限,然后删除用户,mysql5之后drop 命令可以删除用户的同时删除用户的相关权限
  21. '''
  22. 更改密码
  23. '''
  24. mysql> set password for zx_root =password('xxxxxx');
  25. '''
  26. 或者
  27. '''
  28. mysql> update mysql.user set password=password('xxxx') where user='otheruser'

权限:

'''查看用户权限''' 
mysql> show grants for zx_root;
''' 赋予权限'''
mysql> grant select on dmc_db.*  to zx_root;
'''回收权限''' 
mysql> revoke  select on dmc_db.*  from  zx_root;  //如果权限不存在会报错

'''直接创建用户,包含授权''' 
grant all privileges on `test_zlj\_%`.* to test_zlj identified by 'admin';

上面的命令也可使用多个权限同时赋予和回收,权限之间使用逗号分隔
mysql> grant select,update,delete,insert  on dmc_db.*  to  zx_root;

如果想立即看到结果使用
flush  privileges ;
命令更新 

设置权限时必须给出一下信息
1,要授予的权限
2,被授予访问权限的数据库或表
3,用户名
grant和revoke可以在几个层次上控制访问权限
1,整个服务器,使用 grant ALL privileges 和 revoke  ALL privileges
2,整个数据库,使用 on database.*
3,特定表,使用 on  database.table
4,特定的列
5,特定的存储过程

user表中host列的值的意义
%              匹配所有主机
localhost    localhost不会被解析成IP地址,直接通过UNIXsocket连接
127.0.0.1      会通过TCP/IP协议连接,并且只能在本机访问;
::1                 ::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1

指定前缀:

  • 注意,最后的下划线会和 % 冲突,最终导致下划线失效
  • 所以必须使用转义符转义 ``sql '''直接创建用户,包含授权''' grant all privileges ontestzlj\%`.* to test_zlj identified by ‘admin’;

```