1

1.先安装MySQL,直接yum安装
* yum -y install mysql mysql-server

2.再安装一些依赖的包

  • yum -y install openssl openssl-devel libss-dev gcc gcc-c++ mysql-devel
  • yum install gcc libffi-devel python-devel openssl-devel mysql-devel -y
3.下载bind, bind官网(https://www.isc.org/)
4.下载mysql-bind 补丁源码
5.解压bind和mysql-bind源码压缩文件
    tar zxvf bind-9.10.3-P2.tar.gz

    tar zxvf mysql-bind.tar.gz
6.将mysql-bind源码目录下的mysqldb.c 和 mysqldb.h拷贝到bind源码目录下的bin/named和bin/named/include/ 目录下
    cd mysql-bind

    cp -f mysqldb.c mysqldb.h ../bind-9.10.3-P2/bin/named/

    cp -f mysqldb.c mysqldb.h ../bind-9.10.3-P2/bin/named/include/
7.修改bind源码目录下bin/named/Makefile.in文件
    cd ../bind-9.10.3-P2

    vim bin/named/Makefile.in

    将以下几行:  

    DBDRIVER_OBJS =                          

    DBDRIVER_SRCS =                          

    DBDRIVER_INCLUDES =                      

    DBDRIVER_LIBS =

    修改为:

    DBDRIVER_OBJS = mysqldb.@O@

    DBDRIVER_SRCS = mysqldb.c

    DBDRIVER_INCLUDES = -I/usr/include/mysql  -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC   -DUNIV_LINUX -DUNIV_LINUX

    DBDRIVER_LIBS = -rdynamic -L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -lssl -lcrypto

    DBDRIVER_INCLUDES 的值是通过 mysql_config --cflags 命令获取的

    DBDRIVER_LIBS 的值是通过 mysql_config --libs 命令获取的

8.修改bind源码目录下bin/named目录下的main.c文件

    vim bin/named/main.c

    加入 #include "mysqldb.h"

    如下:

#include <config.h>

#include "mysqldb.h"

#include <ctype.h>

#include <stdlib.h>

#include <string.h> 

    然后在注释代码段 /*  xxdb_init(); */ 后加入 mysqldb_init();

    在注释代码段 /*  xxdb_clear(); */后加入 mysqldb_clear();
9.修改mysqldb.c
  • 修改mysqldb.c中的#include <named/mysqldb.h>为 #include <include/mysqldb.h>
10.安装bind
    ./configure --prefix=/usr/local/bind --enable-threads  # 指定安装目录和开启多线程的处理能力

    make

    make install
11.配置bind
    cd /usr/local/bind/etc

    /usr/local/bind/sbin/rndc-confgen -r /dev/urandom>rndc.conf

    cat rndc.conf|tail |head -9 |sed "s/^#//g">named.conf   # 生成配置文件
12.创建一个数据库
############这版不太好用
    Create database mydomain;

    创建一张数据表

    CREATE TABLE dnsrecord ( 

      name varchar(255) default NULL,

      ttl int(11) default NULL, 

      rdtype varchar(255) default NULL,

      rdata varchar(255) default NULL )TYPE=MyISAM;

    插入一些测试数据

    INSERT INTO dnsrecord VALUES ('test.net', 259200, 'SOA', 'test.net.  www.test.net  200505101 28800 7200 86400 28800');

    INSERT INTO dnsrecord VALUES ('test.net', 259200, 'NS', 'ns1.test.net.');

    INSERT INTO dnsrecord VALUES ('ns1.test.net', 259200, 'A', '192.168.2.2');

    INSERT INTO dnsrecord VALUES ('www.test.net', 259200, 'A', '192.168.2.1');
##########################好使###############################

DROP TABLE IF EXISTS 10_outside;
CREATE TABLE 10_outside (
 name varchar(255) default NULL,
 ttl int(11) default NULL,
 rdtype varchar(255) default NULL,
 rdata varchar(255) default NULL
);

--
-- Dumping data for table `10_outside`
--

LOCK TABLES 10_outside WRITE;
INSERT INTO 10_outside VALUES ('25.71.210.10.in-addr.arpa',3600,'PTR','cas1.test.mydomain.com.cn.');
INSERT INTO 10_outside VALUES ('10.in-addr.arpa',3600,'SOA','test.mydomain.com.cn. zhengyu.staff.mydomain.com.cn. 20070319 1800 600 604800 600');
INSERT INTO 10_outside VALUES ('10.in-addr.arpa',3600,'NS','cas1.test.mydomain.com.cn.');
INSERT INTO 10_outside VALUES ('10.in-addr.arpa',3600,'NS','cas2.test.mydomain.com.cn.');
INSERT INTO 10_outside VALUES ('10.in-addr.arpa',3600,'NS','cas3.test.mydomain.com.cn.');
INSERT INTO 10_outside VALUES ('27.71.210.10.in-addr.arpa',3600,'PTR','cas2.test.mydomain.com.cn.');
UNLOCK TABLES;

--
-- Table structure for table `test_mydomain_com_cn_outside`
--

DROP TABLE IF EXISTS test_mydomain_com_cn_outside;
CREATE TABLE test_mydomain_com_cn_outside (
 name varchar(255) default NULL,
 ttl int(11) default NULL,
 rdtype varchar(255) default NULL,
 rdata varchar(255) default NULL
);

--
-- Dumping data for table `test_mydomain_com_cn_outside`
--

LOCK TABLES test_mydomain_com_cn_outside WRITE;
INSERT INTO test_mydomain_com_cn_outside VALUES ('test.mydomain.com.cn',3600,'SOA','test.mydomain.com.cn. zhengyu.staff.mydomain.com.cn. 20070319 1800 600 604800 600');
INSERT INTO test_mydomain_com_cn_outside VALUES ('test.mydomain.com.cn',3600,'NS','cas1.test.mydomain.com.cn.');
INSERT INTO test_mydomain_com_cn_outside VALUES ('test.mydomain.com.cn',3600,'NS','cas2.test.mydomain.com.cn.');
INSERT INTO test_mydomain_com_cn_outside VALUES ('test.mydomain.com.cn',3600,'NS','cas3.test.mydomain.com.cn.');
INSERT INTO test_mydomain_com_cn_outside VALUES ('cas1.test.mydomain.com.cn',3600,'A','10.210.71.25');
INSERT INTO test_mydomain_com_cn_outside VALUES ('cas2.test.mydomain.com.cn',3600,'A','10.210.71.27');
INSERT INTO test_mydomain_com_cn_outside VALUES ('cas3.test.mydomain.com.cn',3600,'A','10.210.132.80');
INSERT INTO test_mydomain_com_cn_outside VALUES ('yhzh.test.mydomain.com.cn',3600,'A','10.218.26.191');
INSERT INTO test_mydomain_com_cn_outside VALUES ('yhzh.test.mydomain.com.cn',3600,'A','10.218.26.192');
INSERT INTO test_mydomain_com_cn_outside VALUES ('yhzh.test.mydomain.com.cn',3600,'A','10.218.26.193');
INSERT INTO test_mydomain_com_cn_outside VALUES ('yhzh.test.mydomain.com.cn',3600,'A','10.218.26.194');
INSERT INTO test_mydomain_com_cn_outside VALUES ('*',3600,'A','10.210.71.1');
INSERT INTO test_mydomain_com_cn_outside VALUES ('conf.test.mydomain.com.cn',3600,'CNAME','cas2.test.mydomain.com.cn.');
UNLOCK TABLES;


############################################
13.继续配置bind 
vim /usr/local/bind/etc/named.conf

在后面按照以下格式加入

zone "mydomain.com" {

    type master;

    notify no; 

    database "mysqldb dbname tablename hostname user password"; };

mydomain.com为要解析的域名

dbname 为数据库名

hostname为数据库服务器地址

user 为可操作前面数据库表的数据库用户名

password 为对应数据库用户名的密码

配置完成

在命令行下运行

/usr/local/bind/sbin/named -c /usr/local/bind/etc/named.conf -g

查看没问题后

/usr/local/bind/sbin/named -c /usr/local/bind/etc/named.conf 

dig命令直接生成named.root文件

[root@silence etc]# ls
bind.keys  named.conf  named.root  rndc.conf  root.zone

[root@silence etc]# dig > named.root
[root@silence etc]# cat root.zone 
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>>
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45071
;; flags: qr rd ra; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;.                IN    NS

;; ANSWER SECTION:
.            1659    IN    NS    d.root-servers.net.
.            1659    IN    NS    e.root-servers.net.
.            1659    IN    NS    i.root-servers.net.
.            1659    IN    NS    f.root-servers.net.
.            1659    IN    NS    b.root-servers.net.
.            1659    IN    NS    m.root-servers.net.
.            1659    IN    NS    c.root-servers.net.
.            1659    IN    NS    l.root-servers.net.
.            1659    IN    NS    g.root-servers.net.
.            1659    IN    NS    k.root-servers.net.
.            1659    IN    NS    h.root-servers.net.
.            1659    IN    NS    a.root-servers.net.
.            1659    IN    NS    j.root-servers.net.

;; Query time: 25 msec
;; SERVER: 114.114.114.114#53(114.114.114.114)
;; WHEN: 四 4月 29 11:34:25 CST 2021
;; MSG SIZE  rcvd: 239
  • named.conf

    [root@localhost etc]# cat named.root 
    
    ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>>
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45071
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 1
    
    ;; OPT PSEUDOSECTION:
    ; EDNS: version: 0, flags:; udp: 512
    ;; QUESTION SECTION:
    ;.                IN    NS
    
    ;; ANSWER SECTION:
    .            1659    IN    NS    d.root-servers.net.
    .            1659    IN    NS    e.root-servers.net.
    .            1659    IN    NS    i.root-servers.net.
    .            1659    IN    NS    f.root-servers.net.
    .            1659    IN    NS    b.root-servers.net.
    .            1659    IN    NS    m.root-servers.net.
    .            1659    IN    NS    c.root-servers.net.
    .            1659    IN    NS    l.root-servers.net.
    .            1659    IN    NS    g.root-servers.net.
    .            1659    IN    NS    k.root-servers.net.
    .            1659    IN    NS    h.root-servers.net.
    .            1659    IN    NS    a.root-servers.net.
    .            1659    IN    NS    j.root-servers.net.
    
    ;; Query time: 25 msec
    ;; SERVER: 114.114.114.114#53(114.114.114.114)
    ;; WHEN: 四 4月 29 11:34:25 CST 2021
    ;; MSG SIZE  rcvd: 239
    
    [root@localhost etc]# 
    [root@localhost etc]# 
    [root@localhost etc]# cat named.conf 
     key "rndc-key" {
      algorithm hmac-md5;
      secret "80nDj9zCohA586hr93XELg==";
     };
     
     controls {
      inet 127.0.0.1 port 953
          allow { 127.0.0.1; } keys { "rndc-key"; };
     };
    
    options {
      listen-on port 53 { 127.0.0.1;172.16.197.63; };
      directory "/usr/local/bind";
      allow-query-cache { any; };
      allow-query   { any; };
      dnssec-enable yes;
      dnssec-validation yes;
      dnssec-lookaside auto;
     };
     zone "." { 
      type hint; 
      file "/usr/local/bind/etc/named.root"; 
     };
     zone "mydomain.com" {
     type forward;    
     forwarders { 49.7.233.84;114.114.114.114;8.8.8.8; };####forwarders是转发你的dns请求到其他dns 地址
     forward first;
     };
     logging {
         channel bind_log {
                 file "/usr/local/bind/logs/bind.log" versions 3 size 20m;
                 severity info;
                 print-time yes;
                 print-severity yes;
                 print-category yes;
         };
         category default {
                 bind_log;
         };
     };
     zone "test.mydomain.com.cn" IN{ 
      type master;
      notify no;
      database "mysqldb mydomain test_mydomain_com_cn_outside 172.16.197.63 root xxxx"; 
     };
     zone "hbjj.com" IN{
      type master;
      notify no;
      database "mysqldb mydomain test_mydomain_com_cn_outside 172.16.197.63 root xxxx";
     };
     zone "16.172.in-addr.arpa" IN{
      type master;
      notify no;
      database "mysqldb mydomain 10_outside 172.16.197.63 root xxxx";
     };
    

运行bind

写入系统服务

vim /etc/init.d/named

####################################################################

#!/bin/bash

    #

    # description: named daemon  描述信息

    # chkconfig: - 25 88    启动和关闭级别

    #

    # 启动named的pid文件、其实文件路径我们都可以自己指定的

    pidFile=/usr/local/bind/var/run/named.pid

    # 启动服务时的锁文件、判断一个服务是不是启动是靠这个锁文件的

    lockFile=/var/lock/subsys/named

    # named的配置文件路径

    confFile=/usr/local/bind/etc/named.conf

    # 判断/etc/rc.d/init.d/functions路径下的functios是否存在这个文件、存在就sources进来

    [ -r /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions

    # 定义启动方法

    start() {

            # 判断锁文件是否存在、如果存在就说服务已经启动并退出

            if [ -e $lockFile ]; then

                echo "named is already running..."

                exit 0

            fi

            # 打印服务已经启动的信息

            echo -n "Starting named:"

            # 调用functions中的daemon方法、并用--pidfile指定pid文件在什么地方、还指定用户和配置文件、我们本来是直接启用named的、现在利用daemon这个函数来启用这个服务、他会获得很多额外的属性、如果成功了会帮我们打印什么[ OK ]的、还可以做判断

            daemon --pidfile "$pidFile" /usr/local/bind/sbin/named -c "$confFile"

            RETVAL=$?

            echo

            # 判断上面的命令是否执行成功、说明启动成功

            if [ $RETVAL -eq 0 ]; then

                    # 如果判断成功、就创建锁文件

                    touch $lockFile

                    return $RETVAL

            else

                    # 否则、就是失败了、那就删除锁文件和pid文件

                    rm -f $lockFile $pidFile

                    return 1

            fi

    }

    # 定义停止方法

    stop() {

            # 如果锁文件不存在

            if [ ! -e $lockFile ]; then

                    # 说明服务还没有启动

                    echo "named is stopped."

            fi

            echo -n "Stopping named:"

            killproc named

            RETVAL=$?

            echo

            # 判断以上的命令是否执行停止成功、如果成功

            if [ $RETVAL -eq 0 ];then

                    # 删除锁文件和pid文件

                    rm -f $lockFile $pidFile

                    return 0

            else

                    # 否则打印说没能正常停止

                    echo "Cannot stop named."

                    # 调用functions中的函数、会打印失败

                    failure

                    return 1

            fi

    }

    # 定义重启服务

    restart() {

            # 调用停止方法

            stop

            # 睡眠2秒

            sleep 2

            # 调用启动方法

            start

    }

    # 定义重新加载服务方法

    reload() {

            echo -n "Reloading named: "

            # killprco HUP信号、named进程的信号

            killproc named -HUP

            RETVAL=$?

            echo

            return $RETVAL

    }

    # 定义服务状态

    status() {

            if pidof named &> /dev/null; then

                    echo -n "named is running..."

                    success

                    echo

            else

                    echo -n "named is stopped..."

                    success

                    echo

            fi

    }

    # 定义错误提示信息

    usage() {

            echo "Usage: named {start|stop|restart|status|reload}"

    }

    case $1 in

    start)

            start ;;

    stop)

            stop ;;

    restart)

            restart ;;

    status)

            status ;;

    reload)

            reload ;;

    *)

            usage

            exit 4 ;;

    esac

测试

image.png

[root@localhost etc]# /usr/local/bind/bin/dig -t A saas-cloud-dev.hbjj.com @172.16.197.63

; <<>> DiG 9.9.3-P1 <<>> -t A saas-cloud-dev.hbjj.com @172.16.197.63
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 36451
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;saas-cloud-dev.hbjj.com.    IN    A

;; ANSWER SECTION:
saas-cloud-dev.hbjj.com.    3600 IN    A    192.168.6.45

;; AUTHORITY SECTION:
hjj.com.    3600    IN    NS    master.hbjj.com.

;; ADDITIONAL SECTION:
master.hbjj.com.    3600    IN    A    192.168.6.45

;; Query time: 6 msec
;; SERVER: 172.16.197.63#53(172.16.197.63)
;; WHEN: Thu Apr 29 14:49:38 CST 2021
;; MSG SIZE  rcvd: 112

[root@localhost etc]# nslookup saas-cloud-dev.hbjj.com
Server:        172.16.197.63
Address:    172.16.197.63#53

Name:    saas-cloud-dev.hbjj.com
Address: 192.168.6.45


[root@silence ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 172.16.197.63

配置原理

###################
 zone "hbjj.com" IN{
     type master;
     notify no;
     database "mysqldb mydomain test_mydomain_com_cn_outside 172.16.197.63 root xxxx";
#####################



                    
#####################     
zone "abc.com" IN {
    type master;
    file "/usr/local/named/etc/named.abc.com";
};     
     
$TTL 600
@               IN  SOA     master.abc.com. woodie.abc.com.( 2017022802 3H 15M 1W 1D )
@               IN  NS      master.abc.com.
master          IN  A       192.168.80.110
@               IN  MX  10  www.abc.com.
www             IN  A       192.168.80.121
bbs             IN  CNAME   www.abc.com.
ftp             IN  CNAME   www.abc.com.
linux           IN  CNAME   www.abc.com.
slave           IN  A       192.168.80.120
122             IN  A       192.168.80.122   
##################################
    
  • 两种模式是一个原理都必须配置 SOA, NS等 例:
    image.png

锅包肉
97 声望17 粉丝

这个人很懒,没有什么说的。