工程师技术(二):postfix基础邮件服务、postfix空客户端邮件服务、搭建mariadb数据库系统、配置一个数据库、使用数据库查询

一、postfix基础邮件服务

目标:

本例要求在虚拟机server0上配置 postfix 基础服务,具体要求如下:

    1> 监听本机的所有接口
    2> 将邮件域和邮件服务主机名都改为 example.com

然后在server0上使用mail命令测试发信/收信操作:

    1> 由 root 给本机用户 mike 发一封测试邮件
    2> 查收用户 mike 的邮箱,读取邮件内容,确保是从 root@example.com 发过来的

方案:

电子邮箱:1234567@qq.com表示在互联网区域qq.com内的一台邮件服务器上属于用户1234567的一个电子邮箱(目录)。

postfix发信服务(TCP 25,SMTP)的功能:

    1> 为用户提供电子邮箱
    2> 为邮箱用户向其他邮件服务器发送邮件
    3> 为邮箱用户投递/存储收到的邮件
dovecot取信服务(TCP 110/143,POP3/IMAP)的功能:为邮箱用户提取邮件。

步骤:

步骤一:配置postfix基础邮件服务

1)安装postfix软件包

    [root@server0 ~]# yum  -y  install  postfix
    .. ..

2)调整邮件服务配置

    [root@server0 ~]# vim  /etc/postfix/main.cf
    .. ..
    inet_interfaces = all                         //监听接口
    mydomain = example.com                          //邮件域
    myhostname = example.com                          //本服务器主机名

3)启动postfix服务

    [root@server0 ~]# systemctl  restart  postfix

4)查看邮件服务监听状态

    [root@server0 ~]# netstat  -antpu | grep  :25
    tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      1739/master         
    tcp6       0      0 :::25                   :::*                    LISTEN      1739/master  

步骤二:使用mail命令发信/收信

1)给用户root发一封测试邮件

    [root@server0 ~]# echo  '1111'  |  mail  -s  'mail1'  root

2)由管理员收取指定用户root的邮件

    [root@server0 ~]# mail  -u  root
    Heirloom Mail version 12.5 7/5/10.  Type ? for help.
    "/var/mail/root": 1 message 1 new
    >N  1 root                  Sat Nov 26 17:40  18/532   "mail"
    & 1                                                  //读取第1封邮件内容
    Message  1:
    From root@example.com  Sat Nov 26 17:40:06 2016
    Return-Path: <root@example.com>
    X-Original-To: root
    Delivered-To: root@example.com
    Date: Sat, 26 Nov 2016 17:40:06 +0800
    To: root@example.com
    Subject: mail1                                      //检查邮件标题
    User-Agent: Heirloom mailx 12.5 7/5/10
    Content-Type: text/plain; charset=us-ascii
    From: root@example.com (root)
    Status: R
    1111                                              //检查邮件内容
    & q                                                  //退出mail程序
    Held 1 message in /var/mail/root
    [root@server0 ~]#

二、postfix空客户端邮件服务

目标:

本例要求初始化后端邮件服务器desktop0.example.com,操作如下:

   lab  smtp-nullclient  setup

然后将虚拟机server0配置为空客户端邮件服务器,具体要求如下:

   1> 此系统不接收外部发送来的邮件,本地发送的任何邮件都会自动路由到 smtp0.example.com
   2> 所发出的邮件显示来自于 desktop0.example.com
   3> 在 server0 上发送一封标题为 Test1 的邮件给本地用户 student ;实际结果将由 desktop0 上的本地用户 student 接收到此邮件

方案:

postfix空客户端邮件服务器的功能:

   1> 不为用户提供电子邮箱
   2> 为邮箱用户向其他邮件服务器发送邮件
   3> 不接受(投递/存储)发送给给本服务器的邮件

空客户端邮件服务器的工作环境:

客户端 ---> 空客户端邮件服务器(发)---> 普通邮件服务器(发/收)

步骤:

步骤一:将server0配置为空客户端邮件服务器

1)重建postfix服务配置

删除配置文件/etc/postfix/main.cf,然后重装postfix软件包:

    [root@server0 ~]# rm  -rf  /etc/postfix/main.cf
    [root@server0 ~]# yum  -y  reinstall  postfix
    .. ..

2)修改main.cf配置文件,调整或修改现有配置

    [root@server0 ~]# vim  /etc/postfix/main.cf
    #mydomain = example.com                              //移除上一案例的配置
    #myhostname = example.com
    relayhost = [smtp0.example.com]                     //目标邮件服务器
    inet_interfaces = loopback-only                      //仅本机
    myorigin = desktop0.example.com                      //发件来源域
    mynetworks = 127.0.0.0/8  [::1]/128                  //信任网络
    mydestination =                                     //将投递域设为空

3)启动postfix服务

    [root@server0 ~]# systemctl  restart  postfix

4)查看邮件服务监听状态

    [root@server0 ~]# netstat  -antpu | grep  :25
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2054/master         
    tcp6       0      0 ::1:25                  :::*                    LISTEN      2054/master

步骤二:将desktop0配置为后端邮件服务器

1)执行 lab smtp-nullclient setup

    [root@desktop0 ~]# lab  smtp-nullclient  setup
    Setting up desktop machine...
    Setting up dovecot...
    Generating new dovecot certificate...
    Setting up postfix... Generating new postfix certificates...
    Setting up firewall...
    Setting up user student...
    Installing httpd to share cert...

2)确认发信服务(postfix)

    [root@desktop0 ~]# netstat  -antpu | grep  :25
    tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      31366/master        
    tcp6       0      0 :::25                   :::*                    LISTEN      31366/master       

3)确认取信服务(dovecot)

    [root@desktop0 ~]# netstat  -antpu | grep  dovecot
    tcp        0      0 0.0.0.0:143             0.0.0.0:*               LISTEN      31229/dovecot       
    .. ..

步骤三:使用mail命令测试空客户端邮件服务器

1)在server0(空客户端邮件服务器)上给本机用户student发信

    [root@server0 ~]# echo  'Mail Data.'  |  mail  -s  'Test1'  student
    [root@server0 ~]# mail  -u  student                  //本机用户并不会收到邮件
    No mail for student

2)在desktop0(后端邮件服务器)上的同名用户student可以收到信

    [root@desktop0 ~]# mail  -u  student
    Heirloom Mail version 12.5 7/5/10.  Type ? for help.
    "/var/mail/student": 1 message 1 new
    >N  1 root                  Sat Nov 26 18:29  21/833   "Test"
    & 1                                                  //读取第1封邮件内容
    Message  1:
    From root@desktop0.example.com  Sat Nov 26 18:29:08 2016
    Return-Path: <root@desktop0.example.com>
    X-Original-To: student@desktop0.example.com
    Delivered-To: student@desktop0.example.com
    Date: Sat, 26 Nov 2016 18:29:08 +0800
    To: student@desktop0.example.com
    Subject: Test1                                      //检查邮件标题
    User-Agent: Heirloom mailx 12.5 7/5/10
    Content-Type: text/plain; charset=us-ascii
    From: root@desktop0.example.com (root)
    Status: R
    Mail Data.                                         //检查邮件内容
    & q                                                  //退出mail程序
    Held 1 message in /var/mail/student
    [root@desktop0 ~]#

三、搭建mariadb数据库系统

目标:

本例要求在虚拟机server0上安装 MariaDB 数据库系统:

   1> 安装 mariadb-server、mariadb 软件包
   2> 启动 mariadb 服务,并确认监听状态

然后在客户端访问此数据库服务:

   1> 使用 mysql 命令访问本机的数据库服务,用户名为 root,密码为空
   2> 执行 SHOW DATABASES; 指令列出有哪些库
   3> 退出 mysql 交互界面

方案:

数据库表及相关软件的基本知识:

   1> 数据(记录):用来表示一个事物(实体)的一些信息(属性)的文字/图片文件等,例如字符串“:tedu.cn”
   2> 数据表:存放很多条数据记录的容器,例如学员联系信息表、学员月考成绩表
   3>  数据表的每一行:存放一条记录
   4>  数据表的每一列/字段:很多个事物的同一个属性
   5> 数据库:存放很多个相互关联的表格的容器,例如NSD1609学员档案库
   6>  数据库管理系统(DBMS):用来管理(创建库/添加/查询/删除/授权等)数据库信息的软件平台

MariaDB服务端:软件包mariadb-server、系统服务mariadb

MariaDB客户端:软件包mariadb、管理工具mysql

MariaDB服务端配置文件:/etc/my.cnf

传输协议及端口:TCP 3306

mysql命令的简单用法:

    mysql  [-u用户名]  [-p[密码]]

步骤:

步骤一:搭建MariaDB数据库服务器

1)安装软件包mariadb-server、mariadb

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

2)启动系统服务mariadb,并设置开机自启

    [root@server0 ~]# systemctl  restart  mariadb
    [root@server0 ~]# systemctl  enable  mariadb
    ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'

3)检查监听状态

    [root@server0 ~]# netstat  -antpu | grep  :3306
    tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2922/mysqld  

步骤二:访问本机的MariaDB数据库系统

1)以用户root连接本机的mariadb(或mysqld)数据库服务

    [root@server0 ~]# mysql  -uroot
    Welcome to the MariaDB monitor.  Commands end with ; or g.
    Your MariaDB connection id is 3
    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)]>

2)查看当前数据库系统内有哪些库

    MariaDB [(none)]> SHOW DATABASES;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    4 rows in set (0.00 sec)

3)退出操作环境

    MariaDB [(none)]> QUIT
    Bye
    [root@server0 ~]#

四、配置一个数据库

目标:

本例要求在虚拟机server0上部署 MariaDB 数据库,具体要求如下:

    1> 此数据库系统只能被 localhost 访问
    2> 新建一个数据库名为 Contacts,其中应该包含来自数据库复制的内容,复制文件的 URL 为:http://classroom/pub/materials/users.sql
    3> 除了 root 用户,此数据库只能被用户 Raikon 查询,此用户的密码为atenorth
    4> root用户的密码为 atenorth

方案:

为数据库账号修改密码:

    mysqladmin  [-u用户名]  [-p[旧密码]]  password  '新密码'

导入/恢复到数据库:

    mysql  [-u用户名]  [-p[密码]]  数据库名  <  备份文件.sql

为数据库用户授权/撤销权限:

    grant  权限1,权限2...  on  库名.表名  to  用户名@客户机地址  identified  by '密码';
    revoke 权限1,权限2... on  库名.表名  from  用户名@客户机地址;

步骤:

步骤一:禁止mariadb服务提供网络监听(只服务于本机)

1)修改配置文件

    [root@server0 ~]# vim  /etc/my.cnf
    [mysqld]
    skip-networking                                          //跳过网络

2)重启mariadb服务

    [root@server0 ~]# systemctl  restart  mariadb              //重启服务

3)确认结果

    [root@server0 ~]# netstat  -anptu  |  grep  :3306          //已经不提供端口监听
    [root@server0 ~]# pgrep  -l  mysqld                      //但进程仍在
    3127 mysqld_safe
    3297 mysqld

步骤二:配置数据库管理密码

1)使用mysqladmin为用户root设置密码

原管理账号root的密码为空,因此无需验证旧密码:

    [root@server0 ~]# mysqladmin  -u  root  password  'atenorth'

2)验证新密码是否可用

 root使用空密码从本机连接将会失败:

    [root@server0 ~]# mysql  -uroot
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

必须指定正确的新密码才能连接成功:

    [root@server0 ~]# mysql  -uroot  -patenorth
    Welcome to the MariaDB monitor.  Commands end with ; or g.
    Your MariaDB connection id is 4
    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.
    .. ..

步骤三:建Contacts库并导入备份数据

1)创建新库Contacts,并退出操作环境

    MariaDB [(none)]> CREATE  DATABASE  Contacts;
    Query OK, 1 row affected (0.00 sec)
    MariaDB [(none)]> QUIT
    Bye

2)下载指定的数据库备份

    [root@server0 ~]# wget  http://classroom.example.com/pub/materials/users.sql
    --2016-11-26 19:00:37--  http://classroom.example.com/pub/materials/users.sql
    Resolving classroom.example.com (classroom.example.com)... 172.25.254.254
    Connecting to classroom.example.com (classroom.example.com)|172.25.254.254|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 2634 (2.6K) [application/sql]
    Saving to: ‘users.sql’
    100%[=====================>] 2,634       --.-K/s   in 0s      
    2016-11-26 19:00:37 (269 MB/s) - ‘users.sql’ saved [2634/2634]
    [root@server0 ~]# ls  -lh  users.sql                      //确认下载的文件
    -rw-r--r--. 1 root root 2.6K Mar 31  2016 users.sql

3)导入数据库

    [root@server0 ~]# mysql  -uroot  -patenorth  Contacts  <  users.sql

4)重新连入操作环境,确认导入结果

    [root@server0 ~]# mysql  -uroot  -patenorth
    .. ..
    MariaDB [(none)]> USE  Contacts;                     //使用指定库
    Database changed
    MariaDB [Contacts]> SHOW  TABLES;                  //列出有哪些表
    +--------------------+
    | Tables_in_Contacts |
    +--------------------+
    | base               |
    | location           |
    +--------------------+
    2 rows in set (0.00 sec)

步骤四:为Contacts库授权

1)允许用户Raikon从本机访问,具有查询权限,密码为atenorth

    MariaDB [Contacts]> GRANT  select  ON  Contacts.*  TO  Raikon@localhost  IDENTIFIED BY  'atenorth';
    Query OK, 0 rows affected (0.00 sec)

2)退出操作环境

    MariaDB [Contacts]> QUIT
    Bye
    [root@server0 ~]#

五、使用数据库查询

目标:

本例要求配置MariaDB数据库,完成以下任务:

   1> 禁止空密码root用户访问mariadb数据库
   2> 在系统server0上使用数据库Contacts,通过SQL查询回答下列问题:密码是solicitous的人的名字?有多少人的姓名是Barbara同时居住在 Sunnyvale?

方案:

表记录增删改查:

    insert  into  [库名.]表名  values(值1,值2,值3);
    delete  from  [库名.]表名  where ...;
    update  [库名.]表名  set  字段名=字段值  where ....;
    select  字段列表  from  [库名.]表名  where  字段名1=值  and|or  字段名2=值;

统计查询结果的数量:

    select  count(*)  from  [库名.]表名  where  .. ..;

步骤:

步骤一:清理空密码root用户

1)确认空密码root用户记录

MariaDB服务端默认的mysql库user表保存了用户授权记录。

使用DESC指令查看表结构,以便了解相关字段名:

    MariaDB [(none)]> DESC  mysql.user;
    +------------------------+-----------------------------------+------+-----+---------+-------+
    | Field                  | Type                              | Null | Key | Default | Extra |
    +------------------------+-----------------------------------+------+-----+---------+-------+
    | Host                   | char(60)                          | NO   | PRI |         |       |
    | User                   | char(16)                          | NO   | PRI |         |       |
    | Password               | char(41)                          | NO   |     |         |       |

列出user表中的Host、User、Password字段,限定密码为空的root用户:

    MariaDB [(none)]> SELECT  Host,User,Password  FROM  mysql.user  WHERE  User='root'  AND  Password='';
    +---------------------+------+----------+
    | Host                | User | Password |
    +---------------------+------+----------+
    | server0.example.com | root |          |
    | 127.0.0.1           | root |          |
    | ::1                 | root |          |
    +---------------------+------+----------+
    3 rows in set (0.00 sec)
    MariaDB [(none)]>

2)删除空密码root用户记录

使用DELETE指令删除掉需要清除的授权记录:

    MariaDB [(none)]> DELETE  FROM  mysql.user  WHERE  User='root'  AND  Password='';
    Query OK, 3 rows affected (0.00 sec)

再次查询,确认删除结果:

    MariaDB [(none)]> SELECT  Host,User,Password  FROM  mysql.user  WHERE  User='root'  AND  Password='';
    Empty set (0.00 sec)

步骤二:按条件查询表记录

1)按单个条件查询

找出密码是solicitous的人的名字?

    MariaDB [(none)]> SELECT  name  FROM  Contacts.base  WHERE  Password='solicitous';
    +-------+
    | name  |
    +-------+
    | James |
    +-------+
    1 row in set (0.00 sec)

2)按多个条件在关联的两张表中查询

有多少人的姓名是Barbara同时居住在 Sunnyvale?

    MariaDB [(none)]> USE  Contacts;
    .. ..
    Database changed
    MariaDB [Contacts]> SELECT  COUNT(*)  FROM  base,location  WHERE  base.name='Barbara'  AND  location.city='Sunnyvale'  AND  base.id=location.id;
    +----------+
    | COUNT(*) |
    +----------+
    |        1 |
    +----------+
    1 row in set (0.00 sec)

原文地址:https://www.cnblogs.com/baichuanhuihai/p/8259627.html