1. 程式人生 > >MySQL使用者許可權管理詳解

MySQL使用者許可權管理詳解

一、使用者授權

mysql> grant all privileges on *.* to 'yangxin'@'%' identified by 'yangxin123456' with grant option;

•all privileges:表示將所有許可權授予給使用者。也可指定具體的許可權,如:SELECT、CREATE、DROP等。

•on:表示這些許可權對哪些資料庫和表生效,格式:資料庫名.表名,這裡寫“*”表示所有資料庫,所有表。如果我要指定將許可權應用到test庫的user表中,可以這麼寫:test.user

•to:將許可權授予哪個使用者。格式:”使用者名稱”@”登入IP或域名”。%表示沒有限制,在任何主機都可以登入。比如:”yangxin”@”192.168.0.%”,表示yangxin這個使用者只能在192.168.0IP段登入

•identified by:指定使用者的登入密碼 •with grant option:表示允許使用者將自己的許可權授權給其它使用者

可以使用GRANT給使用者新增許可權,許可權會自動疊加,不會覆蓋之前授予的許可權,比如你先給使用者新增一個SELECT許可權,後來又給使用者添加了一個INSERT許可權,那麼該使用者就同時擁有了SELECT和INSERT許可權。

使用者詳情的許可權列表請參考MySQL官網說明:http://dev.mysql.com/doc/refman/5.7/en/privileges-provided.html

二、重新整理許可權

對使用者做了許可權變更之後,一定記得重新載入一下許可權,將許可權資訊從記憶體中寫入資料庫。

mysql> flush privileges;

三、檢視使用者許可權

mysql> grant select,create,drop,update,alter on *.* to 'yangxin'@'localhost' identified by 'yangxin0917' with grant option;mysql> show grants for 'yangxin'@'localhost';

四、回收許可權

刪除yangxin這個使用者的create許可權,該使用者將不能建立資料庫和表。

mysql> revoke create on *.* from '
[email protected]
';mysql> flush privileges;

五、刪除使用者

mysql> select host,user from user;

mysql> drop user 'yangxin'@'localhost';

六、使用者重新命名

shell> rename user 'test3'@'%' to 'test1'@'%';

七、修改密碼

1> 更新mysql.user表

mysql> use mysql;

mysql5.7之前

mysql> update user set password=password('123456') where user='root';

mysql5.7之後

mysql> update user set authentication_string=password('123456') whereuser='root';

mysql> flush privileges;

2> 用set password命令

語法:set password for ‘使用者名稱’@‘登入地址’=password(‘密碼’)

mysql> set password for 'root'@'localhost'=password('123456');

3> mysqladmin

語法:mysqladmin -u使用者名稱 -p舊的密碼 password 新密碼

mysql> mysqladmin -uroot -p123456 password 1234abcd

注意:mysqladmin位於mysql安裝目錄的bin目錄下

八、忘記密碼

1> 新增登入跳過許可權檢查配置

修改my.cnf,在mysqld配置節點新增skip-grant-tables配置

[mysqld]skip-grant-tables

2> 重新啟動mysql服務

shell> service mysqld restart

3> 修改密碼

此時在終端用mysql命令登入時不需要使用者密碼,然後按照修改密碼的第一種方式將密碼修改即可。

4> 還原登入許可權跳過檢查配置

將my.cnf中mysqld節點的skip-grant-tables配置刪除,然後重新啟動服務即可。

文章轉自:http://www.jb51.net/article/87979.htm