mysql mysql運維 之 使用者與許可權

2021-10-17 21:35:46 字數 2983 閱讀 5316

目錄

1.建立使用者

2.授權

3.設定與更改使用者密碼

4. 撤銷使用者許可權

5.刪除使用者

關於mysql許可權的幾點常識:

mysql的許可權系統主要用來驗證使用者的操作許可權。

在mysql內部,許可權資訊存放在mysql資料庫的granttable裡。當mysql啟動後,granttable裡的資訊會寫入記憶體。

mysql 使用user name 加 host name 來作為識別符號。通過這種識別符號,可以用來區分不同host上的相同的user name。

mysql 許可權控制有2種策略:

1)根據密碼是否正確來控制客戶端的連線。

2)假設可正常connect,server還可以檢查每個satement是否有許可權去執行。如果只有某張表的select 許可權,就不能進行drop操作。

如果使用者的許可權改變,當前已連線的會話使用者不會受影響,下次登入才會生效。

create user 'username'@'host' identified by 'password';

說明:username:你將建立的使用者名稱

host:指定使用者可在哪個主機上登陸,若是本地使用者可用localhost,若想讓該使用者從任意遠端主機登陸,可使用萬用字元%

password:該使用者的登陸密碼,密碼可以為空,如果為空則該使用者可以不需要密碼登陸伺服器

例子:create user 'dog'@'localhost' identified by '123456';

create user 'pig'@'192.168.1.101' idendified by '123456';

create user 'eng'@'%' identified by '1cydx@#56';

create user 'pig'@'%' identified by '';

create user 'pig'@'%';

建立使用者並授予全部許可權

grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;

flush privileges;

grant privileges on databasename.tablename to 'username'@'host';

說明:privileges:使用者的操作許可權,如select,insert,update等,如果要授予所的許可權則使用all

databasename:資料庫名

tablename:表名,如果要授予該使用者對所有資料庫和表的相應操作許可權則可用*表示,如*.*

例子:grant select, insert on test.user to 'pig'@'%';

grant all on *.* to 'pig'@'%';

grant all on maindataplus.* to 'pig'@'%';

注意:用以上命令授權的使用者不能給其它使用者授權,如果想讓該使用者可以授權,用以下命令:

grant privileges on databasename.tablename to 'username'@'host' with grant option;

# 授權語法

grant 許可權 on 資料庫.表 to '使用者'@'ip位址' ;

# db1下所有表授予select許可權 grant select on db1.* to 'zekai'@'%';

# 所有資料庫都授予select許可權 grant select on *.* to 'zekai'@'%';

# 授予多個許可權 grant select,insert on *.* to 'zekai'@'%';

# 授予全部許可權,除了建立使用者 grant all privileges on *.* to 'zekai'@'%';

#把dss schema下所有表的全部許可權授予給analytics使用者

grant all privileges on old.* to 'analytics'@'%';

grant all privileges on dss.* to 'analytics'@'%';

grant select on old.* to 'efc'@'%';

grant select on dss.* to 'efc'@'%';

set password for 'username'@'host' = password('newpassword');

如果是當前登陸使用者用:

set password = password("newpassword");

例子:set password for 'pig'@'%' = password("123456");

set password =password('g48zueq2ouz');

命令:revoke privilege on databasename.tablename from 'username'@'host';

說明:privilege, databasename, tablename:同授權部分

例子:revoke select on *.* from 'pig'@'%';

revoke all privilege on *.* from 'pig'@'%';

注意:假如你在給使用者'pig'@'%'授權的時候是這樣的(或類似的):grant select on test.user to 'pig'@'%',則在使用revoke select on *.* from 'pig'@'%';命令並不能撤銷該使用者對test資料庫中user表的select 操作。相反,如果授權使用的是grant select on *.* to 'pig'@'%';則revoke select on test.user from 'pig'@'%';命令也不能撤銷該使用者對test資料庫中user表的select許可權。

具體資訊可以用命令show grants for 'pig'@'%'; 檢視

drop user 'username'@'host';

運維使用者與許可權相關

使用者相關 useradd c 備註 加上備註文字。備註文字會儲存在passwd的備註欄位中。d 登入目錄 指定使用者登入時的起始目錄。d 變更預設值 e 有效期限 指定帳號的有效期限。f 緩衝天數 指定在密碼過期後多少天即關閉該帳號。g 群組 指定使用者所屬的群組。g 群組 指定使用者所屬的附 組...

oracle之使用者

命令都是在命令列視窗執行 建立使用者 1 登陸管理員使用者 sqlplus system 密碼 sqlplus system briup 注意不要以分號結尾 2 建立使用者 create user 使用者名稱 identified by 密碼 create user jd2001 2005 iden...

使用者模組之使用者登入

使用者登入流程 接受資料 接受瀏覽器傳遞過來的資料 校驗資料 資料完整性校驗 all 業務處理 登入校驗 登入認證 authenticate username username,password password 認證一組給定的使用者名稱和密碼 判斷使用者已啟用 記錄使用者的登入狀態,login r...