MySQL用户权限控制一例

时间:2022-10-01 12:02:20
 
Preface
 
    I supposed we are encountering a situation that there's an anonymous user has connected in our MySQL database with an account which has large privileges.The user is doing some query operations with bad performance.Which may subsequently lead to a high load of our database server.How to solve this issue efficiently and immediately?There's a little trick we can use below.
 
Example
 
Create a test account.
 (root@localhost mysql3306.sock)[(none)]>create user aaron8219@'192.168.1.%' identified by 'zlm';
Query OK, rows affected (0.00 sec) (root@localhost mysql3306.sock)[(none)]>select user,host from mysql.user;
+---------------+-------------+
| user | host |
+---------------+-------------+
| rpl_mgr | % |
| aaron8219 | 192.168..% |
| repl | 192.168..% |
| replica | 192.168..% |
| zlm | 192.168..% |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-------------+
rows in set (0.00 sec) (root@localhost mysql3306.sock)[(none)]>grant all privileges on *.* to aaron8219@'192.168.1.%'; //Grant the supreme privileges to the user.
Query OK, rows affected (0.00 sec) (root@localhost mysql3306.sock)[(none)]>show grants for aaron8219@'192.168.1.%';
+----------------------------------------------------------+
| Grants for aaron8219@192.168..% |
+----------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'aaron8219'@'192.168.1.%' |
+----------------------------------------------------------+
row in set (0.00 sec)
Connect to database with the new account.
 [root@zlm2 :: ~]
#mysql -uaaron8219 -pzlm -h192.168.1.
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. (aaron8219@192.168.1.101 )[(none)]>show databases; //The user "aaron8219" can see all the databases in the current MySQL instance.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| sysbench |
| zlm |
+--------------------+
rows in set (0.01 sec) (aaron8219@192.168.1.101 )[(none)]>create database aaron8219;
Query OK, row affected (0.00 sec) (aaron8219@192.168.1.101 )[(none)]>use aaron8219;
Database changed
(aaron8219@192.168.1.101 )[aaron8219]>create table t1(
-> id int,
-> name char()
-> ) engine=innodb;
Query OK, rows affected (0.02 sec)
Create another precise account which name is equal to the one above and with an intact ip address.
 
 (root@localhost mysql3306.sock)[(none)]>create user aaron8219@'192.168.1.101' identified by 'zlm';
Query OK, rows affected (0.00 sec) (root@localhost mysql3306.sock)[(none)]>select user,host from mysql.user;
+---------------+---------------+
| user | host |
+---------------+---------------+
| rpl_mgr | % |
| aaron8219 | 192.168..% |
| repl | 192.168..% |
| replica | 192.168..% |
| zlm | 192.168..% |
| aaron8219 | 192.168.1.101 |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+---------------+
rows in set (0.00 sec) (root@localhost mysql3306.sock)[(none)]>grant all privileges on aaron8219.* to aaron8219@'192.168.1.101'; //Grant the privileges only on "aaron8219" database.
Query OK, rows affected (0.00 sec) (root@localhost mysql3306.sock)[(none)]>show grants for aaron8219@'192.168.1.101';
+----------------------------------------------------------------------+
| Grants for aaron8219@192.168.1.101 |
+----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'aaron8219'@'192.168.1.101' |
| GRANT ALL PRIVILEGES ON `aaron8219`.* TO 'aaron8219'@'192.168.1.101' |
+----------------------------------------------------------------------+
rows in set (0.00 sec)
Connect to database with the account again.
 [root@zlm2 :: ~]
#mysql -uaaron8219 -pzlm -h192.168.1.
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. (aaron8219@192.168.1.101 )[(none)]>show databases; //Only the "aaron8219" database can be list.
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaron8219 |
+--------------------+
rows in set (0.00 sec) (aaron8219@192.168.1.101 )[(none)]>show grants for aaron8219@'192.168.1.101';
+----------------------------------------------------------------------+
| Grants for aaron8219@192.168.1.101 |
+----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'aaron8219'@'192.168.1.101' |
| GRANT ALL PRIVILEGES ON `aaron8219`.* TO 'aaron8219'@'192.168.1.101' |
+----------------------------------------------------------------------+
rows in set (0.00 sec) (aaron8219@192.168.1.101 )[(none)]>use aaron8219;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
(aaron8219@192.168.1.101 )[aaron8219]>show tables;
+---------------------+
| Tables_in_aaron8219 |
+---------------------+
| t1 |
+---------------------+
row in set (0.00 sec) (aaron8219@192.168.1.101 )[aaron8219]>insert into t1 values(,'abc');
Query OK, row affected (0.00 sec) (aaron8219@192.168.1.101 )[aaron8219]>select * from t1;
+------+------+
| id | name |
+------+------+
| | abc |
+------+------+
row in set (0.00 sec) //Eventrually,the privileges of account aaron8219@'192.168.1.%' has been restricted merely on database "aaron8219".
//Further more,we can revoke all the privileges on it either.
Revoke the all privileges of the account.
 (root@localhost mysql3306.sock)[(none)]>revoke all privileges on aaron8219.* from aaron8219@'192.168.1.101';
Query OK, rows affected (0.00 sec) (root@localhost mysql3306.sock)[(none)]>show grants for aaron8219@'192.168.1.101';
+---------------------------------------------------+
| Grants for aaron8219@192.168.1.101 |
+---------------------------------------------------+
| GRANT USAGE ON *.* TO 'aaron8219'@'192.168.1.101' |
+---------------------------------------------------+
row in set (0.00 sec)
Connect to database with the account third times.
 [root@zlm2 :: ~]
#mysql -uaaron8219 -pzlm -h192.168.1.
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. (aaron8219@192.168.1.101 )[(none)]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
row in set (0.00 sec) (aaron8219@192.168.1.101 )[(none)]>create database test;
ERROR (): Access denied for user 'aaron8219'@'192.168.1.101' to database 'test' //This time,the account of aaron8219 login with ip "192.168.1.101" can do nothing in the target instance.
 

MySQL用户权限控制一例的更多相关文章

  1. Mysql用户权限控制(5.7以上版本)

    1.1. 最简单的MySql权限   最简单也是最高效的,如果解决新手们删库跑路的问题其实也是很简单的,对于正式库只给一个增删改查的权限,或者只给一个查询权限(是不是就解决了删库的可能性?) 使用Ro ...

  2. mysql用户权限

    mysql> show grants for root@'localhost';+-------------------------------------------------------- ...

  3. linux下通过acl配置灵活目录文件权限(可用于ftp,web服务器的用户权限控制)

    linux下通过acl配置灵活目录文件权限(可用于ftp,web服务器的用户权限控制) 发表于2012//07由feng linux 本身的ugo rwx的权限,对于精确的权限控制很是力不从心的,ac ...

  4. Vue-Access-Control:前端用户权限控制解决方案

    原文地址:http://refined-x.com/2017/11/28/Vue2.0用户权限控制解决方案/ Vue-Access-Control是一套基于Vue/Vue-Router/axios 实 ...

  5. asp.net core根据用户权限控制页面元素的显示

    asp.net core根据用户权限控制页面元素的显示 Intro 在 web 应用中我们经常需要根据用户的不同允许用户访问不同的资源,显示不同的内容,之前做了一个 AccessControlHelp ...

  6. mysql用户权限操作

    mysql用户权限操作1.创建用户mysql -urootcreate database zabbix default charset utf8;grant all on zabbix.* to za ...

  7. Linux上Mysql数据库 用户权限控制

    Linux安装mysql 点我直达 Mysql限制root用户ip地址登录 修改mysql库里边的user表: update mysql.user set host='localhost' where ...

  8. 烂泥:nginx、php-fpm、mysql用户权限解析

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://ilanni.blog.51cto.com/526870/1561097 本文首发 ...

  9. MYSQL用户权限管理学习笔记

    MYSQL 用户管理 1.权限表 MYSQL是一个多用户的数据库,MYSQL的用户可以分为两大类: (1)       超级管理员用户(root),拥有全部权限 (2)       普通用户,由roo ...

随机推荐

  1. js event 事件冒泡和事件捕获详细介绍

    . 参考: http://www.jb51.net/article/42492.htm 图: 假设一个元素div,它有一个下级元素p.<div> <p>元素</p> ...

  2. Android&plus;PHP服务器&plus;MySQL实现安卓端的登录

    时隔已久的一个任务,今天终于可以画上一个句号了.心情是万分的激动,虽然这份小成就来的有点迟但还是按捺不住心情的澎湃.下面我就先上几张图片来展示一下我的成绩 android源代码: 首先最重要的一件事是 ...

  3. Eclipse 的 Debug 介绍与技巧【转载】

    没有任何程序员能够一气呵成的写出没有任何 Bug 的代码,所以很多程序员有相当一部分时间是花费在 Debug 上的,程序调试是每个程序员必须面对的工作.如何使用 Eclipse 进行有效的.尤其是高效 ...

  4. AngularJS的一点学习笔记

    ng-options="item.action for item in todos" ng-options表达式的基本形式, 形如 "<标签> for &lt ...

  5. 线段树 Codeforces Round &num;197 &lpar;Div&period; 2&rpar; D&period; Xenia and Bit Operations

    题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...

  6. hive-学习笔记

    1.hive模糊搜索表  show tables like '*name*'; 2.查看表结构信息  desc formatted table_name;  desc table_name; 3.查看 ...

  7. ionic 项目分享【转】

    写在文章前:由于最近研究ionic框架,深感这块的Demo寥寥可数,而大家又都藏私,堂堂天朝,何时才有百家争鸣之象,开源精神吾辈当仁不让! 由于昨晚找资料太匆匆 忘记出处了,记得是在http://bb ...

  8. 电子笔记本的思考(1)(ver0&period;3)

    章节:电子笔记本的思考(1)   陶哲轩在<解题·成长·快乐——陶哲轩教你学数学>中着重强调,用纸笔来“缓存”思维对于数学解题的重要性: 用选定的符号表达你所知道的信息,并画一个示意图.把 ...

  9. hadoop记录-浅析Hadoop中的DistCp和FastCopy(转载)

    DistCp(Distributed Copy)是用于大规模集群内部或者集群之间的高性能拷贝工具. 它使用Map/Reduce实现文件分发,错误处理和恢复,以及报告生成. 它把文件和目录的列表作为ma ...

  10. FuelPHP 系列(四) ------ Validate 验证

    一.可用规则: 1.required 不能为 null, false or empty string.: 2.required_with 关联某个字段,关联字段有值则该字段必须有值: 3.match_ ...