一、导入hellodb.sql生成数据库
1.1 系统环境
内容 | 版本信息 |
---|---|
操作系统 | CentOS Linux release 7.8.2003 (Core) |
MySQL Server | 5.7.32 MySQL Community Server - (GPL) |
IP172.20.200.131 192.168.17.122 |
准备hellodb_innodb.sql脚本
--
-- Current Database: `hellodb`
--
CREATE DATABASE `hellodb`;
USE `hellodb`;
--
-- Table structure for table `classes`
--
DROP TABLE IF EXISTS `classes`;
CREATE TABLE `classes` (
`ClassID` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`Class` varchar(100) DEFAULT NULL,
`NumOfStu` smallint(5) unsigned DEFAULT NULL,
PRIMARY KEY (`ClassID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
--
-- Dumping data for table `classes`
--
LOCK TABLES `classes` WRITE;
INSERT INTO `classes` VALUES (1,‘Shaolin Pai‘,10),(2,‘Emei Pai‘,7),(3,‘QingCheng Pai‘,11),(4,‘Wudang Pai‘,12),(5,‘Riyue Shenjiao‘,31),(6,‘Lianshan Pai‘,27),(7,‘Ming Jiao‘,27),(8,‘Xiaoyao Pai‘,15);
UNLOCK TABLES;
--
-- Table structure for table `coc`
--
DROP TABLE IF EXISTS `coc`;
CREATE TABLE `coc` (
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`ClassID` tinyint(3) unsigned NOT NULL,
`CourseID` smallint(5) unsigned DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
--
-- Dumping data for table `coc`
--
LOCK TABLES `coc` WRITE;
INSERT INTO `coc` VALUES (1,1,2),(2,1,5),(3,2,2),(4,2,6),(5,3,1),(6,3,7),(7,4,5),(8,4,2),(9,5,1),(10,5,9),(11,6,3),(12,6,4),(13,7,4),(14,7,3);
UNLOCK TABLES;
--
-- Table structure for table `courses`
--
DROP TABLE IF EXISTS `courses`;
CREATE TABLE `courses` (
`CourseID` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`Course` varchar(100) NOT NULL,
PRIMARY KEY (`CourseID`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
--
-- Dumping data for table `courses`
--
LOCK TABLES `courses` WRITE;
INSERT INTO `courses` VALUES (1,‘Hamo Gong‘),(2,‘Kuihua Baodian‘),(3,‘Jinshe Jianfa‘),(4,‘Taiji Quan‘),(5,‘Daiyu Zanghua‘),(6,‘Weituo Zhang‘),(7,‘Dagou Bangfa‘);
UNLOCK TABLES;
--
-- Table structure for table `scores`
--
DROP TABLE IF EXISTS `scores`;
CREATE TABLE `scores` (
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`StuID` int(10) unsigned NOT NULL,
`CourseID` smallint(5) unsigned NOT NULL,
`Score` tinyint(3) unsigned DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
--
-- Dumping data for table `scores`
--
LOCK TABLES `scores` WRITE;
INSERT INTO `scores` VALUES (1,1,2,77),(2,1,6,93),(3,2,2,47),(4,2,5,97),(5,3,2,88),(6,3,6,75),(7,4,5,71),(8,4,2,89),(9,5,1,39),(10,5,7,63),(11,6,1,96),(12,7,1,86),(13,7,7,83),(14,8,4,57),(15,8,3,93);
UNLOCK TABLES;
--
-- Table structure for table `students`
--
DROP TABLE IF EXISTS `students`;
CREATE TABLE `students` (
`StuID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(50) NOT NULL,
`Age` tinyint(3) unsigned NOT NULL,
`Gender` enum(‘F‘,‘M‘) NOT NULL,
`ClassID` tinyint(3) unsigned DEFAULT NULL,
`TeacherID` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`StuID`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;
--
-- Dumping data for table `students`
--
LOCK TABLES `students` WRITE;
INSERT INTO `students` VALUES (1,‘Shi Zhongyu‘,22,‘M‘,2,3),(2,‘Shi Potian‘,22,‘M‘,1,7),(3,‘Xie Yanke‘,53,‘M‘,2,16),(4,‘Ding Dian‘,32,‘M‘,4,4),(5,‘Yu Yutong‘,26,‘M‘,3,1),(6,‘Shi Qing‘,46,‘M‘,5,NULL),(7,‘Xi Ren‘,19,‘F‘,3,NULL),(8,‘Lin Daiyu‘,17,‘F‘,7,NULL),(9,‘Ren Yingying‘,20,‘F‘,6,NULL),(10,‘Yue Lingshan‘,19,‘F‘,3,NULL),(11,‘Yuan Chengzhi‘,23,‘M‘,6,NULL),(12,‘Wen Qingqing‘,19,‘F‘,1,NULL),(13,‘Tian Boguang‘,33,‘M‘,2,NULL),(14,‘Lu Wushuang‘,17,‘F‘,3,NULL),(15,‘Duan Yu‘,19,‘M‘,4,NULL),(16,‘Xu Zhu‘,21,‘M‘,1,NULL),(17,‘Lin Chong‘,25,‘M‘,4,NULL),(18,‘Hua Rong‘,23,‘M‘,7,NULL),(19,‘Xue Baochai‘,18,‘F‘,6,NULL),(20,‘Diao Chan‘,19,‘F‘,7,NULL),(21,‘Huang Yueying‘,22,‘F‘,6,NULL),(22,‘Xiao Qiao‘,20,‘F‘,1,NULL),(23,‘Ma Chao‘,23,‘M‘,4,NULL),(24,‘Xu Xian‘,27,‘M‘,NULL,NULL),(25,‘Sun Dasheng‘,100,‘M‘,NULL,NULL);
UNLOCK TABLES;
--
-- Table structure for table `teachers`
--
DROP TABLE IF EXISTS `teachers`;
CREATE TABLE `teachers` (
`TID` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`Age` tinyint(3) unsigned NOT NULL,
`Gender` enum(‘F‘,‘M‘) DEFAULT NULL,
PRIMARY KEY (`TID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
--
-- Dumping data for table `teachers`
--
LOCK TABLES `teachers` WRITE;
INSERT INTO `teachers` VALUES (1,‘Song Jiang‘,45,‘M‘),(2,‘Zhang Sanfeng‘,94,‘M‘),(3,‘Miejue Shitai‘,77,‘F‘),(4,‘Lin Chaoying‘,93,‘F‘);
UNLOCK TABLES;
--
-- Table structure for table `toc`
--
DROP TABLE IF EXISTS `toc`;
CREATE TABLE `toc` (
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CourseID` smallint(5) unsigned DEFAULT NULL,
`TID` smallint(5) unsigned DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
运行sql脚本
[root@localhost ~]# mysql -uroot -p‘MyNewPassword!112233‘
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 6
Server version: 5.7.32 MySQL Community Server - (GPL)
Copyright (c) 2000, 2020, 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.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> source /root/hellodb_innodb.sql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use hellodb;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.01 sec)
1.2 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
mysql> use hellodb;
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
mysql>
mysql> select Name,Age from students where Age > 25 and Gender=‘M‘;
+--------------+-----+
| Name | Age |
+--------------+-----+
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Yu Yutong | 26 |
| Shi Qing | 46 |
| Tian Boguang | 33 |
| Xu Xian | 27 |
| Sun Dasheng | 100 |
+--------------+-----+
7 rows in set (0.00 sec)
1.3 以ClassID为分组依据,显示每组的平均年龄
mysql> select classid,avg(age) as 平均年龄 from students group by classid;
+---------+--------------+
| classid | 平均年龄 |
+---------+--------------+
| NULL | 63.5000 |
| 1 | 20.5000 |
| 2 | 36.0000 |
| 3 | 20.2500 |
| 4 | 24.7500 |
| 5 | 46.0000 |
| 6 | 20.7500 |
| 7 | 19.6667 |
+---------+--------------+
8 rows in set (0.00 sec)
1.4 以ClassID为分组依据,显示平均年龄大于30的分组及平均年龄
mysql> select classid,avg(age) as 平均年龄 from students group by classid having avg(age) >30;
+---------+--------------+
| classid | 平均年龄 |
+---------+--------------+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+--------------+
3 rows in set (0.00 sec)
1.5 显示以L开头的名字的同学的信息
mysql> select * from students where name like ‘L%‘;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
二、数据库授权magedu用户,允许192.168.17.0/24网段可以连接mysql
#MySQL Server端增加192.168.17网段地址
[root@localhost ~]# ip add add 192.168.17.122 dev eth0 label eth0:1
[root@localhost ~]# systemctl restart mysqld
#加密密码
mysql> select password(‘Magedu112233!‘);
+-------------------------------------------+
| password(‘Magedu112233!‘) |
+-------------------------------------------+
| *1403418EB84FFD259ED024853D5455172CD03A62 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
#创建magedu用户
mysql> create user ‘magedu‘@‘192.168.17.%‘ identified by ‘*1403418EB84FFD259ED024853D5455172CD03A62‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
登录192.168.17网段电脑,测试登录情况
[root@slave ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.200.202 netmask 255.255.0.0 broadcast 172.20.255.255
inet6 fe80::8d83:991b:3df5:2457 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:90:19:8f txqueuelen 1000 (Ethernet)
RX packets 10233 bytes 13257774 (12.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5562 bytes 389911 (380.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.17.123 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:90:19:8f txqueuelen 1000 (Ethernet)
#连接MySQL服务器的192.168.17.122地址
[root@slave ~]# mysql -h192.168.17.122 -umagedu -p‘Magedu112233!‘
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 8
Server version: 5.7.32 MySQL Community Server - (GPL)
Copyright (c) 2000, 2020, 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.
mysql>