一文搞懂MySQL兄弟數據庫MariaDB的安裝和使用

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

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


一文搞懂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

<code>[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 ~]# /<code>

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

2.2 初始化MariaDB

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

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

<code>[root@mariadb ~]# mysql_secure_installation /<code>


一文搞懂MySQL兄弟數據庫MariaDB的安裝和使用


注意:上邊設置的root密碼為MariaDB數據的root賬戶的密碼,而非Centos系統的root賬戶和密碼。

2.3 測試安裝是否成功

在虛擬機中通過mysql命令登錄,並用show databases命令查看默認有哪些數據庫,如果能查看說明安裝成功並能正常連接。

<code>[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 ~]#/<code>

mysql命令中,-u參數用來指定以root管理員的身份登錄,而-p參數用來驗證該用戶在數據庫中的密碼值。

注意事項:

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

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

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

2.4 修改密碼

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

<code>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: 輸入新密碼登錄/<code>

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

三、MariaDB賬戶管理

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

3.1 添加賬戶

添加賬戶的語句為:“CREATE USER 用戶名@主機名 IDENTIFIED BY '密碼'; ”

<code>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/<code>

創建用戶後,存儲在mysql數據庫的user表中,可以進行查看。

通過上邊的方式創建的heima用戶僅僅是一個普通用戶,沒有數據庫的任何操作權限。

<code>[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 ~]# /<code>

我們用heima賬戶登錄,通過查詢看不到mysql數據庫,說明該用戶連數據庫查看的權限都沒有。

授權使用grant語句,語法格式為:"grant 權限 on 數據庫.表名稱 to 賬戶名@主機名"。

舉幾個例子:

  • 對某個特定數據庫中的特定表單給予授權

GRANT 權限ON 數據庫.表單名稱TO 賬戶名@主機名

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

GRANT 權限 ON 數據庫.*TO 賬戶名@主機名

  • 對所有數據庫及所有表單給予授權

GRANT 權限 ON.TO 賬戶名@主機名

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

GRANT 權限1,權限2 ON 數據庫.*TO 賬戶名@主機 名

  • 對所有數據庫及所有表單給予全部授權

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

用root管理員賬戶登錄,通過grant語句給heima用戶對msyql數據庫user表的增刪改查的授權:

<code>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)]> /<code>

通過show grants命令可以看到對用戶授予了哪些權限。

授權完成後,切換到heima用戶,再次查看數據庫就可以看到剛才授權的mysql數據庫了,並且可以操作mysql數據庫中user表的內容

<code>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 ~]# /<code>
3.2.2 移除賬戶權限

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

<code>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/<code>

四、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的數據庫

<code>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)]>/<code>

4.2 創建數據庫表

切換到剛才創建的heima數據庫,在其中創建user表,包含姓名和年齡兩個字段

<code>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]> /<code>

五、MariaDB表數據管理

數據庫表中數據的查找分為CRUD,也就是通常所說的增、刪、改、查。

5.1 添加數據

使用insert into語句向heima數據庫的user表中插入數據

<code>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]> /<code>

5.2 查詢數據

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

在user表插入一條數據,然後根據年齡查詢小於1歲的用戶

<code>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]>/<code>

5.3 修改數據

修改數據使用update語句,在user表中將heima用戶的年齡修改為19歲

<code>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]> /<code>

5.4 刪除數據

刪除數據使用delete語句,以下分別演示,刪除用戶名為leo的用戶和刪除所有用戶

<code>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]> /<code>

六、MariaDB數據庫備份及恢復

為了保證數據的安全性需要定期備份數據庫,一旦出現問題可以通過備份文件進行恢復。

6.1 數據庫備份

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

下面將 之前創建的heima數據庫中的內容導出成一個文件,並保存到root管理員的家目錄中:

<code>[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 ~]#/<code>

此時模擬數據庫故障,直接用root登錄MariaDB數據,然後刪除整個heima數據庫

<code>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 ~]# /<code>

6.2 數據庫恢復

要恢復數據庫,先用root登錄數據庫,再次建一個空的heima數據庫

<code>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 ~]# /<code>

然後再用mysq重定向將剛備份的數據庫文件導入mysql命令即可恢復

<code>[root@mariadb ~]# mysql -uroot -p heimaEnter 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/<code>

這樣就完成了數據表中內容的恢復。

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


分享到:


相關文章: