SQL语句实践--DDL&&DCL语句之管理用户

一、查看当前用户列表

  select user,host from mysql.user;

 

 

 

二、创建数据库用户

  语法:create user '用户名'@‘主机’ identified by '密码';

 

  创建一个liuhui的用户,只允许本机登陆,密码是liuhui123

  create user 'liuhui'@'localhost' identified by 'liuhui123';

 

 

 

  创建一个bbs用户,授权10.0.0.0/24网段机器访问

  create user 'bbs'@'10.0.0.%' identified  by 'bbs123';

 

 

 

  登陆测试:

  mysql -ubbs -h 10.0.0.8 -p '密码'                       #异地或者本机登陆bbs用户必须要指定ip

 

 

 

三、查看用户授权

  使用create创建的用户仅仅只是空用户(即除了可以连接数据库以外,没有任何数据库权限),有关用户授权还必须用grant命令,grant命令还可以同时完成创建用户和授权两种操作

  

  查看用户对应授权权限

  show grants for bbs@'10.0.0.%';  

 

 

   USAGE:表示连接权限

 

四、删除数据库用户 

 

  语法:drop user '用户名'@‘主机域’

  删除blog用户:

  drop user 'blog'@'localhost';

 

 

  如果使用drop删除不了用户,很可能因为用户或者主机部分是特殊字符内容等,此时用以下方法删除,以bbs用户为例:

  delete from mysql.user where user='bbs' and host='10.0.0.%';

  flush privileges;

 

   select user ,host from mysql.user where user='bbs';

 

  bbs用户被成功删除

 

五、授权数据库用户

  语法:grant(授权命令) all/all privileges(对应权限) on dbname.*(目标:库和表) to username@localhosst(用户名和客户端主机)identified by 'password' (用户密码);

 

  例:创建test用户,对oldboy库具备所以权限,允许从localhost登陆数据库,密码是test123;

  grant all privileges on oldboy.* to test@localhost identified by 'test123';

 

  查看授权用户test的权限

  show grants for 'test'@'localhost';

 

 

  例:授权于root同等权限地位的system用户权限

  首先我们来看下root用户都有那些权限:show gants for 'root'@'localhost';

  

 

  然后创建一个和root一样权限的用户system:grant all on *.* to 'system'@'localhost' identified by 'system123' with grant option;

  

 

   允许创建代理用户:grant proxy  on ''@'' to 'system'@'localhost' with grant option;

  

 

 

 

  

  根据grant语法,授权局域网内主机可可连接登陆MYSQL

  方法1:命令+百分号匹配法:

  grant all on *.*  to 'test'@'10.0.0.%' identified by '密码';

 

  第二种方法:命令+子网掩码配置方法:

  grant all on *.* to  'test'@'10.0.0.0/255.255.255.0' identified by '密码';

  注意:不能用10.0.0.0/24来代替10.0.0.0/255.255.255.0

 

  第三种方法:首先创建用户,然后在授权

  create user 'test'@'10.0.0.%' identified by '密码';

  grant all on *.* to 'test'@'10.0.0.%'

 

六、回收权限

  取消用户testd对数据库oldboy的只读(select)权限

  revoke select on oldboy.* from 'test'@'localhost';

  

 

 

   

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

  

 

posted @ 2020-08-17 08:59  运维cainiao  阅读(183)  评论(0编辑  收藏  举报