IT评测·应用市场-qidao123.com

标题: 15 用户管理 [打印本页]

作者: 络腮胡菲菲    时间: 2024-9-7 06:50
标题: 15 用户管理
如果我们只能利用root用户,如许存在安全隐患。这时,就需要利用mysql的用户管理

张三只能操纵mytest这个库,李四只能操纵msg这个库,如果给他们root账户,就可以操纵全部库,风险太大
用户

用户信息

用户都存储在体系数据库mysql的user表中
   mysql> use mysql;
Database changed
mysql> select host,user,authentication_string from user;

–可以通过desc user初步查看一下表布局
  字段表明:
host:表示这个用户可以从哪个主机登录,如果是localhost,表示只能从本机登录
user:用户名
authentication_string:用户暗码通过password函数加密过后的
*_priv:用户拥有的权限
创建用户

语法:
   create user ‘用户名’@‘登录主机/ip’ identified by ‘暗码’;
  案例:
   mysql> create user ‘tmp’@‘localhost’ identified by ‘12345678’;
Query OK, 0 rows affected (0.06 sec)

mysql> select user,host,authentication_string from user;


– 此时便可以利用新账号新暗码进行登陆

–备注:可能现实在设置暗码的时间,因为mysql自己的认证品级比力高,一些简单的暗码无法设置,会爆出如下报错:-- ERROR 1819 (HY000): Your password does not satisfy the current policy requirements-- 解决方案:https://blog.csdn.net/zhanaolu4821/article/details/93622812–查看暗码设置相关要求:SHOW VARIABLES LIKE ‘validate_password%’;

关于新增用户这里,需要大家注意,不要容易添加一个可以从任意地方登陆的user。
  删除用户

语法:
   drop user ‘用户名’@‘主机名’
  示例:
   select user,host,authentication_string from user;

mysql> drop user whb; --尝试删除
ERROR 1396 (HY000): Operation DROP USER failed for ‘temp’@‘%’ – <= 直接给个用户名,不能删除,它
默认是%,表示全部地方可以登陆的用户
mysql> drop user ‘temp’@‘localhost’; --删除用户
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,authentication_string from user;

  修改用户暗码

语法:

   set password=password(‘新的暗码’);
  –自己下来试试
  
   set password for ‘用户名’@‘主机名’=password(‘新的暗码’);
    mysql> select host,user, authentication_string from user;


mysql> set password for ‘whb’@‘localhost’=password(‘87654321’);
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select host,user, authentication_string from user;

  数据库的权限

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

给用户权限

刚创建的用户没有任何权限,需要给用户授权
   grant 权限列表 on 库.对象名 to ‘用户名’@‘登录位置’ [indentified by ‘暗码’]
  分析:
权限列表,多个权限用逗号分开
   grant select on …
grant select, delete, create on …
grant all [privileges] on … – 表示赋予该用户在该对象上的全部权限
  *.*:代表本体系中的全部数据库的全部对象(表,视图,存储过程等)
库.*:表示某个数据库中的全部数据对象(表,视图,存储过程等)
identified by可选,如果用户存在,赋予权限的同时修改暗码,如果不存在就创建用户
   –利用root账号 --终端A
mysql> show databases;


mysql> use test;
Database changed
mysql> show tables;


–给用户tmp赋予test数据库下全部文件的select权限
mysql> grant select on test.* to ‘tmp’@‘localhost’;
Query OK, 0 rows affected (0.01 sec)
–利用tmp账号
–终端B
mysql> show databases;


–暂停等root用户给tmp赋完权之后,在查看
mysql> show databases;


mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;


mysql> select * from account;


–没有删除权限
mysql> delete from account;
ERROR 1142 (42000): DELETE command denied to user ‘whb’@‘localhost’ for table ‘account’

备注:特定用户现有查看权限
mysql> show grants for ‘tmp’@‘%’;

mysql> show grants for ‘root’@‘%’;

  注意:如果发现赋权限后,没有生效,实验如下指令:
   flush privileges;
  接纳权限

语法:
   revoke 权限列表 on 库.对象名 from ‘用户名’@‘登录位置’;
  示例:
   – 接纳tmp对test数据库的全部权限 --root身份,终端A
mysql> revoke all on test.* from ‘tmp’@‘localhost’;

Query OK, 0 rows affected (0.00 sec) --tmp身份,终端B
mysql> show databases;


mysql> show databases;


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4