MySQL權限簡介html
權限簡單的理解就是mysql容許權力之內的事情,不可越界。好比只容許執行select操做,那麼就不能執行update操做。只容許從某臺機器上鍊接mysql,那麼就不能從除那臺機器之外的其餘機器鏈接mysql。mysql
那麼Mysql的權限是如何實現的呢?這就要說到mysql的兩階段驗證,下面詳細介紹:sql
第一階段:服務器首先會檢查是否容許鏈接。由於建立用戶的時候會加上主機限制,能夠限制成本地、某個IP、某個IP段、以及任何地方等,只容許從配置的指定地方登錄。數據庫
第二階段:若是能鏈接,Mysql會檢查發出的每一個請求,看是否有足夠的權限實施它。好比要更新某個表、或者查詢某個表,Mysql會查看對哪一個表或者某個列是否有權限。再好比,要運行某個存儲過程,Mysql會檢查對存儲過程是否有執行權限等。apache
一、GRANT命令使用說明:服務器
先來看一個例子,建立一個只容許從本地登陸的超級用戶jack,並容許將權限賦予別的用戶,密碼爲:jack.併發
mysql> grant all privileges on *.* to jack@'localhost' identified by "jack" with grant option; Query OK, 0 rows affected (0.01 sec)
GRANT命令說明:
ALL PRIVILEGES 是表示全部權限,也可使用select、update等權限。app
ON 用來指定權限針對哪些庫和表。socket
*.* 中前面的*號用來指定數據庫名,後面的*號用來指定表名。ide
TO 表示將權限賦予某個用戶。
jack@'localhost' 表示jack用戶,@後面接限制的主機,能夠是IP、IP段、域名以及%,%表示任何地方。注意:這裏%有的版本不包括本地,之前碰到過給某個用戶設置 了%容許任何地方登陸,可是在本地登陸不了,這個和版本有關係,遇到這個問題再加一個localhost的用戶就能夠了。
IDENTIFIED BY 指定用戶的登陸密碼。
WITH GRANT OPTION 這個選項表示該用戶能夠將本身擁有的權限受權給別人。注意:常常有人在建立操做用戶的時候不指定WITH GRANT OPTION選項致使後來該用戶不能使用GRANT命令建立用戶或者給其它用戶受權。
備註:可使用GRANT重複給用戶添加權限,權限疊加,好比你先給用戶添加一個select權限,而後又給用戶添加一個insert權限,那麼該用戶就同時擁有了select和insert權限。
二、刷新權限
使用這個命令使權限生效,尤爲是你對那些權限表user、db、host等作了update或者delete更新的時候。之前遇到過使用grant後權限沒有更新的狀況,只要對權限作了更改就使用FLUSH PRIVILEGES命令來刷新權限。
mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
三、查看權限
查看當前用戶的權限:
mysql> show grants; +---------------------------------------------------------------------+ | Grants for root@localhost | +---------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION | | GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION | +---------------------------------------------------------------------+ 2 rows in set (0.00 sec) 查看某個用戶的權限: mysql> show grants for 'jack'@'%'; +-----------------------------------------------------------------------------------------------------+ | Grants for jack@% | +-----------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'jack'@'%' IDENTIFIED BY PASSWORD '*9BCDC990E611B8D852EFAF1E3919AB6AC8C8A9F0' | +-----------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
四、回收權限
mysql> revoke delete on *.* from 'jack'@'localhost'; Query OK, 0 rows affected (0.01 sec)
五、刪除用戶
mysql> select host,user,password from user; +-----------+------+-------------------------------------------+ | host | user | password | +-----------+------+-------------------------------------------+ | localhost | root | | | rhel5.4 | root | | | 127.0.0.1 | root | | | ::1 | root | | | localhost | | | | rhel5.4 | | | | localhost | jack | *9BCDC990E611B8D852EFAF1E3919AB6AC8C8A9F0 | +-----------+------+-------------------------------------------+ 7 rows in set (0.00 sec) mysql> drop user 'jack'@'localhost'; Query OK, 0 rows affected (0.01 sec)
六、對帳戶重命名
mysql> rename user 'jack'@'%' to 'jim'@'%'; Query OK, 0 rows affected (0.00 sec)
七、修改密碼
1、用set password命令 mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456'); Query OK, 0 rows affected (0.00 sec) 2、用mysqladmin [root@rhel5 ~]# mysqladmin -uroot -p123456 password 1234abcd 備註: 格式:mysqladmin -u用戶名 -p舊密碼 password 新密碼 3、用update直接編輯user表 mysql> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set PASSWORD = PASSWORD('1234abcd') where user = 'root'; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) 4、在忘記root密碼的時候: [root@rhel5 ~]# mysqld_safe --skip-grant-tables & [1] 15953 [root@rhel5 ~]# 130911 09:35:33 mysqld_safe Logging to '/mysql/mysql5.5/data/rhel5.4.err'. 130911 09:35:33 mysqld_safe Starting mysqld daemon with databases from /mysql/mysql5.5/data [root@rhel5 ~]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.22 Source distribution Copyright (c) 2000, 2011, 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> \s -------------- mysql Ver 14.14 Distrib 5.5.22, for Linux (i686) using EditLine wrapper Connection id: 2 Current database: Current user: root@ SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.5.22 Source distribution Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /tmp/mysql.sock Uptime: 36 sec Threads: 1 Questions: 5 Slow queries: 0 Opens: 23 Flush tables: 1 Open tables: 18 Queries per second avg: 0.138 -------------- mysql> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set password = PASSWORD('123456') where user = 'root'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
=======================
咱們要關注兩個表,分別是在MySQL數據庫中user表和db表。
user表在某種程度上是獨一無二的,由於它是惟一一個在權限請求的認證和受權階段都起做用的表,也是惟一一個存數MySQL服務器相關權限的權限表。在認證階段,它只是負責爲用戶受權訪問MySQL服務器,肯定用戶每小時的最大鏈接數和最大併發數;在受權階段,user肯定容許訪問服務器的用戶是否被賦予了操做數據庫的全局權限,肯定用戶每小時的最大查詢數和更新數。
db表用於爲每一個用戶針對每一個數據庫賦予權限。具體的能夠查看db的字段。
=======================
grant 普通數據用戶,查詢、插入、更新、刪除 數據庫中全部表數據的權利。
grant select on testdb.* to common_user@’%’
grant insert on testdb.* to common_user@’%’
grant update on testdb.* to common_user@’%’
grant delete on testdb.* to common_user@’%’
或者,用一條 MySQL 命令來替代:
grant select, insert, update, delete on testdb.* to common_user@’%’
9>.grant 數據庫開發人員,建立表、索引、視圖、存儲過程、函數等權限。
grant 建立、修改、刪除 MySQL 數據表結構權限。
grant create on testdb.* to developer@’192.168.0.%’;
grant alter on testdb.* to developer@’192.168.0.%’;
grant drop on testdb.* to developer@’192.168.0.%’;
grant 操做 MySQL 外鍵權限。
grant references on testdb.* to developer@’192.168.0.%’;
grant 操做 MySQL 臨時表權限。
grant create temporary tables on testdb.* to developer@’192.168.0.%’;
grant 操做 MySQL 索引權限。
grant index on testdb.* to developer@’192.168.0.%’;
grant 操做 MySQL 視圖、查看視圖源代碼 權限。
grant create view on testdb.* to developer@’192.168.0.%’;
grant show view on testdb.* to developer@’192.168.0.%’;
grant 操做 MySQL 存儲過程、函數 權限。
grant create routine on testdb.* to developer@’192.168.0.%’; -- now, can show procedure status
grant alter routine on testdb.* to developer@’192.168.0.%’; -- now, you can drop a procedure
grant execute on testdb.* to developer@’192.168.0.%’;
10>.grant 普通 DBA 管理某個 MySQL 數據庫的權限。
grant all privileges on testdb to dba@’localhost’
其中,關鍵字 「privileges」 能夠省略。
11>.grant 高級 DBA 管理 MySQL 中全部數據庫的權限。
grant all on *.* to dba@’localhost’
12>.MySQL grant 權限,分別能夠做用在多個層次上。
1. grant 做用在整個 MySQL 服務器上:
grant select on *.* to dba@localhost; -- dba 能夠查詢 MySQL 中全部數據庫中的表。
grant all on *.* to dba@localhost; -- dba 能夠管理 MySQL 中的全部數據庫
2. grant 做用在單個數據庫上:
grant select on testdb.* to dba@localhost; -- dba 能夠查詢 testdb 中的表。
3. grant 做用在單個數據表上:
grant select, insert, update, delete on testdb.orders to dba@localhost;
4. grant 做用在表中的列上:
grant select(id, se, rank) on testdb.apache_log to dba@localhost;
5. grant 做用在存儲過程、函數上:
grant execute on procedure testdb.pr_add to ’dba’@’localhost’
grant execute on function testdb.fn_add to ’dba’@’localhost’
注意:修改完權限之後 必定要刷新服務,或者重啓服務,刷新服務用:FLUSH PRIVILEGES。全局權限的設置語句:
1
|
GRANT
ALL
ON
*.*
to
'root'
@
'localhost'
|
2,Database Level: 數據庫級別的權限,經過databasename.* 設置權限。設置語句以下:
1
|
GRANT
ALL
ON
databasename.*
to
'root'
@
'localhost'
|
1
2
|
GRANT
SELECT
on
test.*
to
'root'
@
'localhost'
;
REVOKE
SELECT
ON
*.*
FROM
'root'
@
'localhost'
;
|
3,Table Level:表級別的權限能被全局權限和數據庫級別權限覆蓋
1
2
|
GRANT
SELECT
ON
test.test
to
'root'
@
'localhost'
;
SHOW GRANTS
FOR
'root'
@
'localhost'
;
|
1
|
GRANT
SELECT
ON
test
to
'root'
@
'localhost'
;
|
4,Column Level:表的某個列的權限。它會被前面三個權限給覆蓋掉
1
|
GRANT
SELECT
(id)
ON
test
to
'root'
@
'localhost'
;
|
5,Routine Level:是針對函數和存儲過程的權限,他會被1,2,3個權限給覆蓋掉。
1
|
GRANT
EXECUTE
ON
test.p
to
'root'
@
'localhost'
;
|
select current_user();
select user();