MySQL使用者權限管理詳解_Mysql

來源:互聯網
上載者:User

使用者權限管理主要有以下作用:
1. 可以限制使用者訪問哪些庫、哪些表
2. 可以限制使用者對哪些表執行SELECT、CREATE、DELETE、DELETE、ALTER等操作
3. 可以限制使用者登入的IP或網域名稱
4. 可以限制使用者自己的許可權是否可以授權給別的使用者

一、使用者授權

複製代碼 代碼如下:
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 'yangxin@localhost';
mysql> flush privileges;

五、刪除使用者

mysql> select host,user from user;+---------------+---------+| host   | user |+---------------+---------+| %    | root || %    | test3 || %    | yx  || 192.168.0.% | root || 192.168.0.% | test2 || 192.168.0.109 | test || ::1   | yangxin || localhost  | yangxin |+---------------+---------+8 rows in set (0.00 sec)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') where user='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配置刪除,然後重新啟動服務即可。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.