一、导入hellodb.sql生成数据库
[root@centos7 ~]# ll hellodb_innodb.sql -rw-r--r-- 1 root root 7786 Dec 4 20:24 hellodb_innodb.sql [root@centos7 ~]# mysql < hellodb_innodb.sql
1、在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄;
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE 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.00 sec)
2、以ClassID为分组依据,显示每组的平均年龄;
MariaDB [hellodb]> 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.00 sec)
3、显示第2题中平均年龄大于30的分组及平均年龄;
MariaDB [hellodb]> 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.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 *.* to magedu@'192.168.0.%' identified by 'magedu'; Query OK, 0 rows affected (0.00 sec) #另一台主机192.168.0.105上测试,可以看到可以连接 [root@centos7 ~]# mysql -umagedu -pmagedu -h192.168.0.104 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 8 Server version: 5.5.60-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
三、总结mysql常见的存储引擎以及特点
1、MyISAM
不支持事务;表级锁定;读写相互阻塞,写入不能读,读时不能写;只缓存索引;不支持外键约束;不支持聚簇索引;读取数据较快,占用资源较少;不支持MVCC高并发;崩溃恢复性较差;MySQL5.5.5前默认的数据库引擎;
2、InnoDB
支持事务;行级锁;读写阻塞与事务隔离级别相关;可缓存数据和索引;支持聚簇索引;崩溃恢复性更好;支持MVCC高并发;MySQL5.5后支持全文索引;从MySQL5.5.5开始为默认的数据库引擎;
3、Memory
将所有数据保存在RAM中,在需要快速查找引用和其他类似数据的环境下,可提供极快的访问;默认使用HASH索引;支持表级锁定;适用存放临时数据;以前也被称为HEAP引擎;
4、Archive
为存储和检索大量很少参考的存档或安全审核信息,只支持SELECT和INSERT操作;支持行级锁和专用缓存区;
5、Performance_Schema
为Performance_Schema数据库使用,主要用于收集数据库服务器性能参数;
6、CSV
使用逗号分隔值的格式,将数据存储在文本文件中;可以使用CSV引擎以CSV格式导入和导出其他软件和应用程序之间的数据交换;
7、BLACKHOLE
黑洞存储引擎,接受但不存储数据,检索总是返回一个空集;该功能可用于分布式数据库设计,数据自动复制,但不是本地存储;