数据库用户密码和权限管理

windows启动mysql服务

E:\>cd E:\mysql-8.0.15-winx64\bin
E:\mysql-8.0.15-winx64\bin>net start mysql
E:\mysql-8.0.15-winx64\bin>net stop mysql
E:\mysql-8.0.15-winx64\bin>mysql -u root -p

创建用户

mysql> CREATE USER username IDENTIFIED BY 'password';  #identified by 会将纯文本密码加密作为散列值存储
mysql> CREATE USER username@localhost IDENTIFIED BY 'password';   #限制本地地址登录
  • 查看用户列表
mysql> select host,user from mysql.user; 
mysql> desc mysql.user;      #查看user表结构 需要具体的项可结合表结构来查询
  • 为用户授权,授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";
  1. 授权用户拥有testDB数据库的某些权限:
mysql>grant select,update on testDB.* to username@localhost identified by 'password'; 
mysql>flush privileges; //刷新系统权限表 
  1. 授权username用户拥有testDB数据库的所有权限:
mysql>grant all privileges on testDB.* to username@localhost identified by 'password';
mysql>flush privileges; //刷新系统权限表
  1. 授权username用户拥有所有数据库的某些权限:
mysql>grant select,update on *.* to username@localhost identified by 'password';
mysql>flush privileges; //刷新系统权限表
  1. 授权username用户拥有所有数据库的所有权限
mysql>grant all privileges on *.* to username@localhost identified by 'password';
mysql>flush privileges; //刷新系统权限表
  1. @"%" 表示对所有非本地主机授权,不包括localhost。
mysql>grant all privileges on *.* to username@"%" identified by 'password';
mysql>flush privileges; //刷新系统权限表
  1. 查看数据库中具体某个用户的权限
mysql> show grants for 'cactiuser'@'%';  

删除用户

  • 必须先root用户登录
mysql -u root -p
mysql>drop database testDB; //删除用户的数据库
drop user 用户名;
drop user 用户名@'%';  //删除账户及权限
drop user 用户名@localhost;

修改指定用户密码

  • 必须先root用户登录
mysql -u root -p
mysql>ALTER user 'username'@'localhost' IDENTIFIED BY '123456';

修改root密码:

  1. 如果当前root用户authentication_string字段下有内容,先将其设置为空,否则直接进行二步骤。
update user set authentication_string='' where user='root'
  1. 使用ALTER修改root用户密码:
ALTER user 'root'@'localhost' IDENTIFIED BY 'password'

此处有两点需要注意:
1、不需要flush privileges来刷新权限。
2、密码要包含大写字母,小写字母,数字,特殊符号。
修改成功; 重新使用用户名密码登录即可;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值