mysql授權


1,創建mysql用及授予權限:

在mysql中輸入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;
View Code

 通過grant 命令創建用戶並授權:

mysql> grant all privileges on wordpress.* to 'userdb'@'localhost' identified by 'admin';
Query OK, 0 rows affected (0.00 sec)

生產環境針對主庫(寫入主讀為輔)用戶的授權;

普通環境:

  1. 本機:lnmplamp環境數據庫授權
  2. grant all privileges ON blog.* to blog@localhost identified by 123456
  3. 應用服務器和數據庫服務器不在一個主機上授權;
  4. grant all privileges ON blog.* to blog@10.0.0.% identified by 123
  5. 嚴格的授權:重視安全,忽略了方便;
  6. grant select,insert,update,delete ON blog.* to blog@10.0.0.% identified by 123
  7. 生產環境從庫(只讀)用戶的授權;
  8. grant select ON blog.* to blog@10.0.0.% identified by 123
  9. 查看授權用戶oldboy的具體的授權權限 
  10. show grants for oldboy’@’localhost’;

第一種:授權用戶

  1. grant all on test.* to oldboy@127.0.0.% identified by oldboy123
  2. show grants for oldboy@127.0.0.%’; 查看授權用戶
  3. +-------------------------------------------------------------------------------------------------------------+
  4. | Grants for root@127.0.0.1|
  5. +-------------------------------------------------------------------------------------------------------------+
  6. | GRANT USAGE ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
  7. | GRANT ALL PRIVILEGES ON `test`.* TO 'root'@'127.0.0.1' |
  8. +-------------------------------------------------------------------------------------------------------------+
  9. 2 rows in set (0.00 sec)  

  

■ 第二種:授權方法

 
  1. create user bbs@'172.16.1.1/255.255.255.0' identified by '123456'
  2. 先授權可以登錄的
  3. mysql> show grants for bbs@'172.16.1.1/255.255.255.0';
  4. mysql> grant select on wordpress.* to bbs@'172.16.1.1/255.255.255.0';

授權局域網主機連接遠程數據庫

a.一條命令百分號匹配法

 
  1. grant all on *.* totest@10.0.0.%’identified by test123’;

b、一條命令子網掩碼配置法

 
  1. grant all on *.* to test@10.0.0.0/255.255.255.0 identified by test123’;

c、兩條命令實現 
先創建用戶並設置密碼;

 
  1. create user test@10.0.0.%’ identified by test123’;
  2. 再對用戶授權指定權限和管理庫表
  3. grant all on *.* to test@10.0.0.0/255.255.255.0

最后記得上述每條grant命令都要刷新權限

 
  1. flush privilege

數據庫遠程登錄

 
  1. mysql -uwordpress -poldboy123 -h 172.16.1.51 -P3306
  2. -h指定IP地址,-P指定服務端口號

創建類似於root系列的管理員用戶,可以創建下級用戶的用戶

 
  1. grant all privileges on *.* to root@'127.0.0.1' identified by 'oldboy123' with grant option;
  2. 只需要在最后輸入with grant option

回收用戶權限

 
    1. REVOKE INSERT ON *.* FROM 'jeffrey'@'localhost';


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM