概念: mysql是一个tcp服务器,可用来操作服务器上的文件数据,接收用户端发送的指令时,需要考虑到安全问题。 mysql 自带的数据库中有4个表用于用户管理的: 优先级从高到低: user --> db --> tables_priv -->columns_priv
一、创建用户
先选择user 表,在此创建的用户信息在db表零也会存在!
create user 用户名@‘主机地址’ identified by ‘密码‘;
create user michael@‘127.0.0.1‘ identified by‘123‘;
#此处创建的用户michael在db 和 user 里都能查到。
二、给用户授权
grant [权限的名称 select... insert...|all] on 数据库.表名 to 用户名@‘主机地址‘;
grant select(id ,name), insert(id,name) on day41.stu to michael@‘localhost‘;
#给用户michael设置 查找,赋值 day41.stu里的id,name 的功能。
grant all on day41.stu to michael@‘localhost‘ ;
#可以访问day41里的stu这个表
grant all on day41.* to michael@‘localhost‘;
#可以访问day41这个库里所有的表
grant all on *.* to michael@‘localhost‘ ;
#可以访问所有的库和表
三、权限授予 将user拥有的权限授予其他账户,自身还有,相当于复制
grant [权限的名称 select... insert... |all] on 数据库.表名 to 用户名@‘主机地址’ with grant option;
grant all on *.* to michael@‘127.0.0.1’;
#当用户michael不存在时,会自动创建该用户!
四、删除权限
revoke 权限的名称 on 数据库.表名 from 用户名@‘主机地址’;
revoke all on *.* from michael@‘127.0.0.1‘;
#此处删除michael所拥有的权限,会 残留权限grant_priv,可以利用字段的改值方法将其重新赋值为N即可!!
update mysql.user set grant_priv = ‘n‘ where user=‘michael‘ and host=‘localhost‘;
#这一步删去了grant_priv 这一权限。
操作完以后,可以利用:
flush privileges;
五、删除用户
drop user 用户名@‘主机地址’;
这一步会将db里面之前创建的用户信息也删除!
原文:https://www.cnblogs.com/changwenjun-666/p/10878510.html