查看当前用户身份是:
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';
但是这个不如直接删除用户来的方便,哪怕是删除用户,在建用户都比查询常常的权限表快。
MySQL权限管理详解
2万+

被折叠的 条评论
为什么被折叠?



