mysql 用户操作_MySQL基础操作----用户操作

一,查询数据库已有的用户

mysql>use mysql;

mysql> select user,host fromuser;+------------------+-----------+

| user | host |

+------------------+-----------+

| debian-sys-maint | localhost |

| mysql.session | localhost |

| mysql.sys | localhost |

| root | localhost |

+------------------+-----------+

4 rows in set (0.00 sec)

可以看到现在有四个user

二,创建用户

命令:mysql> create user 用户名@ 主机 identified by '密码';

@localhost 表示在本地用户

@‘192.168.1.1’ 表示在此服务器才可以登录

@% 表示可以在任何主机登录

create use@localhost ;  表示不需要密码登录

例如:

mysql> mysql> create use@localhost identified by '1234';

Query OK, 0 rows affected (0.00 sec)

现在再查用户

mysql> select user,host fromuser;+------------------+-----------+

| user | host |

+------------------+-----------+

| debian-sys-maint | localhost |

| mysql.session | localhost |

| mysql.sys | localhost |

| root | localhost |

| test | localhost |

+------------------+-----------+

5 rows in set (0.00 sec)

三,删除用户

有两种方式

1,drop user 用户名

例如: drop user test@localhost;

2,delete from user where user='用户名' and host ='host信息';

例如:delete from user where user='test' and host='localhost';

drop 仅仅不仅仅是将user表中的数据删除了,还删除了数据库或者其他权限表中的信息,delete只删除了user表中的信息,其他表的信息还是存在,delete删除之后还应该flush privileges。(我觉得drop user username比较好用一些)

四,给用户授权和查询权限

用户的权限分为:全局级权限、数据库级权限、表级别权限、列级别权限、子程序层级权限

查询权限有两种方式

1)show grants for usename@host;

例如:show grants for test@localhost;

2)select * from databasename.tablename where user='username'\G

1 全局权限(mysql.user)

grant privileges on  *.* to usename@host identified by 'password';

grant insert,select on *.* to test@localhost identified by '1234';

flush privileges;#授予用户test@localhost 密码是1234,insert和select权限,全局级(应该是所有的数据库和数据表都可以用),

查询权限

select * from mysql.uer where user='test'\G

2数据库级权限

grant privileges on databasename.* to username@host identified by 'password';

grant insert,select on mydatabase.* to test@localhost identified by '1234';

flush privileges;#授权完需要flush一下# 将mydatabase数据库下的所有表的insert和select权限授予test@localhost用户,用户密码是1234

查询权限

selec * from mysql.db where user='test'\G  or  show grants for test@localhost;

3 数据表级权限

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

grant insert,update on mydatabase.mytable to test@localhost identified by '1234';#(我试过不写host之后的东西也可以)

flush privileges;#将mydatabase下mytable这个表的insert、update权力授予test@localhost

查询权限

show grants for test@localhost ; or select * from mysql.tables_priv \G

4 表中列级权限

grant privileges (列名) on databasename.tablename to username@host identified by 'password';

grant insert,update (id,name)on mydatabase.mytable to test@localhost identified by '1234';

flush privileges;#把数据库mydatabase下的数据表mytable下的id和name两列的insert和update功能授予test@localhost。

查询列权限

select * form mysql.columns_priv; or show grants for test@localhost;

5 存储过程、函数级的权限  (这个木有用过,暂时不晓得干嘛用)

grant execute on procedure mydatabase.mytableto 'test'@'localhost';

grant execute on functionmydatabase.mytable to'test'@'localhost';

查询权限

select * from mysql.procs_priv where User='test';

6 可以授予的权限

一般常用的权限包括但不仅限于:select,insert,update,delete,create等

授予全部权限用 all privileges,但是privileges可以省略掉,只用all就可以。

7 被授权用户不能授权其他用户

如果想让该用户对其他用户授予相同的授权可以用

GRANT privileges ON databasename.tablename TO 'username'@'host'WITH GRANT OPTION;

五,撤销授权

revoke privilege ON databasename.tablename FROM 'username'@'host';

怎样授权就需要怎样撤销,比如

grant insert on *.* to test@localhost identified by '1234';删除授权时需要 revoke insert on *,* test@localhost;

假如 grant select on test.tablename to test@localhost identified by '1234'; 如果要用 revoke insert on *,* test@localhost;撤销命令,则撤销不掉test.talbename下的select授权。

六,创建授权一起做

grant 可以给存在的用户授权,但是如果用户不存在,也可以创建用户并授权,例如;

grant insert,update on *.* to test1@localhost identified by '123';#创建test1@localhost 并授予全局的insert权限

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值