现在的位置: 首页 > 综合 > 正文

数据库Mysql添加用户并授权

2019年11月12日 综合 ⁄ 共 1446字 ⁄ 字号 评论关闭

  查询所有用户

  -- 方式1

  mysql> select host, user, password from mysql.user; -- 5.7版本之前的

  mysql> select host, user, authentication_string from mysql.user; -- 5.7版本之后的,包括5.7

  -- 方式2

  mysql> select distinct concat('User: ''',user,'''@''',host,''';') as query from mysql.user;

  查询用户权限:all表示所有权限,select表示只查权限,update表示只改权限,delete表示只删权限等。

  -- 方式1

  mysql> show grants for "user"@"host";

  mysql> show grants for "root"@"localhost";

  -- 方式2

  mysql> select * from mysql.user where user='root'\G;

  添加授权用户(新创建的用户,默认情况下是没有任何权限的):使用root用户登录数据库

  命令格式如下:

  mysql> create user "用户名"@"IP地址" identified by "密码";

  mysql> create user "haidon" identified by "123456"; -- 此时密码为123456,host值为%。

  mysql> create user "haidon"@"%" identified by "123456"; -- 此时密码为123456

  分配用户权限(给用户授权)

  命令格式如下:

  mysql> grant 权限类型 on 数据库名.表名 to '用户名'@'ip地址' identified by '用户密码' with grant option;

  常用的权限类型有以下几种:

  all privileges:所有权限。

  select:读取权限。

  create:创建权限。

  delete:删除权限。

  update:更新权限。

  drop:删除数据库、数据表权限。

  -- 允许访问所有数据库下的所有表

  mysql> grant all privileges on *.* to '用户名'@'指定ip' identified by '用户密码' ;

  -- 允许访问指定数据库下的所有表

  mysql> grant all privileges on test.* to '用户名'@'指定ip' identified by '用户密码' ;

  -- 允许访问指定数据库下的指定表

  mysql> grant all privileges on test.test to '用户名'@'指定ip' identified by '用户密码' ;

  mysql> grant all privileges on tornado.* to 'haidon'@'%' identified by '123456';

  收回用户权限(使用root用户操作)

  mysql> revoke select on tornado.* from "haidon"@"%";

  mysql> revoke all on tornado.* from "haidon"@"%";

  删除授权用户

  mysql> drop user "haidon"@"%"; -- 删除方法1

  mysql> delete from mysql.user where user="haidon"; -- 删除方法2

  刷新权限

  mysql> flush privileges;

抱歉!评论已关闭.