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 https://www.centos.bz/wp-content/uploads/2012/02/CHINANET.acl
  2. wget https://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) 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) 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.      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');
  11.  
  12.      --A
  13.  
  14.      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);
  15.  
  16.      --CNAME
  17.  
  18.      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:https://www.centos.bz/wp-content/uploads/2012/02/CNC.acl
电信acl:https://www.centos.bz/wp-content/uploads/2012/02/CHINANET.acl

标签:MySQLDNS 发布于:2019-11-23 01:59:25