当前位置:懂科普 >

IT科技

> mysql删除用户权限

mysql删除用户权限

mysql中怎么删除用户呢?不知道的小伙伴来看看小编今天的分享吧!

mysql删除用户有两种方法:

1、使用drop

drop user XXX;删除已存在的用户,默认删除的是'XXX'@'%'这个用户,如果还有其他的用户如'XXX'@'localhost'等,不会一起被删除。如果要删除'XXX'@'localhost',使用drop删除时需要加上host即drop user 'XXX'@'localhost'。

2、使用delete

delete from user where user='XXX' and host='localhost';其中XXX为用户名,localhost为主机名。

3、drop和delete的区别:

drop不仅会将user表中的数据删除,还会删除其他权限表的内容。而delete只删除user表中的内容,所以使用delete删除用户后需要执行FLUSH PRIVILEGES;刷新权限,否则下次使用create语句创建用户时会报错。

mysql删除用户权限

拓展资料:

MySQL添加用户、删除用户、授权及撤销权限

一、创建用户:

mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));

#这样就创建了一个名为:test 密码为:1234 的用户。

注意:此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录。如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录。也可以指定某台机器(例如192.168.1.10),或某个网段(例如192.168.1.%)可以远程登录。

二、为用户授权:

授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 

首先为用户创建一个数据库(testDB):

mysql>create database testDB;

授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):

mysql>grant all privileges on testDB.* to test@localhost identified by '1234';

mysql>flush privileges;//刷新系统权限表,即时生效

如果想指定某库的部分权限给某用户本地操作,可以这样来写:

mysql>grant select,update on testDB.* to test@localhost identified by '1234';

mysql>flush privileges; 

#常用的权限有select,insert,update,delete,alter,create,drop等。可以查看mysql可授予用户的执行权限了解更多内容。

授权test用户拥有所有数据库的某些权限的远程操作:   

mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";

#test用户对所有数据库都有select,delete,update,create,drop 权限。

查看用户所授予的权限:

mysql> show grants for test@localhost;

mysql删除用户权限 第2张

三、删除用户:

mysql>Delete FROM user Where User='test' and Host='localhost';

mysql>flush privileges;

删除账户及权限:

>drop user 用户名@'%';

>drop user 用户名@ localhost; 

四、修改指定用户密码:

mysql>update mysql.user set password=password('新密码') where User="test" and Host="localhost";

mysql>flush privileges;

五、撤销已经赋予用户的权限:

revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:

mysql>grant all on *.* to dba@localhost;

mysql>revoke all on *.* from dba@localhost;

六、MySQL grant、revoke 用户权限注意事项:

grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。

如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 "grant option"

mysql>grant select on testdb.* to dba@localhost with grant option;

mysql>grant select on testdb.* to dba@localhost with grant option;

这个特性一般用不到。实际中,数据库权限最好由 DBA 来统一管理。

  • 文章版权属于文章作者所有,转载请注明 https://dongkepu.com/itkeji/pz30z7.html