delete user mysql_MySQL两种删除用户语句的区别(delete user和drop user)

tip:

在mysql中,我们经常需要创建用户和删除用户,创建用户时,我们一般使用create user或者grant语句来创建,create语法创建的用户没有任何权限,需要再使用grant语法来分配权限,而grant语法创建的用户直接拥有所分配的权限。在一些测试用户创建完成之后,做完测试,可能用户的生命周期就结束了,需要将用户删除,而删除用户在mysql中一般有两种方法,一种是drop user,另外一种是delete from mysql.user,那么这两种方法有什么区别呢?我们这里通过例子演示。

delete from mysql.user

首先,我们看看delete from mysql.user的方法。我们创建两个用户用来测试,测试环境是mysql5.5版本,用户名分别为yeyz@'%'和yeyz@'localhost',创建用户的语法如下:

mysql 15:13:12>>create user yeyz@'%' identified by '123456';

query ok, rows affected (. sec)

mysql 15:20:01>>grant select,create,update,delete on yeyz.yeyz to yeyz@'%';

query ok, rows affected (. sec)

mysql 15:29:48>>grant usage on yeyz.yeyz to 'yeyz'@localhost identified by '123456';

query ok, rows affected (. sec)

mysql--dba_admin@127...1:(none) 15:20:39>>show grants for yeyz@'%';

+-----------------------------------------------------------------------------------------------------+

| grants for yeyz@% |

+-----------------------------------------------------------------------------------------------------+

| grant usage on *.* to 'yeyz'@'%' identified by password '*6bb4837eb74329105ee4568dda7dc67ed2ca2ad9' |

| grant select, update, delete, create on `yeyz`.`yeyz` to 'yeyz'@'%' |

+-----------------------------------------------------------------------------------------------------+

此时我们通过delete的方法手动删除mysql.user表中的这两个用户,在去查看用户表,我们发现:

mysql 15:20:43>>delete from mysql.user where user='yeyz';

query ok, rows affected (. sec)

mysql 15:21:40>>select user,host from mysql.user;

+------------------+-----------------+

| user | host |

+------------------+-----------------+

| dba_yeyz | localhost |

| root | localhost |

| tkadmin | localhost |

+------------------+-----------------+

rows in set (. sec)

已经没有这两个yeyz的用户了,此时我们使用show grants for命令查看刚才删除的用户,我们发现依旧是存在这个用户的权限说明的:

mysql 15:24:21>>show grants for yeyz@'%';

+-----------------------------------------------------------------------------------------------------+

| grants for yeyz@% |

+-----------------------------------------------------------------------------------------------------+

| grant usage on *.* to 'yeyz'@'%' identified by password '*6bb4837eb74329105ee4568dda7dc67ed2ca2ad9' |

| grant select, update, delete, create on `yeyz`.`yeyz` to 'yeyz'@'%' |

+-----------------------------------------------------------------------------------------------------+

rows in set (0.00 sec)

说明我们虽然从mysql.user表里面删除了这个用户,但是在db表和权限表里面这个用户还是存在的,为了验证这个结论,我们重新创建一个yeyz@localhost的用户,这个用户我们只给它usage权限,其他的权限我们不配置,如下:

mysql ::>>grant usage on yeyz.yeyz to 'yeyz'@localhost identified by '123456';

query ok, rows affected (. sec)

这个时候,我们使用yeyz@localhost这个用户去登陆数据库服务,然后进行相关的update操作,如下:

[dba_mysql@tk-dba-mysql-stat-- ~]$ /usr/local/mysql/bin/mysql -uyeyz --socket=/data/mysql_4306/tmp/mysql.sock --port= -p -hlocalhost

enter password:

welcome to the mysql monitor. commands end with ; or \g.

your mysql connection id is

server version: 5.5.-log mysql community server (gpl)

copyright (c) , , oracle and/or its affiliates. all rights reserved.

oracle is a registered trademark of oracle corporation and/or its

affiliates. other names may be trademarks of their respective

owners.

type 'help;' or '\h' for help. type '\c' to clear the current input statement.

mysql--yeyz@localhost:(none) 15:31:05>>select * from yeyz.yeyz;

+------+

| id |

+------+

| 3 |

| 4 |

| 5 |

+------+

rows in set (. sec)

mysql--yeyz@localhost:(none) 15:31:16>>delete from yeyz.yeyz where id=;

query ok, row affected (. sec)

mysql--yeyz@localhost:(none) 15:31:32>>select * from yeyz.yeyz;

+------+

| id |

+------+

| 3 |

| 4 |

+------+

rows in set (. sec)

最终出现的结果可想而知,一个usage权限的用户,对数据库总的表进行了update操作,而且还成功了。这一切得益于我们delete from mysql.user的操作,这种操作虽然从user表里面删除了记录,但是当这条记录的host是%时,如果重新创建一个同名的新用户,此时新用户将会继承以前的用户权限,从而使得用户权限控制失效,这是很危险的操作,尽量不要执行。

再开看看drop的方法删除用户

首先,我们删除掉刚才的那两个用户,然后使用show grants for语句查看他们的权限:

mysql ::>>drop user yeyz@'%';

query ok, rows affected (0.00 sec)

mysql ::>>drop user yeyz@'localhost';

query ok, rows affected (0.00 sec)

mysql ::>>

mysql ::>>show grants for yeyz@'%';

error (): there is no such grant defined for user 'yeyz' on host '%'

mysql ::>>show grants for yeyz@'localhost';

error (): there is no such grant defined for user 'yeyz' on host '192.168.18.%'

可以看到,权限已经完全删除了,此时我们重新创建一个只有select权限的用户:

mysql ::>>grant select on *.* to 'yeyz'@'localhost' identified by password '*6bb4837eb74329105ee4568dda7dc67ed2ca2ad9';

query ok, rows affected (. sec)

我们使用这个用户登录到数据库服务,然后尝试进行select、update以及create操作,结果如下:

[dba_mysql@tk-dba-mysql-stat-10-104 ~]$ /usr/local/mysql/bin/mysql -uyeyz --socket=/data/mysql_4306/tmp/mysql.sock --port=4306 -p -hlocalhost

enter password:

welcome to the mysql monitor. commands end with ; or \g.

your mysql connection id is

server version: 5.5.19-log mysql community server (gpl)

copyright (c) , , oracle and/or its affiliates. all rights reserved.

oracle is a registered trademark of oracle corporation and/or its

affiliates. other names may be trademarks of their respective

owners.

type 'help;' or '\h' for help. type '\c' to clear the current input statement.

mysql ::>>select * from yeyz.yeyz;

+------+

| id |

+------+

| |

| |

| |

+------+

rows in set (0.00 sec)

mysql ::>>update yeyz.yeyz set id= where id=;

error (): update command denied to user 'yeyz'@'localhost' for table 'yeyz'

mysql ::>>create table test (id int);

error (d000): no database selected

mysql ::>>create table yeyz.test (id int);

error (): create command denied to user 'yeyz'@'localhost' for table 'test'

可以发现,这个用户只可以进行select操作,当我们尝试进行update操作和create操作的时候,系统判定这种操作没有权限,直接拒绝了,这就说明使用drop user方法删除用户的时候,会连通db表和权限表一起清除,也就是说删的比较干净,不会对以后的用户产生任何影响。

结论:

当我们想要删除一个用户的时候,尽量使用drop user的方法删除,使用delete方法可能埋下隐患,下次如果创建同名的用户名时,权限控制方面存在一定的问题。

这个演示也解决了一些新手朋友们的一个疑问:为什么我的用户只有usage权限,却能访问所有数据库,并对数据库进行操作?这个时候,你需要看看日志,查询自己有没有进行过delete from mysql.user的操作,如果有,这个问题就很好解释了。

以上就是mysql两种删除用户语句的区别(delete user和drop user)的详细内容,更多关于mysql 删除用户的资料请关注萬仟网其它相关文章!

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值