语法:
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(‘新的暗码’);
–自己下来试试
root用户修改指定用户的暗码
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’;