permissions
用户和权限信息都放在这张 mysql.user表内!
# 账户、密码、限制连接者所在的IP地址
select user,authentication_string,host from mysql.user;
1
2
2
# 用户管理
# 创建和删除用户
create user '用户名'@'连接者的IP地址' identified by '密码';
create user egon1@127.0.0.1 identified by 'root123';
drop user egon1@127.0.0.1;
# %表示可以匹配值 可以是127.0.0.1、127.0.0.2、127.0.0.3 ...
create user egon2@'127.0.0.%' identified by 'root123';
drop user egon2@'127.0.0.%';
# egon3@'%' 表示可以在任意的IP地址下通过egon3这一账户连接mysql
create user egon3@'%' identified by 'root123';
drop user egon3@'%';
# 建议 '账号'@'IP'都加上引号!
create user 'egon4'@'%' identified by 'root123';
drop user 'egon4'@'%';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 修改用户和IP
rename user '用户名'@'IP地址' to '新用户名'@'IP地址';
rename user egon1@127.0.0.1 to egon1@localhost;
rename user 'egon1'@'127.0.0.1' to 'egon1'@'localhost';
1
2
2
# 修改密码
set password for '用户名'@'IP地址' = Password('新密码')
set password for 'egon4'@'%' = Password('123123');
-- ★修改root账户密码:
alter user'root'@'localhost' identified by '新密码';
flush privileges;
1
2
3
4
5
2
3
4
5
# 授权管理
# 授权
grant 权限 on 数据库.表 to '用户'@'IP地址'
*代表所有!
数据库名.* 数据库中的所有
数据库名.表名 指定数据库中的某张表
数据库名.存储过程名 指定数据库中的存储过程
*.* 所有数据库
1
2
3
4
2
3
4
-- 用户egon拥有所有数据库的所有权限
grant all privileges on *.* TO 'egon'@'localhost';
-- 用户egon拥有数据库day26的所有权限
grant all privileges on day26.* TO 'egon'@'localhost';
-- 用户egon拥有数据库day26中info表的所有权限
-- 账号'egon'@'localhost'就只能查到day26数据库 只能查到info表
grant all privileges on day26.info TO 'egon'@'localhost';
-- 用户egon拥有数据库day26中info表的查询权限
grant select on day26.info TO 'egon'@'localhost';
-- 用户egon拥有数据库day26所有表的查询和插入权限
grant select,insert on day26.* TO 'egon'@'localhost';
grant all privileges on day26db.* to 'egon4'@'%';
注意:flush privileges; -- 将数据读取到内存中,从而立即生效。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 权限
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 由复制从属使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 查看授权
show grants for '用户'@'IP地址'
show grants for 'egon'@'localhost';
show grants for 'egon4'@'%';
1
2
2
# 取消授权
revoke 权限 on 数据库.表 from '用户'@'IP地址'
revoke ALL PRIVILEGES on day26.* from 'egon'@'localhost';
revoke ALL PRIVILEGES on day26db.* from 'egon4'@'%';
注意:flush privileges; -- 将数据读取到内存中,从而立即生效。
1
2
3
4
2
3
4
一般情况下,在很多的正规公司. 数据库都是由DBA来统一进行管理.DBA为每个项目的数据库创建用户.并赋予相关的权限.