目录

MySQL5.7.17

1.1、普通用户的管理

(1)使用create user语句创建普通用户:

create user的基本语法格式为:

create user user[identified by 'password';]

说明:

a、user 的格式为:’user_name'@'host name'。

b、localhost表示本地主机;

c、identified by 指定用户密码,注意用户名和密码大小写;

d、可以同时创建多个user,中间使用 隔开

例:

mysql>create user 
     >'user01'@'localhost' identified by 'xxxx',
     >'user02'@'localhost' identified by 'xxxx';

创建成功后,可以通过 select user from user; 来进行查看用户是否创建成功

1.2、使用grant语句创建用户

语法:

grant priv_type on database.table to user [identified by 'password'] [with grant option]

例:

grant select,delete on *.* to 'user'@'localhost' identified by 'xxxxx' with grant option;

1.3、删除普通用户

(1)使用drop user删除用户

语法:drop user user_name;

drop user kylin@localhost;

        drop user语句删除用户的时候不会去关闭这个用户正在使用的会话,但是当这个用户关闭了会话再次进行登录的时候就会失败。drop user语句执行者需要具有MySQL的全局create user权限和delete权限。

(2)使用delete语句删除用户。delete基本语法格式为:

       delete from user where host = 'hostname' and user = 'username';

       如果删除的用户在数据库中创建了数据表、索引或数据库对象,这些信息继续存在。

1.4、修改用户名称

        可以使用 rename user语句来实现。如果旧账户不存再或者新账户已存在,则会出现错误。

基本语法:

rename user old_user to new_user;

支持同时修改多个用户的名称:

mysql>rename user 
     >'demo01'@'localhost' to 'test01'@'localhost',
     >'demo02'@'localhost' to 'test02'@'localhost';  

1.5、修改用户密码

要修改某个用户的登录密码,可以使用mysqladmin、update或set password语句。

(1)root用户修改密码

① 使用mysqladmin命令。mysqladmin命令的基本语法格式为:

mysqladmin -u user -h localhost -p pasword 'newpassword';

② 使用update修改用户 root 的密码

update mysql.user set password = password('newPassword') where user = 'root' and host = 'localhost';

(2)使用set语句修改用户密码,其语法格式为:

set password [for user] = password('newPassword');

set password for 'test01'@localhost = password('newPassword');

注:在mysql8.0以上版本,

 update mysql.user set password='newpassword' where user='root';

 

 update mysql.user set password=PASSWORD('newpassword') where User='root'; 这两条命令已经不起作用了。

MySQL8.0版本

        在进行修改用户密码的使用,由于MySQL8.0版本在user表中取消了password字段authentication_string:字段表示用户密码,而authentication_string字段下只能是mysql加密后的41位字符串密码。

alter user 'username'@'hostname' identified by 'password'; --- 修改用户username的密码为password

flush privileges;   -- 刷新权限


Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐