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. 2018.12.30 Intellij IDEA设置main方法自动补全

    Eclipse与 Intellij IDEA设置方法自动补全 1.首先,点击File-->Settings-->Editor-->Live Templates 设置你想输出的模板 右 ...

  2. [转]CUDA和OpenGL互操作的实现及分析

    CUDA和OpenGL互操作的实现及分析刘进锋.郭雷(西北工业大学 自动化学院,陕西西安710129) 1 CUDA与OpenGL概述 OpenGL是图形硬件的软件接口,它是在SGI等多家世界著名的计 ...

  3. HDU1215 七夕节(模拟 数学)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1215 七夕节 Time Limit: 2000/1000 MS (Java/Others)    Me ...

  4. Notepad++ 插件之 TextFX (安装及作用)

    <安装:打开 notepad++  插件 -> Plugin Manager -> Show Plugin Manager -> available ->选中 TextF ...

  5. 第13章 GPIO输入—按键检测

    本章参考资料:<STM32F76xxx参考手册>.库帮助文档<STM32F779xx_User_Manual.chm>. 按键检测使用到GPIO外设的基本输入功能,本章中不再赘 ...

  6. T4模板之文本模板

    网址:https://docs.microsoft.com/en-us/visualstudio/modeling/design-time-code-generation-by-using-t4-te ...

  7. UDP端口启动后一段时间无法接收到数据

    接口需求:开发一个UDP协议的接口作为服务端接收来自客户端的认证数据,数据量每分钟7w+条; 数据格式:标准的redius协议,redius协议的相关知识在网上查资料,提供线索:http://blog ...

  8. kubeadm常见报错和解决方法

    k8s随着社区不断壮大国内使用率现在也是比较高的,常用的部署方式主要还是以二进制和kubeadm为主,当然1.13之前大部分人还是以二进制,但是随着版本更新kubeadm已经逐步适用于生成环境,由于k ...

  9. python脚本批量杀死redis链接

    #!/usr/bin/python #coding:utf8 import sys import commands,time,re ##自定义redis链接类 from redismodel impo ...

  10. (第02节)集成Sping框架

    通过第一节创建好的Web项目,接下来就是集成Spring框架 首先让我们看下创建好的Web项目的基本结构 其中,Java跟test是我自己创的,然后就是一般的webapp文件,和pom配置文件,要在w ...