首頁 資料庫 mysql教程 MySQLStudy之--MySQL用户及权限管理_MySQL

MySQLStudy之--MySQL用户及权限管理_MySQL

Jun 01, 2016 pm 12:59 PM
權限 使用者

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语句的影响。

案例分析:
一、创建用户并授权(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)


1、建立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 ;
+-------+-----------+
| 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" 表示该用户可以为其他用户分配权限。


2、我们用 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 ;
+-------+-----------+
| 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 ;
+-------+-----------+
| 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)

3、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)


4、回收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失败!

二、修改用户口令:

1、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>

2、普通用户修改自己密码:
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>
---新密码登陆成功 !

三、删除用户:
1、回收用户所有权限
mysql> revoke all on prod.* from tom2;
Query OK, 0 rows affected (0.01 sec)

2、删除用户
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;
+-------+-----------+
| 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。


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何用小紅書號找出用戶?能查到手機號碼嗎? 如何用小紅書號找出用戶?能查到手機號碼嗎? Mar 22, 2024 am 08:40 AM

隨著社群媒體的迅速發展,小紅書已經成為了備受青睞的社群平台之一。用戶可以透過建立小紅書號來展示個人身份,並與其他用戶交流互動。如果你需要找某個用戶的小紅書號碼,可以按照以下簡單步驟來操作。一、如何用小紅書號找出用戶? 1.開啟小紅書APP,點選右下角的「發現」按鈕,然後選擇「筆記」選項。 2.在筆記清單中,找到你想找的用戶發布的筆記。點擊進入筆記詳情頁。 3.在筆記詳情頁中,點選使用者頭像下方的「追蹤」按鈕,即可進入該使用者的個人首頁。 4.在使用者個人主頁右上角,點選三個點按鈕,然後選擇「個人資訊

一鍵開啟root權限(快速取得root權限) 一鍵開啟root權限(快速取得root權限) Jun 02, 2024 pm 05:32 PM

可以讓使用者對系統進行更深入的操作和定制,root權限是一種管理員權限,在Android系統中。取得root權限通常需要一系列繁瑣的步驟,對於一般使用者來說可能不太友善、然而。透過一鍵開啟root權限,本文將介紹一種簡單而有效的方法,幫助使用者輕鬆取得系統權限。了解root權限的重要性及風險擁有更大的自由度,root權限可以讓使用者完全控製手機系統。加強安全控制等,客製化主題、使用者可刪除預先安裝應用程式。例如誤刪系統檔案導致系統崩潰,過度使用root權限也有風險、不慎安裝惡意軟體等,然而。在使用root權限前

以超級使用者登入Ubuntu 以超級使用者登入Ubuntu Mar 20, 2024 am 10:55 AM

在Ubuntu系統中,root使用者通常是停用狀態的。要啟動root用戶,可以使用passwd指令設定密碼,然後使用su-指令以root身分登入。根用戶是具有系統管理權限且不受限制的使用者。他擁有存取和修改檔案、使用者管理、軟體安裝和刪除,以及系統配置變更等權限。根用戶與一般用戶有著明顯的區別,根用戶擁有系統中最高的權限和更廣泛的控制權。根用戶可以執行重要的系統命令和編輯系統文件,而普通用戶則無法做到這一點。在本指南中,我將探討Ubuntu根用戶,如何以根用戶身份登錄,以及它與一般用戶的不同之處。注意

教學:Ubuntu系統如何刪除一般使用者帳號? 教學:Ubuntu系統如何刪除一般使用者帳號? Jan 02, 2024 pm 12:34 PM

Ubuntu系統增加了許多用戶,想在不用的用戶想要刪除,該怎麼刪除呢?下面我們就來看看詳細的教學。 1.開啟終端命令列,運用userdel指令刪除指定的用戶,注意要加sudo權限指令,如下圖所示2、在刪除的時候一定注意是在管理員目錄下的,普通的用戶是沒有這個權限的,如下圖所示3、刪除指令執行完了以後怎麼判斷是否真正刪除了呢?下面我們運用cat指令開啟passwd文件,如下圖所示4、我們看到passwd檔案中已經沒有了所刪除的使用者資料了,這證明使用者已經被刪除了,如下圖所示5、然後我們進入home文件

什麼是 sudo,為什麼它如此重要? 什麼是 sudo,為什麼它如此重要? Feb 21, 2024 pm 07:01 PM

sudo(超級使用者執行)是Linux和Unix系統中的關鍵指令,允許一般使用者以root權限執行特定指令。 sudo的功能主要體現在以下幾個方面:提供權限控制:sudo透過授權使用者以臨時方式取得超級使用者權限,從而實現了對系統資源和敏感操作的嚴格控制。普通用戶只能在需要時透過sudo獲得臨時的特權,而不需要一直以超級用戶登入。提升安全性:透過使用sudo,可以避免在常規操作中使用root帳號。使用root帳戶進行所有操作可能會導致意外的系統損壞,因為任何錯誤或不小心的操作都將具有完全的權限。而

qq空間如何設定權限訪問 qq空間如何設定權限訪問 Feb 23, 2024 pm 02:22 PM

qq空間如何設定權限存取?在QQ空間中是可以設定權限訪問,但是多數的小夥伴不知道QQ空間如何設定權限存取的功能,接下來就是小編為使用者帶來的qq空間設定權限存取方法圖文教程,有興趣的用戶快來一起看看吧! QQ使用教學qq空間如何設定權限存取1、先開啟QQ應用,主頁點選左上角【頭像】點選;2、然後左側展開個人資訊專區,點選左下角【設定】功能;3、進入設定頁面滑動,找到其中的【隱私】選項;4、接下來在隱私的介面,其中的【權限設定】服務;5、之後挑戰到最新頁面選擇【空間動態】;6、再次在QQ空間設置

Linux系統中的使用者密碼儲存機制解析 Linux系統中的使用者密碼儲存機制解析 Mar 20, 2024 pm 04:27 PM

Linux系統中的使用者密碼儲存機制解析在Linux系統中,使用者密碼的儲存是非常重要的安全機制之一。本文將解析Linux系統中使用者密碼的儲存機制,包括密碼的加密儲存、密碼的驗證過程以及如何安全地管理使用者密碼。同時,將透過具體的程式碼範例展示密碼儲存的實際操作流程。一、密碼的加密儲存在Linux系統中,使用者密碼並不是以明文的形式儲存在系統中,而是經過加密後儲存。 L

Discuz論壇權限管理:閱讀權限設定指南 Discuz論壇權限管理:閱讀權限設定指南 Mar 10, 2024 pm 05:33 PM

Discuz論壇權限管理:閱讀權限設定指南在Discuz論壇管理中,權限設定是至關重要的一環。其中,閱讀權限的設定尤其重要,它決定了不同使用者在論壇中能夠看到的內容範圍。本文將詳細介紹Discuz論壇的閱讀權限設置,以及如何針對不同的需求進行靈活的配置。一、閱讀權限基礎概念在Discuz論壇中,閱讀權限主要有以下幾個概念需要了解:預設閱讀權限:新使用者註冊後預設

See all articles