mysql 用戶及權限管理 小結

隨筆 - 23  文章 - 0  評論 - 2html

mysql 用戶及權限管理 小結

MySQL 默認有個root用戶,可是這個用戶權限太大,通常只在管理數據庫時候才用。若是在項目中要鏈接 MySQL 數據庫,則建議新建一個權限較小的用戶來鏈接。mysql

在 MySQL 命令行模式下輸入以下命令能夠爲 MySQL 建立一個新用戶:sql

1數據庫

CREATE USER username IDENTIFIED BY 'password';apache

新用戶建立完成,可是此刻若是以此用戶登錄的話,會報錯,由於咱們尚未爲這個用戶分配相應權限,分配權限的命令以下:緩存

1服務器

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';oracle

授予username用戶在全部數據庫上的全部權限。socket

若是此時發現剛剛給的權限太大了,若是咱們只是想授予它在某個數據庫上的權限,那麼須要切換到root 用戶撤銷剛纔的權限,從新受權:ide

1

2

EVOKE ALL PRIVILEGES ON *.* FROM 'username'@'localhost';

GRANT ALL PRIVILEGES ON wordpress.* TO 'username'@'localhost' IDENTIFIED BY 'password';

甚至還能夠指定該用戶只能執行 select 和 update 命令:

1

GRANT SELECT, UPDATE ON wordpress.* TO 'username'@'localhost' IDENTIFIED BY 'password';

這樣一來,再次以username登錄 MySQL,只有wordpress數據庫是對其可見的,而且若是你只受權它select權限,那麼它就不能執行delete 語句。

另外每當調整權限後,一般須要執行如下語句刷新權限:

1

FLUSH PRIVILEGES;

刪除剛纔建立的用戶:

1

DROP USER username@localhost;

仔細上面幾個命令,能夠發現不論是受權,仍是撤銷受權,都要指定響應的host(即 @ 符號後面的內容),由於以上及格命令實際上都是在操做mysql 數據庫中的user表,能夠用以下命令查看相應用戶及對應的host:

1

SELECT User, Host FROM user;

 

 

 

MySQL Study之--MySQL用戶及權限管理
MySQL服務器經過MySQL權限表來控制用戶對數據庫的訪問,MySQL權限表存放在mysql數據庫裏,由mysql_install_db腳本初始化。這些MySQL權限表分別user,db,table_priv,columns_priv和host。下面分別介紹一下這些表的結構和內容:
user權限表:記錄容許鏈接到服務器的用戶賬號信息,裏面的權限是全局級的。
db權限表:記錄各個賬號在各個數據庫上的操做權限。
table_priv權限表:記錄數據表級的操做權限。
columns_priv權限表:記錄數據列級的操做權限。
host權限表:配合db權限表對給定主機上數據庫級操做權限做更細緻的控制。這個權限表不受GRANT和REVOKE語句的影響。

案例分析:
1、建立用戶並受權(root用戶)
[root@mysrv ~]# mysql -u root -poracle

mysql> select version()\g
+-------------------------------------------+
| version() |
+-------------------------------------------+
| 5.6.25-enterprise-commercial-advanced-log |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| prod |
| test |
+--------------------+
5 rows in set (0.01 sec)


一、創建tom用戶並受權(特權管理用戶)

mysql> grant all on prod.* to 'tom'@'%' identified by 'tom' with grant option;
Query OK, 0 rows affected (0.00 sec)

查看用戶建立是否成功:
mysql> select user,host from user ;

1

2

3

4

5

6

7

8

9

10

11

12

13

+-------+-----------+

| user  | host      |

+-------+-----------+

| tom   | %         |

| root  | 127.0.0.1 |

| root  | ::1       |

|       | localhost |

| root  | localhost |

| scott | localhost |

|       | mysrv     |

| root  | mysrv     |

+-------+-----------+

8 rows in set (0.00 sec)

查看tom用戶的受權:
mysql> show grants for tom;
+----------------------------------------------------------------------------------------------------+
| Grants for tom@% |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'%' IDENTIFIED BY PASSWORD '*71FF744436C7EA1B954F6276121DB5D2BF68FC07' |
| GRANT ALL PRIVILEGES ON `prod`.* TO 'tom'@'%' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------+

GRANT 語法:
GRANT privileges (columns)
ON what
TO user IDENTIFIED BY "password"
WITH GRANT OPTION


權限列表:
ALTER: 修改表和索引。
CREATE: 建立數據庫和表。
DELETE: 刪除表中已有的記錄。
DROP: 拋棄(刪除)數據庫和表。
INDEX: 建立或拋棄索引。
INSERT: 向表中插入新行。
REFERENCE: 未用。
SELECT: 檢索表中的記錄。
UPDATE: 修改現存表記錄。
FILE: 讀或寫服務器上的文件。
PROCESS: 查看服務器中執行的線程信息或殺死線程。
RELOAD: 重載受權表或清空日誌、主機緩存或表緩存。
SHUTDOWN: 關閉服務器。
ALL: 全部權限,ALL PRIVILEGES同義詞。
USAGE: 特殊的 "無權限" 權限。
用 戶帳戶包括 "username" 和 "host" 兩部分,後者表示該用戶被容許從何地接入。tom@'%' 表示任何地址,默承認以省略。還能夠是 "tom@192.168.1.%"、"tom@%.abc.com" 等。數據庫格式爲 db@table,能夠是 "test.*" 或 "*.*",前者表示 test 數據庫的全部表,後者表示全部數據庫的全部表。
子句 "WITH GRANT OPTION" 表示該用戶能夠爲其餘用戶分配權限。 


二、咱們用 root 再建立幾個用戶,而後由 test 數據庫的管理員tom爲他們分配權限。

mysql> create user 'tom1' identified by 'tom1' ,'tom2' identified by 'tom2';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from user ;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

+-------+-----------+

| user  | host      |

+-------+-----------+

| tom   | %         |

| tom1  | %         |

| tom2  | %         |

| root  | 127.0.0.1 |

| root  | ::1       |

|       | localhost |

| root  | localhost |

| scott | localhost |

|       | mysrv     |

| root  | mysrv     |

+-------+-----------+

10 rows in set (0.00 sec)

root用戶退出,tom登錄,並受權用戶訪問prod庫

[root@mysrv ~]# mysql -u tom -ptom 
ERROR 1045 (28000): Access denied for user 'tom'@'localhost' (using password: YES)

tom用戶竟不能登錄!!!

再對tom用戶受權:
mysql> grant all on prod.* to 'tom'@'localhost' identified by 'tom' with grant option;;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for tom;
+----------------------------------------------------------------------------------------------------+
| Grants for tom@% |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'%' IDENTIFIED BY PASSWORD '*71FF744436C7EA1B954F6276121DB5D2BF68FC07' |
| GRANT ALL PRIVILEGES ON `prod`.* TO 'tom'@'%' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> use mysql;
Database changed
mysql> select user,host from user ;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

+-------+-----------+

| user  | host      |

+-------+-----------+

| tom   | %         |

| tom1  | %         |

| tom2  | %         |

| root  | 127.0.0.1 |

| root  | ::1       |

|       | localhost |

| root  | localhost |

| scott | localhost |

| tom   | localhost |

|       | mysrv     |

| root  | mysrv     |

+-------+-----------+

11 rows in set (0.00 sec)

tom登錄:
[root@mysrv ~]# mysql -u tom -ptom prod
mysql> select database();
+------------+
| database() |
+------------+
| prod |
+------------+
1 row in set (0.01 sec)

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| tom@localhost |
+----------------+
1 row in set (0.00 sec)

建立表:

mysql> show tables;
+----------------+
| Tables_in_prod |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)

mysql> create table t2 as select * from t1;
Query OK, 3 rows affected (0.15 sec)
Records: 3 Duplicates: 0 Warnings: 0

查看錶信息:

mysql> desc t2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> show create table t2;
+-------+---------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------+
| t2 | CREATE TABLE `t2` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

mysql> show create table t2\G;
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> select * from t2;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
+------+-------+
3 rows in set (0.00 sec)

三、tom用戶爲tom1,tom2受權
mysql> grant select on prod.* to tom1;
Query OK, 0 rows affected (0.00 sec)

mysql> grant select on prod.* to tom2;
Query OK, 0 rows affected (0.02 sec)

mysql> grant insert,update on prod.* to tom2;
Query OK, 0 rows affected (0.00 sec)

tom2登錄(從遠程登錄):

C:\Users\Administrator>mysql -h 192.168.8.240 -utom2 -ptom2

mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)

mysql> use prod;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| prod |
+------------+
1 row in set (0.00 sec)

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| tom2@% |
+----------------+
1 row in set (0.00 sec)

mysql> show grants for tom2;
+------------------------------------------------------------------+
| Grants for tom2@% |
+------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom2'@'%' IDENTIFIED BY PASSWORD |
| GRANT SELECT, INSERT, UPDATE ON `prod`.* TO 'tom2'@'%' |
+------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show tables;
+----------------+
| Tables_in_prod |
+----------------+
| t1 |
| t2 |
+----------------+
2 rows in set (0.00 sec)

mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
+------+-------+
3 rows in set (0.00 sec)

mysql> select * from t2;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
+------+-------+
3 rows in set (0.00 sec)

mysql> insert into t1 values (40,'john');
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.09 sec)

mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
| 40 | john |
+------+-------+
4 rows in set (0.00 sec)

mysql> update t1 set name='ellen' where id=40;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
| 40 | ellen |
+------+-------+
4 rows in set (0.00 sec)

mysql> delete from t1;
ERROR 1142 (42000): DELETE command denied to user 'tom2'@'192.168.8.254' for tab
le 't1'
mysql> commit;
Query OK, 0 rows affected (0.05 sec)

mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
| 40 | ellen |
+------+-------+
4 rows in set (0.00 sec)


四、回收tom2的update權限:
mysql> revoke update on prod.* from tom2;
Query OK, 0 rows affected (0.00 sec)

tom2再從新登錄:
C:\Users\Administrator>mysql -h 192.168.8.240 -utom2 -ptom2

mysql> use prod;
Database changed
mysql> update t1 set name='lily' where id=10;
ERROR 1142 (42000): UPDATE command denied to user 'tom2'@'192.168.8.254' for tab
le 't1'
---update失敗!

2、修改用戶口令:

一、root用戶修改普通用戶口令
mysql> set password for tom1=password('oracle');
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

tom1從新登錄:
C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -ptom1
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'tom1'@'192.168.8.254' (using passwor
d: YES)
---舊口令登錄失敗!

C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -poracle
mysql>

二、普通用戶修改本身密碼:
C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -poracle
mysql> set password=password('tom1');
Query OK, 0 rows affected (0.00 sec)

從新登錄:
C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -ptom1

mysql>
---新密碼登錄成功 !

3、刪除用戶:
一、回收用戶全部權限
mysql> revoke all on prod.* from tom2;
Query OK, 0 rows affected (0.01 sec)

二、刪除用戶
mysql> drop user tom2;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from user;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

+-------+-----------+

| user  | host      |

+-------+-----------+

| jerry | %         |

| rose  | %         |

| tom   | %         |

| tom1  | %         |

| root  | 127.0.0.1 |

| root  | ::1       |

|       | localhost |

| jerry | localhost |

| root  | localhost |

| rose  | localhost |

| scott | localhost |

| tom   | localhost |

|       | mysrv     |

| root  | mysrv     |

+-------+-----------+

14 rows in set (0.00 sec)

------- 摘要 -------------------------------------- 

建立用戶:
GRANT insert, update ON testdb.* TO user1@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
CREATE USER user2 IDENTIFIED BY 'password';
分配權限:
GRANT select ON testdb.* TO user2;
查看權限:
SHOW GRANTS FOR user1;
修改密碼:
SET PASSWORD FOR user1 = PASSWORD('newpwd');
SET PASSWORD = PASSWORD('newpwd');
移除權限:
REVOKE all ON *.* FROM user1;
刪除用戶:
DROP USER user1;
數據庫列表:
SHOW DATABASES;
數據表列表:
SHOW TABLES;
當前數據庫:
SELECT DATABASE();
當前用戶:
SELECT USER();
數據表結構:
DESCRIBE table1;
刷新權限:
FLUSH PRIVILEGES;

grant和revoke能夠在幾個層次上控制訪問權限
1,整個服務器,使用 grant ALL 和revoke ALL
2,整個數據庫,使用on database.*
3,特色表,使用on database.table
4,特定的列
5,特定的存儲過程

user表中host列的值的意義
% 匹配全部主機
localhost localhost不會被解析成IP地址,直接經過UNIXsocket鏈接
127.0.0.1 會經過TCP/IP協議鏈接,而且只能在本機訪問;
::1 ::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1

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@’%’
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.%’;
grant 普通 DBA 管理某個 MySQL 數據庫的權限。
grant all privileges on testdb to dba@’localhost’
其中,關鍵字 「privileges」 能夠省略。
grant 高級 DBA 管理 MySQL 中全部數據庫的權限。
grant all on *.* to dba@’localhost’

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。

相關文章
相關標籤/搜索