MySQL的用户和权限

MySQL权限管理详解

查看当前用户身份是:

mysql> select user();

想对用户和权限进行设置就要了解到系统库:
mysql库

在这里所有的设置也都是用表来存储设置的。

mysql> use mysql;  
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |  //单列权限:对 库.表 上的某些列拥有权限
| db                        |  //表示对库进行操作的权限表,用户可以操作库里的所有表
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |  //单表权限:对 库.表 拥有所有的权限
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |  //表示用户的权限,信息等内容的表
+---------------------------+
32 rows in set (0.00 sec)

创建用户:

mysql> create user user1@'localhost' identified by '123456';

//创建用户,后边的是表示本机,localhost  user1是用户名。后边的指定密码。

mysql> flush privileges;

//刷新权限列表。
这时,用户表  user中已经有了这条数据了。
mysql> select host,user from user;
+--------------+-------------+
| host         | user        |
+--------------+-------------+
| 172.16.12.1  | serveruser1 |
| 172.16.12.10 | clientuser1 |
| localhost    | mysql.sys   |
| localhost    | root        |
| localhost    | user1       |//就是这个.......
+--------------+-------------+

mysql> drop user user1@localhost;

//删除用户

mysql> select host,user from user;
+--------------+-------------+
| host         | user        |
+--------------+-------------+
| 172.16.12.1  | serveruser1 |
| 172.16.12.10 | clientuser1 |
| localhost    | mysql.sys   |
| localhost    | root        |
+--------------+-------------+
//已经没了。

授予权限:可以创建用户
grant 权限列表 on 库.表 to 用户名@‘主机名’ identified by ‘密码’;
     权限列表:
          all 所有权限
          权限,权限,…
     库.表:
          * . * 所有的库.所有的表
          库名 . * 单库.所有的表
          库名.表名 单库.单表
     用户名@‘主机名(IP)’

1、授予全部权限。
创建用户时授权

mysql> grant all on *.* to user1@'localhost' identified by '123456';

刷新权限列表

mysql> flush privileges;

查看权限

mysql> show grants for user1@'localhost' ;
+----------------------------------------------------+
| Grants for user1@localhost                         |
+----------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'localhost' |
+----------------------------------------------------+

可以查看一下具体的权限:

mysql> use mysql
mysql> select * from user where user="user1"\G;

发现没有授予其他用户的权限,别的都有。

如果想给和root完全一样的权限:

mysql> grant all on *.* to user@'localhost' identified by '12345' with grant option;
mysql> flush privileges;

2 . 单库权限

mysql> grant all on db2.* to user2@'localhost' identified by '12345';
mysql> flush privileges;

3 . 单表权限

mysql> grant all on db2.shop to user3@'localhost' identified by '12345';
mysql> flush privileges;

用这个用户登录:

mysql> use gsc1;
mysql> show tables;
+----------------+
| Tables_in_gsc1 |
+----------------+
| shop           |
+----------------+

mysql> create table tt1(id int);
ERROR 1142 (42000): CREATE command denied to user 'user3'@'localhost' for table 'tt1'
//能看到数据库和这1个表,但是不能创建别的表。

4 . 单列权限

mysql> grant select (id),update(name) on gsc1.tt1 to user4@'localhost' identified by '123456';
mysql> flush privileges;

用这个登录后

mysql> select * from tt1;
ERROR 1142 (42000): SELECT command denied to user 'user4'@'localhost' for table 'tt1'
mysql> select id from tt1;
+------+
| id   |
+------+
|  111 |
+------+
//全表不能查,但是能查id列,能修改name列。

远程登录时:

mysql -h IP -u -p
[root@centos7-bj ~]# mysql -h 172.16.0.51 -u user6 -p

收回权限:

mysql> revoke all on db2.* from user2@'localhost';

但是这个不如直接删除用户来的方便,哪怕是删除用户,在建用户都比查询常常的权限表快。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值