bind+mysql搭建dns

1、前提:编译安装好mysql5.6

bind9安装包:

2、编译前的configure:

./configure --prefix=/usr/local/bind9 --with-dlz-mysql=/usr/local/mysql5 --enable-epoll --disable-openssl-version-check --disable-ipv6 --enable-largefile --disable-threads

3、编译与安装:

make -j${cores} && make install

4、named.conf的配置:

options {
        directory "/var/named/data";
        pid-file "/var/run/named.pid";
        allow-query { any; };
        allow-query-cache { none; };
        allow-transfer { none; };
        allow-update { none; };
        allow-recursion { none; };
        recursion no;
        version "unknown-version";
};
include "/var/named/data/cn.zone"; cn.zone是中国地区IP网段表;
include "/var/named/data/zone.conf";

5、cn.zone的内容:

acl "CN" {
1.0.1.0/24;
1.0.2.0/23;
1.0.8.0/21;
1.0.32.0/19;
….(省略)
};

6、zone.conf的内容:

view "CN"       {
match-clients { CN; };
dlz "Mysql zone" {
database "mysql
 {host=127.0.0.1 dbname=dnsdb ssl=false port=20430 user=bind_thebizark pass=RNSYf3ehdtCbk.uc5ffmdOietnlc}
{select zone from dns_records where zone = '\$zone$' and view = 'DF' limit 1}
{select ttl, type, mx_priority, case when lower(type)='txt' then concat('\"', data, '\"') when lower(type) = 'soa' then concat_ws(' ', data, resp_person, serial, refresh, retry, expire, minimum) else data end as mydata from dns_records where (zone = '\$zone$' and host = '\$record$' and view = 'CN' and not (type = 'SOA' or type = 'NS')) or (zone = '\$zone$' and host = '\$record$' and view = 'DF' and not (type = 'SOA' or type = 'NS') and not exists (select id from dns_records where zone = '\$zone$' and host = '\$record$' and view = 'CN' and not (type = 'SOA' or type = 'NS')))}
{select ttl, type, mx_priority, case when lower(type)='txt' then concat('\"', data, '\"') when lower(type) = 'soa' then concat_ws(' ', data, resp_person, serial, refresh, retry, expire, minimum) else data end as mydata from dns_records where zone = '\$zone$' and (type = 'SOA' or type='NS') and view='DF'};
};
};
 
view "ANY"       {
match-clients { ANY; };
dlz "Mysql zone" {
database "mysql
 {host=127.0.0.1 dbname=dnsdb ssl=false port=20430 user=bind_thebizark pass=RNSYf3ehdtCbk.uc5ffmdOietnlc}
{select zone from dns_records where zone = '\$zone$' and view = 'DF' limit 1}
{select ttl, type, mx_priority, case when lower(type)='txt' then concat('\"', data, '\"') when lower(type) = 'soa' then concat_ws(' ', data, resp_person, serial, refresh, retry, expire, minimum) else data end as mydata from dns_records where (zone = '\$zone$' and host = '\$record$' and view = 'ANY' and not (type = 'SOA' or type = 'NS')) or (zone = '\$zone$' and host = '\$record$' and view = 'DF' and not (type = 'SOA' or type = 'NS') and not exists (select id from dns_records where zone = '\$zone$' and host = '\$record$' and view = 'ANY' and not (type = 'SOA' or type = 'NS')))}
{select ttl, type, mx_priority, case when lower(type)='txt' then concat('\"', data, '\"') when lower(type) = 'soa' then concat_ws(' ', data, resp_person, serial, refresh, retry, expire, minimum) else data end as mydata from dns_records where zone = '\$zone$' and (type = 'SOA' or type='NS') and view='DF'};
};
};
Categories: 系统运维