Tip:
在MySQL中,我们经常需要创建用户和删除用户,创建用户时,我们一般使用create user或者grant语句来创建,create语法创建的用户没有任何权限,需要再使用grant语法来分配权限,而grant语法创建的用户直接拥有所分配的权限。在一些测试用户创建完成之后,做完测试,可能用户的生命周期就结束了,需要将用户删除,而删除用户在MySQL中一般有两种方法,一种是drop user
,另外一种是delete from mysql.user
,
本文来源gaodai.ma#com搞##代!^码@网3
那么这两种方法有什么区别呢?我们这里通过例子演示。
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) [email protected]:(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)