MySql 5.7中添加用户,新建数据库,用户授权,删除用户,修改密码

1、新建用户 

创建test用户,密码是1234

  
mysql -u root -p
#输入密码
#本地登录 
mysql>CREATE USER 'test'@'localhost'  IDENTIFIED BY '1234'; 

 

 

远程登录 

mysql>CREATE USER 'test'@'%'  IDENTIFIED BY '1234'; 
mysql>quit ;

  

#测试是否创建成功

mysql -u test -p 

  

2、为用户授权

a.授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by '密码'; 

MySQL grant 权限,分别可以作用在多个层次上。

 

#grant 作用在整个 MySQL 服务器上: 
mysql>grant select on *.* to root@localhost; 
# 可以查询 MySQL 中所有数据库中的表。 
mysql>grant all on *.* to root@localhost; 
# 可以管理 MySQL 中的所有数据库 
#grant 作用在单个数据库上: 
mysql>grant select on testdb.* to root@localhost; 
# 可以查询 testdb 中的表。
#grant 作用在单个数据表上: 
mysql>grant select, insert, update, delete on testdb.orders to test@localhost;

  

 

查看权限

#查看当前用户(自己)权限: 
mysql>show grants; 
#查看其他 MySQL 用户权限: 
mysql>show grants for 'test'@localhost;

  

撤销已经赋予给 MySQL 用户权限的权限.

revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可: 

 

mysql>grant all on *.* to test@localhost;
mysql>revoke all on *.* from test@localhost;

 

b.登录MYSQL,这里以ROOT身份登录:

mysql -u root -p

  

为用户创建一个数据库(testDB)

mysql>create database testDB;  
mysql>create database testDB default charset utf8 collate utf8_general_ci;

   

授权test用户拥有testDB数据库的所有权限:

mysql>grant all privileges on testDB.* to 'test'@localhost identified by "123456";  
mysql>flush privileges; #刷新系统权限表

  

 

指定部分权限给用户:

mysql>grant select,update on testDB.* to 'test'@localhost identified by "123456";  
mysql>flush privileges; #刷新系统权限表

  

 

授权test用户拥有所有数据库的某些权限: 

mysql>grant select,delete,update,create,drop on . to test@"%" identified by by "123456";  

”%” 表示对所有非本地主机授权,不包括localhost

 

删除用户、数据库

mysql>mysql -u root -p  Delete FROM mysql.user Where User="test" and Host="localhost";  
mysql>flush privileges;  
mysql>drop database testDB;

   

删除账户及权限:

mysql>drop user test@'%';  
mysql>drop user test@localhost;

  

 

修改指定用户密码

mysql>mysql -u root -p  update mysql.user set authentication_string=password("123456") where User="guest" and Host="localhost";  
mysql>flush privileges;

  

 

 

 

原文地址:https://www.cnblogs.com/saryli/p/11887265.html