一、用户
- 用户的概念:用户可以登陆数据库和管理数据库,因为数据库安全问题和业务需求我们需要使用权限管理。
- 定义用户:用户名@‘IP白名单’ (白名单指的是允许登陆MySQL的IP地址)
- cooh@‘localhost’:cooh只能在本地使用socket登入MySQL
- cooh@‘10.0.0.01’:cooh只能通过 1.0.0.01 远程登入MySQL
- cooh@‘10.0.0.%’: cooh可以通过 1.0.0.xx 远程登入MySQL
- cooh@‘10.0.0.0/255.255.254.0’:子网掩码认证登入MySQL
- 查询用户
select user, host, authentication_string from user;
user Host authentication_string root localhost *13E2C5B0B4E64D072DF491DE5255E17ED91EBC8F mysql.session localhost *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE mysql.sys localhost *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE - 创建用户
-- 创建一个名为 user2 的用户密码为 mysql 不限定ip链接 create user user2@'%' identified by 'mysql';
注意:MySQL 8.0 版本必须先创建用户再授权 v5.0系列的版本创建和授权可以同时进行,需要注意的是新创建的用户如果没有指定权限默认只有登陆的权限。
- 修改用户
-- 修改user2用户的密码为 A186 alter user user2@'%' identified by 'A186';
- 删除用户
-- 删除用户2 drop user user2@'%';
二、权限管理
-
权限概念:限定用户对数据库对操作,表现方式为增删改查。
-
查看权限:show privileges;
Privilege Context Comment Alter Tables To alter the table Alter routine Functions,Procedures To alter or drop stored functions/procedures Create Databases,Tables,Indexes To create new databases and tables Create routine Databases To use CREATE FUNCTION/PROCEDURE Create temporary tables Databases To use CREATE TEMPORARY TABLE Create view Tables To create new views Create user Server Admin To create new users Delete Tables To delete existing rows Drop Databases,Tables To drop databases, tables, and views Event Server Admin To create, alter, drop and execute events Execute Functions,Procedures To execute stored routines File File access on server To read and write files on the server Grant option Databases,Tables,Functions,Procedures To give to other users those privileges you possess Index Tables To create or drop indexes Insert Tables To insert data into tables Lock tables Databases To use LOCK TABLES (together with SELECT privilege) Process Server Admin To view the plain text of currently executing queries Proxy Server Admin To make proxy user possible References Databases,Tables To have references on tables Reload Server Admin To reload or refresh tables, logs and privileges Replication client Server Admin To ask where the slave or master servers are Replication slave Server Admin To read binary log events from the master Select Tables To retrieve rows from table Show databases Server Admin To see all databases with SHOW DATABASES Show view Tables To see views with SHOW CREATE VIEW Shutdown Server Admin To shut down the server Super Server Admin To use KILL thread, SET GLOBAL, CHANGE MASTER, etc. Trigger Tables To use triggers Create tablespace Server Admin To create/alter/drop tablespaces Update Tables To update existing rows Usage Server Admin No privileges - allow connect only 上表为MySQL的所有权限和操作对象及介绍,我们设定权限的时候可以参考使用。
-
设置权限
前提:给其它用户设置权限的前提是 root 或者你拥有权限并拥有 Grant option(将自己的权限赋予其他用户的权限)
设置方式:- ALL:指所有权限,前提是你使用的用户拥有的权限
- 权限1,权限2:可以指定具体的权限面向普通用户
- Grant option:给其它用户授权
对象范围:
*.* 指数据库中所有的库和表
库名.* 指定数据库中所有定表
库名.表名 指定数据库中定指定表
-- 将数据库new_data中的new表中的 new_title、new_content 查询权限给 user1 grant select(new_title, new_content) on new_data.new to user1@'localhost';
如果需要查询创建的权限需要到指定的表中查找:
- User:存储创建的用户和密码包括全局实例级别管理权限设置
- db:存储数据库级别的权限设置
- tables_priv:存放表级别的设置权限
- columns_priv:存放字段级别的设置权限
- procs_priv:存放存储过程中的设置权限
-- 查询列级别的权限 select * from mysql.columns_priv;
Host Db User Table_name Column_name Timestamp Column_priv localhost new_data user1 new new_content 0000-00-00 00:00:00 Select localhost new_data user1 new new_title 0000-00-00 00:00:00 Select 上表为权限查询结果
-
回收权限
回收权限使用 revoke 权限 on 范围 form 用户名@host
-- 回收相关字段的权限 revoke select(new_content, new_title) on new_data.new from user1@'localhost';