Mysql 用户权限

# 新建用户
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
grant usage on *.* to 'username'@'%' identified by 'password';

# 修改密码
ALTER USER testuser IDENTIFIED BY '123456';

# 修改当前登录用户
ALTER USER USER() IDENTIFIED BY '123456';

# 授权
grant all privileges on testDB.* to test@localhost identified by '1234';

# 刷新系统权限表
flush privileges;

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

grant all privileges on testDB.* to test@localhost identified by '1234';
flush privileges;//刷新系统权限表

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

2.4 如果想指定部分权限给一用户,可以这样来写:

grant select,update on testDB.* to test@localhost identified by '1234';
flush privileges; //刷新系统权限表

2.5 授权test用户拥有所有数据库的某些权限:

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

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

//@"%" 表示对所有非本地主机授权,不包括localhost。(localhost地址设为127.0.0.1,如果设为真实的本地地址,不知道是否可以,没有验证。)

//对localhost授权:加上一句grant all privileges on testDB.* to test@localhost identified by '1234';即可。

2.6 查看用户授权

show grants for lumengwei@%; 查看用的权限

select * from mysql.dbname where user='test'\G;

mysql.db 和mysql.host 数据库层级

mysql.tables_priv 表层级

mysql.tables_priv 列层级

CREATE ROUTINE, ALTER ROUTINE,

EXECUTE和GRANT权限适用于已存储的子程序。这些权限可以被授予为全局层级和数据库层级。而且,除了CREATE ROUTINE外,这些权限可以被授予为子程序层级,并存储在mysql.procs_priv表中。
  1. 删除用户
mysql>Delete FROM user Where User='test' and Host='localhost';
mysql>flush privileges;
mysql>drop database testDB; //删除用户的数据库
# 删除账户及权限:
>drop user 用户名@'%';
>drop user 用户名@ localhost;
  1. 修改指定用户密码

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


ALTER USER "root"@"localhost" IDENTIFIED  BY "你的新密码";

5.撤销授权

REVOKE privilege ON databasename.tablename FROM 'username'@'host';
revoke all privileges on *.* from developer@127.0.0.1;