美文网首页
Mysql添加 为用户授权 删除用户

Mysql添加 为用户授权 删除用户

作者: 衣忌破 | 来源:发表于2019-04-27 20:27 被阅读0次

添加用户

  • 方法一

insert into mysql.user(Host,User,Password) values("localhost","user0",password("1234"));

此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录。如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录。也可以指定某台机器可以远程登录;以下其它方法中亦如此。

ps: mysql5.7后mysql.user表中没有了Password字段,因此该方法不使用该版本后的mysql;

  • 方法二
    利用CREATE USER命令来创建用户

create user user0@localhost identified by '888888';

  • 方法三
    利用grant授权命令来创建用户

grant select,insert,update,delete,create,drop on . to user0@localhost identified by '888888';

grant命令主要是用于对用户进行授权操作,但当用户不存在时,会自己创建该用户,所以也常被用于创建用户使用。上面的命令是创建一个用户名为“user0”、密码为"888888"且对所有数据库表有增、删、改、清空权限的用户。

为用户授权

  1. 授权格式

grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";

  1. 登录MYSQL(有ROOT权限),这里以ROOT身份登录:

mysql -u root -p 密码

  1. 创建一个数据库(testDB):

create database testDB;

4.授权user0用户拥有testDB数据库的所有权限(某个数据库的所有权限):

grant all privileges on testDB.* to user0@localhost identified by '1234';
flush privileges;//刷新系统权限表

5.指定部分权限给用户

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

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

grant select,delete,update,create,drop on *.* to user0@"%" identified by "1234";

user0用户对所有数据库都有select,delete,update,create,drop 权限。

@"%" 表示对所有非本地主机授权,不包括localhost。(localhost地址设为127.0.0.1,如果设为真实的本地地址,不知道是否可以,没有验证。)

删除用户

  1. 登录root

mysql -u root -p 密码

  1. 从mysql.user表删除指定用户

Delete FROM mysql.user Where User='user0' and Host='localhost';
flush privileges;
drop database testDB; //删除用户的数据库

  1. 删除账户及权限:

drop user 用户名@'%';
drop user 用户名@ localhost;

修改指定用户密码

  1. 登录root

mysql -u root -p 密码

  1. 修改密码

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

  1. 刷新权限

flush privileges;

相关文章

网友评论

      本文标题:Mysql添加 为用户授权 删除用户

      本文链接:https://www.haomeiwen.com/subject/lhqlnqtx.html