如何撤销一个表的MySQL用户权限?

时间:2015-12-01 12:24:33

标签: mysql sql privileges

当我为某些特定表授予用户权限时:

GRANT ALL PRIVILEGES ON table1.* TO 'user1'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON table2.* TO 'user1'@'localhost' IDENTIFIED BY 'password';

如何撤销此用户的权限,仅适用于table1

2 个答案:

答案 0 :(得分:8)

Google是你的朋友! http://dev.mysql.com/doc/refman/5.7/en/revoke.html

语法:

REVOKE ALL PRIVILEGES ON table1.* FROM 'user1'@'localhost';

为了进一步解释这个答案 - 我将教导如何钓鱼(而不仅仅是给你一条鱼)。

MySQL文档最初看起来很混乱 - REVOKE的“语法”如下所示:

REVOKE
    priv_type [(column_list)]
      [, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    FROM user [, user] ...

REVOKE ALL PRIVILEGES, GRANT OPTION
    FROM user [, user] ...

REVOKE PROXY ON user
    FROM user [, user] ...

这意味着有3种方式可以调用它:

  1. REVOKE priv_type ...
  2. REVOKE ALL PRIVILEGES, GRANT ...
  3. REVOKE PROXY ON ...
  4. 这三个由MySQL文档页面中的空行分隔。

    对于其中每一个,都有“可选”参数/设置/值。这些用方括号表示,例如:

    REVOKE priv_type [(column_list)] ...
    

    (column_list)是可选的。你可以提供它,但你没有 to。

    同样地,你可以将它们链接在一起 - 它们缩进了下一行以表明这一点(并使用...来表明你可以继续重复):

    priv_type [(column_list)]
      [, priv_type [(column_list)]] ...    <-- indented, and note the "..."
    

    MySQL文档中存在更复杂的示例 - 例如CREATE TABLE您有可选标记列表:

    [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
    

    {x|y|z}语法表示您必须指定其中一个({...}是非可选的,[...]表示内部的所有内容都是可选的 - 所以如果您指定COLUMN_FORMAT,以下三个标志之一是必需),管道(|)表示您只能指定列表的一个FIXED / DYNAMIC / DEFAULT)。

    最后要说的是 - 非常了解MySQL文档版本。它在网站上的几个地方都有说明 - 我个人只看网址:

    http://dev.mysql.com/doc/refman/5.7/en/create-table.html
    

    请注意它中的5.7。这意味着您正在阅读的文档可能不适用于MySQL 5.7以外的任何版本。这让我经常被咬了很多次......通常当我在枪下试图解决恐慌事件时!一定要仔细检查一下。

答案 1 :(得分:1)

@Nadeem Taj。你不正确。如果您之前授予了所有权限。 .

REVOKE ALL PRIVILEGES on Tblname.* 将 Tblname 视为架构名称。

证明:

mysql<root@127.0.0.1:[amp]> create user 'myuser'@'%' identified by 'password';
Query OK, 0 rows affected (0.02 sec)

mysql<root@127.0.0.1:[mysql]> grant all privileges on  *.* to 'myuser'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql<root@127.0.0.1:[mysql]> create database specialdb;
Query OK, 1 row affected (0.01 sec)

mysql<root@127.0.0.1:[mysql]> use specialdb;
Database changed
mysql<root@127.0.0.1:[specialdb]> create table tbl1(col1 int primary key);
Query OK, 0 rows affected (0.03 sec)

mysql<root@127.0.0.1:[mysql]> insert into specialdb.tbl1(col1) values(1),(2),(3);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql<root@127.0.0.1:[specialdb]> REVOKE ALL PRIVILEGES ON tbl1.* FROM 'myuser'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql<root@127.0.0.1:[specialdb]> exit
Bye

$ mysql mysql  -umyuser -p
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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<myuser@127.0.0.1:[mysql]> select * from specialdb.tbl1;
+------+
| col1 |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.01 sec)

mysql<myuser@127.0.0.1:[mysql]> 
相关问题