程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> Mysql下建立用戶授權權限例子

Mysql下建立用戶授權權限例子

編輯:關於MYSQL數據庫
    用戶授權在mysql中使用grant命令就可以了,我相信各位都會有了解過了,下面小編為各位介紹一個Mysql下建立用戶授權權限例子,希望本文章對各位有幫助.    


    建立用戶,授權數據庫:

    mysql> create user 'byrd'@'localhost' identified by 'admin123';    #建立主機為localhost,密碼為admin123的用戶byrd
    Query OK, 0 rows affected (0.05 sec)
    mysql> show grants for 'byrd'@'localhost';    #查看byrd權限,USAGE表示連接權限
    +-------------------------------------------------------------------------------------------------------------+
    | Grants for byrd@localhost                                                                                   |
    +-------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'byrd'@'localhost' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' |
    +-------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    mysql> select user,host from mysql.user;
    +------+-----------+
    | user | host      |
    +------+-----------+
    | root | 127.0.0.1 |
    | root | ::1       |
    | root | lamp      |
    | byrd | localhost |
    | root | localhost |
    +------+-----------+
    5 rows in set (0.00 sec)
    mysql> grant all on gbk.* to 'byrd'@'localhost';    #用戶byrd、主機localhost對數據庫gbk擁有所有權限
    Query OK, 0 rows affected (0.01 sec)

    mysql> show mysqls for 'byrd'@'localhost';
    +-------------------------------------------------------------------------------------------------------------+
    | Grants for byrd@localhost                                                                                   |
    +-------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'byrd'@'localhost' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' |
    | GRANT ALL PRIVILEGES ON `gbk`.* TO 'byrd'@'localhost'                                                       |
    +-------------------------------------------------------------------------------------------------------------+
    2 rows in set (0.00 sec)

    mysql> flush privileges;
    mysql> grant all on gbk.* to test@'localhost' identified by 'admin123';    #建立用戶test,用戶gbk數據庫所有權限,同上
    mysql> show grants for 'test'@'localhost';
    +-------------------------------------------------------------------------------------------------------------+
    | Grants for test@localhost                                                                                   |
    +-------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'test'@'localhost' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' |
    | GRANT ALL PRIVILEGES ON `gbk`.* TO 'test'@'localhost'                                                       |
    +-------------------------------------------------------------------------------------------------------------+
    2 rows in set (0.00 sec)
    Query OK, 0 rows affected (0.00 sec)
    遠程連接同上:

    mysql> grant all on gbk.* to 'user'@'授權可連接主機' identified by 'admin123';    #這是Server端
    [root@lamp ~]# /usr/local/mysql/bin/mysql -uroot -p'admin123' -h hk.t4x.org    #這是client端

    補充:ALL PRIVILEGES權限包括:

    mysql> show grants for 'byrd'@'localhost';
    +-------------------------------------------------------------------------------------------------------------+
    | Grants for byrd@localhost                                                                                   |
    +-------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'byrd'@'localhost' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C' |
    | GRANT ALL PRIVILEGES ON `gbk`.* TO 'byrd'@'localhost'                                                       |
    +-------------------------------------------------------------------------------------------------------------+
    mysql> revoke insert on `gbk`.* from 'byrd'@'localhost';
    Query OK, 0 rows affected (0.00 sec)

    mysql> show grants for byrd@'localhost';
    +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Grants for byrd@localhost                                                                                                                                                                                                   |
    +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'byrd'@'localhost' IDENTIFIED BY PASSWORD '*01A6717B58FF5C7EAFFF6CB7C96F7428EA65FE4C'                                                                                                                 |
    | GRANT SELECT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `gbk`.* TO 'byrd'@'localhost' |
    +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    ALL PRIVILEGES權限包括:GRANT SELECT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT,insert

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved