mysql学习------权限机制
db权限表:记录各个帐号在各个数据库上的操作权限。
table_priv权限表:记录数据表级的操作权限。
columns_priv权限表:记录数据列级的操作权限。
host权限表(已废弃):配合db权限表对给定主机上数据库级操作权限作更细致的控制。这个权限表不受GRANT和REVOKE语句的影响。
账户权限信息被存储在mysql数据库的user、db、host、tables_priv、columns_priv和procs_priv表中。在MySQL启动时时,服务器将这些数据库表内容读入内存。
GRANT和REVOKE语句所用的涉及权限的名称显示在下表,还有在授权表中每个权限的表列名称和每个权限有关的上下文。
权限 |
列 |
上下文 |
CREATE |
Create_priv |
数据库、表或索引 |
DROP |
Drop_priv |
数据库或表 |
GRANT OPTION |
Grant_priv |
数据库、表或保存的程序 |
REFERENCES |
References_priv |
数据库或表 |
ALTER |
Alter_priv |
表 |
DELETE |
Delete_priv |
表 |
INDEX |
Index_priv |
表 |
INSERT |
Insert_priv |
表 |
SELECT |
Select_priv |
表 |
UPDATE |
Update_priv |
表 |
CREATE VIEW |
Create_view_priv |
视图 |
SHOW VIEW |
Show_view_priv |
视图 |
ALTER ROUTINE |
Alter_routine_priv |
保存的程序 |
CREATE ROUTINE |
Create_routine_priv |
保存的程序 |
EXECUTE |
Execute_priv |
保存的程序 |
FILE |
File_priv |
服务器主机上的文件访问 |
CREATE TEMPORARY TABLES |
Create_tmp_table_priv |
服务器管理 |
LOCK TABLES |
Lock_tables_priv |
服务器管理 |
CREATE USER |
Create_user_priv |
服务器管理 |
PROCESS |
Process_priv |
服务器管理 |
RELOAD |
Reload_priv |
服务器管理 |
REPLICATION CLIENT |
Repl_client_priv |
服务器管理 |
REPLICATION SLAVE |
Repl_slave_priv |
服务器管理 |
SHOW DATABASES |
Show_db_priv |
服务器管理 |
SHUTDOWN |
Shutdown_priv |
服务器管理 |
SUPER |
Super_priv |
服务器管理 |
案例分析:
[root@mysrv ~]# mysql -u root -poracle
mysql> select version()\g
+-------------------------------------------+
| version() |
+-------------------------------------------+
| 5.6.25-enterprise-commercial-advanced-log |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| prod |
| test |
+--------------------+
5 rows in set (0.01 sec)
1、建立tom用户并授权(特权管理用户)
mysql> grant all on prod.* to 'tom'@'%' identified by 'tom' with grant option;
Query OK, 0 rows affected (0.00 sec)
查看用户创建是否成功:
mysql> select user,host from user ;
- +-------+-----------+
- | user | host |
- +-------+-----------+
- | tom | % |
- | root | 127.0.0.1 |
- | root | ::1 |
- | | localhost |
- | root | localhost |
- | scott | localhost |
- | | mysrv |
- | root | mysrv |
- +-------+-----------+
- 8 rows in set (0.00 sec)
查看tom用户的授权:
mysql> show grants for tom;
+----------------------------------------------------------------------------------------------------+
| Grants for tom@% |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'%' IDENTIFIED BY PASSWORD '*71FF744436C7EA1B954F6276121DB5D2BF68FC07' |
| GRANT ALL PRIVILEGES ON `prod`.* TO 'tom'@'%' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------+
GRANT 语法:
GRANT privileges (columns)
ON what
TO user IDENTIFIED BY "password"
WITH GRANT OPTION
权限列表:
ALTER: 修改表和索引。
CREATE: 创建数据库和表。
DELETE: 删除表中已有的记录。
DROP: 抛弃(删除)数据库和表。
INDEX: 创建或抛弃索引。
INSERT: 向表中插入新行。
REFERENCE: 未用。
SELECT: 检索表中的记录。
UPDATE: 修改现存表记录。
FILE: 读或写服务器上的文件。
PROCESS: 查看服务器中执行的线程信息或杀死线程。
RELOAD: 重载授权表或清空日志、主机缓存或表缓存。
SHUTDOWN: 关闭服务器。
ALL: 所有权限,ALL PRIVILEGES同义词。
USAGE: 特殊的 "无权限" 权限。
用 户账户包括 "username" 和 "host" 两部分,后者表示该用户被允许从何地接入。tom@'%' 表示任何地址,默认可以省略。还可以是 "tom@192.168.1.%"、"tom@%.abc.com" 等。数据库格式为 db@table,可以是 "test.*" 或 "*.*",前者表示 test 数据库的所有表,后者表示所有数据库的所有表。
子句 "WITH GRANT OPTION" 表示该用户可以为其他用户分配权限。
2、我们用 root 再创建几个用户,然后由 test 数据库的管理员tom为他们分配权限。
mysql> create user 'tom1' identified by 'tom1' ,'tom2' identified by 'tom2';
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from user ;
- +-------+-----------+
- | user | host |
- +-------+-----------+
- | tom | % |
- | tom1 | % |
- | tom2 | % |
- | root | 127.0.0.1 |
- | root | ::1 |
- | | localhost |
- | root | localhost |
- | scott | localhost |
- | | mysrv |
- | root | mysrv |
- +-------+-----------+
- 10 rows in set (0.00 sec)
root用户退出,tom登陆,并授权用户访问prod库
[root@mysrv ~]# mysql -u tom -ptom
ERROR 1045 (28000): Access denied for user 'tom'@'localhost' (using password: YES)
tom用户竟不能登陆!!!
再对tom用户授权:
mysql> grant all on prod.* to 'tom'@'localhost' identified by 'tom' with grant option;;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for tom;
+----------------------------------------------------------------------------------------------------+
| Grants for tom@% |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'%' IDENTIFIED BY PASSWORD '*71FF744436C7EA1B954F6276121DB5D2BF68FC07' |
| GRANT ALL PRIVILEGES ON `prod`.* TO 'tom'@'%' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> use mysql;
Database changed
mysql> select user,host from user ;
- +-------+-----------+
- | user | host |
- +-------+-----------+
- | tom | % |
- | tom1 | % |
- | tom2 | % |
- | root | 127.0.0.1 |
- | root | ::1 |
- | | localhost |
- | root | localhost |
- | scott | localhost |
- | tom | localhost |
- | | mysrv |
- | root | mysrv |
- +-------+-----------+
- 11 rows in set (0.00 sec)
tom登陆:
[root@mysrv ~]# mysql -u tom -ptom prod
mysql> select database();
+------------+
| database() |
+------------+
| prod |
+------------+
1 row in set (0.01 sec)
mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| tom@localhost |
+----------------+
1 row in set (0.00 sec)
创建表:
mysql> show tables;
+----------------+
| Tables_in_prod |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
mysql> create table t2 as select * from t1;
Query OK, 3 rows affected (0.15 sec)
Records: 3 Duplicates: 0 Warnings: 0
查看表信息:
mysql> desc t2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> show create table t2;
+-------+---------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------+
| t2 | CREATE TABLE `t2` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
mysql> show create table t2\G;
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> select * from t2;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
+------+-------+
3 rows in set (0.00 sec)
3、tom用户为tom1,tom2授权
mysql> grant select on prod.* to tom1;
Query OK, 0 rows affected (0.00 sec)
mysql> grant select on prod.* to tom2;
Query OK, 0 rows affected (0.02 sec)
mysql> grant insert,update on prod.* to tom2;
Query OK, 0 rows affected (0.00 sec)
tom2登陆(从远程登陆):
C:\Users\Administrator>mysql -h 192.168.8.240 -utom2 -ptom2
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> use prod;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| prod |
+------------+
1 row in set (0.00 sec)
mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| tom2@% |
+----------------+
1 row in set (0.00 sec)
mysql> show grants for tom2;
+------------------------------------------------------------------+
| Grants for tom2@% |
+------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom2'@'%' IDENTIFIED BY PASSWORD <secret> |
| GRANT SELECT, INSERT, UPDATE ON `prod`.* TO 'tom2'@'%' |
+------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_prod |
+----------------+
| t1 |
| t2 |
+----------------+
2 rows in set (0.00 sec)
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
+------+-------+
3 rows in set (0.00 sec)
mysql> select * from t2;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
+------+-------+
3 rows in set (0.00 sec)
mysql> insert into t1 values (40,'john');
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.09 sec)
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
| 40 | john |
+------+-------+
4 rows in set (0.00 sec)
mysql> update t1 set name='ellen' where id=40;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
| 40 | ellen |
+------+-------+
4 rows in set (0.00 sec)
mysql> delete from t1;
ERROR 1142 (42000): DELETE command denied to user 'tom2'@'192.168.8.254' for tab
le 't1'
mysql> commit;
Query OK, 0 rows affected (0.05 sec)
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
| 40 | ellen |
+------+-------+
4 rows in set (0.00 sec)
4、回收tom2的update权限:
mysql> revoke update on prod.* from tom2;
Query OK, 0 rows affected (0.00 sec)
tom2再重新登陆:
C:\Users\Administrator>mysql -h 192.168.8.240 -utom2 -ptom2
mysql> use prod;
Database changed
mysql> update t1 set name='lily' where id=10;
ERROR 1142 (42000): UPDATE command denied to user 'tom2'@'192.168.8.254' for tab
le 't1'
---update失败!
二、修改用户口令:
1、root用户修改普通用户口令
mysql> set password for tom1=password('oracle');
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
tom1重新登陆:
C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -ptom1
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'tom1'@'192.168.8.254' (using passwor
d: YES)
---旧口令登陆失败!
C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -poracle
mysql>
2、普通用户修改自己密码:
C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -poracle
mysql> set password=password('tom1');
Query OK, 0 rows affected (0.00 sec)
重新登陆:
C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -ptom1
mysql>
---新密码登陆成功 !
mysql> revoke all on prod.* from tom2;
Query OK, 0 rows affected (0.01 sec)
2、删除用户
mysql> drop user tom2;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from user;
- +-------+-----------+
- | user | host |
- +-------+-----------+
- | jerry | % |
- | rose | % |
- | tom | % |
- | tom1 | % |
- | root | 127.0.0.1 |
- | root | ::1 |
- | | localhost |
- | jerry | localhost |
- | root | localhost |
- | rose | localhost |
- | scott | localhost |
- | tom | localhost |
- | | mysrv |
- | root | mysrv |
- +-------+-----------+
- 14 rows in set (0.00 sec)
------- 摘要 --------------------------------------
创建用户:
GRANT insert, update ON testdb.* TO user1@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
CREATE USER user2 IDENTIFIED BY 'password';
分配权限:
GRANT select ON testdb.* TO user2;
查看权限:
SHOW GRANTS FOR user1;
修改密码:
SET PASSWORD FOR user1 = PASSWORD('newpwd');
SET PASSWORD = PASSWORD('newpwd');
移除权限:
REVOKE all ON *.* FROM user1;
删除用户:
DROP USER user1;
数据库列表:
SHOW DATABASES;
数据表列表:
SHOW TABLES;
当前数据库:
SELECT DATABASE();
当前用户:
SELECT USER();
数据表结构:
DESCRIBE table1;
刷新权限:
FLUSH PRIVILEGES;
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 on testdb.* to common_user@’%’
grant insert on testdb.* to common_user@’%’
grant update on testdb.* to common_user@’%’
grant delete on testdb.* to common_user@’%’
或者,用一条 MySQL 命令来替代:
grant select, insert, update, delete on testdb.* to common_user@’%’
grant 数据库开发人员,创建表、索引、视图、存储过程、函数。。。等权限。
grant 创建、修改、删除 MySQL 数据表结构权限。
grant create on testdb.* to developer@’192.168.0.%’;
grant alter on testdb.* to developer@’192.168.0.%’;
grant drop on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 外键权限。
grant references on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 临时表权限。
grant create temporary tables on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 索引权限。
grant index on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 视图、查看视图源代码 权限。
grant create view on testdb.* to developer@’192.168.0.%’;
grant show view on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 存储过程、函数 权限。
grant create routine on testdb.* to developer@’192.168.0.%’; -- now, can show procedure status
grant alter routine on testdb.* to developer@’192.168.0.%’; -- now, you can drop a procedure
grant execute on testdb.* to developer@’192.168.0.%’;
grant 普通 DBA 管理某个 MySQL 数据库的权限。
grant all privileges on testdb to dba@’localhost’
其中,关键字 “privileges” 可以省略。
grant 高级 DBA 管理 MySQL 中所有数据库的权限。
grant all on *.* to dba@’localhost’
1. grant 作用在整个 MySQL 服务器上:
grant select on *.* to dba@localhost; -- dba 可以查询 MySQL 中所有数据库中的表。
grant all on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有数据库
2. grant 作用在单个数据库上:
grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表。
3. grant 作用在单个数据表上:
grant select, insert, update, delete on testdb.orders to dba@localhost;
4. grant 作用在表中的列上:
grant select(id, se, rank) on testdb.apache_log to dba@localhost;
5. grant 作用在存储过程、函数上:
grant execute on procedure testdb.pr_add to ’dba’@’localhost’
grant execute on function testdb.fn_add to ’dba’@’localhost’
注意:修改完权限以后 一定要刷新服务,或者重启服务,刷新服务用:FLUSH PRIVILEGES。
mysql学习------权限机制的更多相关文章
- 我的MYSQL学习心得(十三) 权限管理
我的MYSQL学习心得(十三) 权限管理 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) ...
- Mysql学习笔记(十三)权限管理
学习内容: 1.权限管理: 关于mysql的权限简单的理解就是mysql允许你做你权利以内的事情,不可以越界.比如只允许你执行select操作,那么你就不能执行update操作.只允许你从某台机器上连 ...
- 我的MYSQL学习心得 mysql的权限管理
这一篇<我的MYSQL学习心得(十三)>将会讲解MYSQL的用户管理 在mysql数据库中,有mysql_install_db脚本初始化权限表,存储权限的表有: 1.user表 2.db表 ...
- mysql学习【第6篇】:权限和数据库设计
狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! mysql学习[第6篇]:权限和数据库设计 用户和权限管理 /* 用户和权限管理 */ ---- ...
- MySQL学习——管理用户权限
MySQL学习——管理用户权限 摘要:本文主要学习了使用DCL语句管理用户权限的方法. 了解用户权限 什么是用户 用户,指的就是操作和使用MySQL数据库的人.使用MySQL数据库需要用户先通过用户名 ...
- 【大白话系统】MySQL 学习总结 之 缓冲池(Buffer Pool) 的设计原理和管理机制
一.缓冲池(Buffer Pool)的地位 在<MySQL 学习总结 之 InnoDB 存储引擎的架构设计>中,我们就讲到,缓冲池是 InnoDB 存储引擎中最重要的组件.因为为了提高 M ...
- MySQL学习笔记二:权限管理
1. 创建和删除用户,mysql中的用户是由用户名和主机名来确定的 create user "user_name@host_name" identified by passwd; ...
- python 学习笔记十八 django深入学习三 分页,自定义标签,权限机制
django Pagination(分页) django 自带的分页功能非常强大,我们来看一个简单的练习示例: #导入Paginator>>> from django.core.p ...
- MYSQL用户权限管理学习笔记
MYSQL 用户管理 1.权限表 MYSQL是一个多用户的数据库,MYSQL的用户可以分为两大类: (1) 超级管理员用户(root),拥有全部权限 (2) 普通用户,由roo ...
随机推荐
- 微信小程序input组件抖动及textarea组件光标错位解决方案
问题一: 使用微信小程序input组件时,在移动端唤起focus或blur事件时,因光标占位导致内容出现叠影及抖动现象. 解决方案: 用<textarea>组件代替了<input/& ...
- ElasticSearch 2 (25) - 语言处理系列之同义词
ElasticSearch 2 (25) - 语言处理系列之同义词 摘要 词干提取有助于通过简化屈折词到它们词根的形式来扩展搜索的范围,而同义词是通过关联概念和想法来扩展搜索范围的.或许没有文档能与查 ...
- Final发布点评
1. 约跑App——nice!:为改进演示效果,本组使用摄像头实时采集投影的方式展示其作品,是一种演示的创新.本组重点放在了修改上次来着其他组发现的bug,不过新功能上好像没有加入多少,可能是保证软 ...
- Ubuntu中sublime和Foxit Reader不能使用中文输入法解决方案
虽然Ubuntu下面很多软件同windows下一样,但是经常会出现各种各样的小问题,其中最让人头疼的是软件中的输入法问题. sublime作为一个跨平台的编辑软件,可以支持win,linux和mac系 ...
- Node.js & SSR
Node.js & SSR nest.js https://github.com/nestjs/nest next.js 中文文档 https://nextjs.org/learn/ Grap ...
- Java 多线程初级汇总
多线程概述 抢占式多任务 直接中断而不需要事先和被中断程序协商 协作多任务 被中断程序同意交出控制权之后才能执行中断 多线程和多进程区别? 本质的区别在于每个进程有它自己的变量的完备集,线程则共享相同 ...
- pgm3
这部分主要讨论了一些概念性的东西.一个是常用的 local probabilistic models,一个是如何用 template-based representation. 这部分主要是一些概念, ...
- codeforces 889A
A. Petya and Catacombs time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 通过my.ini修改mysql默认编码为gbk
如何一次性修改后台显示语言为gbk 1. 找到my.ini(这是一个Mysql的配置文件) 1.1 要先打开显示隐藏文件的设置:https://jingyan.baidu.com/article/da ...
- 如何解决每次打开office 都会出现正在配置的问题
原因:安装offiece的时候直接选择以前安装过的office文件夹,导致文件冲突 解决方法:卸载,然后对准备要安装的文件夹清空或者重新建个新文件夹安装