MySQL 添加用户并分配权限

编辑于 2016-05-09

* 移动设备下, 可左滑手指以查看较宽代码

新建数据库

create database test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

新建用户

对于 MySQL5.7+

# localhost 代表本机
CREATE USER hzx@'localhost' IDENTIFIED BY 'passwd';
insert into mysql.user(Host,User,Password) values(‘localhost’,'jeecn',password('jeecn'));
flush privileges;

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

为用户授权

grant all on jeecn.* to jeecn@localhost identified by 'passwd';
flush privileges;

即授权 jeecn 用户拥有 jeecn 数据库的所有权限

删除用户

DELETE FROM user WHERE User=”jeecn” and Host=”localhost”;

修改指定用户密码

update mysql.user set password=password(‘新密码’) where User=”jeecn” and Host=”localhost”;
flush privileges;
quit;