CentOS 6.3 x64下借助MySQL和DNS view实现智能DNS
开篇说明
上次的文章简单使用了BIND的视图功能实现了简单的智能dns,此篇博文结合了MySQL实现真正意义上的智能DNS系统
需要准备东西
首先yum安装mysql。。。
yum install mysql mysql-server mysql-devel
下载bind主程序源码:
ftp://ftp.isc.org/isc/bind9/9.8.5-P1/bind-9.8.5-P1.tar.gz
下载bind-sdb驱动:
http://nchc.dl.sourceforge.net/project/mysql-bind/mysql-bind/mysql-bind-0.2%20src/mysql-bind.tar.gz
解压:
tar xfbind-9.8.5-P1.tar.gz
tar xfmysql-bind.tar.gz
编译安装bind
将mysql-sdb的补丁文件拷贝到bind安装源文件目录下
cp mysql-bind/mysqldb.c bind-9.8.5-P1/bin/named/
cp mysql-bind/mysqldb.h bind-9.8.5-P1/bin/named/
修改bind源码目录下的bin/named/Makefile.in修改以下几行,其中DBDRIVER_LIBS后的参数可以使用/usr/bin/mysql_config --libs获取
DBDRIVER_OBJS= mysqldb.@O@
DBDRIVER_SRCS= mysqldb.c
DBDRIVER_INCLUDES= -I'/usr/include/mysql'
DBDRIVER_LIBS= -rdynamic -L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -lssl-lcrypto
修改bind源码目录下的bin/named/main.c,添加以下几行
/*#include "xxdb.h" */
后面添加
#include "mysqldb.h"
/*xxdb_init();*/
在后面添加
mysqldb_init();
再找到
/*xxdb_clear();*/
在后面添加
mysqldb_clear();
修改bin/named/mysqldb.c
将
#include <named/mysqldb.h>
修改为
#include <bin/named/mysqldb.h>
编译安装
./configure--prefix=/usr --sysconfdir=/etc
make&& make install
下载电信和联通的地址列表ACL,存放在/etc/named目录下
http://www.CentOS.bz/wp-content/uploads/2012/02/CHINANET.acl
http://www.centos.bz/wp-content/uploads/2012/02/CNC.acl
在/etc/named.conf添加2条,从而将电信和联通的ACL地址列表包含进来
include"/etc/named/CHINANET.acl"
include"/etc/named/CNC.acl"
然后在主配置文件中定义2个视图:
CNC是联通的视图 ,CHINANET是电信的视图
view CNC{
match-clients { CNC; };
recursion no;
zone "." IN {
type hint;
file "named.ca";
};
zone "localhost" IN {
type master;
file "named.localhost";
};
zone "0.0.127.in-addr.arpa" IN {
type master;
file "named.loopback";
};
zone "lustlost.com" IN {
type master;
notify no;
database "mysqldb smartdns CNClocalhost root 123456"; #这里定义的数据库相关的配置,字段分别为:SQLtype DataBase Table Host User Password
};
};
view CHINANET {
match-clients { CHINANET; };
recursion no;
zone "." IN {
type hint;
file "named.ca";
};
zone "localhost" IN {
type master;
file "named.localhost";
};
zone "0.0.127.in-addr.arpa" IN {
type master;
file "named.loopback";
};
zone "lustlost.com" IN {
type master;
notify no;
database "mysqldb smartdnsCHINANET localhost root 123456";
};
};
然后创建数据库:
create database smartdns;
use smartdns;
创建联通表
CREATE TABLE CNC(name varchar(255) default NULL,ttl int(11) default NULL,rdtypevarchar(255) default NULL,rdata varchar(255) default NULL) TYPE=MyISAM;
插入SOA其实授权信息
INSERTINTO CNC VALUES('lustlost.com',259200,'SOA','lustlost.com.www.lustlost.com.20130617 28800 720086400 28800');
插入NS记录(实验环境,地址皆为内网IP)
INSERT INTO CNC VALUES ('lustlost.com', 259200, 'NS','ns1.lustlost.com.'),('lustlost.com', 259200, 'NS', 'ns2.lustlost.com.'),('lustlost.com', 259200, 'MX', '10 mail.lustlost.com.'),('ns1.lustlost.com',259200, 'A', '192.168.1.1'),('ns2.lustlost.com', 259200, 'A','192.168.1.2'),('mail.lustlost.com', 259200, 'A','192.168.1.3'),('www.lustlost.com', 259200, 'A', '192.168.1.4');
创建电信表
CREATE TABLE CHINANET (name varchar(255) default NULL,ttl int(11) default NULL,rdtypevarchar(255) default NULL,rdata varchar(255) default NULL) TYPE=MyISAM;
插入SOA其实授权信息
INSERT INTO CHINANET VALUES ('lustlost.com',259200,'SOA','lustlost.com.www.lustlost.com.2013061728800 7200 86400 28800');
插入NS记录(实验环境,地址皆为内网IP)
INSERT INTO CHINANET VALUES ('lustlost.com', 259200, 'NS','ns1.lustlost.com.'),('lustlost.com', 259200, 'NS', 'ns2.lustlost.com.'),('lustlost.com', 259200, 'MX', '10 mail.lustlost.com.'),('ns1.lustlost.com',259200, 'A', '192.168.2.1'),('ns2.lustlost.com', 259200, 'A', '192.168.2.2'),('mail.lustlost.com',259200, 'A', '192.168.2.3'),('www.lustlost.com', 259200, 'A', '192.168.2.4');
完毕....