1、 導入hellodb.sql生成數據庫
[root@test-centos7-node1 ~]# rz rz waiting to receive. zmodem trl+C ȡ 100% 7 KB 7 KB/s 00:00:01 0 Errors Transferring hellodb_innodb.sql... [root@test-centos7-node1 ~]# ls hellodb_innodb.sql [root@test-centos7-node1 ~]# mysql < hellodb_innodb.sql [root@test-centos7-node1 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 5.5.64-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) MariaDB [(none)]> 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 MariaDB [hellodb]> show tables; +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | students | | teachers | | toc | +-------------------+ 7 rows in set (0.00 sec) MariaDB [hellodb]>
說明:導入數據庫,可以用輸入重定向到方式,如上所示,也可以在數據庫里用source sql腳本路徑的方式導入,兩種的方式核心思想就是把sql腳本交給mysql數據庫引擎執行一遍,生成數據。
(1) 在students表中,查詢年齡大於25歲,且為男性的同學的名字和年齡
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
MariaDB [hellodb]> desc students;
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| StuID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Name | varchar(50) | NO | | NULL | |
| Age | tinyint(3) unsigned | NO | | NULL | |
| Gender | enum('F','M') | NO | | NULL | |
| ClassID | tinyint(3) unsigned | YES | | NULL | |
| TeacherID | int(10) unsigned | YES | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
MariaDB [hellodb]> 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)
MariaDB [hellodb]>
(2) 以ClassID為分組依據,顯示每組的平均年齡
MariaDB [hellodb]> select * from students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 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 | +-------+---------------+-----+--------+---------+-----------+ 25 rows in set (0.00 sec) 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) MariaDB [hellodb]>
(3) 顯示第2題中平均年齡大於30的分組及平均年齡
ariaDB [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) 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) MariaDB [hellodb]>
(4) 顯示以L開頭的名字的同學的信息
MariaDB [hellodb]> select * from students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 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 | +-------+---------------+-----+--------+---------+-----------+ 25 rows in set (0.00 sec) 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) MariaDB [hellodb]>
2、數據庫授權magedu用戶,允許192.168.1.0/24網段可以連接mysql
MariaDB [hellodb]> select user,host,password from mysql.user; +------+--------------------+----------+ | user | host | password | +------+--------------------+----------+ | root | localhost | | | root | test-centos7-node1 | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | test-centos7-node1 | | +------+--------------------+----------+ 6 rows in set (0.00 sec) MariaDB [hellodb]> grant all on *.* to magedu@'192.168.1.%' identified by 'admin'; Query OK, 0 rows affected (0.01 sec) MariaDB [hellodb]> select user,host,password from mysql.user; +--------+--------------------+-------------------------------------------+ | user | host | password | +--------+--------------------+-------------------------------------------+ | root | localhost | | | root | test-centos7-node1 | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | test-centos7-node1 | | | magedu | 192.168.1.% | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | +--------+--------------------+-------------------------------------------+ 7 rows in set (0.00 sec) MariaDB [hellodb]>
說明:此方式是創建用戶+授權一起做,當然也可以用先創建賬號,然后在授權,有關mysql的賬號創建和授權可以參考https://www.cnblogs.com/qiuhom-1874/p/9741166.html
3、總結mysql常見的存儲引擎以及特點。
MyISAM引擎特點:
1)不支持事務
2)支持表級別鎖定
3)讀寫相互阻塞,寫入不能讀,讀時不能寫
4)只緩存索引
5)不支持外鍵約束
6)不支持聚簇索引
7)讀取數據較快,占用資源較少
8)不支持MVCC(多版本並發控制機制)高並發
9)崩潰恢復性較差
10)mysql5.5前默認的數據庫引擎
MyISAM存儲引擎適用於只讀(或者寫較少)、表較小(可以接受長時間進行修復操作)的場景
MyISAM引擎文件有三個.frm(表格式定義文件) .MYD(數據文件) .MYI(索引文件)
InnoDB引擎特點:
1)支持事務,適合處理大量短期事務
2)支持行級別鎖
3)讀寫阻塞與事務隔離級別相關
4)可緩存數據和索引
5)支持聚簇索引
6)崩潰恢復性更好
7)支持MVCC高並發
8)從mysql5.5后支持全文索引,mysql5.5開始默認的數據庫引擎,InnoDB數據庫文件所有InnoDB表達數據和索引放置於同一個表空間中,表空間文件是由主配置文件中的datadir來指定存放的位置,數據文件:ibddata1,idbdata2……,若每個表單獨使用一個表空間存儲表的數據和索引,可在主配置文件中用innodb_file_per_table=ON來啟用,兩類文件放在數據庫獨立目錄中,其中數據文件(存儲數據和索引)是以標名加.ibd結尾的文件,表格式文件是表名加.frm結尾的文件
