bind-dlz結合mysql實現智能DNS

下面介紹bind結合mysql實現智能dns,以centos-6 32爲例安裝

安裝mysql

  1. yum install gcc gcc-c++ openssl-devel wget ncurses-devel make

  2. groupadd mysql

  3. useradd -g mysql mysql -s /sbin/nologin

  4. cd /tmp

  5. wget http://cdn.mysql.com/Downloads/MySQL-5.1/mysql-5.1.65.tar.gz

  6. tar xzf mysql-5.1.65.tar.gz

  7. cd mysql-5.1.65

  8. ./configure --prefix=/usr/local/mysql/ --without-pthread --with-unix-socket-path=/tmp/mysql.sock --with-extra-charsets=gbk,gb2312,utf8

  9. make

  10. make install

  11. cp support-files/my-medium.cnf /etc/my.cnf

  12. /usr/local/mysql/bin/mysql_install_db --user=mysql

  13. chown -R root.mysql /usr/local/mysql

  14. chown -R mysql /usr/local/mysql/var

  15. cp support-files/mysql.server /etc/init.d/mysqld

  16. chown root.root /etc/rc.d/init.d/mysqld 

  17. chmod 755 /etc/rc.d/init.d/mysqld

  18. chkconfig --add mysqld

  19. chkconfig  mysqld on

  20. ln -s /usr/local/mysql/bin/mysql /usr/bin

  21. ln -s /usr/local/mysql/bin/mysqladmin /usr/bin

  22. service mysqld start

  23. mysqladmin -u root password root

安裝bind

  1. cd /tmp

  2. wget http://ftp.isc.org/isc/bind9/cur/9.9/bind-9.9.1-P2.tar.gz

  3. tar xzf bind-9.9.1-P2.tar.gz

  4. cd bind-9.9.1-P2

  5. ./configure --prefix=/usr/local/bind/ --disable-openssl-version-check --with-dlz-mysql=/usr/local/mysql

  6. make

  7. make install

配置bind

  1. cd /usr/local/bind/etc

  2. ../sbin/rndc-confgen -r /dev/urandom >rndc.conf

  3. tail -n10 rndc.conf | head -n9 | sed -e s/#\//g>named.conf

  4.  

  5. vi named.conf

  6. 在後面增加:

  7. include "/usr/local/bind/etc/CHINANET.acl"; //聯通ACL

  8. include "/usr/local/bind/etc/CNC.acl"; //電信ACL

  9. include "/usr/local/bind/etc/view.conf"; //DLZ相關的配置

下載acl文件:

  1. wget http://www.centos.bz/wp-content/uploads/2012/02/CHINANET.acl

  2. wget http://www.centos.bz/wp-content/uploads/2012/02/CNC.acl

view.conf內容:
其中需要修改的字段爲user=root pass=root,即此處mysql用戶爲root,密碼爲root。

  1. view "CHINANET_view" { 

  2.   match-clients  { CHINANET; }; 

  3.   allow-query-cache { none; }; 

  4.   allow-recursion { none; }; 

  5.   allow-transfer { none; }; 

  6.   recursion no; 

  7.  

  8.     dlz "Mysql zone" { 

  9.     database "mysql 

  10.     {host=127.0.0.1 dbname=dns_data ssl=false port=3306 user=root pass=root} 

  11.     {select zone from dns_records where zone = '$zone$' and  view = 'any' limit 1} 

  12.     {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=(select view from dns_records where zone = '$zone$' and host = '$record$' and (view='CHINANET' or view='any') order by priority asc limit 1)}"; 

  13. }; 

  14. };

  15. view "CNC_view" { 

  16.   match-clients  { CNC; }; 

  17.   allow-query-cache { none; }; 

  18.   allow-recursion { none; }; 

  19.   allow-transfer { none; }; 

  20.   recursion no; 

  21.  

  22.     dlz "Mysql zone" { 

  23.     database "mysql 

  24.     {host=127.0.0.1 dbname=dns_data ssl=false port=3306 user=root pass=root} 

  25.     {select zone from dns_records where zone = '$zone$' and  view = 'any' limit 1} 

  26.     {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=(select view from dns_records where zone = '$zone$' and host = '$record$' and (view='CNC' or view='any') order by priority asc limit 1)}"; 

  27. }; 

  28. };

  29. view "any_view" { 

  30.   match-clients  { any; }; 

  31.   allow-query-cache { none; }; 

  32.   allow-recursion { none; }; 

  33.   allow-transfer { none; }; 

  34.   recursion no; 

  35.  

  36.     dlz "Mysql zone" { 

  37.     database "mysql 

  38.     {host=127.0.0.1 dbname=dns_data ssl=false port=3306 user=root pass=root} 

  39.     {select zone from dns_records where zone = '$zone$' and  view = 'any' limit 1} 

  40.     {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'}"; 

  41. }; 

  42. };

數據庫配置

  1. mysql>create database dns_data;        //創建數據庫名爲 dns_data 

  2.    mysql>use dns_data; 

  3.    DROP TABLE IF EXISTS `dns_records`; 

  4. CREATE TABLE `dns_records` ( 

  5.    `id` int(10) unsigned NOT NULL auto_increment, 

  6.    `zone` varchar(255) NOT NULL, 

  7.    `host` varchar(255) NOT NULL default '@', 

  8.    `type` enum('MX','CNAME','NS','SOA','A','PTR') NOT NULL, 

  9.    `data` varchar(255) default NULL, 

  10.    `ttl` int(11) NOT NULL default '800', 

  11.    `view` char(20) default 'any',     //any 代表默認,SOA 查詢需,其它可以分,CNC…… 

  12.    `mx_priority` int(11) default NULL, 

  13.    `priority` int(3) default 255,  //any爲255,其它如CNC,CHINANET等線路爲200

  14.    `refresh` int(11) NOT NULL default '3600', 

  15.    `retry` int(11) NOT NULL default '3600', 

  16.    `expire` int(11) NOT NULL default '86400', 

  17.    `minimum` int(11) NOT NULL default '3600', 

  18.    `serial` bigint(20) NOT NULL default '2008082700', 

  19.    `resp_person` varchar(64) NOT NULL default 'root.domain.com.', 

  20.    `primary_ns` varchar(64) NOT NULL default 'ns1.domain.com.', 

  21.    `data_count` int(11) NOT NULL default '0', 

  22.    PRIMARY KEY          (`id`), 

  23.    KEY `type` (`type`), 

  24.    KEY `host` (`host`), 

  25.    KEY `zone` (`zone`) 

  26. ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=gbk;

啓動bind服務

# /usr/local/bind/sbin/named -uroot -g -d 9 //調試狀態,如果沒有報錯說明環境配置正確。
做成啓動服務. Debug 的時候多用此模式啓動bind.
# /usr/local/bind/sbin/rndc reload 重載 named.conf 相關配置文件.
# /usr/local/bind/sbin/named -uroot -c /usr/local/bind/etc/named.conf 啓動 bind 服務. 

#插入記錄的sql實例

  1. --SOA 

  2.  

  3.      INSERT   INTO   `dns_records`   (`zone`,   `host`,   `type`,   `data`,   `ttl`,`mx_priority`,   `refresh`,   `retry`,   `expire`,      `minimum`, `serial`, `resp_person`, `primary_ns`, `data_count`) VALUES    ('centos.bz',     '@',   'SOA',   'ns1.centos.bz.',    10,   NULL,     3600,    3600,   86400,    10,   2008082700, 'root.centos.bz.', 'ns1.centos.bz.', 0); 

  4.  

  5.      --@ NS 

  6.  

  7.      INSERT INTO `dns_records` (`zone`, `host`, `type`, `data`) VALUES      ('centos.bz', '@', 'NS', 'ns1.centos.bz.'),      ('centos.bz', '@', 'NS', 'ns2.centos.bz.'); 

  8.  

  9.      --NS A 

  10.  

  11.      INSERT INTO `dns_records` (`zone`, `host`, `type`, `data`) VALUES      ('centos.bz', 'ns1', 'A', '211.100.72.137'),   ('centos.bz', 'ns2', 'A', '219.232.244.11'); 

  12.  

  13.      --A 

  14.  

  15.      INSERT INTO `dns_records` (`zone`, `host`, `type`, `data`, `ttl`, `view`,`priority`) VALUES   ('centos.bz', 'www', 'A', '210.51.36.116', 3600, 'CNC',200),      ('centos.bz', 'www', 'A', '221.238.249.178', 3600, 'CHINANET',200),      ('centos.bz', 'www', 'A', '211.103.156.230', 3600, 'any',255); 

  16.  

  17.      --CNAME 

  18.  

  19.      INSERT INTO dns_records (zone,host,type,DATA,view,,priority)      VALUES ('centos.bz', 'man', 'CNAME', 'www','CNC',200),      ('centos.bz', 'man', 'CNAME', 'www','CHINANET',200),  ('centos.bz', 'man', 'CNAME', 'www','any',255);

聯通acl:http://www.centos.bz/wp-content/uploads/2012/02/CNC.acl
電信acl:http://www.centos.bz/wp-content/uploads/2012/02/CHINANET.acl

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章