1. 程式人生 > >mariadb(mysql)[詳解]

mariadb(mysql)[詳解]

安裝:  

[[email protected] ~]# yum install mariadb -y           #客戶端

[[email protected] ~]# yum install mariadb-server -y     #服務端

啟動服務:

[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# ss -tnl | grep 3306                 #檢視埠確定是否被監聽
LISTEN     0      50           *:3306                     *:*           

說明:若是mysql啟動 將mariadb改為mysql即可

進入mariadb:

[[email protected] ~]# mysql                      #注意
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

初始化mariadb:

[[email protected] ~]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):           #直接回車即可
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y             #是否設定mariadb中root使用者的密碼
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y          #刪除匿名使用者
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n         #是否允許root遠端連線
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y     #是否刪除test這個測試庫
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y     #是否重新載入許可權表
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

資料庫及表的查詢:


MariaDB [(none)]> show databases;        #資料庫的查詢
+--------------------+ 
| Database           |
+--------------------+
| hellodb            |
| information_schema |
| mage               |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.16 sec)

MariaDB [(none)]> use hellodb;           #進入指定的資料庫
Database changed
MariaDB [hellodb]> show tables;           #資料庫中表的查詢
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+
7 rows in set (0.07 sec)


MariaDB [hellodb]> desc classes;            #表的描述
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| ClassID  | tinyint(3) unsigned  | NO   | PRI | NULL    | auto_increment |
| Class    | varchar(100)         | YES  |     | NULL    |                |
| NumOfStu | smallint(5) unsigned | YES  |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
3 rows in set (0.14 sec)

MariaDB [hellodb]> select * from classes;       #查詢表的全部內容
+---------+----------------+----------+
| ClassID | Class          | NumOfStu |
+---------+----------------+----------+
|       1 | Shaolin Pai    |       10 |
|       2 | Emei Pai       |        7 |
|       3 | QingCheng Pai  |       11 |
|       4 | Wudang Pai     |       12 |
|       5 | Riyue Shenjiao |       31 |
|       6 | Lianshan Pai   |       27 |
|       7 | Ming Jiao      |       27 |
|       8 | Xiaoyao Pai    |       15 |
+---------+----------------+----------+
8 rows in set (0.05 sec)
MariaDB [hellodb]> select class from classes;     #查詢表的指定列
+----------------+
| class          |
+----------------+
| Shaolin Pai    |
| Emei Pai       |
| QingCheng Pai  |
| Wudang Pai     |
| Riyue Shenjiao |
| Lianshan Pai   |
| Ming Jiao      |
| Xiaoyao Pai    |
+----------------+
8 rows in set (0.05 sec)

MariaDB [hellodb]> select * from classes where classid=2;      #按條件查詢
+---------+----------+----------+
| ClassID | Class    | NumOfStu |
+---------+----------+----------+
|       2 | Emei Pai |        7 |
+---------+----------+----------+
1 row in set (0.08 sec)

資料庫及表的建立:

MariaDB [(none)]> create database zxl;     #資料庫的建立
Query OK, 1 row affected (0.20 sec)

MariaDB [(none)]> show databases;          #顯示庫
+--------------------+
| Database           |
+--------------------+
| hellodb            |
| information_schema |
| mage               |
| mysql              |
| performance_schema |
| zxl                |
+--------------------+
6 rows in set (0.00 sec)

MariaDB [(none)]> use zxl;       #進入指定的庫
Database changed
MariaDB [zxl]> show tables;       #顯示庫裡的表
Empty set (0.00 sec)

MariaDB [zxl]> create table M33;   #不指定列的情況下表的建立是不可以的 
ERROR 1113 (42000): A table must have at least 1 column
MariaDB [zxl]> create table M33(id tinyint unsigned primary key,name varchar(20));  #建立表(主鍵在此可以定義,但不是僅可以在此定義)
Query OK, 0 rows affected (0.33 sec)

MariaDB [zxl]> show tables;         #顯示所有的表
+---------------+
| Tables_in_zxl |
+---------------+
| M33           |
+---------------+
1 row in set (0.00 sec)

MariaDB [zxl]> desc M33;           #描述表
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id    | tinyint(3) unsigned | NO   | PRI | NULL    |       |
| name  | varchar(20)         | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

表資料的增刪改:

MariaDB [zxl]> insert into M33 values(1,'zhangsan');  #資料的插入
Query OK, 1 row affected (0.08 sec)

MariaDB [zxl]> select * from M33;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
+----+----------+
1 row in set (0.00 sec)
MariaDB [zxl]> insert into M33 (id) values(2);   #插入指定列的資料
Query OK, 1 row affected (0.09 sec)

MariaDB [zxl]> select * from M33;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
|  2 | NULL     |
+----+----------+
2 rows in set (0.00 sec)

MariaDB [zxl]> insert into M33 (name) values ('lisi');   #主鍵不能為空且唯一,所以這樣插入資料不成功
ERROR 1364 (HY000): Field 'id' doesn't have a default value

MariaDB [zxl]> select * from M33;   #刪除資料前的表
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
|  2 | NULL     |
+----+----------+
2 rows in set (0.00 sec)

MariaDB [zxl]> delete  from M33 where id=2;   #刪除指定的行資料,如果沒有where的限制條件,那麼表的內容將全部清空
Query OK, 1 row affected (0.02 sec)

MariaDB [zxl]> select * from M33;    #刪除後資料的顯示
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
+----+----------+
1 row in set (0.00 sec)

MariaDB [zxl]> update M33 set name='lisi' where id=1;   #修改指定行的指定列的內容
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [zxl]> select * from M33;
+----+------+
| id | name |
+----+------+
|  1 | lisi |
+----+------+
1 row in set (0.00 sec)



簡單函式的運用:

MariaDB [hellodb]> select avg(age) age from students;  #平均age的函式,avg(age) age中括號外的age是別名,完整的寫法為avg(age) as age ,其中這個as可以省略
+---------+
| age     |
+---------+
| 28.2000 |
+---------+
1 row in set (0.00 sec)

MariaDB [hellodb]> select gender, avg(age) age from students group by gender; #分組求平均
+--------+---------+
| gender | age     |
+--------+---------+
| F      | 26.7000 |
| M      | 29.2000 |
+--------+---------+
2 rows in set (0.00 sec)

MariaDB [hellodb]> select gender, avg(age) age from students where age>20  group by  gender;  #在where限制條件後再求分組的平均
+--------+---------+
| gender | age     |
+--------+---------+
| F      | 30.1429 |
| M      | 36.2222 |
+--------+---------+
2 rows in set (0.00 sec)

說明:除了avg還有abs求絕對值 max最大值min最小值 sum和等的函式,其用法一致

表連線:

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    |                |
| phone     | char(11)            | YES  |     | NULL    |                |
| Gender    | enum('F','M')       | NO   |     | NULL    |                |
| ClassID   | tinyint(3) unsigned | YES  |     | NULL    |                |
| TeacherID | int(10) unsigned    | YES  |     | NULL    |                |
+-----------+---------------------+------+-----+---------+----------------+
7 rows in set (0.04 sec)

MariaDB [hellodb]> desc classes;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| ClassID  | tinyint(3) unsigned  | NO   | PRI | NULL    | auto_increment |
| Class    | varchar(100)         | YES  |     | NULL    |                |
| NumOfStu | smallint(5) unsigned | YES  |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)


MariaDB [hellodb]> select students.name ,classes.class from students join classes on students.classid=classes.classid;
+----------------+----------------+
| name           | class          |
+----------------+----------------+
| Hou Yi         | Emei Pai       |
| Ya Se          | Shaolin Pai    |
| An Qila        | Emei Pai       |
| Da Ji          | Wudang Pai     |
| Sun Shangxiang | QingCheng Pai  |
| Huang Zhong    | Riyue Shenjiao |
| Liu Bei        | QingCheng Pai  |
| Guan Yu        | Ming Jiao      |
| Zhang Fei      | Lianshan Pai   |
| Di Renjie      | QingCheng Pai  |
| Li Yuanfang    | Lianshan Pai   |
| Lan Lingwang   | Shaolin Pai    |
| Wang Zhaojun   | Emei Pai       |
| Bai Qi         | QingCheng Pai  |
| A Ke           | Wudang Pai     |
| Cai Wenji      | Shaolin Pai    |
| Lv Bu          | Wudang Pai     |
| Diao Chan      | Ming Jiao      |
| Gong Sunli     | Lianshan Pai   |
| Ming Shiyin    | Ming Jiao      |
| Dun Shan       | Lianshan Pai   |
| Zhou Yu        | Shaolin Pai    |
| Mi Yue         | Wudang Pai     |
+----------------+----------------+
23 rows in set (0.00 sec)
#上邊是兩表連線

MariaDB [hellodb]> desc classes;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| ClassID  | tinyint(3) unsigned  | NO   | PRI | NULL    | auto_increment |
| Class    | varchar(100)         | YES  |     | NULL    |                |
| NumOfStu | smallint(5) unsigned | YES  |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
3 rows in set (0.03 sec)

MariaDB [hellodb]> desc courses;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| CourseID | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| Course   | varchar(100)         | NO   |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
2 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    |                |
| phone     | char(11)            | YES  |     | NULL    |                |
| Gender    | enum('F','M')       | NO   |     | NULL    |                |
| ClassID   | tinyint(3) unsigned | YES  |     | NULL    |                |
| TeacherID | int(10) unsigned    | YES  |     | NULL    |                |
+-----------+---------------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)

MariaDB [hellodb]> select s.name as student_name, co.course as course from students s join coc c on s.classid=c.classid join courses co on c.courseid=co.courseid;
+----------------+----------------------+
| student_name   | course               |
+----------------+----------------------+
| Hou Yi         | Kuihua Baodian       |
| Hou Yi         | Dugu Jiujian         |
| Ya Se          | Kuihua Baodian       |
| Ya Se          | Xixing Dafa          |
| An Qila        | Kuihua Baodian       |
| An Qila        | Dugu Jiujian         |
| Da Ji          | Xixing Dafa          |
| Da Ji          | Kuihua Baodian       |
| Sun Shangxiang | Hama Gong            |
| Sun Shangxiang | Dagou Bangfa         |
| Huang Zhong    | Hama Gong            |
| Liu Bei        | Hama Gong            |
| Liu Bei        | Dagou Bangfa         |
| Guan Yu        | Taiji Quan           |
| Guan Yu        | XiangLong Shibazhang |
| Zhang Fei      | XiangLong Shibazhang |
| Zhang Fei      | Taiji Quan           |
| Di Renjie      | Hama Gong            |
| Di Renjie      | Dagou Bangfa         |
| Li Yuanfang    | XiangLong Shibazhang |
| Li Yuanfang    | Taiji Quan           |
| Lan Lingwang   | Kuihua Baodian       |
| Lan Lingwang   | Xixing Dafa          |
| Wang Zhaojun   | Kuihua Baodian       |
| Wang Zhaojun   | Dugu Jiujian         |
| Bai Qi         | Hama Gong            |
| Bai Qi         | Dagou Bangfa         |
| A Ke           | Xixing Dafa          |
| A Ke           | Kuihua Baodian       |
| Cai Wenji      | Kuihua Baodian       |
| Cai Wenji      | Xixing Dafa          |
| Lv Bu          | Xixing Dafa          |
| Lv Bu          | Kuihua Baodian       |
| Diao Chan      | Taiji Quan           |
| Diao Chan      | XiangLong Shibazhang |
| Gong Sunli     | XiangLong Shibazhang |
| Gong Sunli     | Taiji Quan           |
| Ming Shiyin    | Taiji Quan           |
| Ming Shiyin    | XiangLong Shibazhang |
| Dun Shan       | XiangLong Shibazhang |
| Dun Shan       | Taiji Quan           |
| Zhou Yu        | Kuihua Baodian       |
| Zhou Yu        | Xixing Dafa          |
| Mi Yue         | Xixing Dafa          |
| Mi Yue         | Kuihua Baodian       |
+----------------+----------------------+
45 rows in set (0.00 sec)

#三表連線