一、创建用户:
create user '用户名'@'允许登录的主机名' identified by '密码';
eg:
create user 'yukawa'@'localhost' identified by '539812';
# 'localhost' 表示设置 host 为 localhost,表明该用户只允许在本机登录,若把它设置为 '%' 表示允许该
# 用户可以在任意主机登录
二、删除用户:
drop user '用户名'@'主机名';
eg :
drop user 'yukawa'@'localhost';
三、查看用户:
1、查看当前用户:
(1)select current_user();
(2)select user();
2、查看所有用户:
select user,host from mysql.user;
四、修改:
1、改名:
# rename 不止可以修改用户名,也可以修改 host
rename user 'yukawa'@'localhost' to 'yakawa'@'%';
2、改密码:
(1)mysql 5.7 以前必须用这种方法:
SET PASSWORD FOR 'yakawa'@'localhost' = PASSWORD('123456');
(2)
alter user 'yakawa'@'localhost' identified by '123456';
(3)
# msyql 5.7 以前
UPDATE mysql.user SET authentication_string=PASSWORD('newpassword') WHERE User='yakawa';
# mysql 5.7 以后
update user set password=password('新密码') where user='yakawa';
(4)若忘记了密码:
a、需要打开两个 cmd 命令行;
b、其中一个输入:
mysqld --console --skip-grant-tables --shared-memory
c、另外一个直接不输入密码进入后,修改密码;
注意:
flush privileges; 来更新数据
五、权限:
1、查看用户权限:
show grants for 'yakawa'@'localhost';
2、添加权限:
GRANT 权限 ON 数据库.* TO 'yakawa'@'%';
eg:
# 赋予该用户仅仅有连接到服务器的权限
GRANT USAGE ON *.* TO 'yakawa'@'%'
# 赋予该用户查询 security 数据库的所有表的权限
grant select on security.* to 'yakawa'@'%'
# 赋予该用户对于 security 数据库的所有表的全部权限
grant all on security.* to 'yakawa'@'%'
3、撤销权限:
revoke all on security.* from 'yakawa'@'%';