MySQL 安全機(jī)制

MySQL 安全控制

DCL(Data Control Language 數(shù)據(jù)庫控制語言)

用于數(shù)據(jù)庫授權(quán)、角色控制等操作

GRANT 授權(quán),為用戶賦予訪問權(quán)限

REVOKE 取消授權(quán),撤回授權(quán)權(quán)限

用戶管理
創(chuàng)建用戶
    create user '用戶名'@'IP地址' identified by '密碼';
刪除用戶
    drop user '用戶名'@'IP地址';
修改用戶
    rename user '用戶名'@'IP地址' to '新用戶名'@'IP地址' ;
修改密碼
    // 第一種方法:
    set password for '用戶名'@'IP地址'=Password('新密碼')

    // 第二種方法:
    alter user '用戶名'@'IP地址' identified by '新密碼';

   // 第三種方法(忘記密碼時(shí),必須使用此方法修改密碼):
    UPDATE mysql.user SET authentication_string='' WHERE user='root' and host='localhost';

PS:用戶權(quán)限相關(guān)數(shù)據(jù)保存在mysql數(shù)據(jù)庫的user表中,所以也可以直接對(duì)其進(jìn)行操作(不建議)

權(quán)限管理
grant  權(quán)限 on 數(shù)據(jù)庫.表  to  '用戶'@'IP地址' identified by '密碼';   -- 授權(quán)并設(shè)置密碼
revoke 權(quán)限 on 數(shù)據(jù)庫.表 from '用戶'@'IP地址'    -- 取消權(quán)限

查看授權(quán)信息

查看授權(quán)語句

show grants for '用戶'@'IP地址';  

查看生效的授權(quán)信息

針對(duì)所有庫和表的權(quán)限,比如 *.* 。 去 mysql.user 中查看

select * from mysql.user where user='shark'\G

針對(duì)具體到庫的權(quán)限,比如db_name.* 。 去 mysql.db 中查看

select * from mysql.db  where user='shark'\G

針對(duì)具體表的授權(quán),在 mysql.tables_priv 中查看

select * from mysql.tables_priv where user='shark'\G

假如是 MySQL8.x

CREATE USER '你的用戶名'@'localhost' IDENTIFIED BY '你的密碼';
#創(chuàng)建新的用戶
GRANT ALL PRIVILEGES ON 你的數(shù)據(jù)庫名.* TO '你的用戶名'@'localhost';
#把剛剛創(chuàng)建的數(shù)據(jù)庫的管理權(quán)限給予剛剛創(chuàng)建的MySQL用戶
FLUSH PRIVILEGES;
#刷新權(quán)限,使用設(shè)置生效

關(guān)于權(quán)限

all privileges  除grant外的所有權(quán)限
select          僅查權(quán)限
select,insert   查和插入權(quán)限
...
usage                   無訪問權(quán)限
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和存儲(chǔ)過程
file                    使用select into outfile 和 load data infile
grant option            使用grant 和 revoke
index                   使用index
insert                  使用insert
lock tables             使用lock table
process                 使用show full processlist
show databases          使用show databases
show view               使用show view
update                  使用update
reload                  使用flush
shutdown                使用mysqladmin shutdown(關(guān)閉MySQL)
super                   使用change master、kill、logs、purge、master和set global。還允許mysqladmin調(diào)試登陸
replication client      服務(wù)器位置的訪問
replication slave       由復(fù)制從屬使用

關(guān)于數(shù)據(jù)庫和表

對(duì)于目標(biāo)數(shù)據(jù)庫以及內(nèi)部其他:
數(shù)據(jù)庫名.*           數(shù)據(jù)庫中的所有
數(shù)據(jù)庫名.表          指定數(shù)據(jù)庫中的某張表
數(shù)據(jù)庫名.存儲(chǔ)過程     指定數(shù)據(jù)庫中的存儲(chǔ)過程
*.*                所有數(shù)據(jù)庫

關(guān)于用戶和 IP

用戶名@IP地址         用戶只能在此 IP 下才能訪問
用戶名@192.168.1.%   用戶只能在此 IP 段下才能訪問(通配符%表示任意)
用戶名@%.shark.com
用戶名@%             用戶可以再任意IP下訪問(默認(rèn)IP地址為%)

Example

create user 'shark'@'%';
grant all privileges on *.*  to 'shark'@'%' identified by '123';

立刻生效

/*將數(shù)據(jù)讀取到內(nèi)存中,從而立即生效。*/
flush privileges

也可以在創(chuàng)建用戶的同時(shí)直接授權(quán)(mysql8.x 不可以)

grant select on *.*     /*設(shè)置查詢數(shù)據(jù)的權(quán)限在所有的庫和表*/
 to 'shark_2'@"%"       /*指定用戶名和來源 ip*/
 identified by '123';   /*設(shè)置密碼*/
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • MySQL 安全控制 DCL(Data Control Language 數(shù)據(jù)庫控制語言) 用于數(shù)據(jù)庫授權(quán)、角色控...
    Lengfin閱讀 385評(píng)論 0 0
  • MySQL 安全控制 DCL(Data Control Language 數(shù)據(jù)庫控制語言) 用于數(shù)據(jù)庫授權(quán)、角色控...
    運(yùn)維開發(fā)_西瓜甜閱讀 3,451評(píng)論 0 16
  • MySQL 安全控制DCL(Data Control Language 數(shù)據(jù)庫控制語言)用于數(shù)據(jù)庫授權(quán)、角色控制等...
    依然frighting閱讀 440評(píng)論 0 0
  • MySQL 安全控制 DCL(Data Control Language 數(shù)據(jù)庫控制語言) 用于數(shù)據(jù)庫授權(quán)、角色控...
    你笑的那么美丶閱讀 117評(píng)論 0 0
  • mysql用戶管理:創(chuàng)建用戶 方法一、CREATE USER user1@'localhost' IDENTIFI...
    name_cc2f閱讀 524評(píng)論 0 0

友情鏈接更多精彩內(nèi)容