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
通過挂載CD光牒或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!
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
2.4 修改密碼
通過set密碼可以修改root使用者的密碼,假設密碼修改為888888
MariaDB [(none)]> set password=password('888888');
Query OK, 0 rows affected (0.00 sec)
[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';
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
Your MariaDB connection id is 15
我們用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;
MariaDB [(none)]> show grants for heima@localhost; +-----------------+
GRANT SELECT, INSERT, UPDATE, DELETE ON . TO 'heima'@'localhost' |
2 rows in set (0.00 sec)
MariaDB [(none)]>
通過show grants指令可以看到對使用者授予了哪些權限。
授權完成後,切換到heima使用者,再次檢視資料庫就可以看到剛才授權的mysql資料庫了,并且可以操作mysql資料庫中user表的内容
2 rows in set (0.01 sec)
MariaDB [(none)]> use mysql;
MariaDB [mysql]> show tables; |
---|
Tables_in_mysql |
MariaDB [mysql]> exit
3.2.2 移除賬戶權限
當員工離職或其他原因需要移除賬戶權限時,可以使用root管理者登入,通過revoke語句進行移除
MariaDB [(none)]> revoke select,update,delete,insert on mysql.user from heima@localhost;
四、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)
4 rows in set (0.00 sec)
MariaDB [(none)]>
4.2 建立資料庫表
切換到剛才建立的heima資料庫,在其中建立user表,包含姓名和年齡兩個字段
MariaDB [(none)]> use heima
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 |
MariaDB [heima]> describe user; | |||||
---|---|---|---|---|---|
Field | Type | Null | Key | Default | Extra |
name | char(15) | YES | NULL | ||
age | int(11) |
MariaDB [heima]>
五、MariaDB表資料管理
資料庫表中資料的查找分為CRUD,也就是通常所說的增、删、改、查。
5.1 添加資料
使用insert into語句向heima資料庫的user表中插入資料
MariaDB [heima]> insert into user(name,age) values('heima',18);
MariaDB [heima]> select * from user; | |
---|---|
18 |
5.2 查詢資料
查詢使用select語句,并可以結合where、group by、order by等語句進行綜合查詢。
在user表插入一條資料,然後根據年齡查詢小于1歲的使用者
MariaDB [heima]> insert into user(name,age) values("leo",1);
MariaDB [heima]> select * from user where age<2; | |
---|---|
leo | 1 |
MariaDB [heima]>
5.3 修改資料
修改資料使用update語句,在user表中将heima使用者的年齡修改為19歲
MariaDB [heima]> update user set age=19 where name='heima';
Rows matched: 1 Changed: 1 Warnings: 0
19 |
5.4 删除資料
删除資料使用delete語句,以下分别示範,删除使用者名為leo的使用者和删除所有使用者
MariaDB [heima]> delete from user where name='leo';
MariaDB [heima]> delete from user;
Empty set (0.00 sec)
六、MariaDB資料庫備份及恢複
為了保證資料的安全性需要定期備份資料庫,一旦出現問題可以通過備份檔案進行恢複。
6.1 資料庫備份
備份資料庫資料使用mysqldump指令,格式為“mysqldump [參數] [資料庫名稱]”。參數與mysql指令基本相同,-u參數用于定義登入資料庫的賬戶名稱,-p參數代表密碼提示符。
下面将 之前建立的heima資料庫中的内容導出成一個檔案,并儲存到root管理者的家目錄中:
[root@mariadb ~]# mysqldump -u root -p heima> /root/heima-db-back.dump
[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資料,然後删除整個heima資料庫
MariaDB [(none)]> drop database heima;
6.2 資料庫恢複
要恢複資料庫,先用root登入資料庫,再次建一個空的heima資料庫
然後再用mysq重定向将剛備份的資料庫檔案導入mysql指令即可恢複
[root@mariadb ~]# mysql -uroot -p heima/heima-db-back.dump
[root@mariadb ~]# mysql -uroot -p888888
MariaDB [(none)]> use heima;
MariaDB [heima]>exit
這樣就完成了資料表中内容的恢複。
下一篇文章将是入門系列的最後一篇文章,綜合講解LNMP環境搭建動态WEB網站。
作者:黑馬騰雲
原文位址
https://www.cnblogs.com/heimatengyun/p/12572369.html