Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1729911
  • 博文数量: 782
  • 博客积分: 2455
  • 博客等级: 大尉
  • 技术积分: 4140
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-06 21:37
个人简介

Linux ,c/c++, web,前端,php,js

文章分类

全部博文(782)

文章存档

2015年(8)

2014年(28)

2013年(110)

2012年(307)

2011年(329)

分类:

2012-02-09 17:16:18

原文地址:dns+mysql学习手札 作者:liuhanzhao

这两天在学习dns相关的东西,就从网上下到了bind-9.4.2源码包,通过configure --help查看,发现居然可以支持多种数据库

--with-dlz-postgres=PATH Build with Postgres DLZ driver yes|no|path.
                               (Required to use Postgres with DLZ)
--with-dlz-mysql=PATH Build with MySQL DLZ driver yes|no|path.
                               (Required to use MySQL with DLZ)
--with-dlz-bdb=PATH Build with Berkeley DB DLZ driver yes|no|path.
                               (Required to use Berkeley DB with DLZ)
--with-dlz-filesystem=PATH Build with filesystem DLZ driver yes|no.
                               (Required to use file system driver with DLZ)
--with-dlz-ldap=PATH Build with LDAP DLZ driver yes|no|path.
                               (Required to use LDAP with DLZ)
--with-dlz-odbc=PATH Build with ODBC DLZ driver yes|no|path.
                               (Required to use ODBC with DLZ)
--with-dlz-stub=PATH   Build with stub DLZ driver yes|no.
                               (Required to use stub driver with DLZ)

这里的DLZ又是什么呢
DLZ (Dynamically Loadable Zones) is a patch for BIND version 9 that simplifies BIND administration and reduces memory usage and startup time. DLZ allows you to store your zone data in a database. Unlike using scripts, the changes in your database are immediately reflected in BIND's response to DNS queries, so there is no need to reload or restart BIND. You see, BIND "dynamically loads" the "zone" data it needs to answer a query from the database.

既然这样,我就想自己也来做个bind+mysql的组合
我的环境,vmware+FreeBSD7.0
软件:bind-9.4.2.tar.gz
     mysql-5.1.24-rc.tar.gz

也许有人会说我为什么不使用ports安装,而要从源码编译,说来惭愧,不知道是不是我vmware配置上哪里有问题,不管我用nat方式还是host-only的方式,我的bsd都没法联网,索性也就不用port方式了,反正源码方式都会了,port就更不在话下了,废话不多说了

mysql的安装
mysqldownload到mysql的源码包,我使用的是5.1.24

tar zxvf mysql-5.1.24-rc.tar.gz
cd mysql-5.1.24-rc
./configure --prefix=/usr/local/mysql --with-charset=gb2312 --with-mysqld-user=mysql
make
make install

添加mysql用户和组
pw groupadd mysql
pw useradd mysql -g mysql

cd /usr/local/mysql/
chown -R mysql:mysql .
/usr/local/mysql/bin//mysql_install_db
chown -R root .
chown -R mysql var

启动mysql
/usr/local/mysql/bin/mysqld_safe &

到此mysql就安装完毕了,接下来进行bind的安装

由于初始mysql的root用户是没有设置密码的,最好使用

mysqladmin -u user_name -h host_name password "newpwd"

进行密码的更改


mysql数据库的建立

CREATE DATABASE dns;
GRANT ALL ON dns.* TO 'dns'@'localhost' IDENTIFIED BY 'YourPasswordHere'


CREATE TABLE `records` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `zone` varchar(255) NOT NULL,
  `ttl` int(11) NOT NULL default '86400',
  `type` varchar(255) NOT NULL,
  `host` varchar(255) NOT NULL default '@',
  `mx_priority` int(11) default NULL,
  `data` text,
  `primary_ns` varchar(255) default NULL,
  `resp_contact` varchar(255) default NULL,
  `serial` bigint(20) default NULL,
  `refresh` int(11) default NULL,
  `retry` int(11) default NULL,
  `expire` int(11) default NULL,
  `minimum` int(11) default NULL,
  PRIMARY KEY (`id`),
  KEY `type` (`type`),
  KEY `host` (`host`),
  KEY `zone` (`zone`)
);

records表将记录所有zones文件的信息

INSERT INTO `records` (`id`, `zone`, `ttl`, `type`, `host`, `mx_priority`, `data`, `primary_ns`, `resp_contact`, `serial`, `refresh`, `retry`, `expire`, `minimum`) VALUE (100, 'test.cu', 86400, 'SOA', '@', NULL, NULL, 'Real_server01.test.cu.', 'admin.test.cu.', 2007080601, 10800, 7200, 604800, 86400);
INSERT INTO `records` (`id`, `zone`, `ttl`, `type`, `host`, `mx_priority`, `data`, `primary_ns`, `resp_contact`, `serial`, `refresh`, `retry`, `expire`, `minimum`) VALUE (101, 'test.cu', 86400, 'NS', '@', NULL, 'Real_server01.test.cu.', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `records` (`id`, `zone`, `ttl`, `type`, `host`, `mx_priority`, `data`, `primary_ns`, `resp_contact`, `serial`, `refresh`, `retry`, `expire`, `minimum`) VALUE (104, 'test.cu', 86400, 'A', '@', NULL, '192.168.2.188', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `records` (`id`, `zone`, `ttl`, `type`, `host`, `mx_priority`, `data`, `primary_ns`, `resp_contact`, `serial`, `refresh`, `retry`, `expire`, `minimum`) VALUE (105, 'test.cu', 86400, 'A', 'www', NULL, '192.168.2.188', NULL, NULL, NULL, NULL, NULL, NULL, NULL);


bind的安装

./configure --prefix=/usr/local/bind --enable-largefile --enable-threads=no --with-dlz-mysql
make
make install
cd /usr/local/bind/sbin
./rndc-confgen >../etc/rndc.conf
cd ../etc
tail -10 rndc.conf |head -9 |sed s/#\ //g >named.conf
然后在named.conf中加入

dlz "Mysql zone" {
   database "mysql
   {host=localhost dbname=dns user=dns pass=YourPasswordHere}
   {SELECT zone FROM records WHERE zone = '%zone%'}
   {SELECT ttl, type, mx_priority, IF(type = 'TXT', CONCAT('\"
',data,'\"'), data) AS data
    FROM records
    WHERE zone = '%zone%' AND host = '%record%' AND type <> 'SOA' AND type <> 'NS'}
   {SELECT ttl, type, data, primary_ns, resp_contact, serial, refresh, retry, expire, minimum
    FROM records
    WHERE zone = '%zone%' AND (type = 'SOA' OR type='NS')}
   {SELECT ttl, type, host, mx_priority, IF(type = 'TXT', CONCAT('\"
',data,'\"'), data) AS data, resp_contact, serial, refresh, retry, expire, minimum
    FROM records
    WHERE zone = '%zone%' AND type <> 'SOA' AND type <> 'NS'}
   {SELECT zone FROM xfr where zone='%zone%' AND client = '%client%'}"
;
};


ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.16 /usr/lib/

然后执行/usr/local/bind/sbin/named -uroot -d1 -g &
运行bind

之后进行测试
dig @192.168.2.188 www.test.cu返回如下结果

dig @192.168.2.188 www.test.cu
07-May-2008 20:00:32.635
Query String: SELECT zone FROM records WHERE zone = 'www.test.cu'

07-May-2008 20:00:32.636
Query String: SELECT zone FROM records WHERE zone = 'test.cu'

07-May-2008 20:00:32.636
Query String: SELECT ttl, type, mx_priority, IF(type = 'TXT', CONCAT('"',data,'"'), data) AS data
    FROM records
    WHERE zone = 'test.cu' AND host = 'www' AND type <> 'SOA' AND type <> 'NS'

07-May-2008 20:00:32.637
Query String: SELECT ttl, type, mx_priority, IF(type = 'TXT', CONCAT('"',data,'"'), data) AS data
    FROM records
    WHERE zone = 'test.cu' AND host = '@' AND type <> 'SOA' AND type <> 'NS'

07-May-2008 20:00:32.638
Query String: SELECT ttl, type, data, primary_ns, resp_contact, serial, refresh, retry, expire, minimum
    FROM records
    WHERE zone = 'test.cu' AND (type = 'SOA' OR type='NS')


; <<>> DiG 9.2.4 <<>> @192.168.2.188 www.test.cu
; (1 server found)
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24586
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0

;; QUESTION SECTION:
;www.test.cu. IN A

;; ANSWER SECTION:
www.test.cu. 86400 IN A 192.168.2.188

;; AUTHORITY SECTION:
test.cu. 86400 IN NS Real_server01.test.cu.

;; Query time: 5 msec
;; SERVER: 192.168.2.188#53(192.168.2.188)
;; WHEN: Wed May 7 20:00:32 2008
;; MSG SIZE rcvd: 73


说明现在的bind已经可以正常运行了

参考文章:
   
   
阅读(1025) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~