1. 程式人生 > >linux入門系列19--資料庫管理系統(DBMS)之MariaDB

linux入門系列19--資料庫管理系統(DBMS)之MariaDB

前面講完Linux下一系列服務的配置和使用之後,本文簡單介紹一款資料庫管理系統(MySQL的兄弟)MariaDB。

如果你有MySQL或其他資料的使用經驗,MariaDB使用起來將非常輕鬆。

本文講解Centos7預設的資料MariaDB,由於是入門系列文章因此不會深入講解,後面有機會在單獨深入。

一、MariaDB產生背景

資料處理是軟體的核心,軟體的本質就是處理資料,包括輸入輸入、處理、輸出。目前資料庫主要分為關係型資料庫和非關係型資料,關係型資料庫主要有:SQLServer、Oracle、MySQL、MariaDB等;非關係型資料庫(NoSQL)包含:Redis、HBase、MongoDB等等。

相信大家都聽過或者用過MySQL資料庫,它是一款市場佔有率非常高的資料庫管理系統,技術成熟、配置步驟相對簡單,而且具有良好的可擴充套件性。

但是由於Oracle公司在2009年收購了MySQL的母公司Sun,因此MySQL專案也隨之納入了Oracle。被收購後,雖然MySQL仍然保持著開源軟體的身份,但是卻申請了多項商業專利,這就不禁讓人擔心其會被逐漸商業化。

一方面,MySQL本身是一款開源軟體,是全球極客、程式設計師等技術高手在開源社群的大旗下的公共智慧結晶,自己的勞動成果被其他公司商業化自然也傷了一大批開源工作者的心,因此由MySQL專案創始者重新研發了一款名為MariaDB的全新資料庫管理系統。

另一方面,各大公司都會存在競爭或利益關係,MySQL被收購後,谷歌、維基百科等公司決定將MySQL資料庫上的業務轉移到 MariaDB 資料庫,紅帽公司也決定在 RHEL 7、CentOS 7 以及最新的 Fedora 系統中,將 MariaDB 作為預設的資料庫管理系統。

這樣一樣,MariaDB也因此快速佔據了市場。MariaDB當前由開源社群進行維護,是MySQL的分支產品,而且幾乎完全相容 MySQL,並增加了一些新的特性,例如對微秒級別的 支援、執行緒池、子查詢優化、程序報告等。

支援windows、linux等不同的作業系統,本文演示在Centos7下進行安裝。

官網:https://mariadb.org/

二、MariaDB安裝

2.1 安裝MariaDB

通過掛載光碟或yum倉庫安裝MariaDB

[root@mariadb ~]# rpm -q mariadb
package mariadb is not installed
[root@mariadb ~]# yum install mariadb mariadb-server
Loaded plugins: fastestmirror, langpacks
...省略部分內容
Dependency Updated:
  mariadb-libs.x86_64 1:5.5.64-1.el7
Complete!
[root@mariadb ~]# rpm -q mariadb
mariadb-5.5.64-1.el7.x86_64
[root@mariadb ~]# rpm -q mariadb-server
mariadb-server-5.5.64-1.el7.x86_64
[root@mariadb ~]# systemctl start mariadb
[root@mariadb ~]# systemctl enable mariadb
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
[root@mariadb ~]# 

安裝完成後,重啟並設為開機啟動,在正式使用之前先按下邊步驟進行初始化

2.2 初始化MariaDB

為了確保資料庫的安全性和正常運轉,需要通過mysql_secure_installation對資料庫程式進行初始化操作。

初始化的工作主要用於設定root的密碼以及刪除一些無關的賬戶資訊,根據提示一路按y即可完成,主要步驟如下圖所示:

[root@mariadb ~]# mysql_secure_installation 

注意:上邊設定的root密碼為MariaDB資料的root賬戶的密碼,而非Centos系統的root賬戶和密碼。

2.3 測試安裝是否成功

在虛擬機器中通過mysql命令登入,並用show databases命令檢視預設有哪些資料庫,如果能檢視說明安裝成功並能正常連線。

[root@mariadb ~]# mysql -u root -p          
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
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 |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> exit
Bye
[root@mariadb ~]#

mysql命令中,-u引數用來指定以root管理員的身份登入,而-p引數用來驗證該使用者在資料庫中的密碼值。

注意事項:

(1)MariaDB預設埠為3306,在防火牆中服務名稱為mysql。因此MariaDB和MySQL不要同時使用。

(2)本例中直接禁止了root的遠端登入,但實際上有可能需要遠端訪問資料,這可以在上邊的初始化操作中設定允許root管理員遠端訪問;然後在設定防火牆,使其放行對資料庫服務的訪問請求。

[root@mariadb ~]# firewall-cmd --permanent --add-service=mysql
success
[root@mariadb ~]# firewall-cmd --reload 
success

2.4 修改密碼

通過set密碼可以修改root使用者的密碼,假設密碼修改為888888

MariaDB [(none)]> set password=password('888888');
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@mariadb ~]# mysql -uroot -p
Enter password: 輸入新密碼登入

修改密碼後,退出再登入就只能用剛設定的新密碼登入了。

三、MariaDB賬戶管理

為了保障資料庫系統的安全性,以及讓其他使用者協同管理資料庫,生產環境一般不用root管理員賬戶。一般是以在MariaDB資料庫管理系統中建立多個專用的資料庫管理賬戶,然後再分配合理的許可權,以滿足工作需求。

3.1 新增賬戶

新增賬戶的語句為:“CREATE USER 使用者名稱@主機名 IDENTIFIED BY '密碼'; ”

MariaDB [(none)]> create user heima@localhost identified by 'heima';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> 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 host,user,password from user where user='heima';
+-----------+-------+-------------------------------------------+
| host      | user  | password                                  |
+-----------+-------+-------------------------------------------+
| localhost | heima | *58613E96F5518C264EA39AA2A57D3DFEB191E343 |
+-----------+-------+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [mysql]>exit

建立使用者後,儲存在mysql資料庫的user表中,可以進行檢視。

3.2 賬戶授權管理

通過上邊的方式建立的heima使用者僅僅是一個普通使用者,沒有資料庫的任何操作許可權。

[root@mariadb ~]# mysql -uheima -pheima
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
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 |
+--------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@mariadb ~]# 

我們用heima賬戶登入,通過查詢看不到mysql資料庫,說明該使用者連資料庫檢視的許可權都沒有。

3.2.1 賬號授權

(1)grant授權語句

授權使用grant語句,語法格式為:"grant 許可權 on 資料庫.表名稱 to 賬戶名@主機名"。

舉幾個例子:

  • 對某個特定資料庫中的特定表單給予授權

GRANT 許可權ON 資料庫.表單名稱TO 賬戶名@主機名

  • 對某個特定資料庫中的所有表單給予授權

GRANT 許可權 ON 資料庫.*TO 賬戶名@主機名

  • 對所有資料庫及所有表單給予授權

GRANT 許可權 ON.TO 賬戶名@主機名

  • 對某個資料庫中的所有表單給予多個授權

GRANT 許可權1,許可權2 ON 資料庫.*TO 賬戶名@主機 名

  • 對所有資料庫及所有表單給予全部授權

GRANT ALL PRIVILEGES ON .TO 賬戶名@主機

(2)對heima賬戶授權

用root管理員賬戶登入,通過grant語句給heima使用者對msyql資料庫user表的增刪改查的授權:

MariaDB [(none)]> show grants for heima@localhost;
+------------------------------+
| Grants for heima@localhost   |
+------------------------------+
| GRANT USAGE ON *.* TO 'heima'@'localhost' IDENTIFIED BY PASSWORD '*58613E96F5518C264EA39AA2A57D3DFEB191E343' |
+------------------------------+
1 row in set (0.01 sec)
MariaDB [(none)]> grant select,update,delete,insert on mysql.user to heima@localhost;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show grants for heima@localhost;               +-----------------+
| Grants for heima@localhost     |
+----------------------------+
| GRANT USAGE ON *.* TO 'heima'@'localhost' IDENTIFIED BY PASSWORD '*58613E96F5518C264EA39AA2A57D3DFEB191E343' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`user` TO 'heima'@'localhost'    |
+-------------------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> 

通過show grants命令可以看到對使用者授予了哪些許可權。

授權完成後,切換到heima使用者,再次檢視資料庫就可以看到剛才授權的mysql資料庫了,並且可以操作mysql資料庫中user表的內容

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.01 sec)
MariaDB [(none)]> 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]> show tables;
+-----------------+
| Tables_in_mysql |
+-----------------+
| user            |
+-----------------+
1 row in set (0.00 sec)
MariaDB [mysql]> select host,user,password from user where user='heima';
+-----------+-------+-------------------------------------------+
| host      | user  | password                                  |
+-----------+-------+-------------------------------------------+
| localhost | heima | *58613E96F5518C264EA39AA2A57D3DFEB191E343 |
+-----------+-------+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [mysql]> exit
Bye
[root@mariadb ~]# 
3.2.2 移除賬戶許可權

當員工離職或其他原因需要移除賬戶許可權時,可以使用root管理員登入,通過revoke語句進行移除

MariaDB [(none)]> revoke select,update,delete,insert on mysql.user from heima@localhost;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show grants for heima@localhost;
+------------+
| Grants for heima@localhost  |
+------------+
| GRANT USAGE ON *.* TO 'heima'@'localhost' IDENTIFIED BY PASSWORD '*58613E96F5518C264EA39AA2A57D3DFEB191E343' |
+-------------+
1 row in set (0.00 sec)
MariaDB [(none)]> exit

四、MariaDB資料庫和表管理

4.0 知識儲備

簡單列舉幾個最基礎的命令

  • 建立資料庫

CREATE DATABASE 資料庫名稱 (大小寫不敏感,大小寫都是可以的)

  • 描述表

DESCRIBE 表單名稱

  • 更新表單中的資料

UPDATE 表單名稱 SET attribute=新值 WHERE attribute>原始 值

  • 指定使用的資料庫

USE 資料庫名稱

  • 顯示當前已有的資料庫

SHOW databases

  • 顯示當前資料庫中的表

SHOW tables

  • 從表單中選中某個記錄值

SELECT * FROM 表單名稱

  • 從表單中刪除某個記錄值

DELETE FROM 表單名 WHERE attribute=值

4.1 建立資料庫

建立一個名為 heima的資料庫

MariaDB [(none)]> create database heima;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;       
+--------------------+
| Database           |
+--------------------+
| information_schema |
| heima              |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]>

4.2 建立資料庫表

切換到剛才建立的heima資料庫,在其中建立user表,包含姓名和年齡兩個欄位

MariaDB [(none)]> use heima
Database changed
MariaDB [heima]> create table user(name char(15),age int);
Query OK, 0 rows affected (0.01 sec)
MariaDB [heima]> show tables;
+-----------------+
| Tables_in_heima |
+-----------------+
| user            |
+-----------------+
1 row in set (0.00 sec)
MariaDB [heima]> describe user;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(15) | YES  |     | NULL    |       |
| age   | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [heima]> 

五、MariaDB表資料管理

資料庫表中資料的查詢分為CRUD,也就是通常所說的增、刪、改、查。

5.1 新增資料

使用insert into語句向heima資料庫的user表中插入資料

MariaDB [heima]> insert into user(name,age) values('heima',18);
Query OK, 1 row affected (0.00 sec)
MariaDB [heima]> select * from user;                           
+-------+------+
| name  | age  |
+-------+------+
| heima |   18 |
+-------+------+
1 row in set (0.00 sec)
MariaDB [heima]> 

5.2 查詢資料

查詢使用select語句,並可以結合where、group by、order by等語句進行綜合查詢。

在user表插入一條資料,然後根據年齡查詢小於1歲的使用者

MariaDB [heima]> insert into user(name,age) values("leo",1);
Query OK, 1 row affected (0.00 sec)
MariaDB [heima]> select * from user where age<2;
+------+------+
| name | age  |
+------+------+
| leo  |    1 |
+------+------+
1 row in set (0.00 sec)
MariaDB [heima]>

5.3 修改資料

修改資料使用update語句,在user表中將heima使用者的年齡修改為19歲

MariaDB [heima]> update user set age=19 where name='heima';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
MariaDB [heima]> select * from user;
+-------+------+
| name  | age  |
+-------+------+
| heima |   19 |
+-------+------+
1 row in set (0.00 sec)
MariaDB [heima]> 

5.4 刪除資料

刪除資料使用delete語句,以下分別演示,刪除使用者名稱為leo的使用者和刪除所有使用者

MariaDB [heima]> delete from user where name='leo';
Query OK, 1 row affected (0.00 sec)
MariaDB [heima]> select * from user;               
+-------+------+
| name  | age  |
+-------+------+
| heima |   19 |
+-------+------+
1 row in set (0.00 sec)
MariaDB [heima]> delete from user;
Query OK, 1 row affected (0.00 sec)
MariaDB [heima]> select * from user;
Empty set (0.00 sec)
MariaDB [heima]> 

六、MariaDB資料庫備份及恢復

為了保證資料的安全性需要定期備份資料庫,一旦出現問題可以通過備份檔案進行恢復。

6.1 資料庫備份

備份資料庫資料使用mysqldump命令,格式為“mysqldump [引數] [資料庫名稱]”。引數與mysql命令基本相同,-u引數用於定義登入資料庫的賬戶名稱,-p引數代表密碼提示符。

下面將 之前建立的heima資料庫中的內容匯出成一個檔案,並儲存到root管理員的家目錄中:

[root@mariadb ~]# mysqldump -u root -p heima> /root/heima-db-back.dump  
Enter password: 
[root@mariadb ~]# ll heima-db-back.dump 
-rw-r--r--. 1 root root 1794 Feb 13 12:48 heima-db-back.dump
[root@mariadb ~]# pwd
/root
[root@mariadb ~]#

此時模擬資料庫故障,直接用root登入MariaDB資料,然後刪除整個heima資料庫

MariaDB [(none)]> drop database heima;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@mariadb ~]# 

6.2 資料庫恢復

要恢復資料庫,先用root登入資料庫,再次建一個空的heima資料庫

MariaDB [(none)]> create database heima;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| heima              |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@mariadb ~]# 

然後再用mysq重定向將剛備份的資料庫檔案匯入mysql命令即可恢復

[root@mariadb ~]# mysql -uroot -p heima</root/heima-db-back.dump
Enter password: 
[root@mariadb ~]# mysql -uroot -p888888
...省略部分內容
MariaDB [(none)]> use heima;
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 [heima]> show tables;
+-----------------+
| Tables_in_heima |
+-----------------+
| user            |
+-----------------+
1 row in set (0.00 sec)
MariaDB [heima]>exit

這樣就完成了資料表中內容的恢復。

下一篇文章將是入門系列的最後一篇文章,綜合講解LNMP環境搭建動態WEB網站