两台虚拟机,均修改防火墙与主机名

虚拟机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;       #查看表结构