MySQL5.7 常用用户操作

目录

MySQL5.7 常用用户操作

之前的一篇博文讲述了安装MySQL,但是我们在安装后MySQL之后的操作中一般不使用root用户来进行相应的操作,所以要新建用户,并赋予相应的权限后,才能更好的使用和管理数据库。
mysql版本:5.7

1. 新建用户

create user 'username'@'host' identified by 'password';

例子:

create user 'user'@'localhost' identified by '123456';
create user 'user'@'localhost' identified by '';

username : 你将创建的用户名
host : 指定该用户在哪个主机上可以登陆,此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录,如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录;也可以指定某台机器可以远程登录;
password : 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器。

MySQL5.7 mysql.user表password字段改为 authentication_string;

2. 授权

grant privileges on databasename.tablename to 'username'@'host';

privileges : 用户的操作权限,如SELECT , INSERT , UPDATE 等。如果要授予所的权限则使用ALL。
databasename : 数据库名
tablename : 表名
如果要授予该用户对所有数据库和表的相应操作权限则可用*表示, 如*.*.

## 赋予user对数据库store下的所有表的查看和增添权利
grant select, insert on store.* to 'user'@'localhost';

3. 创建用户时授权

grant all privileges on store.* to user@'%' identified by '123456';
flush privileges;

4. 设置与更改用户密码(root)

update mysql.user set authentication_string=password("新密码") where User="test" and Host="localhost"; 
flush privileges;

5. 撤销用户权限

## 具体信息可以用命令show grants for 'username'@'host'; 查看.
revoke privilege on databasename.tablename from 'username'@'host';

6. 删除用户

drop user 'username'@'host';

7. 查看用户的授权

show grants for user@localhost;

8. 显示当前用户信息

select user();

猜你喜欢

转载自www.cnblogs.com/huizhipeng/p/10127261.html