4
JackLin的博客
首页
分类
标签
生活
时间轴
关于我
友链
搜索
管理员
《数据库原理及应用》课后实验6_数据库安全管理
数据库
MySQL
发布日期:
2020-05-21 22:55:00
阅读量:
217
所属分类:
数据库设计
前言:这个系列的的文章主要是关于 **《数据库原理及应用》(MySQL版)-清华大学出版社** 的课后实验报告的总结,相当于作为**数据库系统**课程的期末复习吧! # 实验6_数据库安全管理 > 实验需要掌握知识点 - 掌握用户账号的创建,查看,修改,删除方法 - 掌握用户权限的设置方法 - 掌握角色的创建,删除方法 > 具体 SQL 代码 ``` -- 1. 在本地主机创建用户账号st_01,密码为123456。 create user st_01@localhost identified by '123456'; -- 2. 查看MySQL下所有用户账号列表。 use mysql; select DATABASE(); ## 调用系统函数,查看当前所使用的数据库 select * from user; -- 3. 修改用户账号st_01的密码为111111。 set password for st_01@localhost = '111111'; -- 4. 使用studentsdb数据库中的student_info表。 -- (1)授予用户账号st_01查询表的权限。 ## 属于 MySQL 表级别的权限 use linkai_studentsdb; GRANT SELECT ON TABLE linkai_studentsdb.student_info TO st_01@localhost; -- (2)授予用户账号st_01更新家庭住址列的权限。 ## 属于 MySQL 字段级别的权限 GRANT UPDATE(s_address) ON TABLE linkai_studentsdb.student_info TO st_01@localhost; -- (3)授予用户账号st_01修改表结构的权限。 GRANT ALTER ON TABLE linkai_studentsdb.student_info TO st_01@localhost; -- 5. 使用studentsdb数据库中的student_info表。 -- (1)创建存储过程cn_proc,统计student_info表中的学生人数。 delimiter@@ create procedure cn_proc() BEGIN SELECT COUNT(s_number) 学生人数 FROM student_info; END@@ delimiter; CALL cn_proc(); -- (2)授予用户账号st_01调用cn_proc存储过程的权限。 GRANT EXECUTE ON PROCEDURE linkai_studentsdb.cn_proc TO st_01@localhost; -- (3)以用户账号st_01连接MySQL服务器,调用cn_proc存储过程查看学生人数。 CALL cn_proc(); -- 6. 使用studentsdb数据库。 -- (1)授予用户账号st_01在studentsdb数据库上创建表、删除表、查询数据、插入数据的权限。 ## 数据库级别的权限 GRANT CREATE, DROP, SELECT, INSERT ON linkai_studentsdb.* TO st_01@localhost; -- (2)以用户账号st_01连接MySQL服务器,创建新表st_copy,与表student_info完全相同。 create table linkai_studentsdb.st_copy select * from linkai_studentsdb.student_info; -- (3)以用户账号st_01连接MySQL服务器,删除表st_copy。 drop table linkai_studentsdb.st_copy; -- 7. 撤消用户账号st_01在studentsdb数据库上创建表、删除表、查询数据、插入数据的权限。 REVOKE CREATE, DROP, SELECT, INSERT ON linkai_studentsdb.* from st_01@localhost; -- 8. 撤消用户账号st_01所有权限. REVOKE ALL PRIVILEGES, GRANT OPTION FROM st_01@localhost; -- 9. 使用studentsdb数据库中的student_info表。 -- (1)创建本地机角色student。 (注意:角色管理需要 MySQL8.0以上的版本才支持) create ROLE 'student'@'localhost'; -- (2)授予角色student查询student_info表的权限。 ## 表级权限 GRANT SELECT ON TABLE linkai_studentsdb.student_info TO 'student'@'localhost'; -- (3)创建本地机用户账号st_02,密码为123。 CREATE USER st_02@localhost identified BY '123'; -- (4)授予用户账号st_02角色student的权限。 GRANT 'student'@'localhost' TO st_02@localhost; -- 同时记得激活角色 SET global activate_all_roles_on_login = ON; -- (5)以用户账号st_02连接MySQL服务器,查看student_info表信息。 SELECT * FROM linkai_studentsdb.student_info; -- (6)撤消用户账号st_02角色student的权限 REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'student'@'localhost'; -- (7)删除角色student。 DROP ROLE 'student'@'localhost'; -- 10.删除用户账号st_01、st_02。 DROP USER st_01@localhost, st_02@localhost; ```