BIND动态区域加载,简称BIND DLZ(BIND Dynamic Loadable Zones),是BIND的一个增强型组件,通过BIND DLZ,BIND可以加载动态的区域信息。
使用BIND DLZ有如下几个好处:
      通过DLZ,域名解析信息会存放到后台数据库中,这样,可以利用数据库的特性保证数据的冗余,同时,可以把信息分布式存放在不同的主机上,而且,域名信息发生变化,可以很快重新加载。
      DLZ支持多种数据存储形式,包括文件系统,特别适合那种大型的、海量的域名解析系统。
下面,我们就一步一步来实现DLZ。
准备工作:
mysql源码安装包
bind源码安装包(有些bind版本不支持DLZ功能)
系统环境:centos 5.5
编译安装mysql
#useradd mysql -s /sbin/nologin
#wget http://downloads.mysql.com/archives/mysql-5.1/mysql-5.1.54.tar.gz  //获取mysql源码包
#tar zxvf mysql-5.1.54.tar.gz
#cd mysql-5.1.54
#./configure --prefix=/usr/local/mysql  //指定mysql安装路径
--localstatedir=/data/mysql_db    //指定数据库的库文件存放路径
--with-mysqld-ldflags=-all-static   //以静态方式编译服务器端
--with-client-ldflags=-all-static    //以静态方式编译客户端
--with-extra-charsets=utf8,gbk    //添加utf8、gbk字符集
--with-plugins=innobase,myisam    //添加mysql存储引擎
--with-server-suffix=-community     //为mysqld版本字符串添加后缀
--with-unix-socket-path=/usr/local/mysql/sock/mysql.sock
--enable-thread-safe-client   //以线程方式编译客户端,提高性能
--enable-assembler   //使用汇编,提高性能
--enable-profiling   //启用profile功能
--without-embedded-server   //去除embedded
--without-debug     //去除debug模式,提高性能
--without-bench    //去除bench模式,提高性能
#make && make install  //编译并安装
#cp support-files/my-medium.cnf /etc/my.cnf   //复制mysql配置文件
#chown -R mysql:mysql /usr/local/mysql  //修改目录属主
#/usr/local/mysql/bin/mysql_install_db --user=mysql &    //初始化mysql
#/usr/local/mysql/bin/mysqld_safe --user=mysql &    //启动mysql
#/usr/local/mysql/bin/mysqladmin -uroot password 123456   //设置mysql管理员密码
#echo "/usr/local/mysql/bin/mysqld_safe --user=mysql &" >>/etc/rc.local    //加入开机自启动
#echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile   //加入环境变量
#source /etc/profile   //更新环境变量
编译安装BIND
#wget ftp://192.168.1.31/bind-9.6.0-P1.tar.gz   下载源码包
#tar zxvf bind-9.6.0-P1.tar.gz   解压源码包
#cd bind-9.6.0-P1   进入解压目录
#./configure --prefix=/usr/local/named –with-dlz-mysql --enable-threads=no --with-openssl=no   编译前配置
#make && make install   编译并安装
#/usr/local/named/sbin/rndc-confgen > /usr/local/named/etc/rndc.conf  生成rndc控制命令的key文件
#tail -10 /usr/local/named/etc/rndc.conf |head -9|sed s/#\ //g > /usr/local/named/etc/named.conf   从rndc.conf中提取named.conf用的key
准备BIND DLZ所需的数据库
grant all privileges on named.* to named@localhost identified by "namedpass";    创建一个供BIND DLZ使用的账号。
create database named;   创建数据库
use named;    这个大家都懂得
create table dns_records(zone text,host text,type text,data text not null,ttl int(11),mx_priority text,refresh int(11),retry int(11),expire int(11),minimum int(11),serial bigint(20),resp_person text,primary_ns text);
create table xfr_table(zone text,client text);   
创建BIND DLZ所组要的表
配置BIND DLZ
#vi /usr/local/named/etc/named.conf    编辑named.conf
在named.conf添加如下内容:
dlz "Mysql zone" {      标记这是一个DLZ区域
   database "mysql     区域数据使用mysql数据库存储
   {host=localhost dbname=named ssl=false user=named pass=namedpass}     链接数据库
   {select zone from dns_records where zone = '%zone%'}
   {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 from dns_records where zone = '%zone%' and host = '%record%'}";    
查询的信息
启动BIND DLZ服务,验证DLZ信息是否成功加载
#/usr/local/named/sbin/named –4   启动DNS服务
如果你看到如下红色标注的信息,说明DLZ加载成功
Oct 11 23:24:15 localhost named[2185]: loading configuration from '/usr/local/named/etc/named.conf'
Oct 11 23:24:15 localhost named[2185]: max open files (1024) is smaller than max sockets (4096)
Oct 11 23:24:15 localhost named[2185]: using default UDP/IPv4 port range: [1024, 65535]
Oct 11 23:24:15 localhost named[2185]: using default UDP/IPv6 port range: [1024, 65535]
Oct 11 23:24:15 localhost named[2185]: no IPv6 interfaces found
Oct 11 23:24:15 localhost named[2185]: listening on IPv4 interface lo, 127.0.0.1#53
Oct 11 23:24:15 localhost named[2185]: listening on IPv4 interface eth0, 192.168.81.128#53
Oct 11 23:24:15 localhost named[2185]: Loading 'Mysql zone' using driver mysql
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: 0.IN-ADDR.ARPA
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: 127.IN-ADDR.ARPA
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: 254.169.IN-ADDR.ARPA
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: 2.0.192.IN-ADDR.ARPA
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: 255.255.255.255.IN-ADDR.ARPA
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: 0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.ARPA
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.ARPA
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: D.F.IP6.ARPA
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: 8.E.F.IP6.ARPA
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: 9.E.F.IP6.ARPA
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: A.E.F.IP6.ARPA
Oct 11 23:24:15 localhost named[2185]: automatic empty zone: B.E.F.IP6.ARPA
Oct 11 23:24:15 localhost named[2185]: command channel listening on 127.0.0.1#953
Oct 11 23:24:15 localhost named[2185]: running
验证DLZ动态加载功能
mysql –uroot –p123456    链接mysql
use named;    使用named库
insert into dns_records(zone,host,type,data,ttl,retry) values ('xiaocui.com','mail','A','10.1.1.100','800','10');      插入一条记录
验证刚刚插入的信息能都动态加载
[root@localhost bind-9.6.0-P1]# nslookup 
> server 127.0.0.1
Default server: 127.0.0.1
Address: 127.0.0.1#53 
> mail.xiaocui.com
Server:         127.0.0.1
Address:        127.0.0.1#53
Name:   mail.xiaocui.com
Address: 10.1.1.100
更改一下mail.xiaocui.com的A记录
update dns_records set data='10.0.1.200' where host='mail';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0
使用nslookup再验证一下刚刚修改的信息是否生效
[root@localhost ~]# nslookup 
> server 127.0.0.1
Default server: 127.0.0.1
Address: 127.0.0.1#53 
> mail.xiaocui.com
Server:         127.0.0.1
Address:        127.0.0.1#53
Name:   mail.xiaocui.com
Address: 10.0.1.200
从上面结果可以看出来DLZ功能已经生效了,我们新插入和修改的记录能很快的加载,并且无需重新加载配置文件或重启服务。
DLZ我们就先了解到这里。。