一、 导入hellodb.sql生成数据库
[root@centos7 ~]#mysql <hellodb_innodb.sql
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
MariaDB [hellodb]> select name,gender,age from students where age>25 and gender='M';
+--------------+--------+-----+
| name | gender | age |
+--------------+--------+-----+
| Xie Yanke | M | 53 |
| Ding Dian | M | 32 |
| Yu Yutong | M | 26 |
| Shi Qing | M | 46 |
| Tian Boguang | M | 33 |
| Xu Xian | M | 27 |
| Sun Dasheng | M | 100 |
+--------------+--------+-----+
7 rows in set (0.00 sec)
(2) 以ClassID为分组依据,显示每组的平均年龄
MariaDB [hellodb]> select classid,avg(age) as 平均年龄 from students where classid is not null group by classid;
+---------+--------------+
| classid | 平均年龄 |
+---------+--------------+
| 1 | 20.5000 |
| 2 | 36.0000 |
| 3 | 20.2500 |
| 4 | 24.7500 |
| 5 | 46.0000 |
| 6 | 20.7500 |
| 7 | 19.6667 |
+---------+--------------+
7 rows in set (0.00 sec)
(3) 显示第2题中平均年龄大于30的分组及平均年龄
MariaDB [hellodb]> select classid,avg(age) as 平均年龄 from students where classid is not null group by classid having 平均年龄>30;
+---------+--------------+
| classid | 平均年龄 |
+---------+--------------+
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+--------------+
2 rows in set (0.00 sec)
(4) 显示以L开头的名字的同学的信息
MariaDB [hellodb]> 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.1.0/24网段可以连接mysql
MariaDB [(none)]> grant all on mysql.* to magedu@'192.168.45.%' identified by 'centos';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host,password from mysql.user;
+--------+---------------------+-------------------------------------------+
| user | host | password |
+--------+---------------------+-------------------------------------------+
| root | localhost | |
| root | centos7.localdomain | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | centos7.localdomain | |
| magedu | 192.168.45.% | *128977E278358FF80A246B5046F51043A2B1FCED |
+--------+---------------------+-------------------------------------------+
8 rows in set (0.00 sec)
验证登陆:
[root@centos6 ~]#mysql -umagedu -pcentos -h192.168.45.7
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2013, 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 grants for current_user();
+------------------------------------------------------------------------------------------------------------------+
| Grants for magedu@192.168.45.% |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'magedu'@'192.168.45.%' IDENTIFIED BY PASSWORD '*128977E278358FF80A246B5046F51043A2B1FCED' |
| GRANT ALL PRIVILEGES ON `mysql`.* TO 'magedu'@'192.168.45.%' |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> \s
--------------
mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
Connection id: 5
Current database:
Current user: magedu@192.168.45.6
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.65-MariaDB MariaDB Server
Protocol version: 10
Connection: 192.168.45.7 via TCP/IP
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: latin1
Conn. characterset: latin1
TCP port: 3306
Uptime: 16 min 0 sec
Threads: 2 Questions: 114 Slow queries: 0 Opens: 7 Flush tables: 2 Open tables: 26 Queries per second avg: 0.118
--------------
三、总结mysql常见的存储引擎以及特点。
mysql常见的存储引擎:MyISAM、InnoDB、Memory、Archive、NDB、MRG_MyISAM等
MyIASM:使用于只读(或者写较少)、表较小(可以接受长时间进行修复操作)的场景
不支持事务
表级锁定
读写相互阻塞,写入不能读,读时不能写
只缓存索引
不支持外键约束
不支持聚簇索引
读取数据较快,占用资源较少
不支持MVCC(多版本并发控制机制)高并发
崩溃恢复性较差
MySQL5.5.5前默认的数据库引擎
InnoDB:
行级锁
支持事务,适合处理大量短期事务
读写阻塞与事务隔离级别相关
可缓存数据和索引
支持聚簇索引
崩溃恢复性更好
支持MVCC高并发
从MySQL5.5后支持全文索引
从MySQL5.5.5开始为默认的数据库引擎