当前位置:   article > 正文

MySQL(十一) 用户管理

MySQL(十一) 用户管理

1.用户

1.1 用户信息

MySQL中的用户,都存储在系统数据库mysql的user表中

  1. mysql> select host,user,authentication_string from user;
  2. +-----------+---------------+-------------------------------------------+
  3. | host | user | authentication_string |
  4. +-----------+---------------+-------------------------------------------+
  5. | localhost | root | |
  6. | localhost | mysql.session | |
  7. | localhost | mysql.sys | |
  8. +-----------+---------------+-------------------------------------------+
  9. 3 rows in set (0.00 sec)

user是个表结构

字段解释:

  • host: 表示这个用户可以从哪个主机登陆,如果是localhost,表示只能从本机登陆
  • user: 用户名
  • authentication_string: 用户密码通过password函数加密后的
  • *_priv: 用户拥有的权限

1.2 创建用户

语法:
 

create user '用户名'@'登陆主机/ip' identified by '密码';

案例:

  1. mysql> create user 'dgz'@'localhost' identified by '123456';
  2. ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
  3. # 如果出现上面问题可以先刷新一下
  4. mysql> flush privileges;
  5. Query OK, 0 rows affected (0.01 sec)
  6. mysql> create user 'dgz'@'localhost' identified by '123456';
  7. Query OK, 0 rows affected (0.00 sec)

  1. mysql> select host,user,authentication_string from user;
  2. +-----------+---------------+-------------------------------------------+
  3. | host | user | authentication_string |
  4. +-----------+---------------+-------------------------------------------+
  5. | localhost | root | *03CDB52F58482A55A757B2FDFA03F1A14EDA5D99 |
  6. | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  7. | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  8. | localhost | dgz | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
  9. +-----------+---------------+-------------------------------------------+
  10. 4 rows in set (0.00 sec)

1.3 删除用户

语法:
 

drop user '用户名'@'主机名

案例:

  1. mysql> drop user 'dgz'@'localhost';
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> select host,user,authentication_string from user;
  4. +-----------+---------------+-------------------------------------------+
  5. | host | user | authentication_string |
  6. +-----------+---------------+-------------------------------------------+
  7. | localhost | root | *03CDB52F58482A55A757B2FDFA03F1A14EDA5D99 |
  8. | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  9. | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  10. +-----------+---------------+-------------------------------------------+
  11. 3 rows in set (0.00 sec)

1.4 修改用户密码

语法:
自己改自己密码

set password=password('新的密码');


root用户修改指定用户的密码

set password for '用户名'@'主机名'=password('新的密码');

直接修改mysql库中的user表

  1. mysql> update user set authentication_string=password('222222') where user='dgz';
  2. Query OK, 1 row affected, 1 warning (0.00 sec)
  3. Rows matched: 1 Changed: 1 Warnings: 1
  4. mysql> select host,user,authentication_string from user;
  5. +-----------+---------------+-------------------------------------------+
  6. | host | user | authentication_string |
  7. +-----------+---------------+-------------------------------------------+
  8. | localhost | root | *03CDB52F58482A55A757B2FDFA03F1A14EDA5D99 |
  9. | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  10. | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  11. | localhost | dgz | *A0C1808B1A47CECD5C161FEE647F5427F4EB6F98 |
  12. +-----------+---------------+-------------------------------------------+
  13. 4 rows in set (0.00 sec)

  1. mysql> set password for 'dgz'@'localhost'=password('111111');
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
  3. mysql> select host,user,authentication_string from user;
  4. +-----------+---------------+-------------------------------------------+
  5. | host | user | authentication_string |
  6. +-----------+---------------+-------------------------------------------+
  7. | localhost | root | *03CDB52F58482A55A757B2FDFA03F1A14EDA5D99 |
  8. | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  9. | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  10. | localhost | dgz | *FD571203974BA9AFE270FE62151AE967ECA5E0AA |
  11. +-----------+---------------+-------------------------------------------+
  12. 4 rows in set (0.00 sec)

2.数据库的权限

MySQL数据库提供的权限列表:

2.1 给用户权限

刚创建的用户没有任何权限。需要给用户授权。
语法:

grant 权限列表 on 库.对象名 to '用户名'@'登陆位置' [identified by '密码']

说明:

  • 权限列表,多个权限用逗号分开
    1. grant select on ...
    2. grant select, delete, create on ....
    3. grant all [privileges] on ... -- 表示赋予该用户在该对象上的所有权限
  • *.* : 代表本系统中的所有数据库的所有对象(表,视图,存储过程等)
  • 库.* : 表示某个数据库中的所有数据对象(表,视图,存储过程等)
  • identified by可选。 如果用户存在,赋予权限的同时修改密码,如果该用户不存在,就是创建用户
  1. [root@VM-8-2-centos ~]# mysql -udgz -p
  2. mysql> show databases;
  3. +--------------------+
  4. | Database |
  5. +--------------------+
  6. | information_schema |
  7. +--------------------+
  8. 1 row in set (0.00 sec)
  9. # dgz用户看不到其他数据库
  1. # root端开通权限
  2. mysql> grant all on test3.* to 'dgz'@'localhost';
  3. Query OK, 0 rows affected (0.00 sec)

也可以只开通单个权限,如果只想让用户查看,那么可以只开通select权限

mysql> grant select test3.* to 'dgz'@'localhost';

特定用户现有查看权限

  1. mysql> show grants for 'dgz'@'localhost';
  2. +--------------------------------------------------------+
  3. | Grants for dgz@localhost |
  4. +--------------------------------------------------------+
  5. | GRANT USAGE ON *.* TO 'dgz'@'localhost' |
  6. | GRANT ALL PRIVILEGES ON `test3`.* TO 'dgz'@'localhost' |
  7. +--------------------------------------------------------+
  8. 2 rows in set (0.00 sec)

注意:如果发现赋权限后,没有生效,执行如下指令:

flush privileges;

2.2 回收权限

语法:

revoke 权限列表 on 库.对象名 from '用户名'@'登陆位置'

示例:
 

  1. mysql> revoke all on test3.* from 'dgz'@'localhost';
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> show grants for 'dgz'@'localhost';
  4. +-----------------------------------------+
  5. | Grants for dgz@localhost |
  6. +-----------------------------------------+
  7. | GRANT USAGE ON *.* TO 'dgz'@'localhost' |
  8. +-----------------------------------------+
  9. 1 row in set (0.00 sec)

用户dgz查看不到test3库了

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. +--------------------+
  7. 1 row in set (0.00 sec)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/660023
推荐阅读
相关标签
  

闽ICP备14008679号