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 "密码"
;
- 授权用户拥有testDB数据库的某些权限:
mysql>grant select,update on testDB.* to username@localhost identified by 'password';
mysql>flush privileges; //刷新系统权限表
- 授权username用户拥有testDB数据库的所有权限:
mysql>grant all privileges on testDB.* to username@localhost identified by 'password';
mysql>flush privileges; //刷新系统权限表
- 授权username用户拥有所有数据库的某些权限:
mysql>grant select,update on *.* to username@localhost identified by 'password';
mysql>flush privileges; //刷新系统权限表
- 授权username用户拥有所有数据库的所有权限
mysql>grant all privileges on *.* to username@localhost identified by 'password';
mysql>flush privileges; //刷新系统权限表
@"%"
表示对所有非本地主机授权,不包括localhost。
mysql>grant all privileges on *.* to username@"%" identified by 'password';
mysql>flush privileges; //刷新系统权限表
- 查看数据库中具体某个用户的权限
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密码:
- 如果当前root用户authentication_string字段下有内容,先将其设置为空,否则直接进行二步骤。
update user set authentication_string='' where user='root'
- 使用ALTER修改root用户密码:
ALTER user 'root'@'localhost' IDENTIFIED BY 'password'
此处有两点需要注意:
1、不需要flush privileges来刷新权限。
2、密码要包含大写字母,小写字母,数字,特殊符号。
修改成功; 重新使用用户名密码登录即可;