在MySQL中支持创建账户,并给账户分配权限,例如:只拥有数据库A操作的权限、只拥有数据库B中某些表的权限、只拥有数据库B中某些表的读权限等。
1用户管理
在MySQL的默认数据库 mysql
中的 user
表中存储着所有的账户信息(含账户、权限等)。
select user,authentication_string,host from mysql.user;
- 创建和删除用户
create user '用户名'@'连接者的IP地址' identified by '密码';
create user xxx @127.0.0.1 identified by '123';
drop user xxx@127.0.0.1;
create user xxx@'127.0.0.%' identified by '123';
drop user xxx@'127.0.0.%';
create user xxx@'%' identified by '123';
drop user xxx@'%';
create user 'xxx'@'%' identified by '123';
drop user 'xxx'@'%';
- 修改用户
rename user '用户名'@'IP地址' to '新用户名'@'IP地址';
rename user xxx@127.0.0.1 to xx111@localhost;
rename user 'xxx'@'127.0.0.1' to 'xx111'@'localhost';
- 修改密码
set password for '用户名'@'IP地址' = Password('新密码')
2 授权管理
- 授权
grant 权限 on 数据库.表 to '用户'@'IP地址'
grant all privileges on *.* TO 'xxx'@'localhost'; -- 用户xxx拥有所有数据库的所有权限
grant all privileges on day26.* TO 'xxx'@'localhost'; -- 用户xxx拥有数据库day26的所有权限
grant all privileges on day26.info TO 'xxx'@'localhost'; -- 用户xxx拥有数据库day26中info表的所有权限
grant select on day26.info TO 'xxx'@'localhost'; -- 用户xxx拥有数据库day26中info表的查询权限
grant select,insert on day26.* TO 'xxx'@'localhost'; -- 用户xxx拥有数据库day26所有表的查询和插入权限
注意:flush privileges; -- 将数据读取到内存中,从而立即生效。
- 对于权限
all privileges 除grant外的所有权限
select 仅查权限
select,insert 查和插入权限
...
usage 无访问权限
alter 使用alter table
alter routine 使用alter procedure和drop procedure
create 使用create table
create routine 使用create procedure
create temporary tables 使用create temporary tables
create user 使用create user、drop user、rename user和revoke all privileges
create view 使用create view
delete 使用delete
drop 使用drop table
execute 使用call和存储过程
file 使用select into outfile 和 load data infile
grant option 使用grant 和 revoke
index 使用index
insert 使用insert
lock tables 使用lock table
process 使用show full processlist
select 使用select
show databases 使用show databases
show view 使用show view
update 使用update
reload 使用flush
shutdown 使用mysqladmin shutdown(关闭MySQL)
super 使用change master、kill、logs、purge、master和set global。还允许mysqladmin调试登陆
replication client 服务器位置的访问
replication slave 由复制从属使用
- 对于数据库和表
数据库名.* 数据库中的所有
数据库名.表名 指定数据库中的某张表
数据库名.存储过程名 指定数据库中的存储过程
*.* 所有数据库
- 查看授权
show grants for '用户'@'IP地址'
- 取消权限
revoke 权限 on 数据库.表 from '用户'@'IP地址'