随笔 - 8  文章 - 0  评论 - 0  阅读 - 295

第十一周作业

、 导入hellodb.sql生成数据库

1、导入hellodb.sql生成数据库

环境准备

 

复制代码
[root@Centos7 hc]# cat /etc/yum.repos.d/mysql.repo
[mysql]
name=mysql5.7
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/
gpgcheck=0
[root@Centos7 hc]# yum install mysql-community-server -y
[root@Centos7 hc]# systemctl enable --now mysqld
 mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-01-02 17:29:02 CST; 2h 22min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 32340 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─32340 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Jan 02 17:28:48 Centos7 systemd[1]: Starting MySQL Server...
Jan 02 17:29:02 Centos7 systemd[1]: Started MySQL Server.
[root@Centos7 hc]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@Centos7 hc]# mysql -uroot -p'dk7z=Q:LYNCY'
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 14
Server version: 5.7.36

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> status
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> help alter user
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user root@'localhost' identified by 'Hc@he3778';

[root@Centos7 hc]# mysql -uroot -p'Hc@he3778'
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 15
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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>
复制代码

 

导入hellodb.sql生成数据库

复制代码
[root@Centos7 hc]# mysql -uroot -p'Hc@he3778' < hellodb_innodb.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@Centos7 hc]# mysql -uroot -p'Hc@he3778';
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 30
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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 |
| hellodb            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>
复制代码

切换到hellodb数据库

复制代码
mysql> use hellodb
No connection. Trying to reconnect...
Connection id:    31
Current database: *** NONE ***

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>
复制代码

(1) students表中,查询年龄大于25岁,且为男性的同学的名字和年龄

 

复制代码
mysql> SELECT name,age FROM students WHERE gender = 'M' AND age > 25;
+--------------+-----+
| 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.26 sec)

mysql>
复制代码

 

 

(2) ClassID为分组依据,显示每组的平均年龄

 

复制代码
mysql> SELECT classid,avg(age) FROM students GROUP BY classid;
+---------+----------+
| classid | avg(age) |
+---------+----------+
|    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.64 sec)

mysql>
复制代码

 

(3) 显示第2题中平均年龄大于30的分组及平均年龄

 

复制代码
mysql> SELECT classid,avg(age) as 平均年龄 FROM students GROUP BY classid HAVING 平均年龄>30;
+---------+--------------+
| classid | 平均年龄     |
+---------+--------------+
|    NULL |      63.5000 |
|       2 |      36.0000 |
|       5 |      46.0000 |
+---------+--------------+
3 rows in set (0.10 sec)

mysql>
复制代码

 

(4) 显示以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)

mysql>
复制代码

 

、数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql

 

mysql> GRANT ALL ON hellodb.* TO magedu@'192.168.1.%' IDENTIFIED BY 'Magedu@2021';
Query OK, 0 rows affected, 1 warning (0.00 sec)

 

posted on   天宇hc  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示