1. 程式人生 > >mysql授權

mysql授權

set show mil gif enum 方便 技術分享 本機 ice

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‘;

mysql授權