1. 程式人生 > >MariaDB資料庫服務

MariaDB資料庫服務

一、初始化mariaDB服務程式:

yum install mariadb mariadb-server           //安裝mariaDB

systemctl start mariadb                      //啟動mariadb

systemctl enable mariadb                     //新增到開機啟動項中

初始化資料庫服務程式:

[[email protected] ~]# mysql_secure_installation

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           設定root使用者的資料庫密碼

New password:

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!

設定防火牆對資料庫服務的允許策略:

[[email protected] ~]# firewall-cmd --permanent --add-service=mysql 

success

[[email protected] ~]# firewall-cmd --reload

success

使用root使用者登陸到資料庫中:

[[email protected] ~]# mysql -u root -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 15

Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

MariaDB [(none)]> set password = password('123456');                 //修改當前使用者的資料庫密碼

二、管理資料庫與表單資料:

1、建立一個新的資料庫使用者::CREATE USER 使用者名稱@主機名 IDENTIFIED BY '密碼';

MariaDB [(none)]> create user [email protected] IDENTIFIED BY '123456';

MariaDB [mysql]> show databases;                               //檢視資料庫

MariaDB [(none)]> use mysql;              //進入到mysql資料庫

MariaDB [mysql]> select host,user,password from user where user='cw';     

                                        //檢視新建立的使用者名稱、主機、姓名與密碼資訊

2、修改新建使用者許可權:(預設許可權只能訪問)

 

資料庫GRANT命令的授權操作常用方案:

命令

作用

GRANT 許可權 ON 資料庫.表單名稱 TO 使用者名稱@主機名

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

GRANT 許可權 ON 資料庫.* TO 使用者名稱@主機名

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

GRANT 許可權 ON *.* TO 使用者名稱@主機名

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

GRANT 許可權 1,許可權 2 ON 資料庫.* TO 使用者名稱@主機名

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

GRANT ALL PRIVILEGES ON *.* TO 使用者名稱@主機名

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

舉例:

MariaDB [(none)]> GRANT SELECT,UPDATE,DELETE,INSERT on mysql.user to [email protected];

                                          //給予cw使用者對mysql資料庫的user表單查詢、更新、刪除、插入許可權;

MariaDB [(none)]> show grants for [email protected];

//檢視cw使用者當前的授權

MariaDB [(none)]> revoke SELECT,UPDATE,DELETE,INSERT on mysql.user from [email protected];

      //移除cw使用者對musql資料庫的user表單查詢、更新、刪除、插入許可權

3、建立資料庫與表單

常用的資料庫表單管理命令:

用法

作用

CREATE database 資料庫名稱;

建立新的資料庫

DESCRIBE 表單名稱;

描述表單

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

更新表單中的資料

USE 資料庫名稱;

指定使用的資料庫

SHOW databases;

顯示當前已有的資料庫

SHOW tables;

顯示當前資料庫中的表單

SELECT * FROM 表單名稱;

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

DELETE FROM 表單名 WHERE attribute=值;

從表單中刪除某個記錄值

MariaDB [mysql]> create database linuxprobe;

MariaDB [mysql]> use linuxprobe;

MariaDB [linuxprobe]> create table mybook(name char(15),price int,pages int);

MariaDB [linuxprobe]> describe mybook;          //檢視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)

4、管理表單資料: 向表單中插入新的書籍資料:

MariaDB [linuxprobe]> INSERT INTO mybook(name,price,pages) VALUES('linuxprobe','60',518);

MariaDB [linuxprobe]> select * from mybook;               //查看錶中資料

+------------+-------+-------+

| name       | price | pages |

+------------+-------+-------+

| linuxprobe |    60 |   518 |

+------------+-------+-------+

1 row in set (0.01 sec)

MariaDB [linuxprobe]> update mybook set price=55;           //將價格修改為55元;

MariaDB [linuxprobe]> delete from mybook;                  //刪除書籍表單中的內容;

5、where命令用於資料庫匹配查詢:

引數

作用

=

相等

<>或!=

不相等

大於

小於

>=

大於或等於

<=

小於或等於

BETWEEN

在某個範圍內

LIKE

搜尋一個例子

IN

在列中搜索多個值

MariaDB [linuxprobe]> select * from mybook;

+-------------+-------+-------+

| name        | price | pages |

+-------------+-------+-------+

| linuxprobe1 |    55 |   518 |

| linuxprobe2 |    60 |   518 |

| linuxprobe3 |    70 |   518 |

| linuxprobe4 |    75 |   518 |

| linuxprobe5 |    80 |   518 |

+-------------+-------+-------+

5 rows in set (0.00 sec)

檢視價格大於75的書籍:

MariaDB [linuxprobe]> select * from mybook where price>75;   

搜尋價格不等於80的書籍:

MariaDB [linuxprobe]> select * from mybook where price<>80;

MariaDB [linuxprobe]> select * from mybook where price!=80;

6、資料庫備份與恢復:

mysqldump命令用於備份資料庫數量,格式為“mysqldump [引數] [資料庫名稱]”。

引數

作用

-u

資料庫的使用者名稱稱

-p

密碼提示符

--no-data

至備份資料庫的描述結構,而不要資料

--local-all-tables

備份完成後將不再允許修改資料

將書籍資料庫檔案匯出到家目錄:

[[email protected] ~]# mysqldump -uroot -p123456 linuxprobe > /root/linuxprobeDB_bak.sql

刪除書籍資料庫:

MariaDB [(none)]> drop database linuxprobe;

建立一個空的資料庫:

MariaDB [(none)]> create database linuxprobe_bak;

匯入剛剛備份的資料庫至linuxprobe_bak庫中:

[[email protected] ~]# mysql -uroot -p123456 linuxprobe_bak < /root/linuxprobeDB_bak.sql