1、用户不存在,授权数据库权限的同时,并创建用户。

# 授权,创建用户,设置密码
mysql> grant all on *.* to jhtest@'%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

2、为已存在的用户授权,只需要把 identified by 'jhtest' 去掉。

MySQL grant 权限,分别可以作用在多个层次上。

grant 作用在整个 MySQL 服务器上:
grant select on *.* to dba@localhost;
# 可以查询 MySQL 中所有数据库中的表。
grant all on *.* to dba@localhost;
# 可以管理 MySQL 中的所有数据库


grant 作用在单个数据库上:
grant select on testdb.* to dba@localhost;
# 可以查询 testdb 中的表。


grant 作用在单个数据表上:
grant select, insert, update, delete on testdb.orders to dba@localhost;

3、查看权限

查看当前用户(自己)权限:
show grants;

查看其他 MySQL 用户权限:
show grants for dba@localhost;

4、撤销已经赋予给 MySQL 用户权限的权限。

revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:
grant all on *.* to dba@localhost;
revoke all on *.* from dba@localhost;