MySQL数据库基本操作:导入hellodb.sql并按要求查询;创建账户并授权

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

# 本次在CentOS8.4操作系统及MySQL5.7.36上完成

mysql> select version();
+------------+
| version() |
+------------+
| 5.7.36-log |
+------------+
1 row in set (0.00 sec)

[root@CentOS84 ]#mysql -uroot -ps*****2
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.36-log Source distribution

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 tables;
ERROR 1046 (3D000): No database selected
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytestdb1 |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.06 sec)

mysql> quit

# 导入数据库并验证
[root@CentOS84 ]#mysql -uroot -ps*****2 < /data/hellodb_innodb.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@CentOS84 ]#mysql -uroot -ps*****2
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 10
Server version: 5.7.36-log Source distribution

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 |
| mytestdb1 |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)

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

# 进入hellodb数据库
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

#在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.02 sec)

1.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.02 sec)

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

mysql> select classid,avg(age) from students group by classid having avg(age)>30;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+----------+
3 rows in set (0.01 sec)

1.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)

2. 数据库授权magedu用户,允许192.1mysql密码忘记了怎么办68.1.0/24网段可以连接mysql

# 创建用户
mysql> create user 'magedu'@'192.168.1.%' identified by 'centos';
Query OK, 0 rows affected (0.06 sec)

mysql> show grants for magedu@'192.168.1.%';
+----------------------------------------------+
| Grants for magedu@192.168.1.% |
+----------------------------------------------+
| GRANT USAGE ON *.* TO 'magedu'@'192.168.1.%' |
+----------------------------------------------+
1 row in set (0.00 sec)

#授权
mysql> grant all on *.* to magedu@'192.168.1.%' identified by 'centos';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show grants for magedu@'192.168.1.%';
+-------------------------------------------------------+
| Grants for magedu@192.168.1.% |
+-------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'magedu'@'192.168.1.%' |
+-------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

# 查询数据库内所有的账号
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+------------------------------------+
| query |
+------------------------------------+
| User: 'shone'@'%'; |
| User: 'magedu'@'192.168.1.%'; |
| User: 'summer'@'192.168.1.%'; |
| User: 'mysql.session'@'localhost'; |
| User: 'mysql.sys'@'localhost'; |
| User: 'root'@'localhost'; |
+------------------------------------+
6 rows in set (0.02 sec)