1、简述DNS服务器的原理,并搭建主-辅服务器
DNS工作原理
- 在浏览器中输入www.qq.com域名,操作系统会先检查自己本地的hosts文件是否有这个网址映射关系,如果有,就先调用这个IP地址映射,完成域名解析。
- 如果hosts里没有这个域名的映射,则查找本地DNS解析器缓存,是否有这个网址映射关系,如果有,直接返回,完成域名解析。
- 如果hosts与本地DNS解析器缓存都没有相应的网址映射关系,首先会找TCP/IP参数中设置的首选DNS服务器,在此我们叫它本地DNS服务器,此服务器收到查询时,如果要查询的域名,包含在本地配置区域资源中,则返回解析结果给客户机,完成域名解析,此解析具有权威性。
- 如果要查询的域名,不由本地DNS服务器区域解析,但该服务器已缓存了此网址映射关系,则调用这个IP地址映射,完成域名解析,此解析不具有权威性。
- 如果本地DNS服务器本地区域文件与缓存解析都失效,则根据本地DNS服务器的设置(是否设置转发器)进行查询,如果未用转发模式,本地DNS就把请求发至 “根DNS服务器”,“根DNS服务器”收到请求后会判断这个域名(.com)是谁来授权管理,并会返回一个负责该顶级域名服务器的一个IP。本地DNS服务器收到IP信息后,将会联系负责.com域的这台服务器。这台负责.com域的服务器收到请求后,如果自己无法解析,它就会找一个管理.com域的下一级DNS服务器地址(qq.com)给本地DNS服务器。当本地DNS服务器收到这个地址后,就会找qq.com域服务器,重复上面的动作,进行查询,直至找到www.qq.com主机。
- 如果用的是转发模式,此DNS服务器就会把请求转发至上一级DNS服务器,由上一级服务器进行解析,上一级服务器如果不能解析,或找根DNS或把转请求转至上上级,以此循环。不管是本地DNS服务器用是是转发,还是根提示,最后都是把结果返回给本地DNS服务器,由此DNS服务器再返回给客户机。
环境说明
DNS主服务器:192.168.122.18
DNS辅服务器:192.168.122.28
客户端:192.168.122.38
安装软件,192.168.122.18配置主DNS服务器正向解析
[root@centos8 ~]# yum install -y bind bind-utils
[root@centos8 ~]# vi /etc/named.conf
#注释掉下面2行
// listen-on port 53 { 127.0.0.1; };
// allow-query { localhost; };
[root@centos8 ~]# vi /etc/named.rfc1912.zones
#添加以下管理的区域内容
zone "ddq.com" IN {
type master; #定义dns服务器类型
file "ddq.com.zone"; #相对路径 /var/named下
};
DNS区域数据库文件编写并启动服务
[root@centos8 ~]# cp -p /var/named/named.localhost /var/named/ddq.com.zone
[root@centos8 ~]# vi /var/named/ddq.com.zone
$TTL 1D
@ IN SOA ns1 admin.ddq.com. (
2021070101 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns1
ns1 A 192.168.122.18
master A 192.168.122.18
www A 192.168.122.28
web CNAME www
[root@centos8 ~]# named-checkconf
[root@centos8 ~]# named-checkzone ddq.com /var/named/ddq.com.zone
zone ddq.com/IN: loaded serial 2021070101
OK
[root@centos8 ~]# systemctl enable named.service
Created symlink /etc/systemd/system/multi-user.target.wants/named.service → /usr/lib/systemd/system/named.service.
[root@centos8 ~]# systemctl start named.service
安装httpd服务
在192.168.122.18和192.168.122.28上安装httpd服务
[root@centos8 ~] yum -y install httpd
#在192.168.122.18上输入以下内容
echo master.ddq.com 192.168.122.18 > /var/www/html/index.html
#在192.168.122.28上输入以下内容
echo www.ddq.com 192.168.122.28 > /var/www/html/index.html
验证DNS
#在192.168.122.38上设置DNS
[root@centos8 ~]# cat /etc/resolv.conf
nameserver 192.168.122.18
[root@centos8 ~]# curl master.ddq.com
master.ddq.com 192.168.122.18
[root@centos8 ~]# curl www.ddq.com
www.ddq.com 192.168.122.28
[root@centos8 ~]# curl web.ddq.com
www.ddq.com 192.168.122.28
配置解析一个反向区域并启动服务
[root@centos8 ~]# vi /etc/named.rfc1912.zones
#添加以下内容
zone "122.168.192.in-addr.arpa" IN {
type master;
file "192.168.122.zone";
};
[root@centos8 ~]# cp -p /var/named/named.loopback /var/named/192.168.122.zone
[root@centos8 ~]# vi /var/named/192.168.122.zone
$TTL 1D
@ IN SOA ns1 admin.ddq.com. (
2021070101 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns1.ddq.com.
18 PTR master.ddq.com.
28 PTR www.ddq.com.
[root@centos8 ~]# named-checkconf
[root@centos8 ~]# named-checkzone 122.168.192.in-addr.arpa /var/named/192.168.122.zone
zone 122.168.192.in-addr.arpa/IN: loaded serial 2021070101
OK
重载配置文件和区域数据文件
[root@centos8 ~]# rndc status
number of zones: 104 (97 automatic)
[root@centos8 ~]# rndc reload
server reload successful
[root@centos8 ~]# rndc status
number of zones: 105 (97 automatic)
服务器反向解析测试
#在192.168.122.38上测试
[root@centos8 ~]# dig -x 192.168.122.18
; <<>> DiG 9.11.26-RedHat-9.11.26-4.el8_4 <<>> -x 192.168.122.18
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52402
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 1bf21b0ee39c7752f7c63cba60dd648bcb46976f78fd4651 (good)
;; QUESTION SECTION:
;18.122.168.192.in-addr.arpa. IN PTR
;; ANSWER SECTION:
18.122.168.192.in-addr.arpa. 86400 IN PTR master.ddq.com.
;; AUTHORITY SECTION:
122.168.192.in-addr.arpa. 86400 IN NS ns1.ddq.com.
;; ADDITIONAL SECTION:
ns1.ddq.com. 86400 IN A 192.168.122.18
;; Query time: 0 msec
;; SERVER: 192.168.122.18#53(192.168.122.18)
;; WHEN: 四 7月 01 14:45:35 CST 2021
;; MSG SIZE rcvd: 146
安装软件、192.168.122.28配置辅DNS服务器
[root@centos8 ~]# yum install -y bind bind-utils
[root@centos8 ~]# vi /etc/named.conf
#注释掉下面2行
// listen-on port 53 { 127.0.0.1; };
// allow-query { localhost; };
[root@centos8 ~]# vi /etc/named.rfc1912.zones
#添加以下管理的区域内容
zone "ddq.com" IN {
type slave; #定义dns服务器类型
file "slaves/ddq.com.zone"; #相对路径 /var/named下
masters {192.168.122.18; }; #指向正向DNS服务器IP
};
[root@centos8 ~]# systemctl enable named.service
Created symlink /etc/systemd/system/multi-user.target.wants/named.service → /usr/lib/systemd/system/named.service.
[root@centos8 ~]# systemctl start named.service
在192.168.122.18主DNS服务器上配置ddq.com.zone文件
$TTL 1D
@ IN SOA ns1 admin.ddq.com. (
2021070102 ; serial #每次修改后手动加1,从服务器才会更新
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns1
NS ns2 #添加辅服务器NS记录
ns1 A 192.168.122.18
ns2 A 192.168.122.28 #辅DNS服务器A记录
master A 192.168.122.18
www A 192.168.122.28
web CNAME www
[root@centos8 ~]# named-checkzone ddq.com /var/named/ddq.com.zone
zone ddq.com/IN: loaded serial 2021070102
OK
[root@centos8 ~]# rndc reload
server reload successful
在192.168.122.28辅DNS服务器上重载配置
[root@centos8 ~]# rndc reload
server reload successful
[root@centos8 ~]# ll /var/named/slaves/
总用量 4
-rw-r--r-- 1 named named 369 7月 1 14:57 ddq.com.zone
验证辅DNS
#在192.168.122.38上设置DNS
[root@centos8 ~]# cat /etc/resolv.conf
nameserver 192.168.122.18
nameserver 192.168.122.28
[root@centos8 ~]# dig -t -A www.ddq.com @192.168.122.18
;; Warning, ignoring invalid type -A
; <<>> DiG 9.11.26-RedHat-9.11.26-4.el8_4 <<>> -t -A www.ddq.com @192.168.122.18
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 54108
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: ad1e47da12689ff25609715e60dd68f9179e054b0f045cce (good)
;; QUESTION SECTION:
;www.ddq.com. IN A
;; ANSWER SECTION:
www.ddq.com. 86400 IN A 192.168.122.28
;; AUTHORITY SECTION:
ddq.com. 86400 IN NS ns1.ddq.com.
ddq.com. 86400 IN NS ns2.ddq.com.
;; ADDITIONAL SECTION:
ns1.ddq.com. 86400 IN A 192.168.122.18
ns2.ddq.com. 86400 IN A 192.168.122.28
;; Query time: 0 msec
;; SERVER: 192.168.122.18#53(192.168.122.18)
;; WHEN: 四 7月 01 15:04:29 CST 2021
;; MSG SIZE rcvd: 152
[root@centos8 ~]# dig -t -A www.ddq.com @192.168.122.28
;; Warning, ignoring invalid type -A
; <<>> DiG 9.11.26-RedHat-9.11.26-4.el8_4 <<>> -t -A www.ddq.com @192.168.122.28
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21006
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 68fc6776a4d28e21f60d6ab760dd6901367b7a58ae4b3c4d (good)
;; QUESTION SECTION:
;www.ddq.com. IN A
;; ANSWER SECTION:
www.ddq.com. 86400 IN A 192.168.122.28
;; AUTHORITY SECTION:
ddq.com. 86400 IN NS ns2.ddq.com.
ddq.com. 86400 IN NS ns1.ddq.com.
;; ADDITIONAL SECTION:
ns1.ddq.com. 86400 IN A 192.168.122.18
ns2.ddq.com. 86400 IN A 192.168.122.28
;; Query time: 0 msec
;; SERVER: 192.168.122.28#53(192.168.122.28)
;; WHEN: 四 7月 01 15:04:31 CST 2021
;; MSG SIZE rcvd: 152
2、搭建并实现智能DNS
搭建并实现智能DNS
192.168.100.37 主DNS websrv-other
192.168.100.27 websrv-hz
192.168.122.18 websrv-sh
192.168.100.17 客户端-hz
192.168.122.28 客户端-sh
192.168.100.17上搭建智能DNS
- 配置DNS服务主文件
[root@localhost ~]# yum -y install bind bind-utils httpd
[root@localhost ~]# vi /etc/named.conf
#文件最前面添加acl控制字段
acl hangzhounet {
192.168.100.0/24;
};
acl shanghainet {
192.168.122.0/24;
};
acl othernet {
any;
};
#注释以下几行
// listen-on port 53 { 127.0.0.1; };
// allow-query { localhost; };
/*
zone "." IN {
type hint;
file "named.ca";
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
*/
#文件底部定义view视图
view hangzhouview {
match-clients { hangzhounet; };
include "/etc/named.rfc1912.zones.hz";
};
view shanghaiview {
match-clients { shanghainet; };
include "/etc/named.rfc1912.zones.sh";
};
view otherview {
match-clients { othernet; };
include "/etc/named.rfc1912.zones.other";
};
include "/etc/named.root.key";
- 实现区域配置文件
[root@localhost ~]# vi /etc/named.rfc1912.zones.hz
zone "." IN {
type hint;
file "named.ca";
};
zone "ddq.com" IN {
type master;
file "ddq.com.zone.hz";
}
[root@localhost ~]# vi /etc/named.rfc1912.zones.sh
zone "." IN {
type hint;
file "named.ca";
};
zone "ddq.com" IN {
type master;
file "ddq.com.zone.sh";
}
[root@localhost ~]# vi /etc/named.rfc1912.zones.other
zone "." IN {
type hint;
file "named.ca";
};
zone "ddq.com" IN {
type master;
file "ddq.com.zone.other";
}
[root@localhost ~]# chgrp named /etc/named.rfc1912.zones.*
- 创建区域数据库文件
[root@localhost ~]# vi /var/named/ddq.com.zone.hz
$TTL 1D
@ IN SOA master admin.ddq.com. (
2021070101 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS master
master A 192.168.100.37
websrv A 192.168.100.27
www CNAME websrv
[root@localhost ~]# vi /var/named/ddq.com.zone.sh
$TTL 1D
@ IN SOA master admin.ddq.com. (
2021070101 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS master
master A 192.168.100.37
websrv A 192.168.122.18
www CNAME websrv
[root@localhost ~]# vi /var/named/ddq.com.zone.other
$TTL 1D
@ IN SOA master admin.ddq.com. (
2021070101 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS master
master A 192.168.100.37
websrv A 127.0.0.1
www CNAME websrv
[root@localhost ~]# chgrp named /var/named/ddq.com.zone.*
[root@localhost ~]# systemctl restart named
- 实现位于不同区域的三个web服务器
#分别在三台服务器上安装http服务
#192.168.100.37 主DNS websrv-other
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo 192.168.100.37 主DNS websrv-other > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
#192.168.100.27 websrv-hz
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo 192.168.100.27 websrv-hz > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
#192.168.122.18 websrv-sh
[root@centos8 ~]# yum -y install httpd
[root@centos8 ~]# echo 192.168.122.18 websrv-sh > /var/www/html/index.html
[root@centos8 ~]# systemctl start httpd
- 客户端测试
#分别在三台主机上访问
#192.168.100.17 客户端-hz
[root@localhost ~]# cat /etc/resolv.conf
nameserver 192.168.100.37
[root@localhost ~]# curl www.ddq.com
192.168.100.27 websrv-hz
#192.168.122.28 客户端-sh
[root@centos8 ~]# cat /etc/resolv.conf
nameserver 192.168.100.37
[root@centos8 ~]# curl www.ddq.com
192.168.122.18 websrv-sh
3、通过编译、二进制安装MySQL5.7
安装依赖包
[root@centos7 ~]# yum -y install gcc gcc-c++ cmake make bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel libtirpc-devel rpcgen
准备用户和数据目录
[root@centos7 ~]# useradd -r -s /sbin/nologin -d /data/mysql mysql
[root@centos7 ~]# mkdir -p /data/mysql
[root@centos7 ~]# chown mysql:mysql /data/mysql
下载并解压安装包
[root@centos7 ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33.tar.gz
[root@centos7 ~]# tar zxf mysql-5.7.33.tar.gz
源码编译安装mysql
[root@centos7 ~]# cd mysql-5.7.33
[root@centos7 mysql-5.7.33]# cmake . \
> -DDOWNLOAD_BOOST=ON \
> -DWITH_BOOST=$HOME/my_boost \
> -DCMAKE_INSTALL_PREFIX=/app/mysql \
> -DMYSQL_DATADIR=/data/mysql/ \
> -DSYSCONFDIR=/etc/ \
> -DMYSQL_USER=mysql \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
> -DWITH_DEBUG=0 -DWITH_READLINE=1 \
> -DWITH_SSL=system \
> -DWITH_ZLIB=system \
> -DWITH_LIBWRAP=0 \
> -DENABLED_LOCAL_INFILE=1 \
> -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci
[root@centos7 mysql-5.7.33]# make && make install
准备环境变量
[root@centos7 mysql-5.7.33]# echo 'PATH=/app/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos7 mysql-5.7.33]# . /etc/profile.d/mysql.sh
准备配置文件
[root@centos7 mysql-5.7.33]# cp /etc/my.cnf{,.bak}
[root@centos7 mysql-5.7.33]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
生成数据库文件,并提取root密码
[root@centos7 mysql-5.7.33]# cd /app/mysql/
[root@centos7 mysql]# mysqld --initialize --datadir=/data/mysql/ --user=mysql
#注意生成root的初始密码
修改初始root密码
[root@centos8 mysql]# mysqladmin -uroot -p'p(e*ssYkI7kh' password ddq123
测试登录
[root@centos8 mysql]# mysql -uroot -pddq123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.33 Source distribution
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>