鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 管理员笔记 > bind > >

bind-dlz结合mysql实现智能DNS

来源:互联网 作者:佚名 时间:2015-01-06 20:08
下面介绍bind结合mysql实现智能dns,以centos-6 32为例安装 安装mysql yum install gcc gcc-c++ openssl-devel wget ncurses-devel make groupadd mysql useradd -g mysql mysql -s /sbin/nologin cd /tmp wget http://cdn.mysql.com/Downloads/ MySQL -5.1/
  • 下面介绍bind结合mysql实现智能dns,以centos-6 32为例安装

    安装mysql


    yum install gcc gcc-c++ openssl-devel wget ncurses-devel make 
    groupadd mysql 
    useradd -g mysql mysql -s /sbin/nologin 
    cd /tmp 
    wget http://cdn.mysql.com/Downloads/MySQL-5.1/mysql-5.1.65.tar.gz 
    tar xzf mysql-5.1.65.tar.gz 
    cd mysql-5.1.65 
    ./configure --prefix=/usr/local/mysql/ --without-pthread --with-unix-socket-path=/tmp/mysql.sock --with-extra-charsets=gbk,gb2312,utf8 
    make 
    make install 
    cp support-files/my-medium.cnf /etc/my.cnf 
    /usr/local/mysql/bin/mysql_install_db --user=mysql 
    chown -R root.mysql /usr/local/mysql 
    chown -R mysql /usr/local/mysql/data 
    cp support-files/mysql.server /etc/init.d/mysqld 
    chown root.root /etc/rc.d/init.d/mysqld 
    chmod 755 /etc/rc.d/init.d/mysqld 
    chkconfig --add mysqld 
    chkconfig  mysqld on 
    ln -s /usr/local/mysql/bin/mysql /usr/bin 
    ln -s /usr/local/mysql/bin/mysqladmin /usr/bin 
    service mysqld start 
    mysqladmin -u root password root
    

    安装bind

    cd /tmp 
    wget http://ftp.isc.org/isc/bind9/cur/9.9/bind-9.9.1-P2.tar.gz 
    tar xzf bind-9.9.1-P2.tar.gz 
    cd bind-9.9.1-P2 
    ./configure --prefix=/usr/local/bind/ --disable-openssl-version-check --with-dlz-mysql=/usr/local/mysql 
    make 
    make install
    

    配置bind
    cd /usr/local/bind/etc 
    ../sbin/rndc-confgen -r /dev/urandom >rndc.conf 
    tail -n10 rndc.conf | head -n9 | sed -e s/#\//g>named.conf 
      
    vi named.conf 
    在后面增加: 
    include "/usr/local/bind/etc/CHINANET.conf"; //联通ACL 
    include "/usr/local/bind/etc/CNC.conf"; //电信ACL 
    include "/usr/local/bind/etc/view.conf"; //DLZ相关的配置
    

    view.conf内容:

     

    view "CHINANET_view" { 
      
     match-clients            { CHINANET; }; 
     allow-query-cache       { none; }; 
     allow-recursion         { any; }; 
     allow-transfer          { none; }; 
     recursion               yes; 
    dlz "Mysql zone" { 
       database "mysql 
       {host=localhost dbname=dns_data ssl=false user=root pass=root} 
       {select zone from dns_records where zone = '%zone%' and view='CHINANET'} 
       {select ttl, type, mx_priority, case when lower(type)='txt' then concat('\"', data, '\"') 
            else data end from dns_records where zone = '%zone%' and host = '%record%' 
            and not (type = 'SOA' or type = 'NS') and view='CHINANET'} 
       {select ttl, type, mx_priority, data, resp_person, serial, refresh, retry, expire, minimum 
            from dns_records where zone = '%zone%' and (type = 'SOA' or type='NS') and view='CHINANET'} 
       {select ttl, type, host, mx_priority, data, resp_person, serial, refresh, retry, expire, 
            minimum from dns_records where zone = '%zone%' and not (type = 'SOA' or type = 'NS') and view='CHINANET'} 
       {select zone from xfr_table where zone = '%zone%' and client = '%client%' and view='CHINANET'} 
       {update data_count set count = count + 1 where zone ='%zone%' and view='CHINANET'}"; 
    }; 
    ); 
      
    view "CNC_view" { 
      
     match-clients            { CNC; }; 
     allow-query-cache       { none; }; 
     allow-recursion         { any; }; 
     allow-transfer          { none; }; 
     recursion               yes; 
    dlz "Mysql zone" { 
       database "mysql 
       {host=localhost dbname=dns_data ssl=false user=root pass=root} 
       {select zone from dns_records where zone = '%zone%' and view='CNC'} 
       {select ttl, type, mx_priority, case when lower(type)='txt' then concat('\"', data, '\"') 
            else data end from dns_records where zone = '%zone%' and host = '%record%' 
            and not (type = 'SOA' or type = 'NS') and view='CNC'} 
       {select ttl, type, mx_priority, data, resp_person, serial, refresh, retry, expire, minimum 
            from dns_records where zone = '%zone%' and (type = 'SOA' or type='NS') and view='CNC'} 
       {select ttl, type, host, mx_priority, data, resp_person, serial, refresh, retry, expire, 
            minimum from dns_records where zone = '%zone%' and not (type = 'SOA' or type = 'NS') and view='CNC'} 
       {select zone from xfr_table where zone = '%zone%' and client = '%client%' and view='CNC'} 
       {update data_count set count = count + 1 where zone ='%zone%' and view='CNC'}"; 
    }; 
    ); 
      
    view "any_view" { 
      
     match-clients            { any; }; 
     allow-query-cache       { none; }; 
     allow-recursion         { any; }; 
     allow-transfer          { none; }; 
     recursion               yes; 
    dlz "Mysql zone" { 
       database "mysql 
       {host=localhost dbname=dns_data ssl=false user=root pass=root} 
       {select zone from dns_records where zone = '%zone%' and view='any'} 
       {select ttl, type, mx_priority, case when lower(type)='txt' then concat('\"', data, '\"') 
            else data end from dns_records where zone = '%zone%' and host = '%record%' 
            and not (type = 'SOA' or type = 'NS') and view='any'} 
       {select ttl, type, mx_priority, data, resp_person, serial, refresh, retry, expire, minimum 
            from dns_records where zone = '%zone%' and (type = 'SOA' or type='NS') and view='any'} 
       {select ttl, type, host, mx_priority, data, resp_person, serial, refresh, retry, expire, 
            minimum from dns_records where zone = '%zone%' and not (type = 'SOA' or type = 'NS') and view='any'} 
       {select zone from xfr_table where zone = '%zone%' and client = '%client%' and view='any'} 
       {update data_count set count = count + 1 where zone ='%zone%' and view='any'}"; 
    }; 
    );
    

    数据库配置
    mysql>create database dns_data;        //创建数据库名为 dns_data  www.it165.net
       mysql>use dns_data; 
       DROP TABLE IF EXISTS `dns_records`; 
    CREATE TABLE `dns_records` ( 
       `id` int(10) unsigned NOT NULL auto_increment, 
       `zone` varchar(255) NOT NULL, 
       `host` varchar(255) NOT NULL default '@', 
       `type` enum('MX','CNAME','NS','SOA','A','PTR') NOT NULL, 
       `data` varchar(255) default NULL, 
       `ttl` int(11) NOT NULL default '800', 
       `view` char(20) default 'any',     //any 代表默认,SOA 查询需,其它可以分,CNC…… 
       `mx_priority` int(11) default NULL, 
       `refresh` int(11) NOT NULL default '3600', 
       `retry` int(11) NOT NULL default '3600', 
       `expire` int(11) NOT NULL default '86400', 
       `minimum` int(11) NOT NULL default '3600', 
       `serial` bigint(20) NOT NULL default '2008082700', 
       `resp_person` varchar(64) NOT NULL default 'root.domain.com.', 
       `primary_ns` varchar(64) NOT NULL default 'ns1.domain.com.', 
       `data_count` int(11) NOT NULL default '0', 
       PRIMARY KEY          (`id`), 
       KEY `type` (`type`), 
       KEY `host` (`host`), 
       KEY `zone` (`zone`) 
    ) 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实例


    --SOA 
      
         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); 
      
         --@ NS 
      
         INSERT INTO `dns_records` (`zone`, `host`, `type`, `data`) VALUES      ('centos.bz', '@', 'NS', 'ns1.centos.bz.'),      ('centos.bz', '@', 'NS', 'ns2.centos.bz.'); 
      
         --NS A 
    
         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'); 
      
         --A 
      
         INSERT INTO `dns_records` (`zone`, `host`, `type`, `data`, `ttl`, `view`) VALUES   ('centos.bz', 'www', 'A', '210.51.36.116', 3600, 'CNC'),      ('centos.bz', 'www', 'A', '221.238.249.178', 3600, 'TELECOM'),      ('centos.bz', 'www', 'A', '211.103.156.230', 3600, 'any'); 
      
         --CNAME 
      
         INSERT INTO dns_records (zone,host,type,DATA,view)      VALUES ('centos.bz', 'man', 'CNAME', 'www','CNC'),      ('centos.bz', 'man', 'CNAME', 'www','TELECOM'),  ('centos.bz', 'man', 'CNAME', 'www','any');
    
     
网友评论
<