MySQL5.7 经常使用用户操做

MySQL5.7 经常使用用户操做

[toc]mysql

以前的一篇博文讲述了安装MySQL,可是咱们在安装后MySQL以后的操做中通常不使用root用户来进行相应的操做,因此要新建用户,并赋予相应的权限后,才能更好的使用和管理数据库。
mysql版本:5.7sql

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;ide

2. 受权

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

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

## 赋予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();

9. 重置root密码

  在my.cnf的[mysqld]字段加入skip-grant-tables,而后重启mysql服务,这时的mysql不须要密码便可登陆数据库。而后进入mysql:code

use mysql;
update user set password=password('新密码') WHERE User='root';
flush privileges;

  运行以后最后去掉my.cnf中的skip-grant-tables,重启mysqld便可。ip

相关文章
相关标签/搜索