1. 程式人生 > 其它 >mysql 資料庫使用者管理與使用者授權

mysql 資料庫使用者管理與使用者授權

mysql 資料庫使用者管理與授權

目錄

一 : mysql 使用者管理與恢復root 密碼

1 新建使用者

create user '使用者名稱'@‘登入地址' [ identitified by '密碼' ];


select password('密碼');

create user ’使用者名稱'@‘登入地址’ identified by '加密密文';


使用者名稱:指定將建立的使用者名稱

登入地址指新建立的使用者可以從哪裡登入資料庫。可以適應 ip 地址,網段,主機名,本地使用者為localhost,任意主機都可以登入為’%‘

密碼:

(1)若使用明文面,則直接輸入。如果要使用加密密碼則下使用select password('密碼') ;獲取密文,再在語句中新增password '密文';

(2) 如果省略 'identified by' 部分,則使用者的密碼將為空


#顯示當前登入使用者與登入地點
select user();

#明文建立本地登入使用者test ,並設定密碼abc123
create user 'test'@'localhost' identified by 'abc123';

#密文建立使用者
#先檢視密碼  abc123 的加密密碼
select password('abc123');
#在使用加密後的密碼,建立本地登入使用者test2
create user 'test2'@'localhost' identified by password '*6691484EA6B50DDDE1926A220DA01FA9E575C18A';




2 檢視使用者資訊與顯示登入使用者

select * from mysql.user \G

select User,Host,authentication_string from mysql.user;

mysql 資料庫使用者資訊儲存在mysql 庫 的user表中,包括了使用者名稱,使用者登入地點,使用者許可權,使用者密碼加密密文等。其中 User 項儲存了使用者名稱,Host 項儲存了使用者登入地點,autentication_string 儲存了使用者密碼加密密文

select user,host,authentication_string from mysql.user;



3 重新命名使用者

rename user '使用者名稱'@‘登入地點’ to '新使用者名稱'@'新登入地點';

'使用者名稱'@'登入地點' 必須時存在的。

#將本地登入的使用者test2 該文任意地點都可以登入的使用者test
rename  user 'test2'@'localhost' to 'test'@'%';



4 刪除使用者

drop user '使用者名稱'@'客戶端登入地址';

刪除使用者必須要有管理員許可權才可以。

如果不加 @'客戶端登入地址', 相當於刪除 '使用者名稱'@'%'\

並且,使用者名稱和登入地址要對應

#刪除任意主機登入的使用者 ’test‘@'%'.相當於  drop user t 'test'@'%'
drop user  'test';  
#刪除本地登入的使用者test
drop user  'test'@'localhost' ;



5 修改密碼

  1. 修改當前使用者密碼

set password=password('新密碼');

  1. root 使用者修改其他使用者密碼

set password for '使用者名稱'@'登入地點'=password('密碼新');

  1. 通過修改mysql.user 表 修改使用者密碼

update mysql.user set authentication_string=password('新密碼') where 條件表示式;

#設定當前登入使用者密碼為 123456
set password=password('123456');

#設定本地登入的root 使用者密碼為 abc123
set password for 'root'@'localhost'=password('abc123');

#修改mysql.user 表的 user欄位值為 'user2' 的表記錄,將它的authentication_string 欄位值改為加密後的123456 
#也就是修改 所有 使用者名稱為 user2 的使用者的密碼為123456
update   mysql.user set authentication_string=password('123456') where user='user2';



6 恢復root 密碼

我們可以通過配置檔案中myseld 設定skip-grant-table'項來跳過授權表,匿名登入。然後修改 mysql 庫user表中root 的authentication 欄位,來修改root密碼。


6.1 修改配置檔案,設定跳過密碼項

vim /etc/my.conf
[mysqld]
skip-grant-tables     #在[mysqd] 項中新增 skip-grant-table 跳過授表

6.2 重啟mysql 服務,匿名進入mysql ,修改root 密碼

systemctl restart mysqld  #重啟mysql的服務
mysql             #匿名進入mysql
# 通過修改 mysql.usr 的authentication_string 欄位,來修改 'root'@'locathost' 使用者的密碼
update mysql.user set authentication_string=password('abc123') where user='root' and host='localhost';
#重新整理授權表
flush privileges;
#退出匿名登入
exit

6.3 刪除配置檔案中跳過授權表設定, 重啟服務,使用root 使用者登入資料庫

vim /etc/my.cnf
[mysqld]
#將 skip-grant-tables 刪除
#skip-grant-tables   

#重啟mysqld 服務
systemctl restart mysqld

#使用root 登入進入資料庫
mysql -uroot -pabc123
#檢視當前登入使用者與登入地址
select user();   



二: mysql 授權管理

我們使用create user 建立的使用者,只能登入進入資料庫,但是無法做任何事情。

所以還需要grant 命令進行 使用者授權。同時,此命令,如果當用戶不存在,也可以建立該使用者。


1 授予許可權

grant 許可權列表 on 資料庫名.表名 to '使用者名稱'@'來源地址' identified by ‘密碼’ with grant option;

許可權列表:多個許可權用逗號‘,’ 隔開,如 'select,update' 。 all 表示所有許可權

資料庫名.表名:用於指定授權操作的資料庫名和表名,可以使用萬用字元' *' 表示所有。如 *.* 表示所有庫,所有表

'使用者名稱'@'來源地址' : 使用者指定使用者名稱和可以登入的客戶端地址。可以使用百分號 % 來表示某個區域的所有地址。 'root'@'%' 表示任意主機都可以登入, 'root'@'192.168.23.%' 表示192.168.23.0 整個網段。'root'@'%.mynet.com' 表示所有以.mynet.com 結尾的主機

identified by :用於設定使用者連資料庫時候的密碼。在新建使用者時,如果省略此部分,則使用者密碼為空。但是我們在配置檔案中設定了'NO_AUTO_CREATE_USER'項,所以時無法建立空密碼使用者

with grant option :讓被授權的使用者,可以將相同的許可權授權給他人。

其他事項:grant 授權或者 revoke 撤銷許可權後,需要使用者重新連線mysql 資料庫,或者重新整理授權表



1.1 普通授權

#授權  school庫 的所有表的select許可權給使用者 test,登入密碼是abc123,只能本地登入
grant select on school.* to 'test'@'localhost' identified by 'abc123';
exit  #退出資料庫

#使用使用者 test ,密碼abc123 本地登入nmysql
mysql -utest -pabc123  
#檢視資料庫
show databases;

#切換到 school庫
use school
#嘗試向表中插入資料,結果為許可權被拒絕
insert into class1  value(10,'test',1010110); 


1.2 讓被授權的使用者可以將許可權授予他人

注意,當被授權使用者將許可權授予他人時候,涉及到的許可權,庫,表,必須時本身擁有的許可權。否則報錯。

比如,當本身沒有mysql 庫的許可權時,就不可以在授權給其他使用者時加上密碼。


#將school庫的所有表的select,insert,update,delete 許可權授權給使用密碼 abc123本地登入的使用者test,並且,該使用者可以將這些許可權授權給其他使用者
grant select,insert,update,delete on school.* to 'lisi'@'localhost' 
    -> identified by 'abc123' with grant option;
   
#重新整理授權表
flush privileges;
exit



#當將許可權授權於給一個不存在的用於時,則報錯,不可以使用grant 命令建立使用者
grant select,update,insert,delete on school.* to 'wangwu'@'localhost' identified by 'abc123';

#當授權個一個存在的使用者,但是後面跟了 identified by 項時,則報錯,不可以對mysql 庫操作。因為使用者密碼是儲存在 mysql 庫中,而當前使用者沒有mysql 庫的許可權,所以報錯。
#可以授權時候,後面不跟密碼,或者將mysql 庫user表的相應許可權授權給 當前使用者
grant select,update,insert,delete on school.* to 'test'@'localhost' identified by 'abc123';

#當直接授權給存在的使用者'test'@'localhost'時,則成功。
grant select,update,insert,delete on school.* to 'test'@'localhost' ;


#使用test 使用者本地登入
mysql -utest -pabc123
use school;
#本來沒有insert 許可權的test使用者,現在可以對class1 表進行增刪改查操作
insert into class1 values(13,'test',13131113);



2 檢視使用者許可權

使用者顯示自身的訪問許可權

show grants;


管理員檢視已擁有授權使用者許可權

show grants for '使用者名稱'@'登入地點'


#顯示當前登入使用者的許可權
show grants;
#顯示'test'@'localhost' 使用者的許可權
 show grants for 'test'@'localhost';



3 撤銷使用者許可權

revoke 許可權 on 庫名.表名 from '使用者名稱'@'登入地點' ;

許可權,庫名.表名, '使用者名稱'@'登入地點' 必須與授權表裡對應

all 表示所有許可權

revoke  select   on  school.* from 'test'@'localhost';
revoke  all  on  school.* from 'test'@'localhost';