您的位置:首页 > 数据库 > MySQL

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

2018-10-25 09:36 507 查看

mysql 5.7版本还是和之前的版本有些不一样,这里不做说明。仅仅记录一些简单的sql操作,比如修改密码操作和原来其他版本操作方法不一样,这里也简单整理了下做资料库。
1、新建用户
创建test用户,密码是1234。

 
  1. mysql -u root -p
  2. create user  test@'localhost' identified  by '1234' ; #本地登录
  3. create user  test@'%'  identified  by '1234' ; #远程登录
  4. exit

mysql -utest -p #测试是否创建成功
2、为用户授权

a.授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by ‘密码’;
b.登录MYSQL,这里以ROOT身份登录
c.为用户创建一个数据库(testDB):

 
  1. create database testDB;
  2. create database testDB default charset utf8 collate utf8_general_ci;

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

 
  1. grant all privileges on testDB.* to test@'localhost' identified by  '1234';
  2. flush privileges; #刷新系统权限表

e.指定部分权限给用户:

 
  1. grant select,update on testDB.* to  test@'localhost' identified by  '1234';
  2. flush privileges; #刷新系统权限表

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

 
  1. grant select,delete,update,create,drop on . to test@'%'  identified by '1234';  #”%” 表示对所有非本地主机授权,不包括localhost

3、删除用户

 
  1. mysql -u root -p
  2. delete  from  mysql.user where  'test' and  host='localhost';
  3. flush privileges;
  4. drop database testDB;

删除账户及权限:

 
  1. drop user 用户名@’%’;
  2. drop user 用户名@'localhost';

4、修改指定用户密码

 
  1. mysql -u root -p
  2. update mysql.user set authentication_string=password('新密码') where user='test' and  host='localhost';
  3. flush privileges;
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐