透视数据,MySQL用户权限控制一例

 

透视数据实际上就是行状态转为例状态

 

Preface

先加一张测试表

一、创建数据库表

 

IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL DROP TABLE dbo.Orders;
GO

CREATE TABLE dbo.Orders
(
  orderid   INT        NOT NULL,
  orderdate DATE       NOT NULL,
  empid     INT        NOT NULL,
  custid    VARCHAR(5) NOT NULL,
  qty       INT        NOT NULL,
  CONSTRAINT PK_Orders PRIMARY KEY(orderid)
);

INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
VALUES
  (30001, '20070802', 3, 'A', 10),
  (10001, '20071224', 2, 'A', 12),
  (10005, '20071224', 1, 'B', 20),
  (40001, '20080109', 2, 'A', 40),
  (10006, '20080118', 1, 'C', 14),
  (20001, '20080212', 2, 'B', 12),
  (40005, '20090212', 3, 'A', 10),
  (20002, '20090216', 1, 'C', 20),
  (30003, '20090418', 2, 'B', 15),
  (30004, '20070418', 3, 'C', 22),
  (30007, '20090907', 3, 'D', 30);

SELECT * FROM dbo.Orders;

1.在命令行中进入MySQL数据库
我们的操作都是在命令行中进行的,所以保证你可以进入命令行界面(对于Windows,开始菜单--运行--输入cmd,即可调出命令行界面,Linux和MAC的就不用多说了。)
在命令行中进行如下的命令操作就可以进入到MySQL数据库中:
C:Windowssystem32> mysql -h localhost -u root -p

    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.

把这张表查出来

基本的解释如下:

 

SELECT empid, custid, SUM(qty) AS sumqty
FROM dbo.Orders
GROUP BY empid, custid;

◆-h:后面接的是主机名,表示你要连接到哪台主机的MySQL数据库。

Example

图片 1

◆-u:后面接的是用户名,MySQL默认的就是root。

 

我们将 custid行转换成例 

◆-p:指定需要使用密码登陆MySQL数据库,如果密码为空,该参数可以省略。

Create a test account.

SELECT empid,
SUM(CASE WHEN custid = 'A' THEN qty END) AS A,
SUM(CASE WHEN custid = 'B' THEN qty END) AS B,
SUM(CASE WHEN custid = 'C' THEN qty END) AS C,
SUM(CASE WHEN custid = 'D' THEN qty END) AS D 
FROM dbo.Orders
GROUP BY empid;

详细的解释如下:

 1 (root@localhost mysql3306.sock)[(none)]>create user aaron8219@'192.168.1.%' identified by 'zlm';
 2 Query OK, 0 rows affected (0.00 sec)
 3 
 4 (root@localhost mysql3306.sock)[(none)]>select user,host from mysql.user;
 5 +---------------+-------------+
 6 | user          | host        |
 7 +---------------+-------------+
 8 | rpl_mgr       | %           |
 9 | aaron8219     | 192.168.1.% |
10 | repl          | 192.168.1.% |
11 | replica       | 192.168.1.% |
12 | zlm           | 192.168.1.% |
13 | mysql.session | localhost   |
14 | mysql.sys     | localhost   |
15 | root          | localhost   |
16 +---------------+-------------+
17 8 rows in set (0.00 sec)
18 
19 (root@localhost mysql3306.sock)[(none)]>grant all privileges on *.* to aaron8219@'192.168.1.%'; //Grant the supreme privileges to the user.
20 Query OK, 0 rows affected (0.00 sec)
21 
22 (root@localhost mysql3306.sock)[(none)]>show grants for aaron8219@'192.168.1.%';
23 +----------------------------------------------------------+
24 | Grants for aaron8219@192.168.1.%                         |
25 +----------------------------------------------------------+
26 | GRANT ALL PRIVILEGES ON *.* TO 'aaron8219'@'192.168.1.%' |
27 +----------------------------------------------------------+
28 1 row in set (0.00 sec)

图片 2

前面我们在-h后面填写了localhost,表示的是要登陆到当前主机的MySQL数据库,在这种情况下,-h参数以及localhost完全可以省略,也就是说,使用下面的命令也是可以登陆到本机的MySQL数据库的:

 

sql server 还支持一个子句用于 行转列 PIVOT  是以FROM内嵌 表表达式实现的

2.查看已有的数据库
成功登陆到MySQL数据库中之后,我们首先就来看一下,MySQL数据库中有多少个数据库(你可以理解为,MySQL数据库是一个大仓库,然后这个大仓库下面有多个小的仓库,我们就在这些小仓库中存放我们的数据),我们进行如下的操作:
mysql> show databases;

**Connect to database with the new account.**

SELECT empid, A, B, C, D
FROM (SELECT empid, custid, qty
      FROM dbo.Orders) AS D
  PIVOT(SUM(qty) FOR custid IN(A, B, C, D)) AS P;

3.创建自己的数据库*(create database 数据库名;)

 1 [root@zlm2 09:25:29 ~]
 2 #mysql -uaaron8219 -pzlm -h192.168.1.101
 3 mysql: [Warning] Using a password on the command line interface can be insecure.
 4 Welcome to the MySQL monitor.  Commands end with ; or g.
 5 Your MySQL connection id is 4
 6 Server version: 5.7.21-log MySQL Community Server (GPL)
 7 
 8 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 9 
10 Oracle is a registered trademark of Oracle Corporation and/or its
11 affiliates. Other names may be trademarks of their respective
12 owners.
13 
14 Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
15 
16 (aaron8219@192.168.1.101 3306)[(none)]>show databases; //The user "aaron8219" can see all the databases in the current MySQL instance.
17 +--------------------+
18 | Database           |
19 +--------------------+
20 | information_schema |
21 | mysql              |
22 | performance_schema |
23 | sys                |
24 | sysbench           |
25 | zlm                |
26 +--------------------+
27 6 rows in set (0.01 sec)
28 
29 (aaron8219@192.168.1.101 3306)[(none)]>create database aaron8219;
30 Query OK, 1 row affected (0.00 sec)
31 
32 (aaron8219@192.168.1.101 3306)[(none)]>use aaron8219;
33 Database changed
34 (aaron8219@192.168.1.101 3306)[aaron8219]>create table t1(
35     -> id int,
36     -> name char(10)
37     -> ) engine=innodb;
38 Query OK, 0 rows affected (0.02 sec)

PIVOT  (数据行)  for  要转的列  IN (转那几个数据)

接下来我们就要创建一个属于我们自己的数据库来保存数据了,看下面的操作:
mysql> create database students_info;
Query OK, 1 row affected (0.01 sec)

 

 

这里我们创建了一个名为students_info的数据库,当看到有Query OK, 1 row affected (0.01 sec)时,就说明我们的数据库创建成功了.

**Create another precise account which name is equal to the one above and with an intact ip address.**

4.4.选择要操作的数据库
(use 数据库名[;] 这时你应该注意到,分号";"是放在中括号[]里的,也就是说,加不加分号都是可以的,这意味着,你使用use students_info来进行数据库的选择也是可以的,但为了不容易混淆,建议还是加上去好些吧。)

 

在上面的数据库中,并不是每一个都是我们需要进行操作的,我们只需要对我们刚刚创建的数据库students_info进行操作就可以了,但是,如果要想操作这个数据库,首先你就要告诉MySQL数据库系统,接下来你要使用students_info这个数据库来进行相关的操作,如何告诉系统呢?看下面的操作:
mysql> use students_info;
Database changed

 1 (root@localhost mysql3306.sock)[(none)]>create user aaron8219@'192.168.1.101' identified by 'zlm';
 2 Query OK, 0 rows affected (0.00 sec)
 3 
 4 (root@localhost mysql3306.sock)[(none)]>select user,host from mysql.user;
 5 +---------------+---------------+
 6 | user          | host          |
 7 +---------------+---------------+
 8 | rpl_mgr       | %             |
 9 | aaron8219     | 192.168.1.%   |
10 | repl          | 192.168.1.%   |
11 | replica       | 192.168.1.%   |
12 | zlm           | 192.168.1.%   |
13 | aaron8219     | 192.168.1.101 |
14 | mysql.session | localhost     |
15 | mysql.sys     | localhost     |
16 | root          | localhost     |
17 +---------------+---------------+
18 9 rows in set (0.00 sec)
19 
20 (root@localhost mysql3306.sock)[(none)]>grant all privileges on aaron8219.* to aaron8219@'192.168.1.101'; //Grant the privileges only on "aaron8219" database.
21 Query OK, 0 rows affected (0.00 sec)
22 
23 (root@localhost mysql3306.sock)[(none)]>show grants for aaron8219@'192.168.1.101';
24 +----------------------------------------------------------------------+
25 | Grants for aaron8219@192.168.1.101                                   |
26 +----------------------------------------------------------------------+
27 | GRANT USAGE ON *.* TO 'aaron8219'@'192.168.1.101'                    |
28 | GRANT ALL PRIVILEGES ON `aaron8219`.* TO 'aaron8219'@'192.168.1.101' |
29 +----------------------------------------------------------------------+
30 2 rows in set (0.00 sec)

5.在数据库中创建数据库表(create table 数据库表名(列声明);)

 

mysql> create table network3
-> (
-> id char(10) not null primary key,
-> name char(16) not null,
-> sex char(6) not null,
-> age int not null,
-> address char(36) not null
-> );

Connect to database with the account again.

a.创建一列,名称为id;数据类型为char字符类型,字符的最大长度为10个字符;并且该列内容不允许为空;同时把这一列作为这张表的主键,用来区分表中不同行。

 1 [root@zlm2 09:32:57 ~]
 2 #mysql -uaaron8219 -pzlm -h192.168.1.101
 3 mysql: [Warning] Using a password on the command line interface can be insecure.
 4 Welcome to the MySQL monitor.  Commands end with ; or g.
 5 Your MySQL connection id is 5
 6 Server version: 5.7.21-log MySQL Community Server (GPL)
 7 
 8 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 9 
10 Oracle is a registered trademark of Oracle Corporation and/or its
11 affiliates. Other names may be trademarks of their respective
12 owners.
13 
14 Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
15 
16 (aaron8219@192.168.1.101 3306)[(none)]>show databases; //Only the "aaron8219" database can be list.
17 +--------------------+
18 | Database           |
19 +--------------------+
20 | information_schema |
21 | aaron8219          |
22 +--------------------+
23 2 rows in set (0.00 sec)
24 
25 (aaron8219@192.168.1.101 3306)[(none)]>show grants for aaron8219@'192.168.1.101';
26 +----------------------------------------------------------------------+
27 | Grants for aaron8219@192.168.1.101                                   |
28 +----------------------------------------------------------------------+
29 | GRANT USAGE ON *.* TO 'aaron8219'@'192.168.1.101'                    |
30 | GRANT ALL PRIVILEGES ON `aaron8219`.* TO 'aaron8219'@'192.168.1.101' |
31 +----------------------------------------------------------------------+
32 2 rows in set (0.00 sec)
33 
34 (aaron8219@192.168.1.101 3306)[(none)]>use aaron8219;
35 Reading table information for completion of table and column names
36 You can turn off this feature to get a quicker startup with -A
37 
38 Database changed
39 (aaron8219@192.168.1.101 3306)[aaron8219]>show tables;
40 +---------------------+
41 | Tables_in_aaron8219 |
42 +---------------------+
43 | t1                  |
44 +---------------------+
45 1 row in set (0.00 sec)
46 
47 (aaron8219@192.168.1.101 3306)[aaron8219]>insert into t1 values(1,'abc');
48 Query OK, 1 row affected (0.00 sec)
49 
50 (aaron8219@192.168.1.101 3306)[aaron8219]>select * from t1;
51 +------+------+
52 | id   | name |
53 +------+------+
54 |    1 | abc  |
55 +------+------+
56 1 row in set (0.00 sec)
57 
58 //Eventrually,the privileges of account aaron8219@'192.168.1.%' has been restricted merely on database "aaron8219".
59 //Further more,we can revoke all the privileges on it either.

b.创建一列,名称为name;数据类型为char字符类型,字符的最大长度为16个字符;并且该列内容不允许为空。

 

关于primary key

Revoke the all privileges of the account.

考虑一下,在学生信息表中,假如存放了很多学生的信息,像上面这个表中,可能会存在这样的两个学生,他们的name、sex、age、address都相同的,这很正常吧!所以呢,你就必须得有一个数据项是不同的,我们上面这个表就指定id数据项是必须不同相同的,也就是所谓的主键了。

 1 (root@localhost mysql3306.sock)[(none)]>revoke all privileges on aaron8219.* from aaron8219@'192.168.1.101';
 2 Query OK, 0 rows affected (0.00 sec)
 3 
 4 (root@localhost mysql3306.sock)[(none)]>show grants for aaron8219@'192.168.1.101';
 5 +---------------------------------------------------+
 6 | Grants for aaron8219@192.168.1.101                |
 7 +---------------------------------------------------+
 8 | GRANT USAGE ON *.* TO 'aaron8219'@'192.168.1.101' |
 9 +---------------------------------------------------+
10 1 row in set (0.00 sec)

关于列名称

 

需要注意的是,每一列的名称必须是不相同的才行。

**Connect to database with the account third times.**

 

 1 [root@zlm2 10:18:20 ~]
 2 #mysql -uaaron8219 -pzlm -h192.168.1.101
 3 mysql: [Warning] Using a password on the command line interface can be insecure.
 4 Welcome to the MySQL monitor.  Commands end with ; or g.
 5 Your MySQL connection id is 8
 6 Server version: 5.7.21-log MySQL Community Server (GPL)
 7 
 8 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 9 
10 Oracle is a registered trademark of Oracle Corporation and/or its
11 affiliates. Other names may be trademarks of their respective
12 owners.
13 
14 Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
15 
16 (aaron8219@192.168.1.101 3306)[(none)]>show databases;
17 +--------------------+
18 | Database           |
19 +--------------------+
20 | information_schema |
21 +--------------------+
22 1 row in set (0.00 sec)
23 
24 (aaron8219@192.168.1.101 3306)[(none)]>create database test;
25 ERROR 1044 (42000): Access denied for user 'aaron8219'@'192.168.1.101' to database 'test'
26 
27 //This time,the account of aaron8219 login with ip "192.168.1.101" can do nothing in the target instance.

6.MySQL数据库、数据库与数据库表

 

好了,通过上面的操作,我们不仅仅学会了在MySQL数据库创建一个数据库,还学会了在新建数据库的中创建数据库表,我们来简单的理清一下这三者的关系吧:

MySQL数据库:相当于是一个大仓库,里面有很多个小仓库;

数据库:相当于是大仓库中的小仓库,里面有很多个小的容器;

数据库表:相当于是小仓库中的容器,我们就是在里面存储内容;

 

二、操作数据库中的数据库表

1.向数据表中添加数据

本文由澳门新葡亰平台官网发布于数据库,转载请注明出处:透视数据,MySQL用户权限控制一例

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。