linux學習筆記--工程師技術:郵件服務器,數據庫服務基礎

兩臺虛擬機,均修改防火牆與主機名

虛擬機server0:

# firewall-cmd --set-default-zone=trusted                                #將防火牆默認區域改爲trusted

# echo server0.example.com  >  /etc/hostname              #將主機名改爲server0.example.com

# cat /etc/hostname

虛擬機desktop0:

# firewall-cmd --set-default-zone=trusted

# echo desktop0.example.com  >  /etc/hostname

# cat /etc/hostname

電子郵件服務器的基本功能

– 爲用戶提供電子郵箱存儲空間(用戶名@郵件域名)

– 處理用戶發出的郵件 —— 傳遞給收件服務器

– 處理用戶收到的郵件 —— 投遞到郵箱

     用戶發郵件的協議:  SMTP  端口25

     用戶收郵件的協議:  pop3  端口110    IMAP 端口143        

 

--------------------------------------------------------------------------------------------------------------------------------------------------

虛擬機server0

搭建基本郵件服務器

1. 安裝postfix服務端程序

[root@server0 ~]# rpm -q postfix

postfix-2.10.1-6.el7.x86_64                                           #檢測軟件已安裝

若沒安裝則yum -y install postfix安裝,重啓服務,設置開機自起

2.配置postfix服務,修改配置文件

特別注意:進入vim配置一定要去掉前面的註釋#!!!

[root@server0 ~]# vim /etc/postfix/main.cf

76行   myhostname = server0.example.com        #指定主機名

83行   mydomain = example.com                        #指定後綴域名

這兩行可以不用配置

99行   myorigin = server0.example.com           #默認補全的郵件後綴

116行 inet_interfaces = all                               #允許所有客戶端

164行 mydestination = server0.example.com

                                                                     #判斷郵件後綴爲本域郵件

補充:vim  命令模式      u 撤銷

dd 刪除當前行

有相同行反應命令,後面的會覆蓋前面的,不會執行前面的,例如113行和116行,會執行116行的

3.重起postfix服務,設置爲開機自起

# systemctl restart postfix             #重啓服務                    

# systemctl enable  postfix            #開機自啓

4. 測試郵件的收發

[root@server0 ~]# useradd yg                             #添加發件人yg

[root@server0 ~]# echo 123 | passwd --stdin yg           #更改用戶yg的密碼爲123

[root@server0 ~]# useradd xln                           #添加收件人xln

[root@server0 ~]# echo 123 | passwd --stdin xln         #更改用戶xln的密碼爲123

mail 發信操作

– mail -s '郵件標題' (可以中英文)  -r 發件人    收件人      

郵件標題可以中英文

mail 收信操作

– mail [-u 收件人用戶名]

[root@server0 ~]# mail -s 'test01' -r yg   xln    #yg給xln發送標題爲test01的郵件,下面直接輸入內容

         

 一行行首中只有一個  “.”    的時候,代表結束

[root@server0 ~]# mail -u xln                       #xln收信

 輸入 郵件編號 1 查看郵件內容

 quit或者q退出

------------------------------------------------------------------------------------------------------------------------------------------------

nullclient(空客戶端)郵件服務

nullclient,空客戶端  充當祕書

作用:

– 不提供任何郵箱賬號,因此不需要投遞郵件

– 但是可以爲用戶代發郵件

一、配置desktop0爲郵件服務器

1.配置postfix服務,修改配置文件

[root@desktop0 ~]# vim /etc/postfix/main.cf

99行    myorigin = desktop0.example.com  

116行  inet_interfaces = all          

164行  mydestination = desktop0.example.com

[root@desktop0 ~]# systemctl restart postfix

[root@desktop0 ~]# systemctl enable postfix

二、配置server0爲空客戶端郵件服務器

[root@server0 ~]# vim /etc/postfix/main.cf

 99行     myorigin = desktop0.example.com

 116行   inet_interfaces = localhost                #只允許本機

 164行   mydestination =                                #將投遞域設爲空

 317行   relayhost = [172.25.0.10]                    #指定交給郵件服務器desktop0的IP地址

 

[root@server0 ~]# systemctl restart postfix

三、測試

虛擬機server0上

# echo   abc   |   mail -s Test1 -r  yg   student              

虛擬機desktop0上

# mail -u student

--------------------------------------------------------------------------------------------------------------------------------------------------

數據庫服務基礎

常見的關係型 數據庫管理系統

– 微軟的 SQL Server

– IBM的 DB2

– 甲骨文的 Oracle、MySQL(現被Oracle收購)

– 社區開源版 MariaDB

MySQL和MariaDB只是名字不同,其他所有一模一樣

RHEL7 中的 MariaDB 相關包

– mariadb-server:提供服務端有關的系統程序

              端口號 : 3306

注意:以後如果要爲服務添加端口,最好選擇1024以上的,因爲1024以下的大部分都是被添加了的。

一、部署mariadb數據庫

1.安裝mariadb-server數據庫軟件

[root@server0 ~]# yum -y install mariadb-server

2.啓動mariadb服務

[root@server0 ~]# systemctl restart mariadb   #重啓服務,要等待一段時間,不要ctrl+c退出!

[root@server0 ~]# systemctl enable mariadb     #設置開機自啓

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

數據庫 跟 系統命令操作是兩個完全不同的操作

數據庫是用表格形式表示的,表字段表示列,表記錄表示行!

[root@server0 ~]# mysql            #進入數據庫

MariaDB [(none)]> show databases;           #查看數據庫

MariaDB [(none)]> create database nsd1709;  #創建數據庫nsd1709

MariaDB [(none)]> show databases;        

MariaDB [(none)]> drop database nsd1709;    #刪除數據庫nsd1709

MariaDB [(none)]> show databases;

MariaDB [(none)]> create database nsd;  

MariaDB [(none)]> show databases;

MariaDB [(none)]> quit 或者exit                    #退出數據庫

退出出錯則直接按;退出

MariaDB [(none)]> q

   -> ;

--------------------------------------------------------------------------------------------------------------------------------------------------

數據庫管理員爲root,但與系統用戶root沒有關係,兩個概念不同

爲數據庫賬號修改密碼     退出到系統修改

– mysqladmin [-u用戶名] [-p[舊密碼]] password '新密碼'

[root@server0 ~]# mysqladmin -u  root   password  '123'               #登陸密碼進入mysql的格式

 

[root@server0 ~]# mysql                #進不去,提示要輸入密碼

ERROR 1045 (28000): Access denied for user        'root'@'localhost' (using password: NO)

[root@server0 ~]# mysql  -u  root  -p          #登錄密碼123以密文形式不顯示

Enter password:

[root@server0 ~]# mysql -u root -p123     #免交互登陸,爲了安全最好用上面的操作,加密輸入

禁止監聽,只服務於本機

[root@server0 ~]# vim /etc/my.cnf          #數據庫主配置文件

[mysqld]

skip-networking                                  #跳過網絡監聽

......

[root@server0 ~]# systemctl restart mariadb     #重啓服務

MariaDB [(none)]> 交互指令

– 列出數據庫:show databases;

– 使用/選擇數據庫:use 數據庫名;               #不能回退,直接use切換想要去的庫

– 列出庫裏有哪些表:show tables;           #要進入庫中才能列出

– 創建數據庫:create database 數據庫名;

– 刪除數據庫:drop database 數據庫名;

在沒有導入文件到數據庫之前,添加的庫都爲空

導入/恢復到數據庫            下載http://172.25.254.254/pub/materials/users.sql

– mysql [-u用戶名] [-p[密碼]] 數據庫名  <  備份文件.sql


導出/備份數據庫      

– mysqldump [-u用戶名] [-p[密碼]] 數據庫名  >  備份文件.sql



退出到系統用戶導入

# wget http://172.25.254.254/pub/materials/users.sql  

# mysql -u root -p123 nsd < users.sql                #將備份文件導入到nsd

# mysql -u root -p123

MariaDB [nsd]> use nsd;          #進入nsd庫

MariaDB [nsd]> show tables;      #查看都有那些表格

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

| Tables_in_nsd |

+---------------+                                     #有兩個庫base和location

| base          |

| location      |

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

--------------------------------------------------------------------------------------------------------------------------------------------------

查詢操作 select

# mysql -u root -p123

MariaDB [nsd]> use nsd;

MariaDB [nsd]> select * from base;           #從base庫中查詢信息

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

| id   | name    | password   |

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

|    1 | Tom     | 123        |

|    2 | Barbara | 456        |

|    3 | James   | solicitous |

|    4 | Smith   | tarena     |

|    5 | Barbara | pwd123     |

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

MariaDB [nsd]> select * from location;         #從location庫中查詢信息

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

| id   | city      |

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

|    1 | Beijing   |

|    2 | Paris     |

|    3 | Sunnyvale |

|    4 | Berlin    |

|    5 | Sunnyvale |

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

#base和location兩個表的id表示同一id,比如base中的id 1的tom是住在location的id 1的beijing

MariaDB [nsd]> select id,name from base;   #指定查詢base的id和name

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

| id   | name    |

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

|    1 | Tom     |

|    2 | Barbara |

|    3 | James   |

|    4 | Smith   |

|    5 | Barbara |

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

MariaDB [nsd]> select * from base where name='tom';    #查詢指定tom的所有base中的數據信息

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

| id   | name | password |

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

|    1 | Tom  | 123      |

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

MariaDB [nsd]> select * from location where city='beijing';        #查詢指定beijing地區的所有location中的數據信息

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

| id   | city    |

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

|    1 | Beijing |

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

--------------------------------------------------------------------------------------------------------------------------------------------------

數據庫授權

授權(grant)給某一用戶“增insert、查select、刪delete、改update”權限

insert:插入表記錄

select:查詢表記錄

delete:刪除表記錄

update:修改表記錄

MariaDB [(none)]> 交互指令

– grant 權限列表 (增/查/刪/改) on  數據庫名.表名   to  用戶名@localhost  identified by '密碼';

  當kobe用戶從本地localhost登陸,輸入密碼123,將會獲得庫nsd所有表的查詢權限

# mysql -u root -p123

MariaDB [(none)]> grant select on nsd.* to kobe@localhost identified by '123';

查看MariaDB數據庫中,用戶表信息

MariaDB [mysql]> select user,password from mysql.user;        #查詢mysql庫中用戶表的用戶名和密碼信息

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

| user | password                                  |

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

| root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |

| root |                                           |

| root |                                           |

| root |                                           |

|      |                                           |

|      |                                           |

| kobe | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |

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

--------------------------------------------------------------------------------------------------------------------------------------------------

案例5:使用數據庫查詢

. 在系統 server0 上使用數據庫 nsd,並使用相應的 SQL 查詢以回答下列問題:

SQL:結構化查詢語言

1) 密碼是 solicitous 的人的名字?

> select * from nsd.base where password='solicitous';             #查詢密碼是 solicitous 的所有信息(ID、name)

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

| id   | name  | password   |

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

|    3 | James | solicitous |

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

> select * from nsd.base where password='solicitous' and  id='3';        #密碼和id同時滿足要求

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

| id   | name  | password   |

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

|    3 | James | solicitous |

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

> select * from nsd.base where name='Barbara' or  id='3';       #查詢名字是Barbara或者id是3的數據信息;

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

| id   | name    | password   |

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

|    2 | Barbara | 456        |

|    3 | James   | solicitous |

|    5 | Barbara | pwd123     |

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

2) 有多少人的姓名是 Barbara 同時居住在 Sunnyvale?

> use nsd;

> select * from base,location

where base.name='Barbara' and location.city='Sunnyvale'   and  base.id=location.id;

> select count(*) from base,location    

where base.name='Barbara' and location.city='Sunnyvale' and  base.id=location.id;

                                   #count(*)統計表中的行數

> insert base values(6,'Barbara',123456);                  #插入表記錄(id,name,password)

> insert location values(6,'Sunnyvale');                     #插入表記錄(id,city)

> select * from base;

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

| id   | name    | password   |

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

|    1 | Tom     | 123        |

|    2 | Barbara | 456        |

|    3 | James   | solicitous |

|    4 | Smith   | tarena     |

|    5 | Barbara | pwd123     |

|    6 | Barbara | 123456     |

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

> select * from location;

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

| id   | city      |

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

|    1 | Beijing   |

|    2 | Paris     |

|    3 | Sunnyvale |

|    4 | Berlin    |

|    5 | Sunnyvale |

|    6 | Sunnyvale |

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

. 禁止空密碼root用戶訪問 mariadb 數據庫

> use mysql;

> select user,host,password from user where password='' and user='root';                 #空密碼直接兩個單引號‘’

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

| user | host                | password |

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

| root | server0.example.com |          |

| root | 127.0.0.1           |          |

| root | ::1                 |          |

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

> delete from user where password='' and user='root';                                    #刪除空密碼root用戶

> select user,host,password from user ;

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

| user | host                | password                                  |

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

| root | localhost           | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |

|      | localhost           |                                           |

|      | server0.example.com |                                           |

| kobe | localhost           | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |

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

> desc  user;       #查看錶結構


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章