创建
create user test_user IDENTIFIED by 'xxxxx';
identified by 会将纯文本密码加密作为散列值存储
查看
select host,user,password from user ;
查看当前用户(自己)权限:
show grants;
查看其他 MySQL 用户权限:
show grants for dba@localhost;
删除
drop user '用户名'@'来源';
如果没有指定来源,会把这个用户名的所有账户都删除。
mysql5之前删除用户时必须先使用revoke 删除用户权限,然后删除用户,mysql5之后drop 命令可以删除用户的同时删除用户的相关权限
注意:如果删除一个用户,但是连接还存在(show processlist),可用kill ID删掉。
更改密码
set password for test_user =password('xxxxxx');
update mysql.user set password=password('xxxx') where user='test_user';
查看用户权限
show grants for test_user;
赋予权限
grant select on test_db.* to test_user;
回收权限
revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可,如果权限不存在会报错。
revoke all on *.* from test_user@localhost;
revoke select on test_db.* from test_user;
如果想立即看到结果使用
flush privileges ;
命令更新
设置权限时必须给出一下信息
1,要授予的权限
2,被授予访问权限的数据库或表
3,用户名
grant和revoke可以在几个层次上控制访问权限
1,整个服务器,使用 grant ALL 和revoke ALL
2,整个数据库,使用on database.*
3,特点表,使用on database.table
4,特定的列
5,特定的存储过程
user表中host列的值的意义
% 匹配所有主机
localhost localhost不会被解析成IP地址,直接通过UNIXsocket连接
127.0.0.1 会通过TCP/IP协议连接,并且只能在本机访问;
::1 ::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1
grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利。
grant select, insert, update, delete on test_db.* to test_user@'%'
grant 数据库开发人员,创建表、索引、视图、存储过程、函数。。。等权限。
grant 创建、修改、删除 MySQL 数据表结构权限。
grant create,alter,drop on test_db.* to test_user@'192.168.0.%';
grant 操作 MySQL 外键权限。
grant references on test_db.* to test_user@'192.168.0.%';
grant 操作 MySQL 临时表权限。
grant create temporary tables on test_db.* to test_user@'192.168.0.%';
grant 操作 MySQL 索引权限。
grant index on test_db.* to test_user@'192.168.0.%';
grant 操作 MySQL 视图、查看视图源代码 权限。
grant create view on test_db.* to test_user@'192.168.0.%';
grant show view on test_db.* to test_user@'192.168.0.%';
grant 操作 MySQL 存储过程、函数 权限。
grant create routine on test_db.* to test_user@'192.168.0.%';
grant alter routine on test_db.* to test_user@'192.168.0.%';
grant execute on test_db.* to test_user@'192.168.0.%';
grant 普通 DBA 管理某个 MySQL 数据库的权限。
grant all privileges on test_db to test_user@'localhost'
其中,关键字 “privileges” 可以省略。
grant 高级 DBA 管理 MySQL 中所有数据库的权限。
grant all on *.* to test_user@'localhost'
MySQL grant 权限,分别可以作用在多个层次上。
grant 作用在整个 MySQL 服务器上:
grant select on *.* to test_user@localhost;
test_user 可以查询 MySQL 中所有数据库中的表。
grant all on *.* to test_user@localhost;
test_user 可以管理 MySQL 中的所有数据库
grant 作用在单个数据库上:
grant select on test_db.* to test_user@localhost;
test_user 可以查询 test_db 中的表。
grant 作用在单个数据表上:
grant select, insert, update, delete on test_db.test_table to test_user@localhost;
grant 作用在表中的列上:
grant select(test_col_1, test_col_2, test_col_3) on test_db.test_table to test_user@localhost;
grant 作用在存储过程、函数上:
grant execute on procedure test_db.test_table to test_user@'localhost'
grant execute on function test_db.test_table to test_user@'localhost'
注意:修改完权限以后 一定要刷新服务,或者重启服务,刷新服务用:FLUSH PRIVILEGES。
关于用户权限生效时间:
1,如果是通过GRANT、REVOKE、SET PASSWORD、RENAME USER等修改,权限立即生效,因为这些命令将触发系统重新载入授权表(GRANT TABLES)到内存。
2,如果是手动修改字典表方式(INSERT、UPDATE、DELETE),不会马上生效,触发重启MySQL服务,或者主动触发授权表的重新装载。
表或列粒度的权限将在客户端下次执行操作时生效。
数据库级的权限将在客户端执行USE db_name语句,切换数据库时生效。
全局权限和密码修改,对当前已连接的客户端无效,下次连接时才会生效。