1,MySQL權限體系
mysql 的權限體系大致分為5個層級:
全局層級:
全局權限適用于一個給定服務器中的所有數(shù)據(jù)庫折剃。這些權限存儲在mysql.user表中。GRANT ALL ON .和REVOKE ALL ON .只授予和撤銷全局權限。
數(shù)據(jù)庫層級:
數(shù)據(jù)庫權限適用于一個給定數(shù)據(jù)庫中的所有目標肤舞。這些權限存儲在mysql.db表中。GRANT ALL ON db_name.和REVOKE ALL ON db_name.只授予和撤銷數(shù)據(jù)庫權限均蜜。
表層級:
表權限適用于一個給定表中的所有列李剖。這些權限存儲在mysql.talbes_priv表中。GRANT ALL ON db_name.tbl_name和REVOKE ALL ON db_name.tbl_name只授予和撤銷表權限囤耳。
列層級:
列權限適用于一個給定表中的單一列篙顺。這些權限存儲在mysql.columns_priv表中。當使用REVOKE時充择,您必須指定與被授權列相同的列德玫。
子程序層級:
CREATE ROUTINE, ALTER ROUTINE, EXECUTE和GRANT權限適用于已存儲的子程序。這些權限可以被授予為全局層級和數(shù)據(jù)庫層級椎麦。而且宰僧,除了CREATE ROUTINE外,這些權限可以被授予為子程序層級观挎,并存儲在mysql.procs_priv表中琴儿。
這些權限信息存儲在下面的系統(tǒng)表中:
mysql.user
mysql.db
mysql.host
mysql.table_priv
mysql.column_priv
mysql. procs_priv
當用戶連接進來,mysqld會通過上面的這些表對用戶權限進行驗證嘁捷!
2造成, 千里追蹤之5表
相對于oracle來說,mysql的特性是可以限制ip雄嚣,用戶user晒屎、ip地址host、密碼passwd這3個是用戶管理的基礎现诀,權限的細節(jié)基本在mysql.user夷磕、mysql.db、mysql.host仔沿、mysql.table_priv坐桩、mysql.column_priv這幾張表就可以看到很多細節(jié),接下來仔細分析這些表就可以知道權限的奧秘封锉。
<版權所有绵跷,文章允許轉載,但必須以鏈接方式注明源地址成福,否則追究法律責任!>
原博客地址: http://blog.csdn.net/mchdba/article/details/45921045
原作者:黃杉 (mchdba)
演示過程中需要建立用戶來演示碾局,先簡單介紹下如何創(chuàng)建用戶:
GRANT priv_type ON database.table
TO user[IDENTIFIED BY [PASSWORD] ‘password’]
[,user [IDENTIFIED BY [PASSWORD] ‘password’]…]
示例:
GRANT SELECT, INSERT, UPDATE, DELETE ON d3307.* TO zengxiaoteng@’%’ IDENTIFIED BY ‘0523’;
2.1db表
2.1.1 表結構如下:
mysql> desc mysql.db; +-----------------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------------+---------------+------+-----+---------+-------+ | Host | char(60) | NO | PRI | | | | Db | char(64) | NO | PRI | | | | User | char(16) | NO | PRI | | | | Select_priv | enum('N','Y') | NO | | N | | | Insert_priv | enum('N','Y') | NO | | N | | | Update_priv | enum('N','Y') | NO | | N | | | Delete_priv | enum('N','Y') | NO | | N | | | Create_priv | enum('N','Y') | NO | | N | | | Drop_priv | enum('N','Y') | NO | | N | | | Grant_priv | enum('N','Y') | NO | | N | | | References_priv | enum('N','Y') | NO | | N | | | Index_priv | enum('N','Y') | NO | | N | | | Alter_priv | enum('N','Y') | NO | | N | | | Create_tmp_table_priv | enum('N','Y') | NO | | N | | | Lock_tables_priv | enum('N','Y') | NO | | N | | | Create_view_priv | enum('N','Y') | NO | | N | | | Show_view_priv | enum('N','Y') | NO | | N | | | Create_routine_priv | enum('N','Y') | NO | | N | | | Alter_routine_priv | enum('N','Y') | NO | | N | | | Execute_priv | enum('N','Y') | NO | | N | | | Event_priv | enum('N','Y') | NO | | N | | | Trigger_priv | enum('N','Y') | NO | | N | | +-----------------------+---------------+------+-----+---------+-------+ 22 rows in set (0.02 sec) mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
2.1.2分析如下:
db表存儲了所有對一個數(shù)據(jù)庫的所有操作權限。創(chuàng)建用戶的時候奴艾,都會往Host字段净当,User字段,Password字段錄入用戶信息;
而當執(zhí)行 GRANT SELECT,INSERT ON d3307.* TO u4@’%’ IDENTIFIED BY ‘u40523’;類似的授權語句的話像啼,Select_priv和Insert_priv字段的值會變成Y其它字段仍然是N俘闯;
當你執(zhí)行了GRANT ALL ON d3307.* TO u4@’%’ IDENTIFIED BY ‘u40523’;類似的復制語句的話,后面的字段都會變成Y的值忽冻;
2.1.3 創(chuàng)建單個select培慌、insert授予權限
創(chuàng)建用戶:
GRANT SELECT,INSERT ON d3307.* TO user4@'192.168.52' IDENTIFIED BY 'user0523';
- 1
應該除了Host明未、db、user字段有值,除了Select_priv雄坪、Insert_priv值為Y外喇澡,其它的都是N黄刚。
查看mysql.db表的記錄正是如此灵寺,如下所示:
mysql> SELECT * FROM mysql.db
where user='user4'\G; *************************** 1. row *************************** Host: 192.168.52 Db: d3307 User: user4 Select_priv: Y Insert_priv: Y Update_priv: N Delete_priv: N Create_priv: N Drop_priv: N Grant_priv: N References_priv: N Index_priv: N Alter_priv: N Create_tmp_table_priv: N Lock_tables_priv: N Create_view_priv: N Show_view_priv: N Create_routine_priv: N Alter_routine_priv: N Execute_priv: N Event_priv: N Trigger_priv: N 1 row in set (0.01 sec) ERROR: No query specified mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
2.1.4 授予ALL權限
執(zhí)行sql語句建立用戶:
GRANT ALL ON d3307.* TO dba5@'192.168.52.1' IDENTIFIED BY 'dba0523';
- 1
建立用戶的時候,如下所示慈省,除了Host肩豁、db、user字段外辫呻,所有的_priv字段記錄都會變成Y值清钥,(Grant_priv仍然是N值除非加了WITH GRANT OPTION執(zhí)行GRANT ALL ON d3307.* TO dba5@’192.168.52.1’ IDENTIFIED BY ‘dba0523’ WITH GRANT OPTION ;)
如下所示:
mysql> SELECT * FROM mysql.db
where user='dba5'\G; *************************** 1. row *************************** Host: 192.168.52.1 Db: d3307 User: dba5 Select_priv: Y Insert_priv: Y Update_priv: Y Delete_priv: Y Create_priv: Y Drop_priv: Y Grant_priv: N References_priv: Y Index_priv: Y Alter_priv: Y Create_tmp_table_priv: Y Lock_tables_priv: Y Create_view_priv: Y Show_view_priv: Y Create_routine_priv: Y Alter_routine_priv: Y Execute_priv: Y Event_priv: Y Trigger_priv: Y 1 row in set (0.00 sec) ERROR: No query specified mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
2.2 user表
2.2.1 表結構:
mysql> desc mysql.user; +------------------------+-----------------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------------+-----------------------------------+------+-----+---------+-------+ | Host | char(60) | NO | PRI | | | | User | char(16) | NO | PRI | | | | Password | char(41) | NO | | | | | Select_priv | enum('N','Y') | NO | | N | | | Insert_priv | enum('N','Y') | NO | | N | | | Update_priv | enum('N','Y') | NO | | N | | | Delete_priv | enum('N','Y') | NO | | N | | | Create_priv | enum('N','Y') | NO | | N | | | Drop_priv | enum('N','Y') | NO | | N | | | Reload_priv | enum('N','Y') | NO | | N | | | Shutdown_priv | enum('N','Y') | NO | | N | | | Process_priv | enum('N','Y') | NO | | N | | | File_priv | enum('N','Y') | NO | | N | | | Grant_priv | enum('N','Y') | NO | | N | | | References_priv | enum('N','Y') | NO | | N | | | Index_priv | enum('N','Y') | NO | | N | | | Alter_priv | enum('N','Y') | NO | | N | | | Show_db_priv | enum('N','Y') | NO | | N | | | Super_priv | enum('N','Y') | NO | | N | | | Create_tmp_table_priv | enum('N','Y') | NO | | N | | | Lock_tables_priv | enum('N','Y') | NO | | N | | | Execute_priv | enum('N','Y') | NO | | N | | | Repl_slave_priv | enum('N','Y') | NO | | N | | | Repl_client_priv | enum('N','Y') | NO | | N | | | Create_view_priv | enum('N','Y') | NO | | N | | | Show_view_priv | enum('N','Y') | NO | | N | | | Create_routine_priv | enum('N','Y') | NO | | N | | | Alter_routine_priv | enum('N','Y') | NO | | N | | | Create_user_priv | enum('N','Y') | NO | | N | | | Event_priv | enum('N','Y') | NO | | N | | | Trigger_priv | enum('N','Y') | NO | | N | | | Create_tablespace_priv | enum('N','Y') | NO | | N | | | ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | | | ssl_cipher | blob | NO | | NULL | | | x509_issuer | blob | NO | | NULL | | | x509_subject | blob | NO | | NULL | | | max_questions | int(11) unsigned | NO | | 0 | | | max_updates | int(11) unsigned | NO | | 0 | | | max_connections | int(11) unsigned | NO | | 0 | | | max_user_connections | int(11) unsigned | NO | | 0 | | | plugin | char(64) | YES | | | | | authentication_string | text | YES | | NULL | | | password_expired | enum('N','Y') | NO | | N | | +------------------------+-----------------------------------+------+-----+---------+-------+ 43 rows in set (0.10 sec) mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
2.2.2 分析
存儲用戶記錄的表,存儲了用戶的信息放闺,每一次創(chuàng)建用戶的時候祟昭,都會往這個表里錄入記錄,當你執(zhí)行了怖侦,都會往Host字段篡悟,User字段,Password字段錄入數(shù)據(jù)匾寝,但是后面的Select_priv搬葬、Insert_priv、Update_priv等字段的值艳悔,只有賦予GRANT ALL ON . TO timdba@’192.%’ IDENTIFIED BY ‘timdba0523’;類似的對所有庫的操作權限的時候才會被記錄成Y急凰,否則都記錄成N。
2.2.3 創(chuàng)建對庫所有表有操作權限的普通用戶
創(chuàng)建用戶:
GRANT SELECT,UPDATE ON d3307.* TO user6@'192.168.52.1' IDENTIFIED BY 'user0523';
- 1
分析結果:存儲在mysql.user表里面的記錄當中猜年,Host、User聂渊、Password是有值的,但是其它的Select_priv等*_priv字段值都是N。
驗證結果,去查看表里的存儲記錄,如下所示:
mysql> SELECT * FROM mysql.user where user='user6'\G; *************************** 1. row *************************** Host: 192.168.52.1 User: user6 Password: *A4D1F6ACEBC5D3EB0F6D33C7DCC629E8BE55B75A Select_priv: N Insert_priv: N Update_priv: N Delete_priv: N Create_priv: N Drop_priv: N Reload_priv: N Shutdown_priv: N Process_priv: N File_priv: N Grant_priv: N References_priv: N Index_priv: N Alter_priv: N Show_db_priv: N Super_priv: N Create_tmp_table_priv: N Lock_tables_priv: N Execute_priv: N Repl_slave_priv: N Repl_client_priv: N Create_view_priv: N Show_view_priv: N Create_routine_priv: N Alter_routine_priv: N Create_user_priv: N Event_priv: N Trigger_priv: N Create_tablespace_priv: N ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: mysql_native_password authentication_string: password_expired: N 1 row in set (0.00 sec) ERROR: No query specified mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
2.2.4 創(chuàng)建對于所有表有操作權限的用戶
創(chuàng)建用戶:
mysql> GRANT SELECT,UPDATE ON . TO user7@'%' IDENTIFIED BY 'user0523'; Query OK, 0 rows affected (0.00 sec) mysql>
- 1
- 2
- 3
- 4
分析:
基本的Host、User肉微、Password字段有記錄值,然后grant了select和update所以關于_priv字段中select和update字段有值為Y厚者,其它_priv字段值應該是N。
查看記錄結果,分享正確,如下所示:
mysql> SELECT * FROM mysql.user where user='user7'\G; *************************** 1. row *************************** Host: % User: user7 Password: *A4D1F6ACEBC5D3EB0F6D33C7DCC629E8BE55B75A Select_priv: Y Insert_priv: N Update_priv: Y Delete_priv: N Create_priv: N Drop_priv: N Reload_priv: N Shutdown_priv: N Process_priv: N File_priv: N Grant_priv: N References_priv: N Index_priv: N Alter_priv: N Show_db_priv: N Super_priv: N Create_tmp_table_priv: N Lock_tables_priv: N Execute_priv: N Repl_slave_priv: N Repl_client_priv: N Create_view_priv: N Show_view_priv: N Create_routine_priv: N Alter_routine_priv: N Create_user_priv: N Event_priv: N Trigger_priv: N Create_tablespace_priv: N ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: mysql_native_password authentication_string: password_expired: N 1 row in set (0.00 sec) ERROR: No query specified mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
2.3 tables_priv表
2.3.1 查看表結構
mysql> desc mysql.tables_priv; +-------------+-----------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-----------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-----------------------------+ | Host | char(60) | NO | PRI | | | | Db | char(64) | NO | PRI | | | | User | char(16) | NO | PRI | | | | Table_name | char(64) | NO | PRI | | | | Grantor | char(77) | NO | MUL | | | | Timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | | Table_priv | set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter','Create View','Show view','Trigger') | NO | | | | | Column_priv | set('Select','Insert','Update','References') | NO | | | | +-------------+-----------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-----------------------------+ 8 rows in set (0.00 sec) mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
2.3.2 分析:
記錄了對一個表的單獨授權記錄容为,只有執(zhí)行grant insert on dbname.tablename to user1@’%’identified by ‘pwd’;類似的授權記錄才會在這個表里錄入授權信息得滤;其中各個字段涵義如下:
<colgroup><col style="width: 375px;"><col style="width: 375px;"></colgroup>
|
字段
|
存儲的數(shù)據(jù)
|
|
Host字段
|
用戶的登錄ip范圍
|
|
User字段
|
表所在的數(shù)據(jù)庫名稱
|
|
Table_name字段
|
授權的表的名稱
|
|
Grantor字段
|
執(zhí)行grant建立用戶的授權者
|
|
Timestamp字段
|
0000-00-00 00:00:00
|
|
Table_priv字段
|
所授予的操作表的權限坛猪,比如select就斤、udate、delete等
|
|
Column_priv字段
|
對這個表的某個字段單獨授予的權限
|
另外當賦予all在某張表上的時候庄岖,Table_priv列會多處所有關于表的授權記錄,描述如下:
Select,Insert,Update,Delete,Create,Drop,References,Index,Alter,Create View,Show view,Trigger角骤。
2.3.3 創(chuàng)建單獨操作這個表的用戶
創(chuàng)建用戶:
mysql> GRANT INSERT,SELECT,UPDATE ON d3307.t TO user8@'192.168.52.1' IDENTIFIED BY 'dba0523'; Query OK, 0 rows affected (0.00 sec) mysql>
- 1
- 2
- 3
- 4
分析結果:
應該是Host隅忿、Db、User邦尊、Table_name背桐、Grantor、Timestamp胳赌、Table_priv是有值的,但是Column_priv沒有值匙隔,因為沒有單獨對某一個列做了授權限制的疑苫。
查看權限,如下所示:
mysql> SELECT * FROM mysql.tables_priv where user='user8'\G; *************************** 1. row *************************** Host: 192.168.52.1 Db: d3307 User: user8 Table_name: t Grantor: root@localhost Timestamp: 0000-00-00 00:00:00 Table_priv: Select,Insert,Update Column_priv: 1 row in set (0.00 sec) ERROR: No query specified mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
2.3.4 單獨為某個列授權
授權語句操作:
mysql> GRANT UPDATE(created_time) ON d3307.t TO user8@'192.168.52.1'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT(uname) ON d3307.t TO user8@'192.168.52.1'; Query OK, 0 rows affected (0.00 sec) mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
分析:
單獨為某個列授權纷责,會記錄在這個表的Column_priv字段里面捍掺,會記錄下對單個列的授權操作記錄
查看記錄:
mysql> SELECT * FROM mysql.tables_priv where user='user8'\G; *************************** 1. row *************************** Host: 192.168.52.1 Db: d3307 User: user8 Table_name: t Grantor: root@localhost Timestamp: 0000-00-00 00:00:00 Table_priv: Select,Insert,Update Column_priv: Select,Update 1 row in set (0.00 sec) ERROR: No query specified mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
而且還會在另外一個權限表mysql.columns_priv留下記錄單獨的授權記錄,如下所示:
mysql> SELECT * FROM mysql.columns_priv WHERE USER='user8'; +--------------+-------+-------+------------+--------------+---------------------+-------------+ | Host | Db | User | Table_name | Column_name | Timestamp | Column_priv | +--------------+-------+-------+------------+--------------+---------------------+-------------+ | 192.168.52.1 | d3307 | user8 | t | created_time | 0000-00-00 00:00:00 | Update | | 192.168.52.1 | d3307 | user8 | t | uname | 0000-00-00 00:00:00 | Select | +--------------+-------+-------+------------+--------------+---------------------+-------------+ 2 rows in set (0.00 sec) mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2.4 columns_priv表
2.4.1 表結構如下:
mysql> desc mysql.columns_priv; +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+ | Host | char(60) | NO | PRI | | | | Db | char(64) | NO | PRI | | | | User | char(16) | NO | PRI | | | | Table_name | char(64) | NO | PRI | | | | Column_name | char(64) | NO | PRI | | | | Timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | | Column_priv | set('Select','Insert','Update','References') | NO | | | | +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+ 7 rows in set (0.04 sec) mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
2.4.2 分析
單獨對某一列有操作權限的時候再膳,會將權限信息記錄在這個表里面挺勿,比如新建立一個賬號GRANT UPDATE(uname) ON d3307.t TO user9@’192.168.52.%’ IDENTIFIED BY ‘user0520’; 那么就會在這個表上錄入授權信息記錄,重點看Column_name字段和Column_priv字段的值喂柒。
2.4.3 實際操作
創(chuàng)建用戶操作:
mysql> GRANT UPDATE(uname) ON d3307.t TO user9@'192.168.52.%' IDENTIFIED BY 'user0520'; Query OK, 0 rows affected (0.00 sec) mysql>
- 1
- 2
- 3
- 4
查看結果不瓶,會在這個columns_priv表留下一條記錄:
mysql> SELECT * FROM mysql.columns_priv WHERE USER='user9'; +--------------+-------+-------+------------+-------------+---------------------+-------------+ | Host | Db | User | Table_name | Column_name | Timestamp | Column_priv | +--------------+-------+-------+------------+-------------+---------------------+-------------+ | 192.168.52.% | d3307 | user9 | t | uname | 0000-00-00 00:00:00 | Update | +--------------+-------+-------+------------+-------------+---------------------+-------------+ 1 row in set (0.00 sec) mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2.5 procs_priv表
2.5.1 表結構
mysql> desc proxies_priv; +--------------+------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +--------------+------------+------+-----+-------------------+-----------------------------+ | Host | char(60) | NO | PRI | | | | User | char(16) | NO | PRI | | | | Proxied_host | char(60) | NO | PRI | | | | Proxied_user | char(16) | NO | PRI | | | | With_grant | tinyint(1) | NO | | 0 | | | Grantor | char(77) | NO | MUL | | | | Timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +--------------+------------+------+-----+-------------------+-----------------------------+ 7 rows in set (0.04 sec) mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
2.6.2分析:
procs_priv表可以對存儲過程和存儲函數(shù)進行權限設置。主要字段:proc_priv灾杰。
3蚊丐,創(chuàng)建用戶
3.1、CREATE USER創(chuàng)建用戶
使用CREATE USER語句創(chuàng)建用戶艳吠,必須要擁有CREATE USER權限麦备。其格式如下:
CREATE USER user[IDENTIFIED BY [PASSWORD] 'password'], [user[IDENTIFIED BY [PASSWORD] 'password']]...
- 1
- 2
其中,user參數(shù)表示新建用戶的賬戶昭娩,user由用戶名(User)和主機名(Host)構成凛篙;IDENTIFIED BY關鍵字用來設置用戶的密碼;password參數(shù)表示用戶的密碼栏渺;如果密碼是一個普通的字符串呛梆,就不需要使用PASSWORD關鍵字】恼铮可以沒有初始密碼削彬。
例如
CREATE USER 'sys'@'%' IDENTIFIED BY 'sys';
- 1
執(zhí)行之后user表會增加一行記錄全庸,但權限暫時全部為‘N’。
3.2融痛、用INSERT語句新建普通用戶
可以使用INSERT語句直接將用戶的信息添加到mysql.user表壶笼。但必須擁有mysql.user表的INSERT權限。
另外雁刷,ssl_cipher覆劈、x509_issuer、x509_subject等必須要設置值沛励,否則INSERT語句無法執(zhí)行责语。
示例:
INSERT INTO mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject) VALUES(‘%’,’newuser1’,PASSWORD(‘123456’),”,”,”)
執(zhí)行INSERT之后,要使用命令:FLUSH PRIVILEGES;命令來使用戶生效目派。
3.3坤候、用GRANT語句來新建普通用戶
用GRANT來創(chuàng)建新的用戶時,能夠在創(chuàng)建用戶時為用戶授權企蹭。但需要擁有GRANT權限白筹。
語法如下:
GRANT priv_type ON database.table TO user[IDENTIFIED BY [PASSWORD] 'password'] [,user [IDENTIFIED BY [PASSWORD] 'password']...]
- 1
- 2
- 3
priv_type:參數(shù)表示新yoghurt的權限;
databse.table:參數(shù)表示新用戶的權限范圍谅摄;
user:參數(shù)新用戶的賬戶徒河,由用戶名和主機構成;
IDENTIFIED BY關鍵字用來設置密碼送漠;
password:新用戶密碼顽照;
PS:GRANT語句可以同時創(chuàng)建多個用戶。.與db.的區(qū)別在于闽寡。.對所有數(shù)據(jù)庫生效代兵,所以user表的SELECT會變?yōu)閅。而db.user表為’N’爷狈,更改的是Db表奢人。
4,刪除用戶
4.1 drop user刪除用戶
DROP USER語句刪除普通用戶淆院,需要擁有DROP USER權限何乎。
語法如下:
DROP USER user[,user]...
- 1
user是需要刪除的用戶,由用戶名(User)和主機名(Host)構成土辩。
4.2 DELETE語句刪除普通用戶
可以使用DELETE語句直接將用戶的信息從mysql.user表中刪除支救。但必須擁有對mysql.user表的DELETE權限。DELETE FROM mysql.user WHERE Host = ‘%’ AND User = ‘a(chǎn)dmin’; 刪除完成后拷淘,一樣要FLUSH PRIVILEGES才生效各墨。
5,修改用戶密碼
5.1 使用mysqladmin命令來修改root用戶的密碼
語法:
mysqladmin -u -username -p password "new_password"
- 1
新密碼(new_password)必須用括號括起來启涯,單引號會報錯贬堵。
示例恃轩,修改中要輸入舊的密碼來驗證:
[root@data02 ~]# mysqladmin -u timman -p password "tim" --socket=/usr/local/mysql3307/mysql.sock Enter password: [root@data02 ~]# [root@data02 ~]# mysql --socket=/usr/local/mysql3307/mysql.sock -utimman -ptim -e "select @@port"; +--------+ | @@port | +--------+ | 3307 | +--------+ [root@data02 ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
5.2 修改user表
UPDATE user表的passwor字段的值,也可以達到修改密碼的目的黎做;
UPDATE user SET Password = PASSWORD('123') WHERE USER = 'myuser'; FLUSH PRIVILEGES;
- 1
- 2
刷新后生效叉跛。
5.3 使用SET語句來修改密碼
使用root用戶登錄到MySQL服務器后,可以使用SET語句來修改密碼:
修改自己的密碼蒸殿,不需要用戶名
SET PASSWORD = PASSWORD("123");
- 1
修改其他用戶密碼:
SET PASSWORD FOR 'myuser'@'%'=PASSWORD("123456") FOR 用戶名@主機名
- 1
5.4 GRANT語句來修改普通用戶的密碼
使用GRANT語句修改普通用戶的密碼筷厘,必須擁有GRANT權限。
GRANT priv_type ON database.table TO user [IDENTIFIED BY [PASSWORD] 'password']
- 1
示例:
GRANT SELECT ON . TO 'user10'@'%' IDENTIFIED BY '123'
- 1
5.5 忘記用戶密碼的解決辦法
普通用戶宏所,直接用root超級管理員登錄進去修改密碼就可以了酥艳,但是如果root密碼丟失了,怎么辦呢爬骤?
5.5.1 msyqld_saft方式找回密碼
停止mysql:service mysqld stop;
安全模式啟動:mysqld_safe –skip-grant-tables &
無密碼回車鍵登錄:mysql -uroot –p
重置密碼:use mysql; update user set password=password(“”) where user=’root’ and host=’localhost’; flush privileges;
正常啟動:service mysql restart
再使用mysqladmin: mysqladmin password ‘123456’
5.5.2 使用普通賬號來找回密碼
–>(1):有一個修改test庫的用戶:grant create,delete,update,insert,select on d3307.* to test@’%’ identified by ‘t1’;
–>(2):復制user表文件到test庫下并且賦予mysql用戶訪問權限:
cp /home/data/mysql/data/mysql/user.* /home/data/mysql/data/test/;chown mysql.mysql /home/data/mysql/data/test/user.*
–>(3):mysql -utest -pt1登錄修改root密碼:
–>(4):將test庫的user表文件覆蓋 mysql庫的user表文件
cp /home/data/mysql/data/mysql/user.* /tmp/; mv /home/data/mysql/data/test/user.* /home/data/mysql/data/mysql/ ; chown mysql.mysql /home/data/mysql/data/mysql/user.*;
–>(5):查找mysql進程號充石,并且發(fā)送SIGHUP信號,重新加載權限表霞玄。
pgrep -n mysql; kill -SIGHUP 12234;
–>(6):無密碼登錄骤铃,再使用mysqladmin重新設置密碼。
PS:請參考第20課的視頻溃列,那里有詳細的記錄整個過修改密碼的過程劲厌。
6膛薛,收回用戶權限
查看權限:
SHOW GRANTS; SHOW GRANTS FOR user10@'%';
- 1
或者直接執(zhí)行sql命令去mysql數(shù)據(jù)庫下的user表中查看存儲著用戶的基本權限:
SELECT * FROM mysql.user WHERE USER='user10' AND HOST='%';
- 1
使用revoke關鍵字來收回權限:
REVOKE priv_type[(column_list)] ON database.table FROM user[,user]
- 1
- 2
- 3
示例:
REVOKE EXECUTE ON d3307.* FROM user10@'%';
- 1
7听隐,數(shù)據(jù)庫用戶劃分
7.1 普通數(shù)據(jù)管理用戶:
賦予對業(yè)務表的查詢維護權限即可,授權sql如下:
GRANT SELECT, INSERT, UPDATE, DELETE ON d3307.* TO zengxiaoteng@'%' IDENTIFIED BY '0523';
- 1
7.2 開發(fā)人員賬戶:
賦予增刪改查的權限哄啄,授權sql如下:
GRANT SELECT,INSERT,DELETE,UPDATE ON d3307.* TO huyan@'%' IDENTIFIED BY '0523';
- 1
授予創(chuàng)建雅任、修改、刪除 MySQL 數(shù)據(jù)表結構權限咨跌。
GRANT CREATE ON d3307.* TO huyan@’192.168.52.11’; GRANT ALTER ON d3307.* TO huyan@’192.168.52.11’; GRANT DROP ON d3307.* TO huyan@’192.168.52.11’;
- 1
- 2
- 3
授予操作 MySQL 外鍵權限:
GRANT REFERENCES ON d3307.* TO huyan@’192.168.52.11’;
- 1
授予操作 MySQL 臨時表權限:
GRANT CREATE TEMPORARY TABLES ON d3307.* TO huyan@’192.168.52.11’;
- 1
授予操作 MySQL 索引權限:
GRANT INDEX ON d3307.* TO huyan@’192.168.52.11’;
- 1
授予操作 MySQL 視圖沪么、查看視圖源代碼 權限:
GRANT CREATE VIEW ON d3307.* TO huyan@’192.168.52.11’; GRANT SHOW VIEW ON d3307.* TO huyan@’192.168.52.11’;
- 1
- 2
授予操作 MySQL 存儲過程、函數(shù) 權限:
GRANT CREATE ROUTINE ON d3307.* TO huyan@’192.168.52.11’; GRANT ALTER ROUTINE ON d3307.* TO huyan@’192.168.52.11’; GRANT EXECUTE ON d3307.* TO huyan@’192.168.52.11’;
- 1
- 2
- 3
7.3 DBA人員賬戶
授予普通DBA管理某個MySQL數(shù)據(jù)庫(test)的權限:
GRANT ALL PRIVILEGES ON test TO sysdba@'192.168.52.%';
- 1
授予高級 DBA 管理 MySQL 中所有數(shù)據(jù)庫的權限:
GRANT ALL ON . TO sysdba@'192.168.52.%';
- 1
7.4 數(shù)據(jù)分析人員只讀賬號
只需要分配只讀的權限:
GRANT SELECT ON d3307.* TO dataquery@'192.168.52.129' IDENTIFIED BY '20150523';
- 1
甚至有些用戶锌半,可以只分配讀取某些表列的權限禽车,如下所示:
GRANT SELECT ON test.* TO dataquery@’192.168.52.%’ IDENTIFIED BY ‘20150523’;
GRANT SELECT(id,uname) ON d3307.t TO dataquery@’192.168.52.%’ ;
示列權限登錄操作:
[root@data02 ~]# mysql --socket=/usr/local/mysql3307/mysql.sock -u dataquery -p20150523 -h192.168.52.130 -P3307 Welcome TO the MySQL monitor. Commands END WITH ; OR \g. Your MySQL CONNECTION id IS 18 SERVER VERSION: 5.6.12-LOG Source distribution Copyright (c) 2000, 2013, Oracle AND/OR its affiliates. ALL rights reserved. Oracle IS a registered trademark of Oracle Corporation AND/OR its affiliates. Other NAMES may be trademarks of their respective owners. TYPE 'help;' OR '\h' FOR help. TYPE '\c' TO clear the current input statement. mysql> SELECT * FROM d3307.t; ERROR 1142 (42000): SELECT command denied TO USER 'dataquery'@'data02' FOR TABLE 't' mysql> mysql> SELECT id,uname FROM d3307.t; +----+-------+ | id | uname | +----+-------+ | 1 | a | +----+-------+ 1 ROW IN SET (0.00 sec) mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
8,權限劃分一般原則
數(shù)據(jù)庫一般劃分為線上庫刊殉,測試庫殉摔,開發(fā)庫。
8.1對于線上庫:
DBA:有所有權限记焊,超級管理員權限
應用程序:分配insert逸月、delete、update遍膜、select碗硬、execute瓤湘、events、jobs權限恩尾。
測試人員:select某些業(yè)務表權限
開發(fā)人員:select某些業(yè)務表權限
原則:所有對線上表的操作弛说,除了應用程序之外,都必須經(jīng)由DBA來決定是否執(zhí)行特笋、已經(jīng)什么時候執(zhí)行等剃浇。
8.2 測試庫
DBA:所有權限。
測試人員:有insert猎物、delete虎囚、update、select蔫磨、execute淘讥、jobs權限。
數(shù)據(jù)分析人員:只有select查詢權限
開發(fā)人員:有select權限堤如。
原則:DBA有所有權限蒲列,而且嚴格控制表結構的變更,不允許除了dba之外的人對測試環(huán)境的庫環(huán)境進行修改搀罢,以免影響測試人員測試蝗岖。所有對測試庫的表結構進行的修改必須由測試人員和DBA一起審核過后才能操作。
8.3 開發(fā)庫
DBA:所有權限