接上篇文章《Centos使用Bind配置智能DNS(2)》,此篇文章主要是bind+dlz+mysql组合!
0 1 2 3 4 |
[root@MASTER named]# /usr/local/mysql/bin/mysql -V /usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.6.28, for Linux (x86_64) using EditLine wrapper [root@MASTER named]# /usr/local/mysql/bin/mysql -uroot -p ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/tmp/mysql.sock' (2) [root@MASTER named]# /etc/init.d/mysqld start |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
mysql> update mysql.user set password=PASSWORD('123456') where user='root'; mysql> flush privileges; mysql> create database dnsdb default character set utf8 collate utf8_general_ci; mysql> create user 'dnsdb'@'localhost' identified by '123456'; mysql> grant all on dnsdb.* to 'dnsdb'@'localhost' identified by '123456'; mysql> 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 '120', -> `view` char(20) default 'DF', -> `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 '2015050100', -> `resp_person` varchar(64) NOT NULL default 'root.t4x.org.', -> `primary_ns` varchar(64) NOT NULL default 'ns1.t4x.org.', -> `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=utf8; Query OK, 0 rows affected (0.13 sec) mysql> desc dns_records; +-------------+-----------------------------------------+------+-----+---------------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-----------------------------------------+------+-----+---------------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | zone | varchar(255) | NO | MUL | NULL | | | host | varchar(255) | NO | MUL | @ | | | type | enum('MX','CNAME','NS','SOA','A','PTR') | NO | MUL | NULL | | | data | varchar(255) | YES | | NULL | | | ttl | int(11) | NO | | 120 | | | view | char(20) | YES | | DF | | | mx_priority | int(11) | YES | | NULL | | | refresh | int(11) | NO | | 3600 | | | retry | int(11) | NO | | 3600 | | | expire | int(11) | NO | | 86400 | | | minimum | int(11) | NO | | 3600 | | | serial | bigint(20) | NO | | 2015050100 | | | resp_person | varchar(64) | NO | | root.t4x.org. | | | primary_ns | varchar(64) | NO | | ns1.t4x.org. | | | data_count | int(11) | NO | | 0 | | +-------------+-----------------------------------------+------+-----+---------------+----------------+ 16 rows in set (0.06 sec) mysql> INSERT INTO `dns_records` (`zone`, `host`, `type`, `data`, `ttl`,`mx_priority`, `refresh`, `retry`, `expire`, -> `minimum`, `serial`, `resp_person`, `primary_ns`, `data_count`) VALUES -> ('t4x.org', '@', 'SOA', 'ns1.t4x.org.', 10, NULL, 3600, 3600, 86400, 10, 2015050200, 'root.t4x.org.', 'ns1.t4x.org.', 0); Query OK, 1 row affected (0.12 sec) mysql> INSERT INTO `dns_records` (`zone`,`host`, `type`, `data`) VALUES -> ('t4x.org', '@', 'NS', 'ns1.t4x.org.'), -> ('t4x.org', '@', 'NS', 'ns2.t4x.org.'); Records: 2 Duplicates: 0 Warnings: 0 mysql> INSERT INTO `dns_records` (`zone`, `host`, `type`, `data`) VALUES -> ('t4x.org', 'ns1', 'A', '1.1.1.19'), -> ('t4x.org', 'ns2', 'A', '1.1.1.20'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> INSERT INTO `dns_records` (`zone`, `host`, `type`, `data`, `ttl`, `view`) VALUES -> ('t4x.org', 'www', 'A', '1.1.1.1', 3600, 'LOCAL'), -> ('t4x.org', 'www', 'A', '2.2.2.2', 3600, 'CTCC'), -> ('t4x.org', 'www', 'A', '3.3.3.3', 3600, 'CUCC'), -> ('t4x.org', 'www', 'A', '4.4.4.4', 3600, 'CMCC'), -> ('t4x.org', 'www', 'A', '5.5.5.5', 3600, 'CECC'), -> ('t4x.org', 'www', 'A', '6.6.6.6', 3600, 'OTHERS'); Query OK, 6 rows affected (0.00 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> INSERT INTO `dns_records` (`zone`, `host`, `type`, `data`, `ttl`, `view`) VALUES -> ('t4x.org', 'bbs', 'A', '11.11.11.11', 3600, 'LOCAL'), -> ('t4x.org', 'bbs', 'A', '22.22.22.22', 3600, 'CTCC'), -> ('t4x.org', 'bbs', 'A', '33.33.33.33', 3600, 'CUCC'), -> ('t4x.org', 'bbs', 'A', '44.44.44.44', 3600, 'CMCC'), -> ('t4x.org', 'bbs', 'A', '55.55.55.55', 3600, 'CECC'), -> ('t4x.org', 'bbs', 'A', '66.66.66.66', 3600, 'OTHERS'); Query OK, 6 rows affected (0.00 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> INSERT INTO dns_records (zone,host,type,DATA,view) VALUES -> ('t4x.org', 'blog', 'CNAME', 'www','LOCAL'), -> ('t4x.org', 'blog', 'CNAME', 'www','CTCC'), -> ('t4x.org', 'blog', 'CNAME', 'www','CUCC'), -> ('t4x.org', 'blog', 'CNAME', 'www','CMCC'), -> ('t4x.org', 'blog', 'CNAME', 'www','CECC'), -> ('t4x.org', 'blog', 'CNAME', 'www','OTHERS'); Query OK, 6 rows affected (0.00 sec) Records: 6 Duplicates: 0 Warnings: 0 |
view配置信息:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
[root@MASTER /]# cat /var/named/named.rfc1912.zones view "LOCAL" { match-clients { key local; LOCAL; }; server 1.1.1.20 { keys local; }; recursion yes; allow-update { none; }; allow-transfer { key local; }; also-notify { 1.1.1.20; }; notify yes; zone "." IN { type hint; file "named.root"; }; dlz "local zone" { database "mysql {host=localhost dbname=dnsdb ssl=false user=dnsdb pass=123456} {select zone from dns_records where zone = '$zone$' and view='LOCAL'} {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='LOCAL'} {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='LOCAL'} {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='LOCAL'} {select zone from xfr_table where zone = '$zone$' and client = '$client$' and view='LOCAL'} {update data_count set count = count + 1 where zone ='$zone$' and view='LOCAL'}"; }; }; view "CTCC" { match-clients { key ctcc; CTCC; }; server 1.1.1.20 { keys ctcc; }; recursion yes; allow-update { none; }; allow-transfer { key ctcc; }; also-notify { 1.1.1.20; }; notify yes; zone "." IN { type hint; file "named.root"; }; dlz "ctcc zone" { database "mysql {host=localhost dbname=dnsdb ssl=false user=dnsdb pass=123456} {select zone from dns_records where zone = '$zone$' and view='CTCC'} {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='CTCC'} {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='CTCC'} {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='CTCC'} {select zone from xfr_table where zone = '$zone$' and client = '$client$' and view='CTCC'} {update data_count set count = count + 1 where zone ='$zone$' and view='CTCC'}"; }; }; view "CUCC" { match-clients { key local; CUCC; }; server 1.1.1.20 { keys cucc; }; recursion yes; allow-update { none; }; allow-transfer { key cucc; }; also-notify { 1.1.1.20; }; notify yes; zone "." IN { type hint; file "named.root"; }; dlz "local zone" { database "mysql {host=localhost dbname=dnsdb ssl=false user=dnsdb pass=123456} {select zone from dns_records where zone = '$zone$' and view='CUCC'} {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='CUCC'} {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='CUCC'} {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='CUCC'} {select zone from xfr_table where zone = '$zone$' and client = '$client$' and view='CUCC'} {update data_count set count = count + 1 where zone ='$zone$' and view='CUCC'}"; }; }; view "CMCC" { match-clients { key cmcc; CMCC; }; server 1.1.1.20 { keys cmcc; }; recursion yes; allow-update { none; }; allow-transfer { key cmcc; }; also-notify { 1.1.1.20; }; notify yes; zone "." IN { type hint; file "named.root"; }; dlz "cmcc zone" { database "mysql {host=localhost dbname=dnsdb ssl=false user=dnsdb pass=123456} {select zone from dns_records where zone = '$zone$' and view='CMCC'} {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='CMCC'} {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='CMCC'} {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='CMCC'} {select zone from xfr_table where zone = '$zone$' and client = '$client$' and view='CMCC'} {update data_count set count = count + 1 where zone ='$zone$' and view='CMCC'}"; }; }; view "CECC" { match-clients { key cmcc; CECC; }; server 1.1.1.20 { keys cecc; }; recursion yes; allow-update { none; }; allow-transfer { key cecc; }; also-notify { 1.1.1.20; }; notify yes; zone "." IN { type hint; file "named.root"; }; dlz "cmcc zone" { database "mysql {host=localhost dbname=dnsdb ssl=false user=dnsdb pass=123456} {select zone from dns_records where zone = '$zone$' and view='CECC'} {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='CECC'} {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='CECC'} {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='CECC'} {select zone from xfr_table where zone = '$zone$' and client = '$client$' and view='CECC'} {update data_count set count = count + 1 where zone ='$zone$' and view='CECC'}"; }; }; view "OTHERS" { match-clients { key cmcc; OTHERS; }; server 1.1.1.20 { keys others; }; recursion yes; allow-update { none; }; allow-transfer { key others; }; also-notify { 1.1.1.20; }; notify yes; zone "." IN { type hint; file "named.root"; }; dlz "cecc zone" { database "mysql {host=localhost dbname=dnsdb ssl=false user=dnsdb pass=123456} {select zone from dns_records where zone = '$zone$' and view='OTHERS'} {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='OTHERS'} {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='OTHERS'} {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='OTHERS'} {select zone from xfr_table where zone = '$zone$' and client = '$client$' and view='OTHERS'} {update data_count set count = count + 1 where zone ='$zone$' and view='OTHERS'}"; }; }; include "ctcc.cfg"; include "cucc.cfg"; include "cmcc.cfg"; include "cecc.cfg"; include "local.cfg"; include "others.cfg"; include "keys/ctcc.key"; include "keys/cucc.key"; include "keys/cmcc.key"; include "keys/cecc.key"; include "keys/local.key"; include "keys/others.key"; |
数据修改:
0 1 2 3 |
insert INTO dns_records (zone,host,type,data,ttl,view) values ('t4x.org','ok','A','127.0.0.1','3600','CMCC'); INSERT INTO `dns_records` (`zone`, `host`, `type`, `data`, `ttl`, `view`) VALUES ('t4x.org', 'bbs', 'A', '11.11.11.11', 3600, 'LOCAL'); #插入一条bbs.t4x.org 解析为11.11.11.11的记录 mysql> update dns_records set data = '9.9.9.9' where id =9; #把id为9的修改类型date的改为9.9.9.9 mysql> delete from dns_records where id =12; #删除id为12的数据 |
mysql:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
mysql> select * from dns_records\G *************************** 1. row *************************** id: 1 zone: t4x.org host: @ type: SOA data: ns1.t4x.org. ttl: 10 view: LOCAL mx_priority: NULL refresh: 3600 retry: 3600 expire: 86400 minimum: 10 serial: 2015050200 resp_person: root.t4x.org. primary_ns: ns1.t4x.org. data_count: 0 *************************** 2. row *************************** id: 2 zone: t4x.org host: @ type: NS data: ns1.t4x.org. ttl: 120 view: LOCAL mx_priority: NULL refresh: 3600 retry: 3600 expire: 86400 minimum: 3600 serial: 2015050100 resp_person: root.t4x.org. primary_ns: ns1.t4x.org. data_count: 0 *************************** 3. row *************************** id: 3 zone: t4x.org host: @ type: NS data: ns2.t4x.org. ttl: 120 view: LOCAL mx_priority: NULL refresh: 3600 retry: 3600 expire: 86400 minimum: 3600 serial: 2015050100 resp_person: root.t4x.org. primary_ns: ns1.t4x.org. data_count: 0 *************************** 4. row *************************** id: 4 zone: t4x.org host: ns1 type: A data: 1.1.1.19 ttl: 120 view: LOCAL mx_priority: NULL refresh: 3600 retry: 3600 expire: 86400 minimum: 3600 serial: 2015050100 resp_person: root.t4x.org. primary_ns: ns1.t4x.org. data_count: 0 *************************** 5. row *************************** id: 5 zone: t4x.org host: ns2 type: A data: 1.1.1.20 ttl: 120 view: LOCAL mx_priority: NULL refresh: 3600 retry: 3600 expire: 86400 minimum: 3600 serial: 2015050100 resp_person: root.t4x.org. primary_ns: ns1.t4x.org. data_count: 0 *************************** 6. row *************************** id: 6 zone: t4x.org host: www type: A data: 1.1.1.1 ttl: 3600 view: LOCAL mx_priority: NULL refresh: 3600 retry: 3600 expire: 86400 minimum: 3600 serial: 2015050100 resp_person: root.t4x.org. primary_ns: ns1.t4x.org. data_count: 0 *************************** 7. row *************************** id: 7 zone: t4x.org host: www type: A data: 2.2.2.2 ttl: 3600 view: CTCC mx_priority: NULL refresh: 3600 retry: 3600 expire: 86400 minimum: 3600 serial: 2015050100 resp_person: root.t4x.org. primary_ns: ns1.t4x.org. data_count: 0 *************************** 8. row *************************** id: 8 zone: t4x.org host: www type: A data: 3.3.3.3 ttl: 3600 view: CUCC mx_priority: NULL refresh: 3600 retry: 3600 expire: 86400 minimum: 3600 serial: 2015050100 resp_person: root.t4x.org. primary_ns: ns1.t4x.org. data_count: 0 *************************** 9. row *************************** id: 9 zone: t4x.org host: www type: A data: 4.4.4.4 ttl: 3600 view: CMCC mx_priority: NULL refresh: 3600 retry: 3600 expire: 86400 minimum: 3600 serial: 2015050100 resp_person: root.t4x.org. primary_ns: ns1.t4x.org. data_count: 0 *************************** 10. row *************************** id: 10 zone: t4x.org host: www type: A data: 5.5.5.5 ttl: 3600 view: CECC mx_priority: NULL refresh: 3600 retry: 3600 expire: 86400 minimum: 3600 serial: 2015050100 resp_person: root.t4x.org. primary_ns: ns1.t4x.org. data_count: 0 *************************** 11. row *************************** id: 11 zone: t4x.org host: www type: A data: 6.6.6.6 ttl: 3600 view: OTHERS mx_priority: NULL refresh: 3600 retry: 3600 expire: 86400 minimum: 3600 serial: 2015050100 resp_person: root.t4x.org. primary_ns: ns1.t4x.org. data_count: 0 11 rows in set (0.00 sec) mysql> |
压力测试:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
[root@MASTER /]# cd /byrd/tools/bind-9.9.8/contrib/queryperf [root@MASTER queryperf]# ./configure [root@MASTER queryperf]# make [root@MASTER queryperf]# wc -l abc.txt 997234 abc.txt [root@MASTER queryperf]# tail -2 abc.txt ee.t4x.org A ee.t4x.org A [root@MASTER queryperf]# ./queryperf -d abc.txt -s 1.1.1.19 DNS Query Performance Testing Tool Version: $Id: queryperf.c,v 1.12 2007/09/05 07:36:04 marka Exp $ [Status] Processing input data [Status] Sending queries (beginning with 1.1.1.19) [Status] Testing complete Statistics: Parse input file: once Ended due to: reaching end of file Queries sent: 997234 queries Queries completed: 997234 queries Queries lost: 0 queries Queries delayed(?): 0 queries RTT max: 0.200361 sec RTT min: 0.000038 sec RTT average: 0.002404 sec RTT std deviation: 0.000952 sec RTT out of range: 0 queries Percentage completed: 100.00% Percentage lost: 0.00% Started at: Sun Mar 27 22:16:42 2015 Finished at: Sun Mar 27 22:18:43 2015 Ran for: 120.400257 seconds Queries per second: 8282.656739 qps top - 22:17:43 up 12:16, 4 users, load average: 0.87, 0.25, 0.08 Tasks: 81 total, 1 running, 80 sleeping, 0 stopped, 0 zombie Cpu(s): 56.3%us, 38.7%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 5.0%si, 0.0%st Mem: 494420k total, 461120k used, 33300k free, 1864k buffers Swap: 1015800k total, 302104k used, 713696k free, 90916k cached |
官方文档:http://bind-dlz.sourceforge.net/mysql_driver.html
mysql主从同步:https://note.t4x.org/database/configuration-mysql-master-slave/SourceByrd's Weblog-https://note.t4x.org/service/configuration-intelligence-dns-senior/
mysql数据懒人下载:http://pan.baidu.com/s/1b4IY42 密码: 45v3SourceByrd's Weblog-https://note.t4x.org/service/configuration-intelligence-dns-senior/
参考文档:
1:http://www.tokiwinter.com/set-up-a-bind-dlz-nameserver/
2:http://docs.linuxtone.org/ebooks/CDN/bind-dlz-view2.pdf
3:http://www.it165.net/admin/html/201209/386.html
4:http://blog.chinaunix.net/uid-10435474-id-2957057.html
5:http://www.linuxidc.com/Linux/2014-03/98556.htm
6:http://os.51cto.com/art/201111/305114.htm
7:http://blog.chenxiaosheng.com/posts/2013-09-09/install-bind-mysql-dlz.html
8:http://ruiaylin.github.io/2015/01/15/intelligent%20Dns%20configuration/
9:https://www.centos.bz/2012/09/bind-dlz-with-mysql-intelligent-dns/SourceByrd's Weblog-https://note.t4x.org/service/configuration-intelligence-dns-senior/
SourceByrd's Weblog-https://note.t4x.org/service/configuration-intelligence-dns-senior/