前面講完Linux下一系列服務的配置和使用以後,本文簡單介紹一款數據庫管理系統(MySQL的兄弟)MariaDB。mysql
若是你有MySQL或其餘數據的使用經驗,MariaDB使用起來將很是輕鬆。linux
本文講解Centos7默認的數據MariaDB,因爲是入門系列文章所以不會深刻講解,後面有機會在單獨深刻。程序員
數據處理是軟件的核心,軟件的本質就是處理數據,包括輸入輸入、處理、輸出。目前數據庫主要分爲關係型數據庫和非關係型數據,關係型數據庫主要有:SQLServer、Oracle、MySQL、MariaDB等;非關係型數據庫(NoSQL)包含:Redis、HBase、MongoDB等等。sql
相信你們都聽過或者用過MySQL數據庫,它是一款市場佔有率很是高的數據庫管理系統,技術成熟、配置步驟相對簡單,並且具備良好的可擴展性。數據庫
可是因爲Oracle公司在2009年收購了MySQL的母公司Sun,所以MySQL項目也隨之歸入了Oracle。被收購後,雖然MySQL仍然保持着開源軟件的身份,可是卻申請了多項商業專利,這就不由讓人擔憂其會被逐漸商業化。windows
一方面,MySQL自己是一款開源軟件,是全球極客、程序員等技術高手在開源社區的大旗下的公共智慧結晶,本身的勞動成果被其餘公司商業化天然也傷了一大批開源工做者的心,所以由MySQL項目創始者從新研發了一款名爲MariaDB的全新數據庫管理系統。安全
另外一方面,各大公司都會存在競爭或利益關係,MySQL被收購後,谷歌、維基百科等公司決定將MySQL數據庫上的業務轉移到 MariaDB 數據庫,紅帽公司也決定在 RHEL 七、CentOS 7 以及最新的 Fedora 系統中,將 MariaDB 做爲默認的數據庫管理系統。ide
這樣同樣,MariaDB也所以快速佔據了市場。MariaDB當前由開源社區進行維護,是MySQL的分支產品,並且幾乎徹底兼容 MySQL,並增長了一些新的特性,例如對微秒級別的 支持、線程池、子查詢優化、進程報告等。測試
支持windows、linux等不一樣的操做系統,本文演示在Centos7下進行安裝。優化
經過掛載光盤或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 ~]#
安裝完成後,重啓並設爲開機啓動,在正式使用以前先按下邊步驟進行初始化
爲了確保數據庫的安全性和正常運轉,須要經過mysql_secure_installation對數據庫程序進行初始化操做。
初始化的工做主要用於設置root的密碼以及刪除一些無關的帳戶信息,根據提示一路按y便可完成,主要步驟以下圖所示:
[root@mariadb ~]# mysql_secure_installation
注意:上邊設置的root密碼爲MariaDB數據的root帳戶的密碼,而非Centos系統的root帳戶和密碼。
在虛擬機中經過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
經過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: 輸入新密碼登陸
修改密碼後,退出再登陸就只能用剛設置的新密碼登陸了。
爲了保障數據庫系統的安全性,以及讓其餘用戶協同管理數據庫,生產環境通常不用root管理員帳戶。通常是以在MariaDB數據庫管理系統中建立多個專用的數據庫管理帳戶,而後再分配合理的權限,以知足工做需求。
添加帳戶的語句爲:「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表中,能夠進行查看。
經過上邊的方式建立的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數據庫,說明該用戶連數據庫查看的權限都沒有。
(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 ~]#
當員工離職或其餘緣由須要移除帳戶權限時,可使用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
簡單列舉幾個最基礎的命令
CREATE DATABASE 數據庫名稱 (大小寫不敏感,大小寫都是能夠的)
DESCRIBE 表單名稱
UPDATE 表單名稱 SET attribute=新值 WHERE attribute>原始 值
USE 數據庫名稱
SHOW databases
SHOW tables
SELECT * FROM 表單名稱
DELETE FROM 表單名 WHERE attribute=值
建立一個名爲 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)]>
切換到剛纔建立的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]>
數據庫表中數據的查找分爲CRUD,也就是一般所說的增、刪、改、查。
使用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]>
查詢使用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]>
修改數據使用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]>
刪除數據使用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]>
爲了保證數據的安全性須要按期備份數據庫,一旦出現問題能夠經過備份文件進行恢復。
備份數據庫數據使用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 ~]#
要恢復數據庫,先用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網站。