1、導入hellodb.sql生成數據庫
[root@mariadb ~]# mysql < hellodb_innodb.sql
[root@mariadb ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 10.4.12-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 |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
/*在students表中,查詢年齡大於25歲,且爲男性的同學的名字和年齡*/
MariaDB [(none)]> use hellodb
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.000 sec)
MariaDB [hellodb]> 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.002 sec)
/* 以ClassID爲分組依據,顯示每組的平均年齡*/
MariaDB [hellodb]> select classid,avg(age) as 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.002 sec)
/*顯示第2題中平均年齡大於30的分組及平均年齡*/
MariaDB [hellodb]> select classid,avg(age) as 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.001 sec)
/* 顯示以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.001 sec)
2、數據庫授權magedu用戶,允許192.168.1.0/24網段可以連接mysql
MariaDB [hellodb]> use mysql;
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 [mysql]> select user,host,password from user;
+-------+-----------+-------------------------------------------+
| User | Host | Password |
+-------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| mysql | localhost | invalid |
+-------+-----------+-------------------------------------------+
2 rows in set (0.001 sec)
MariaDB [mysql]> create user 'magedu'@'192.168.0.%' identified by '123456';
Query OK, 0 rows affected (0.001 sec)
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.004 sec)
MariaDB [mysql]> exit
Bye
[root@mariadb ~]# mysql -umagedu -p123456 -h 192.168.0.110
ERROR 2002 (HY000): Can't connect to MySQL server on '192.168.0.110' (115)
[root@mariadb ~]# mysql -umagedu -p123456 -h 192.168.0.102
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 10.4.12-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)]>
3、總結mysql常見的存儲引擎以及特點
總結
MyISAM引擎特點
不支持事務
表級鎖定
讀寫相互阻塞,寫入不能讀,讀時不能寫
只緩存索引
不支持外鍵約束
不支持聚簇索引
讀取數據較快,佔用資源較少
不支持MVCC(多版本併發控制機制)高併發
崩潰恢復性較差
MySQL5.5.5前默認的數據庫引擎
MyISAM存儲引擎適用場景
只讀(或者寫較少)、表較小(可以接受長時間進行修復操作)
MyISAM引擎文件
tbl_name.frm 表格式定義
tbl_name.MYD 數據文件
tbl_name.MYI 索引文件
[root@centos72-test1 mysql]# ll /data/mysql/mysql/func.*
-rw-rw---- 1 mysql mysql 1580 May 7 12:53 /data/mysql/mysql/func.frm
-rw-rw---- 1 mysql mysql 8192 May 7 12:53 /data/mysql/mysql/func.MAD
-rw-rw---- 1 mysql mysql 8192 May 7 12:53 /data/mysql/mysql/func.MAI
[root@centos72-test1 mysql]# file /data/mysql/mysql/func.*
/data/mysql/mysql/func.frm: MySQL table definition file Version 10
/data/mysql/mysql/func.MAD: data
/data/mysql/mysql/func.MAI: data
InnoDB引擎特點
行級鎖
支持事務,適合處理大量短期事務
讀寫阻塞與事務隔離級別相關
可緩存數據和索引
支持聚簇索引
崩潰恢復性更好
支持MVCC高併發
從MySQL5.5後支持全文索引
從MySQL5.5.5開始爲默認的數據庫引擎
InnoDB數據庫文件
所有InnoDB表的數據和索引放置於同一個表空間中
表空間文件:datadir定義的目錄下
數據文件:ibddata1, ibddata2, …(包含數據表和索引)
每個表單獨使用一個表空間存儲表的數據和索引
啓用:innodb_file_per_table=ON (在MariaDB>5.5中 默認就會開啓)
兩類文件放在數據庫獨立目錄中
數據文件(存儲數據和索引):tb_name.ibd (需要innodb_file_per_table=ON)
表格式定義:tb_name.frm