mysql使用者建立,及授權

來源:互聯網
上載者:User

標籤:mysql使用者建立   及授權   


1、首先查看系統中所有的使用者:
   select host,user from mysql.user;


2、刪除系統的多餘帳號文法drop user"user"@"主機域" 注意引號,可以是單或雙引號;
   範例: drop user ‘‘@‘moban2‘
   #如果為空白直接為空白即可;
   #如果drop刪除不了(一般是特殊字元或大寫),可以用下面的方式刪除:
   範例:delete from mysql.user where user=‘root‘ and host=‘127.0.0.0.1‘;

3、建立使用者的時候最好首先通過help查看grant命令協助:
    CREATE USER ‘jeffrey‘@‘localhost‘ IDENTIFIED BY ‘mypass‘;
    GRANT ALL ON db1.* TO ‘jeffrey‘@‘localhost‘;
    GRANT SELECT ON db2.invoice TO ‘jeffrey‘@‘localhost‘;
    GRANT USAGE ON *.* TO ‘jeffrey‘@‘localhost‘ WITH MAX_QUERIES_PER_HOUR 90;

4、營運人員常用的建立方法,使用grant命令建立使用者的時,進行許可權授權:
    範例:grant all privileges  on db1.* to [email protected] identified by "password";
    # grant    all privileges on db1.*  to [email protected]  identified     by‘passwd‘       授權命令  對應許可權(all所有許可權)      目標:庫和表使用者名稱和用戶端主機    使用者密碼    


5、授權完畢後要重新整理許可權:

    flush privileges;


6、查看建立的使用者:

    select host,user from mysql.user;


7、查看建立使用者的許可權: show grants for [email protected];
   #USAGE 表示使用者只可以登入,沒有其它許可權,操作的時候顯示Access denied; 


     或者:
     查看協助:help create user
               CREATE USER ‘jeffrey‘@‘localhost‘
               IDENTIFIED WITH my_auth_plugin;
     先建立使用者

               create user  [email protected] identified by "password";
      查看使用者權限:

               show grants for [email protected];
     在授權:

               grant all on dbname.* to [email protected];
      查看許可權:

               show grants for [email protected];



8、授權區域網路內主機遠端連線資料庫,常見的使用%匹配方法:
   範例: grant all on   *.* to  [email protected]‘10.10.36.%‘ identified by "123456";
   重新整理許可權:flush privileges;
   登入使用-h指定主機,-P指定連接埠

   範例:mysql -u  username_2 -p -h 10.10.36.170


確定mysql 可以授權的許可權,如果不知道可以這樣:
⑴協助查看:help revoke (許可權收回)

    REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user] ...
⑵許可權查看:show grants for [email protected];
⑶收回插入許可權:revoke insert  on  *.* from ‘user_name‘@‘localhost‘;
  #注意此處指定資料庫
⑷登入資料庫後許可權查看:show grants for [email protected];
⑸退出資料庫後: mysql -uroot -p123456 -e "show grants for "username"@"localhost";" | grep -i grant  | tail -1|tr ‘,‘ ‘\n‘ >all.privileges
以下為資料庫中的許可權:
SELECT 查詢\INSERT 插入 \UPDATE 更新\DELETE 刪除 \CREATE 建立庫和表\DROP    刪除庫和表\INDEX    索引\ALTER    修改 \CREATE TEMPORARY TABLES 建立暫存資料表\ LOCK TABLES    鎖表\ EXECUTE    執行\ CREATE VIEW    建立視圖\ SHOW VIEW    顯示視圖\ CREATE ROUTINE 建立預存程序\ALTER ROUTINE    修改預存程序\ EVENT         事件\ TRIGGER     觸發器
或者:select * from mysql.user\G;


9、針對部落格、cms 等產品安裝期間要採用最下話原則 :除了select,insert,update,delete4個權

   限外,還需要create,drop等危險許可權
   範例:grant select,insert,update,delete,create,drop on blog.* to [email protected]‘10.10.36.%‘

    identified by "password";


10、生產資料庫後收回許可權(最好評估):
    範例:revoke create,drop on blog.*  from [email protected]‘10.10.36.%‘;


主從資料庫許可權設定慢慢在補。

本文出自 “一如既往” 部落格,謝絕轉載!

mysql使用者建立,及授權

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.