用户管理

  1. 显示用户账号及信息

    • MySQL 用户账号和信息存储在名为 mysql 的 MySQL 数据库中。 mysql 数据库中有一张名为 user 的表, 它包含所有用户账号。 user 表中有名为 user 的列,它存储的是用户登录名。

       mysql> use mysql;
      Database changed
      mysql>  select user,host from user;
      +------------------+-----------+
      | user             | host      |
      +------------------+-----------+
      | mysql.infoschema | localhost |
      | mysql.session    | localhost |
      | mysql.sys        | localhost |
      | root             | localhost |
      +------------------+-----------+
      4 rows in set (0.005 sec)
      
      mysql>
                                           
  2. 创建重命名及删除用户账号

    • 创建名为 jj 的新用户账号:

      
         create user jj identified by 'happy';
                                      
      
        mysql>  create user jj identified by 'happy';
        Query OK, 0 rows affected (3.228 sec)
      
        mysql>
                                                     
      
        mysql> select user,host from user;
      +------------------+-----------+
      | user             | host      |
      +------------------+-----------+
      | jj               | %         |
      | mysql.infoschema | localhost |
      | mysql.session    | localhost |
      | mysql.sys        | localhost |
      | root             | localhost |
      +------------------+-----------+
      5 rows in set (0.006 sec)
      
      mysql>
           
      • host 为 % 允许任何主机访问; host 为 localhost 允许本地主机访问。

    • 把用户名为 jj 的重新命名为 jjuni:

      
          rename user jj to jjuni;
                                      
      
      mysql> rename user jj to jjuni;
      Query OK, 0 rows affected (0.736 sec)
      
      mysql>
                                                     
      mysql> select user,host from user;
      +------------------+-----------+
      | user             | host      |
      +------------------+-----------+
      | jjuni            | %         |
      | mysql.infoschema | localhost |
      | mysql.session    | localhost |
      | mysql.sys        | localhost |
      | root             | localhost |
      +------------------+-----------+
      5 rows in set (0.006 sec)
      
      mysql>
      
           
    • 删除用户账号

      
           drop user jjuni;