MySQL中的账号与权限管理

MySQL权限管理

权限系统的工作原理    

MySQL权限系统通过下面两个阶段进行认证:
    (1)对连接的用户进行身份认证,合法的用户通过认证、不合法的用户拒绝连接。
    (2)对通过认证的合法用户赋予相应的权限,用户可以在这些权限范围内对数据库做相应的操作。
    对于身份,MySQL是通过IP地址和用户名联合进行确认的,例如MySQL安装默认创建的用户root@localhost表示用户root只能从本地(localhost)进行连接才可以通过认证,此用户从其他任何主机对数据库进行的连接都将被拒绝。也就是说,同样的一个用户名,如果来自不同的IP地址,则MySQL将其视为不同的用户。
    MySQL的权限表在数据库启动地时候就载入内存,当用户通过身份认证后,就在内存中进行相应权限的存取,这样,此用户就可以在数据库中做权限范围内的各种操作了。
 

权限表的存取

    在权限存取的两个过程中,系统会用到“mysql”数据库(安装MySQL时被创建,数据库名字叫“mysql”)中user、host和db这3个最重要的权限表
    
表名userdbhost
用户列 User                    Host                  Host                 
  Password                Db                    Db                   
权限列 Select_priv             User                  Select_priv          
  Insert_priv             Select_priv           Insert_priv          
  Update_priv             Insert_priv           Update_priv          
  Delete_priv             Update_priv           Delete_priv          
  Create_priv             Delete_priv           Create_priv          
  Drop_priv               Create_priv           Drop_priv            
  Reload_priv             Drop_priv             Grant_priv           
  Shutdown_priv           Grant_priv            References_priv      
  Process_priv            References_priv       Index_priv           
  File_priv               Index_priv            Alter_priv           
  Grant_priv              Alter_priv            Create_tmp_table_priv
  References_priv         Create_tmp_table_priv Lock_tables_priv     
  Index_priv              Lock_tables_priv      Create_view_priv     
  Alter_priv              Create_view_priv      Show_view_priv       
  Show_db_priv            Show_view_priv        Create_routine_priv  
  Super_priv              Create_routine_priv   Alter_routine_priv   
  Create_tmp_table_priv   Alter_routine_priv    Execute_priv         
  Lock_tables_priv        Execute_priv          Trigger_priv         
  Execute_priv            Event_priv             
  Repl_slave_priv         Trigger_priv           
  Repl_client_priv         
  Create_view_priv         
  Show_view_priv           
  Create_routine_priv      
  Alter_routine_priv       
  Create_user_priv         
  Event_priv               
  Trigger_priv             
  Create_tablespace_priv   
安全列 ssl_type                 
  ssl_cipher               
  x509_issuer              
  x509_subject             
  max_questions            
  max_updates              
  max_connections          
  max_user_connections     
 
    在这个3表中,最重要的表
    其中,通常用得最多的是用户列和权限列,其中权限列在分为普通权限和管理权限。普通权限主要用于数据库的操作,比如select_priv、create_priv等。而管理权限主要用来对数据库进行管理的操作,比如process_priv、super_priv等。
    当用户进行连接的时候,权限表的存取过程有以下现个阶段。
  • 先从user表中的host、user和passwd这3个字段中判断连接的IP、用户名和密码是否存在于表中,如果存在,则通过身份验证,否则拒绝连接。
  • 如果通过身份验证,则按照以下权限表的顺序得到数据库权限:user->db->tables_priv->coloumns_priv。
     在这几个权限表中,权限范围依次递减,全局权限覆盖局部权限。
     上面的第一阶段好理解,下面以一个例子来详细解释一下第二阶段。
    (1)创建用户cqh@localhost,并赋予所有数据库的所有表的select权限。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql>  grant  select  on  *.*  to  cqh@localhost;
Query OK, 0  rows  affected (0.05 sec)
mysql>  select  from  user  where  user = 'cqh'  and  host= 'localhost'  \G
*************************** 1. row ***************************
                   Host: localhost
                   User : cqh
               Password :
            Select_priv: Y
            Insert_priv: N
            Update_priv: N
            Delete_priv: N
            Create_priv: N
              Drop_priv: N
            ...
    (2)再来看db表:
1
2
mysql>  select  from  db  where  user = 'cqh' ;
Empty  set  (0.00 sec)
    可以发现,user表的select_priv列是“Y”,而db表中并没有记录,也就是说,对所有数据库都具有相同的权限的用户记录并不需要记入db表,而仅仅需要将user表中的select_priv改为“Y”即可。换句话,user表中的每个权限都代表了对所有数据库都有的权限。
    (3)将cqh@localhost上的权限改为只对test数据库上所有表的select权限。
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
mysql>  revoke  select  on  *.*  from  cqh@localhost;
Query OK, 0  rows  affected (0.00 sec)
mysql>  grant  select  on  test.*  to  cqh@localhost;
Query OK, 0  rows  affected (0.00 sec)
mysql>  select  from  user  where  user = 'cqh'  and  host= 'localhost'  \G
*************************** 1. row ***************************
                   Host: localhost
                   User : cqh
               Password :
            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:
  authentication_string:  NULL
1 row  in  set  (0.00 sec)
mysql>  select  from  db  where  user = 'cqh' \G
*************************** 1. row ***************************
                  Host: localhost
                    Db: test
                  User : cqh
           Select_priv: Y
           Insert_priv: N
           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.00 sec)
    这个时候发现,user表中的select_priv变为“N”,而db表中则增加了db为test的一条记录,也就是说,当只授予数据库某些权限时,user表中的相应权限时,user表中的相应权限列保持“N”,而将具体的数据库权限写入db表。
    table和column的权限机制和db类似,这里就不再赘述了。
    从上面的例子可以看出,当用户通过权限认证,进行权限分配时,将按照user->db->tables_priv->coloumns_priv的顺序进行权限分配,即先检查全局权限表user,如果user中对应权限为“Y”,则此用户对所有数据库的权限都为“Y”,将不再检查db、tables_priv和coloumns_priv;如果为“N”,则到db表中检查此用户对应的具体数据库,并得到db中为“Y”的权限;如果db中相应的权限为“N”,则检查tables_priv中此数据库对应的具体表,取得表中为“Y”的权限;如果tables_priv中相应权限为“N”,则检查columns_priv中此表对应的具体列,取得列中为“Y”的权限。
 

账号管理

    账号管理主要包括账号的创建、权限更改和账号的删除。用户连接数据库的第一步都从账号创建开始。
    有两种方法可以用来创建账号:使用GRANT语法创建或者直接操作授权表,但更推荐使用第一种方法,因为操作简单,出错几率更少。

    方式一.创建账号

    GRANT的常用语法如下 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
GRANT
     priv_type [(column_list)]
       [, priv_type [(column_list)]] ...
     ON  [object_type] priv_level
     TO  user_specification [, user_specification] ...
     [REQUIRE {NONE | ssl_option [[ AND ] ssl_option] ...}]
     [ WITH  with_option ...]
GRANT  PROXY  ON  user_specification
     TO  user_specification [, user_specification] ...
     [ WITH  GRANT  OPTION ]
object_type:
     TABLE
   FUNCTION
   PROCEDURE
    来看下面的几个例子。
    例1:创建用户cqh,权限为可以在所有数据库上执行所有权限,只能从本地进行连接。
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
mysql>  grant  all  privileges  on  *.*  to  cqh@localhost;
Query OK, 0  rows  affected (0.00 sec)
mysql>  select  from  user  where  user = 'cqh'  and  host= 'localhost'  \G
*************************** 1. row ***************************
                   Host: localhost
                   User : cqh
               Password :
            Select_priv: Y
            Insert_priv: Y
            Update_priv: Y
            Delete_priv: Y
            Create_priv: Y
              Drop_priv: Y
            Reload_priv: Y
          Shutdown_priv: Y
           Process_priv: Y
              File_priv: Y
             Grant_priv: N
        References_priv: Y
             Index_priv: Y
             Alter_priv: Y
           Show_db_priv: Y
             Super_priv: Y
  Create_tmp_table_priv: Y
       Lock_tables_priv: Y
           Execute_priv: Y
        Repl_slave_priv: Y
       Repl_client_priv: Y
       Create_view_priv: Y
         Show_view_priv: Y
    Create_routine_priv: Y
     Alter_routine_priv: Y
       Create_user_priv: Y
             Event_priv: Y
           Trigger_priv: Y
Create_tablespace_priv: Y
               ssl_type:
             ssl_cipher:
            x509_issuer:
           x509_subject:
          max_questions: 0
            max_updates: 0
        max_connections: 0
   max_user_connections: 0
                 plugin:
  authentication_string:  NULL
1 row  in  set  (0.00 sec)
    可以发现,除了Grant_priv权限外,所有权限在user表里都是“Y”。
    例2:在例1基础上,增加对cqh的grant权限
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
mysql>  grant  all  privileges  on  *.*  to  cqh@localhost  with  grant  option ;
Query OK, 0  rows  affected (0.00 sec)
mysql>  select  from  user  where  user = 'cqh'  and  host= 'localhost'  \G
*************************** 1. row ***************************
                   Host: localhost
                   User : cqh
               Password :
            Select_priv: Y
            Insert_priv: Y
            Update_priv: Y
            Delete_priv: Y
            Create_priv: Y
              Drop_priv: Y
            Reload_priv: Y
          Shutdown_priv: Y
           Process_priv: Y
              File_priv: Y
             Grant_priv: Y
        References_priv: Y
             Index_priv: Y
             Alter_priv: Y
           Show_db_priv: Y
             Super_priv: Y
  Create_tmp_table_priv: Y
       Lock_tables_priv: Y
           Execute_priv: Y
        Repl_slave_priv: Y
       Repl_client_priv: Y
       Create_view_priv: Y
         Show_view_priv: Y
    Create_routine_priv: Y
     Alter_routine_priv: Y
       Create_user_priv: Y
             Event_priv: Y
           Trigger_priv: Y
Create_tablespace_priv: Y
               ssl_type:
             ssl_cipher:
            x509_issuer:
           x509_subject:
          max_questions: 0
            max_updates: 0
        max_connections: 0
   max_user_connections: 0
                 plugin:
  authentication_string:  NULL
1 row  in  set  (0.00 sec)
    例3:在例2基础上,设置密码为“123”。
1
2
mysql>  grant  all  privileges  on  *.*  to  cqh@localhost identified  by  '123'  with  grant  option ;
Query OK, 0  rows  affected (0.00 sec)
    从user表中查看修改的密码:
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
mysql>  select  from  user  where  user = 'cqh'  and  host= 'localhost'  \G
*************************** 1. row ***************************
                   Host: localhost
                   User : cqh
               Password : *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
            Select_priv: Y
            Insert_priv: Y
            Update_priv: Y
            Delete_priv: Y
            Create_priv: Y
              Drop_priv: Y
            Reload_priv: Y
          Shutdown_priv: Y
           Process_priv: Y
              File_priv: Y
             Grant_priv: Y
        References_priv: Y
             Index_priv: Y
             Alter_priv: Y
           Show_db_priv: Y
             Super_priv: Y
  Create_tmp_table_priv: Y
       Lock_tables_priv: Y
           Execute_priv: Y
        Repl_slave_priv: Y
       Repl_client_priv: Y
       Create_view_priv: Y
         Show_view_priv: Y
    Create_routine_priv: Y
     Alter_routine_priv: Y
       Create_user_priv: Y
             Event_priv: Y
           Trigger_priv: Y
Create_tablespace_priv: Y
               ssl_type:
             ssl_cipher:
            x509_issuer:
           x509_subject:
          max_questions: 0
            max_updates: 0
        max_connections: 0
   max_user_connections: 0
                 plugin:
  authentication_string:  NULL
1 row  in  set  (0.00 sec)
    可以发现,密码变成了一堆加密后的字符串。在MySQL5.0里面,密码的算法是生成一个以*开始的41位的字符串,而MySQL4.0之前是16位,因此安全性大大提高。
    例4:创建新用户chenqionghe,可以从任何IP进行连接,权限为test数据库里的所有表进行SELECT、UPDATE、INSERT和DELETE操作,初始密码为“123”。
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
mysql>  grant  select , insert , update , delete  on  test.*  to  'chenqionghe' @ '%'  identified  by  '123' ;
Query OK, 0  rows  affected (0.00 sec)
mysql>  select  from  user  where  user = 'chenqionghe'  and  host= '%'  \G
*************************** 1. row ***************************
                   Host: %
                   User : chenqionghe
               Password : *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
            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:
  authentication_string:  NULL
1 row  in  set  (0.00 sec)
mysql>  select  from  db  where  user = 'chenqionghe'  and  host= '%'  \G
*************************** 1. row ***************************
                  Host: %
                    Db: test
                  User : chenqionghe
           Select_priv: Y
           Insert_priv: Y
           Update_priv: Y
           Delete_priv: Y
           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.00 sec)
    如上文所述,user表中的权限都是“N”,db表中增加的记录权限则都是“Y”。一般地,我们只授予用户适当的权限,而一般不会授予过多的权限,本例中的权限适合大多数应用账号。
    本例中的IP限制为所有IP都可以连接,因此设置为“*”,mysql数据库中是通过user表的host字段来进行控制,host可以是以下类型的值。
  • Host值可以是主机名或IP号,或“localhost"批出本地主机
  • 可以在Host列值使用通配符字符“%”和“_”。
  • Host值“%”匹配任何主机名,空Host值等价于“%”。它们的含义与LIKE操作符的模式匹配操作相同。例如,“%”的Host值与所有主机名匹配,而“%.mysql.com”匹配mysql.com域的所有主机。
host和user组合进行连接的例子
Host值User值被条目匹配的连接
cqh.loc.govcqhcqh,从cqh.loc.gov连接
cqh.loc.gov 任何用户,从cqh.loc.gov连接
%cqhcqh,从任何主机连接
% 任何用户,从任何主机连接
%.loc.govcqhcqh,从在loc.gov域的任何主机连接
x.y.%cqhcqh,从x.y.net、x.y.com、x.y.edu等连接
114.115.166.177cqhcqh,从有114.115.166.177IP地址的主机连接
114.115.166.%cqhcqh,从144.155.166C类子网的任何主机连接
    可能大家会有这样的疑问,如果权限表中的Host既有“cqh.loc.gov”,又有“%”,而此时,连接从主机cqh.loc.gov过来。显然,user表里面这两条记录都符合匹配条件,那系统会选择哪一个呢?
    如果有多个匹配,服务器必须选择使用哪个条目。按照下述原则来解决:
  • 服务器在启动时读入user表后进行排序;
  • 然后当用户试图连接时,以排序的顺序浏览条目;
  • 服务器使用与客户端和用户名匹配的第一行。
    当服务器读取表时,它首先以最具体的Host值排序。主机名和IP号是具体的。“%”意味着“任何主机”并且是最不特定的。有相同Host值的条目首先以最具体的User值排序(空User值意味着“任何用户”并且是最不特定的)。
注意:mysql数据库的user表中host值为%或者空,表示所有外部IP都可以连接,但是不包括本地服务器local,因此,如果要包括本地服务器、必须单独为local赋予权限。
     例5:授予SUPER、PROCESS、FILE权限给用户cqh2@%;
1
2
mysql>  grant  super,process,file  on  *.*  to  'cqh2' @ '%' ;
Query OK, 0  rows  affected (0.00 sec)
    因为这几个权限都属于管理权限,因此不能够指定某个数据库,on后面必须跟“*.*”,下面的语法将提示错误:
1
2
mysql>  grant  super,process,file  on  test.*  to  'cqh2' @ '%' ;
ERROR 1221 (HY000): Incorrect usage  of  DB  GRANT  and  GLOBAL  PRIVILEGES
    例6:只授予登录权限给cqh3@localhost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
mysql>  grant  usage  on  *.*  to  'cqh3' @ 'localhost' ;
Query OK, 0  rows  affected (0.00 sec)
mysql> exit
Bye
[root@iZ28dr6w0qvZ ~]# mysql -ucqh3
Welcome  to  the MySQL monitor.  Commands  end  with  or  \g.
Your MySQL  connection  id  is  1640
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> show databases;
+ --------------------+
Database            |
+ --------------------+
| information_schema |
+ --------------------+
1 row  in  set  (0.00 sec)
    usage权限只能用于数据库登录,不能执行任何操作。
    直接操作权限表也可以进行权限的创建,其实GRANT操作的本质就是修改权限后进行权限的刷新,因此,GRANT比操作权限表更简单,下面继续以上文的例子来说明一下更新权限的用法。
    创建新用户chenqionghe,可以从任何IP进行连接,权限对test库里的所有表进行SELECT、UPDATE、INSERT和DELETE,初始密码为123
1
mysql>  grant  select , insert , update , delete  on  test.*  to  'chenqionghe' @ '%'  identified  by  '123' ;

    方式二:直接操作权限表

    直接操作权限表如下:
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
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter  password :
Welcome  to  the MySQL monitor. Commands  end  with  or  \g.
Your MySQL  connection  id  is  1560
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> use mysql;
Database  changed
mysql>  insert  into  db (host,db, user ,select_priv,insert_priv,update_priv,delete_priv)  values  ( '%' , 'test' , 'chenqionghe' , 'Y' , 'Y' , 'Y' , 'Y' );
Query OK, 1 row affected (0.00 sec)
mysql> flush  privileges ;
 
mysql> exit;
Bye
[root@iZ28dr6w0qvZ ~]# mysql -ucqh3
ERROR 1045 (28000): Access denied  for  user  'cqh3' @ 'localhost'  (using  password NO )
[root@iZ28dr6w0qvZ ~]# mysql -ucqh3 -p
Enter  password :
Welcome  to  the MySQL monitor.  Commands  end  with  or  \g.
Your MySQL  connection  id  is  1643
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> show databases;
+ --------------------+
Database            |
+ --------------------+
| information_schema |
| test               |
+ --------------------+
rows  in  set  (0.00 sec)
    

查看和更改账号的权限

    创建完账号后,时间长了可能就会忘记分配的权限而需要查看账号权限,也在可能会经过一段时间后需要更改以前的账号权限,下面介绍查看和更改这两种操作命令。
  • 查看权限

    账号创建好后,可以通过如下命令查看权限;
1
show grants  for  user @host;
    如以下示例
1
2
3
4
5
6
7
8
mysql> show grants  for  cqh@localhost;
+ ---------------------------------------------------------------------------------------------------------------------------------------+
| Grants  for  cqh@localhost                                                                                                              |
+ ---------------------------------------------------------------------------------------------------------------------------------------+
GRANT  ALL  PRIVILEGES  ON  *.*  TO  'cqh' @ 'localhost'  IDENTIFIED  BY  PASSWORD  '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257'  WITH  GRANT  OPTION  |
GRANT  SELECT  ON  `test`.*  TO  'cqh' @ 'localhost'                                                                                          |
+ ---------------------------------------------------------------------------------------------------------------------------------------+
rows  in  set  (0.00 sec)
    host可以不写,默认是“%”,如下所示
1
2
3
4
5
6
7
mysql> show grants  for  chenqionghe;
+ ------------------------------------------------------------------------------------------------------------+
| Grants  for  chenqionghe@%                                                                                   |
+ ------------------------------------------------------------------------------------------------------------+
GRANT  USAGE  ON  *.*  TO  'chenqionghe' @ '%'  IDENTIFIED  BY  PASSWORD  '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257'  |
+ ------------------------------------------------------------------------------------------------------------+
1 row  in  set  (0.00 sec)
    对于MySQL5.0以后的版本,也可以利用新新增的information_schema数据库进行权限的查看;
1
2
3
4
5
6
7
mysql>  select  from  SCHEMA_PRIVILEGES  where  grantee= "'cqh'@'localhost'" ;
+ -------------------+---------------+--------------+----------------+--------------+
| GRANTEE           | TABLE_CATALOG | TABLE_SCHEMA | PRIVILEGE_TYPE | IS_GRANTABLE |
+ -------------------+---------------+--------------+----------------+--------------+
'cqh' @ 'localhost'  | def           | test         |  SELECT          NO            |
+ -------------------+---------------+--------------+----------------+--------------+
1 row  in  set  (0.00 sec)
  • 更改权限

    可以进行权限的新增和回收。和账号创建一样,权限变更也在两种办法:使用GRANT(新增)和REVOKE(回收)语句,或者更改权限表。
    第二种方法和前面一样,直接对user、db、tables_priv和columns_priv中的权限进行更新即可,这里重点介绍第一种方法。
    和创建账号的语法完全一样,GRANT可以直接用来对账号进行增加。其实GRANT语句在执行的时候,如果权限表中不存在目标账号,则创建账号;如果已经存在,则执行权限的新增。来看下面一个例子。
    (1)cqh3@localhost目前只有登录的权限。
1
2
3
4
5
6
7
mysql> show grants  for  cqh3@localhost;
+ ------------------------------------------+
| Grants  for  cqh3@localhost                |
+ ------------------------------------------+
GRANT  USAGE  ON  *.*  TO  'cqh3' @ 'localhost'  |
+ ------------------------------------------+
1 row  in  set  (0.00 sec)
    (2)赋予cqh3@localhost所有数据库上的所有表的SELECT权限。
1
2
3
4
5
6
7
8
9
mysql>  grant  select  on  *.*  to  'cqh3' @ 'localhost' ;
Query OK, 0  rows  affected (0.00 sec)
mysql> show grants  for  cqh3@localhost;
+ -------------------------------------------+
| Grants  for  cqh3@localhost                 |
+ -------------------------------------------+
GRANT  SELECT  ON  *.*  TO  'cqh3' @ 'localhost'  |
+ -------------------------------------------+
1 row  in  set  (0.00 sec)
    (3)继续给cqh3@localhost赋予SELECT和INSERT权限,和已胡的SELECT权限进行合并。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> show grants  for  cqh3@localhost;
+ -------------------------------------------+
| Grants  for  cqh3@localhost                 |
+ -------------------------------------------+
GRANT  SELECT  ON  *.*  TO  'cqh3' @ 'localhost'  |
+ -------------------------------------------+
1 row  in  set  (0.00 sec)
mysql>  grant  select , insert  on  *.*  to  'cqh3' @ 'localhost' ;
Query OK, 0  rows  affected (0.00 sec)
mysql> show grants  for  cqh3@localhost;
+ ---------------------------------------------------+
| Grants  for  cqh3@localhost                         |
+ ---------------------------------------------------+
GRANT  SELECT INSERT  ON  *.*  TO  'cqh3' @ 'localhost'  |
+ ---------------------------------------------------+
1 row  in  set  (0.00 sec)
    (4)REVOKE语句可以回收已经赋予的权限,语法如下:
1
2
3
4
5
6
7
8
9
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 ] ...
    对于上面的例子,这里决定要收回cqh3@localhost上的INSERT和SELECT权限:
1
2
3
4
5
6
7
8
9
mysql>  revoke  select , insert  on  *.*  from  cqh3@localhost;
Query OK, 0  rows  affected (0.00 sec)
mysql> show grants  for  cqh3@localhost;
+ ------------------------------------------+
| Grants  for  cqh3@localhost                |
+ ------------------------------------------+
GRANT  USAGE  ON  *.*  TO  'cqh3' @ 'localhost'  |
+ ------------------------------------------+
1 row  in  set  (0.00 sec)
    usage权限不能被回收,也就是说,REVOKE用户并不能删除用户。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> show grants  for  cqh3@localhost;
+ ------------------------------------------+
| Grants  for  cqh3@localhost                |
+ ------------------------------------------+
GRANT  USAGE  ON  *.*  TO  'cqh3' @ 'localhost'  |
+ ------------------------------------------+
1 row  in  set  (0.00 sec)
mysql>  revoke  usage  on  *.*  from  cqh@localhost;
Query OK, 0  rows  affected (0.00 sec)
mysql> show grants  for  cqh3@localhost;
+ ------------------------------------------+
| Grants  for  cqh3@localhost                |
+ ------------------------------------------+
GRANT  USAGE  ON  *.*  TO  'cqh3' @ 'localhost'  |
+ ------------------------------------------+
1 row  in  set  (0.00 sec)
  • 修改密码

    方法1:可以用mysqladmin命令在命令行指定密码。
1
shell> mysqladmin -u user_name -h host_name  password  "newpwd"
    方法2:执行SET PASSWORD语句。下例中将账号'chenqionghe'@'%'的密码改为“cqh123”
1
SET  PASSWORD  FOR  'chenqionghe' @ '%'  PASSWORD ( 'cqh123' );
 如果是更改自己的密码,可以省略for语句:
1
SET  PASSWORD  PASSWORD ( 'cqh123' );
    方法3:还可以在全局级别使用GRANT USAGE语句(在*.*)来指定某个账户的密码而不影响账户当前的权限。
1
GRANT  USAGE  ON  *.*  TO  'chenqionghe' @ '%'  IDENTIFIED  BY  'cqh123' ;
    方法4:直接更改数据库的user表。
1
2
3
4
mysql>  INSERT  INTO  user  (Host, User , Password VALUES ( '%' , 'chenqionghe' , PASSWORD ( '333333' ));
mysql> FLUSH  PRIVILEGES ;
mysql>  UPDATE  user  SET  Password  PASSWORD ( '333333' WHERE  Host= '%'  AND  User = 'chenqionghe' ;
mysql> FLUSH  PRIVILEGES ;
注意:更改密码的时候一定要使用PASSWORD函数(mysqladmin和GRANT两种方式不用写,会自动加上)。
  • 删除账号

    要彻底删除账号,同样也有两种方法:DROP USER命令和修改权限表。
    DROP USER语法非常简单,具体如下:
1
DROP  USER  user  [,  user ] ...
   举一个单的例子,将cqh3@localhost用户删除
1
2
3
4
5
6
7
8
9
10
11
mysql> show grants  for  cqh3@localhost;
+ ------------------------------------------+
| Grants  for  cqh3@localhost                |
+ ------------------------------------------+
GRANT  USAGE  ON  *.*  TO  'cqh3' @ 'localhost'  |
+ ------------------------------------------+
1 row  in  set  (0.00 sec)
mysql>  drop  user  cqh3@localhost;
Query OK, 0  rows  affected (0.00 sec)
mysql> show grants  for  cqh3@localhost;
ERROR 1141 (42000): There  is  no  such  grant  defined  for  user  'cqh3'  on  host  'localhost'
    修改权限表方法只要把user用户中的用户记录删除即可,这里不再演示
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值