1驱入、 導(dǎo)入hellodb.sql生成數(shù)據(jù)庫
[root@c7-37-103-mini ~]# mysql < /data/app/hellodb_innodb.sql
[root@c7-37-103-mini ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.65-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]> 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.01 sec)
(1) 在students表中挠轴,查詢年齡大于25歲诵肛,且為男性的同學(xué)的名字和年齡 ;
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.07 sec)
(2) 以ClassID為分組依據(jù)跑芳,顯示每組的平均年齡;
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.04 sec)
#有空值需要排除
MariaDB [hellodb]> select ClassID,avg(age) from students where classid is not null group by classid ;
+---------+----------+
| ClassID | avg(age) |
+---------+----------+
| 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.01 sec)
(3) 顯示第2題中平均年齡大于30的分組及平均年齡;
MariaDB [hellodb]> select ClassID,avg(age) from students where classid is not null and age >30 group by classid ;
+---------+----------+
| ClassID | avg(age) |
+---------+----------+
| 2 | 43.0000 |
| 4 | 32.0000 |
| 5 | 46.0000 |
+---------+----------+
3 rows in set (0.00 sec)
(4) 顯示以L開頭的名字的同學(xué)的信息;
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)
2、數(shù)據(jù)庫授權(quán)magedu用戶帮非,允許192.168.1.0/24網(wǎng)段可以連接mysql
方法1:先創(chuàng)建用戶氧吐,然后授權(quán)
create user 'magedu'@'192.168.1.%' identified by 'centos';
#創(chuàng)建用戶還需要授權(quán)用戶可以管理的數(shù)據(jù)庫,hellodb.*表示可以管理hellodb的所有資源
grant all on hellodb.* to magedu;
方法2: 方法2:授權(quán)時(shí)直接創(chuàng)建用戶
grant all on hellodb.* to 'magedu'@'192.168.1.%' identified by 'centos';
3、總結(jié)mysql常見的存儲引擎以及特點(diǎn)末盔。
MySQL數(shù)據(jù)庫常用的存儲引擎MyISAM 和InnoDB筑舅,MySQL5.5以后其默認(rèn)存儲引擎為InnoDB,相當(dāng)于MyISAM淘汰了陨舱;他們各自的特點(diǎn)如下:
MyISAM引擎特點(diǎn)
不支持事務(wù)
表級鎖定
讀寫相互阻塞翠拣,寫入不能讀,讀時(shí)不能寫
只緩存索引
不支持外鍵約束
不支持聚簇索引
讀取數(shù)據(jù)較快游盲,占用資源較少
不支持MVCC(多版本并發(fā)控制機(jī)制)高并發(fā)
崩潰恢復(fù)性較差
MySQL5.5.5前默認(rèn)的數(shù)據(jù)庫引擎
MyISAM存儲引擎適用場景
只讀(或者寫較少)误墓、表較小(可以接受長時(shí)間進(jìn)行修復(fù)操作)
MyISAM引擎文件
其數(shù)據(jù)庫目錄下包含三個(gè)文件
tbl_name.frm 表格式定義
tbl_name.MYD 數(shù)據(jù)文件
tbl_name.MYI 索引文件
InnoDB引擎特點(diǎn)
行級鎖
支持事務(wù)益缎,適合處理大量短期事務(wù)
讀寫阻塞與事務(wù)隔離級別相關(guān)
可緩存數(shù)據(jù)和索引
支持聚簇索引
崩潰恢復(fù)性更好
支持MVCC高并發(fā)
從MySQL5.5后支持全文索引
從MySQL5.5.5開始為默認(rèn)的數(shù)據(jù)庫引擎
InnoDB數(shù)據(jù)庫文件
所有InnoDB表的數(shù)據(jù)和索引放置于同一個(gè)表空間中
表空間文件:datadir定義的目錄下
數(shù)據(jù)文件:ibddata1, ibddata2, ...
可以設(shè)置每個(gè)表單獨(dú)使用一個(gè)表空間存儲表的數(shù)據(jù)和索引
啟用:innodb_file_per_table=ON
兩類文件放在數(shù)據(jù)庫獨(dú)立目錄中
數(shù)據(jù)文件(存儲數(shù)據(jù)和索引):tb_name.ibd
表格式定義:tb_name.frm