使用MariaDB數據庫管理系統

學習總結所用《 Linux就該這麼學 》

數據庫管理系統

  • MariaDB數據庫管理系統由MySQL項目創始者重新研發
  • MariaDB由開源社區進行維護,不受商業專利限制
  • MariaDB和MySQL在性能上基本保持一致,兩者的操作命令也十分相似
  • 相較於MySQL,MariaDB數據庫管理系統有了很多新鮮的擴展特性,例如對微秒級別的支持、線程池、子查詢優化、進程報告等

初始化MariaDB服務

  1. 安裝MariaDB服務,啓動並加入開機啓動項

    [root@localhost ~]# yum install -y mariadb mariadb-server
    [root@localhost ~]# systemctl start mariadb
    [root@localhost ~]# systemctl enable mariadb
    ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
    
  2. 初始化數據庫mysql_secure_installation(5個步驟)

    [root@localhost ~]# mysql_secure_installation 
    /usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
    
    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 
    New password: (輸入要爲root管理員設置的數據庫密碼)
    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] y (禁止root管理員從遠程登錄)
     ... Success!
    
    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!
    

    如果需要讓root管理員遠程訪問數據庫,則在初始化操作時設置允許root管理員從遠程訪問(n),再設置防火牆,使其放行對數據庫服務程序的訪問請求

    數據庫服務程序默認會佔用3306端口,在防火牆策略中服務名稱統一叫作mysql

    [root@localhost ~]# firewall-cmd --permanent --add-service=mysql
    success
    [root@localhost ~]# firewall-cmd --reload
    success
    
  3. 首次登錄MariaDB數據庫

    [root@localhost ~]# mysql -u root -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 16
    Server version: 5.5.35-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]> 
    

    -u:指定登錄的身份 -p:指定登錄用戶的登錄密碼

  4. 查看數據庫管理系統中有哪些數據庫

    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    3 rows in set (0.00 sec)
    

    數據庫命令必須以分號(;)結尾

  5. 修改root管理員的密碼爲123456,舊密碼登錄失敗,新密碼登錄成功

    MariaDB [(none)]> set password = password("123456");
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> exit
    Bye
    [root@localhost ~]# mysql -u root -p
    Enter password: 
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    [root@localhost ~]# mysql -u root -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 18
    Server version: 5.5.35-MariaDB MariaDB Server
    

管理用戶以及授權

grant命令的常見格式以及解釋

命令 作用
GRANT 權限 ON 數據庫.表單名稱 TO 用戶名@主機名 對某個特定數據庫中的特定表單給予授權
GRANT 權限 ON 數據庫.* TO 用戶名@主機名 對某個特定數據庫中的所有表單給予授權
GRANT 權限 ON . TO 用戶名@主機名 對所有數據庫及所有表單給予授權
GRANT 權限1,權限2 ON 數據庫.* TO 用戶名@主機名 對某個數據庫中的所有表單給予多個授權
GRANT ALL PRIVILEGES ON . TO 用戶名@主機名 對所有數據庫及所有表單給予全部授權(需謹慎操作)
  1. 使用root管理員創建數據庫管理賬戶lili ,主機名是localhost,密碼爲lili123

    MariaDB [(none)]> create user lili@localhost identified by 'lili123';
    Query OK, 0 rows affected (0.00 sec)
    

    此時,用戶lili還沒有數據庫的任何操作權限,甚至沒法查看完整的數據庫列表

  2. 查詢賬戶lili的主機名稱、賬戶名稱以及經過加密的密碼值信息

    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="lili";
    +-----------+------+-------------------------------------------+
    | HOST      | USER | PASSWORD                                  |
    +-----------+------+-------------------------------------------+
    | localhost | lili | *E80B45F4C05E4CEE904BF4BF25035B25529A03A9 |
    +-----------+------+-------------------------------------------+
    1 row in set (0.00 sec)
    
  3. 以root管理員身份授予用戶lili查詢、更新、刪除以及插入等權限

    MariaDB [mysql]> grant select,update,delete,insert on mysql.user to lili@localhost;
    Query OK, 0 rows affected (0.01 sec)
    
  4. 查看用戶lili的權限

    MariaDB [mysql]> show grants for lili@localhost;
    +-------------------------------------------------------------------------------------------------------------+
    | Grants for lili@localhost                                                                                   |
    +-------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'lili'@'localhost' IDENTIFIED BY PASSWORD '*E80B45F4C05E4CEE904BF4BF25035B25529A03A9' |
    | GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`user` TO 'lili'@'localhost'                                |
    +-------------------------------------------------------------------------------------------------------------+
    2 rows in set (0.00 sec)
    
  5. 切換到用戶lili,查看mysql數據庫以及表單user(其餘表單因無權限被繼續隱藏)

    MariaDB [mysql]> exit
    Bye
    [root@localhost ~]# mysql -u lili -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 25
    Server version: 5.5.35-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2013, Oracle, Monty Program 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              |
    +--------------------+
    2 rows in set (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]> show tables;
    +-----------------+
    | Tables_in_mysql |
    +-----------------+
    | user            |
    +-----------------+
    1 row in set (0.00 sec)
    
  6. 切回root管理員,移除剛纔的授權,再查看用戶信息

    MariaDB [mysql]> exit
    Bye
    [root@localhost ~]# mysql -u root -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 26
    Server version: 5.5.35-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]> revoke select,update,delete,insert on mysql.user from lili@localhost;
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> show grants for lili@localhost;
    +-------------------------------------------------------------------------------------------------------------+
    | Grants for lili@localhost                                                                                   |
    +-------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'lili'@'localhost' IDENTIFIED BY PASSWORD '*E80B45F4C05E4CEE904BF4BF25035B25529A03A9' |
    +-------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    

創建數據庫與表單

創建數據庫的命令以及作用

用法 作用
CREATE database 數據庫名稱。 創建新的數據庫
DESCRIBE 表單名稱; 描述表單
UPDATE 表單名稱 SET attribute=新值 WHERE attribute > 原始值; 更新表單中的數據
USE 數據庫名稱; 指定使用的數據庫
SHOW databases; 顯示當前已有的數據庫
SHOW tables; 顯示當前數據庫中的表單
SELECT * FROM 表單名稱; 從表單中選中某個記錄值
DELETE FROM 表單名 WHERE attribute=值; 從表單中刪除某個記錄值
  1. 創建數據庫test,並查看數據庫列表

    MariaDB [(none)]> create database test;
    Query OK, 1 row affected (0.00 sec)
    
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    4 rows in set (0.06 sec)
    
  2. 在數據庫test中創建數據表單mybook,並進行表單初始化(定義存儲數據內容的結構)

    MariaDB [(none)]> use test;
    Database changed
    MariaDB [test]> create table mybook (name char(15),price int,pages int);
    Query OK, 0 rows affected (0.02 sec)
    

管理表單及數據

where命令的參數及作用

參數 作用
= 相等
<>或!= 不相等
> 大於
< 小於
>= 大於或等於
<= 小於或等於
BETWEEN 在某個範圍內
LIKE 搜索一個例子
IN 在列中搜索多個值
  1. 向數據表單mybook插入一條圖書信息

    MariaDB [test]> insert into mybook (name,price,pages) values ('linux','100','800');
    Query OK, 1 row affected (0.00 sec)
    
    MariaDB [test]> select * from mybook;
    +-------+-------+-------+
    | name  | price | pages |
    +-------+-------+-------+
    | linux |   100 |   800 |
    +-------+-------+-------+
    1 row in set (0.00 sec)
    
  2. 修改數據表單mybook的price爲50

    MariaDB [test]> update mybook set price=50;
    Query OK, 1 row affected (0.04 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    MariaDB [test]> select name,price from mybook;
    +-------+-------+
    | name  | price |
    +-------+-------+
    | linux |    50 |
    +-------+-------+
    1 row in set (0.01 sec)
    
  3. 刪除數據表單mybook中的所有內容

    MariaDB [test]> delete from mybook;
    Query OK, 1 row affected (0.02 sec)
    
    MariaDB [test]> select * from mybook;
    Empty set (0.00 sec)
    
  4. 向數據表單mybook插入4條圖書信息

    MariaDB [test]> insert into mybook (name,price,pages) values
        -> ('linux1','30','500'),
        -> ('linux2','50','500'),
        -> ('linux3','80','500'),
        -> ('linux4','100','500');
    Query OK, 4 rows affected (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    
    MariaDB [test]> select * from mybook;
    +--------+-------+-------+
    | name   | price | pages |
    +--------+-------+-------+
    | linux1 |    30 |   500 |
    | linux2 |    50 |   500 |
    | linux3 |    80 |   500 |
    | linux4 |   100 |   500 |
    +--------+-------+-------+
    4 rows in set (0.00 sec)
    
  5. 在mybook表單中查找出價格大於75元且價格不等於80元的圖書

    MariaDB [test]> select * from mybook where price>75 and price!=80;
    +--------+-------+-------+
    | name   | price | pages |
    +--------+-------+-------+
    | linux4 |   100 |   500 |
    +--------+-------+-------+
    1 row in set (0.01 sec)
    

數據庫的備份及恢復

  1. 將數據庫test中的內容導出成一個文件,並保存到root管理員的家目錄中

    [root@localhost ~]# mysqldump -u root -p test > /root/testDB.dump
    Enter password: 
    [root@localhost ~]# ls
    anaconda-ks.cfg  initial-setup-ks.cfg  testDB.dump
    

    -u:登錄數據庫的賬戶名稱 -p:密碼提示符

  2. 進入MariaDB數據庫管理系統,徹底刪除數據庫test,再重新建立數據庫test

    MariaDB [(none)]> drop database test;
    Query OK, 1 row affected (0.06 sec)
    
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    3 rows in set (0.00 sec)
    
    MariaDB [(none)]> create database test;
    Query OK, 1 row affected (0.00 sec)
    
  3. 數據庫恢復

    數據庫登錄時在後面加數據庫名可直接進入到數據庫中

    [root@localhost ~]# mysql -u root -p test < /root/testDB.dump
    Enter password: 
    [root@localhost ~]# mysql -u root -p test
    Enter password: 
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 10
    Server version: 5.5.35-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [test]> show tables;
    +----------------+
    | Tables_in_test |
    +----------------+
    | mybook         |
    +----------------+
    1 row in set (0.00 sec)
    
    MariaDB [test]> describe mybook;
    +-------+----------+------+-----+---------+-------+
    | Field | Type     | Null | Key | Default | Extra |
    +-------+----------+------+-----+---------+-------+
    | name  | char(15) | YES  |     | NULL    |       |
    | price | int(11)  | YES  |     | NULL    |       |
    | pages | int(11)  | YES  |     | NULL    |       |
    +-------+----------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章