您的位置:首页 > 运维架构 > Linux

【Linux】基于Bind_DLZ和MySQL数据的DNS搭建

2015-09-11 16:09 531 查看
参考:http://www.linuxfromscratch.org/blfs/view/cvs/server/bind.html

参考:http://bind-dlz.sourceforge.net/

参考:http://www.shencan.net/index.php/2013/05/21/bind9-dlz-%e6%90%9edns/

依赖:libcaplibxml2OpenSSLgeoip(MIT)

编译参数(官网说明强调编译关闭多线程,即--enable-threads=no):

[root@dnsbind-9.10.2-P4]#./configure--prefix=/usr/local/bind--enable-epoll--enable-largefile--enable-threads=no--with-dlz-mysql=/usr/local/mysql--disable-openssl-version-check


生成rndc.cnf:

[root@dnsetc]#cd/usr/local/bind/etc
[root@dnsetc]#../sbin/rndc-confgen>rndc.conf


生成named.cnf:

[root@dnsetc]#tail-10rndc.conf|head-9|seds/#\//g>named.conf


编辑named.cnf,添加dlzmysql驱动,参考其他资料也可以配置智能DNS。

(官方的dlzmysql驱动中有5个查询和1个更新语句,具体每个查询的功能可以参考官方说明)

key"rndc-key"{
algorithmhmac-md5;
secret"THnBnPv//oADGjeKQMoNng==";
};

controls{
inet127.0.0.1port953
allow{127.0.0.1;}keys{"rndc-key";};
};

options{
allow-recursion{none;};
recursionno;
directory"/usr/local/bind/etc";
version"leju-db-direct";
pid-file"named.pid";
};
logging{
channeldefault_debug{
file"/tmp/bind_query.log"versions3size10m;
severityinfo;
print-timeyes;
print-categoryyes;
};
categoryqueries{
default_debug;
};
#categorydatabase{
#default_debug;
#};
};

dlz"mysqlzone"{
database"mysql
{host=localhostdbname=namessl=falseport=3307user=userpass=passsocket=/tmp/mysql_3306.sock}
{selectzonefromdns_recordswherezone='$zone$'limit1}
{selectttl,type,mx_priority,casewhenlower(type)='txt'thenconcat('\"',data,'\"')whenlower(type)='soa'thenconcat_ws('',data,resp_person,serial,refresh,retry,expire,minimum)elsedataendasmydatafromdns_recordswherezone='$zone$'andhost='$record$'andIsLive=1}";
};


官网给的mysql驱动模板:

dlz"mysqlzone"{
database"mysql
{host=127.0.0.1dbname=namessl=falseport=3306user=userpass=pass}
{selectzonefromdns_recordswherezone='$zone$'limit1}
{selectttl,type,mx_priority,casewhenlower(type)='txt'thenconcat('\"',data,'\"')elsedataendfromdns_recordswherezone='$zone$'andhost='$record$'andnot(type='SOA'ortype='NS')andIsLive=1}
{selectttl,type,mx_priority,datafromdns_recordswherezone='$zone$'and(type='SOA'ortype='NS')}
{selectttl,type,host,mx_priority,data,resp_person,serial,refresh,retry,expire,minimumfromdns_recordswherezone='$zone$'andnot(type='SOA'ortype='NS')andIsLive=1}
{selectzonefromxfr_tablewherezone='$zone$'andclient='$client$'}";
};


模板的查询需要根据环境修改。

如果驱动部分存在查询问题,或者表中值定义存在异常会出现以下报错:

23-Sep-201511:39:47.621dns_rdata_fromtext:buffer-0x7fffc7277da0:1:neareof:unexpectedendofinput
23-Sep-201511:39:47.621dns_sdlz_putrrreturnederror.Errorcodewas:unexpectedendofinput


参考:http://blog.163.com/digoal@126/blog/static/163877040201110235657127/

数据库建表:

CREATETABLE`dns_records`(
`id`int(10)unsignedNOTNULLAUTO_INCREMENT,
`zone`varchar(255)NOTNULL,
`host`varchar(255)NOTNULLDEFAULT'@',
`type`enum('MX','CNAME','NS','SOA','A','PTR')NOTNULL,
`data`varchar(255)DEFAULTNULL,
`ttl`int(11)NOTNULLDEFAULT'800',
`mx_priority`int(11)DEFAULTNULL,
`refresh`int(11)NOTNULLDEFAULT'3600',
`retry`int(11)NOTNULLDEFAULT'3600',
`expire`int(11)NOTNULLDEFAULT'86400',
`minimum`int(11)NOTNULLDEFAULT'3600',
`serial`bigint(20)NOTNULLDEFAULT'2008082700',
`resp_person`varchar(64)NOTNULLDEFAULT'root.domain.com.',
`primary_ns`varchar(64)NOTNULLDEFAULT'ns1.domain.com.',
PRIMARYKEY(`id`)
)ENGINE=InnoDBAUTO_INCREMENT=4DEFAULTCHARSET=utf8


插入数据:

mysql>insertINTOdns_records(zone,host,type,data,ttl,retry)values('helloworld.com','bbs','A','192.168.1.101',86400,15);
mysql>insertINTOdns_records(zone,host,type,data,ttl,retry)values('helloworld.com','news','A','192.168.1.102',86400,15);


debug启动:

[root@dnssbin]#./named-g-d1

[root@dnssbin]#./named-c../etc/named.cnf#启动服务



如果出现MySQL无法建立连接的报错,例如:

mysqldriverfailedtocreatedatabaseconnectionafter4attempts


我通过在dlzmysql驱动中使用socket方式连接数据库解决这个问题。

强制手动刷新

[root@dnssbin]#./rndcflush


停止服务

[root@dnssbin]#./rndcstop


DNS数据说明

type

type字段用于说明RR的类型。常用的RR类型如下:
1、区记录
SOA(StartOfAuthority):SOA记录标示一个授权区定义的开始。SOA记录后的所有信息是控制这个区的。
NS(NameServer):标识区的域名服务器以及授权子域。
2、基本记录
A(Address):用于将主机名转换为IP地址,任何一个主机都只能有一个A记录。
PTR(PoinTeR):将地址转换为主机名。
MX(MaileXchanger):邮件交换记录。控制邮件的路由。
3、安全记录
KEY(PublicKey):储存一个关于DNS名称的公钥。
NXT(Next):与DNSSEC一起使用,用于指出一个特定名称不在域中。
SIG(Signatrue):指出带签名和身份认证的区信息,细节见RFC2535。
4、可选记录
CNAME(CanonicalNAME):给定主机的别名,主机的规范名在A记录中给出。
SRV(Services):指出知名网络服务的信息。
TXT(Text):注释或非关键的信息。
RR的顺序:
SOARR应该放在最前面。通常NSRR紧跟在SOARR之后,其他记录的顺序无关紧要。

data

存放该行host+zone域名解析的IP。

[b]ttl(timetolive)[/b]

它以秒为单位定义该资源记录中的信息存放在高速缓存中的时间长度。通常省略该字段,而使用位于文件开始处的$TTL语句所指定值。

SerialNumber

本区配置数据的序列号,用于从服务器判断何时获取最新的区数据。

可以是32位的任何整数,每当更新区文件时都应该增加此序列号的值,否则named将不会把区的更新数据传送到从服务器。

Refresh

辅助域名服务器多长时间更新数据库。Refresh1到6小时。

Retry

若辅助域名服务器更新数据失败,多长时间再试。Retry20到60分钟。

Expire

若辅助域名服务器无法从主服务器上更新数据,原有的数据何时失效。Expire1周到1月。

Minimum

设置被缓存的否定回答的存活时间。Minimum1到3小时。

缓存时间字段Refresh、Retry、Expire、Minimum可以使用时间单位字符m、h、d、w分别表示分钟、小时、天、星期。

resp_person

区域管理员邮箱

primary_ns

区域主节点机器名
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: