centos7搭建MySQL+BIND-dlz

环境说明

Linux:centos 7.4.1708
Mysql: Ver 15.1 Distrib 10.1.32-MariaDB
BIND: 9.12.1

安装mysql

创建/etc/yum.repos.d/MariaDB.repo文件

> cat /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

yum安装 mariadb

yum install -y MariaDB-server MariaDB-client MariaDB-devel

修改配置文件

> cat /etc/my.cnf.d/mysql-clients.cnf 
[client]
port        = 3306
socket      = /data/db/mysql/mysql.sock

[mysql]
no-auto-rehash

[mysqldump]
quick
max_allowed_packet = 64M

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
> cat /etc/my.cnf.d/server.cnf 
[mysqld]
port            = 3306
datadir         = /data/db/mysql
socket          = /data/db/mysql/mysql.sock
log-error =  /data/db/mysql/error.log
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 64M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 8
log-bin=mysql-bin
binlog_format=mixed
server-id   = 1

default_storage_engine = InnoDB
max_allowed_packet = 256M
max_connections = 2048
open_files_limit = 65535

skip-name-resolve
lower_case_table_names=1

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'

初始化

mkdir /data/db/mysql
chown mysql.mysql -R  /data/db/mysql
mysql_install_db --defaults-file=/etc/my.cnf --datadir=/data/db/mysql/ --user=mysql

启动

systemctl start mysql

授权以及设置字符集

mysql
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '123456' WITH GRANT OPTION;
MariaDB [(none)]> create database binddns;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON binddns.* TO 'bind'@'%' IDENTIFIED BY '123456';

安装bind

下载

wget https://www.isc.org/downloads/file/bind-9-12-1/?version=tar-gz
tar xvf bind-9.12.1.tar.gz
cd xvf bind-9.12.1

安装

useradd -s /sbin/nologin -M named
./configure --with-dlz-mysql=/usr --enable-largefile --enable-threads=yes --prefix=/usr/local/bind --with-openssl
make -j 4
make install

普通配置

环境变量

echo "export PATH=${PATH}:/usr/local/bind/sbin/:/usr/local/bind/bin/" >> /etc/profile
source /etc/profile

配置rndc 配置named.conf

cd /usr/local/bind/etc/
rndc-confgen -r /dev/urandom >rndc.conf
head -5 rndc.conf >named.conf
wget http://www.internic.net/domain/named.root

添加其他配置

controls {
        inet 127.0.0.1 port 953
        allow { 127.0.0.1; } keys { "rndc-key"; };
};

zone "." IN {
    type hint;
    file "/usr/local/bind/etc/named.root";
};

options {
        tcp-clients 50000;
        directory "/usr/local/bind/var";
        pid-file "/usr/local/bind/var/bind.pid";
        dump-file "/usr/local/bind/var/bind_dump.db";
        statistics-file "/usr/local/bind/var/bind.stats";
        rate-limit {
                nxdomains-per-second 3;
                window 1;
        };
        notify yes;
        recursion yes;
        version "ooxx-bind:1.0.24";
        allow-notify       { none; };
        allow-recursion    { any; };
        allow-transfer     { none; };
        allow-query        { any; };
};

logging {
        channel bind_log {
                file "/usr/local/bind/log/bind.log" versions 3 size 20m;
                severity info;
                print-time yes;
                print-severity yes;
                print-category yes;
        };
        category default {
                bind_log;
        };
};

include  "/usr/local/bind/etc/default.zones";

新建default.zones

> cat default.zones 
zone "ooxx.com" IN {
    type master;
    file "/usr/local/bind/zones/ooxx.com.zone";
};

zone "1.1.1.in-addr.arpa" IN {
    type master;
    file "/usr/local/bind/zones/1.1.1.zone";
};

配置解析域文件

> mkdir /usr/local/bind/zones && cd $!
> cat ooxx.com.zone 
$TTL 1D
@   IN  SOA ooxx.com.   admin.ooxx.com. ( 0 1D 1H 1W 3H )
        NS  ns1.ooxx.com.
        NS  ns2.ooxx.com.
        A   127.0.0.1
        AAAA    ::1
        MX  10 mx.ooxx.com.
ttl IN  A   1.1.1.22
www     IN  A   1.1.1.33   
bbs IN  CNAME   www
mx  IN  A   1.1.1.66
ns1 IN  A   1.1.1.11
ns2 IN  A   1.1.1.11

> cat 1.1.1.zone 
$TTL 1D
@       IN      SOA     ooxx.com. admin.ooxx.com. ( 0 2H 10M 7D 1D )
        NS  ttl.ooxx.com.
        A   127.0.0.1
        AAAA    ::1
22  IN      PTR     ooxx.com
33  IN      PTR     www.ooxx.com.
11  IN      PTR     ns1.ooxx.com.
11  IN      PTR     ns2.ooxx.com.
66  IN      PTR     mx.ooxx.com.

测试

mkdir /usr/local/bind/log/
chown -R named:named /usr/local/bind
找个windows,改下dns,访问测试

dlz 配置

添加mysql数据

create database bind;

# 新建record表
CREATE TABLE IF NOT EXISTS records (
  id int(10) unsigned NOT NULL AUTO_INCREMENT,
  zone varchar(255) NOT NULL,
  host varchar(255) NOT NULL,
  type enum('A','MX','CNAME','NS','SOA','PTR','TXT','AAAA','SVR','URL') NOT NULL,
  data varchar(255) NOT NULL,
  ttl int(11) NOT NULL,
  mx_priority int(11) DEFAULT NULL,
  refresh int(11) DEFAULT NULL,
  retry int(11) DEFAULT NULL,
  expire int(11) DEFAULT NULL,
  minimum int(11) DEFAULT NULL,
  serial bigint(20) DEFAULT NULL,
  resp_person varchar(64) DEFAULT NULL,
  primary_ns varchar(64) DEFAULT NULL,
  PRIMARY KEY (id),
  KEY type (type),
  KEY host (host),
  KEY zone (zone)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

# 新建acl表
CREATE TABLE IF NOT EXISTS acl (
  id int(10) unsigned NOT NULL AUTO_INCREMENT,
  zone varchar(255) NOT NULL,
  client varchar(255) NOT NULL,
  PRIMARY KEY (id),
  KEY client (client),
  KEY zone (zone)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

修改配置文件

dlz "mysql-dlz" {
    database "mysql
    {host=127.0.0.1 dbname=binddns ssl=false port=3306 user=bind pass=123456}
    {select name from bind_domain where name = '$zone$' limit 1}
    {select ttl, type, mx, case when lower(type)='txt' then concat('\"', value, '\"') when lower(type) = 'soa' then concat_ws(' ', value, serial, refresh, retry, expire, minimum) else value end from bind_record where domain_id = (select id from bind_domain where name = '$zone$') and name = '$record$'}
    {}
    {select ttl, type, name, mx, data from bind_record where domain_id = (select id from bind_domain where name = '$zone$') and not (type = 'SOA' or type = 'NS')}";
};
# include  "/usr/local/bind/etc/default.zones";

第一次写host=localhost,报错:mysql driver failed to create database connection after 4 attempts,后改成host=127.0.0.1就好了

插入数据

INSERT INTO bind.records ( zone, HOST, type, DATA, ttl ) VALUES ( 'ooxx.com', 'www', 'A', '1.1.1.22', '600' );
INSERT INTO bind.records ( zone, HOST, type, DATA, ttl ) VALUES ( 'ooxx.com', 'mail', 'CNAME', 'www', '600' );
INSERT INTO bind.records ( zone, HOST, type, DATA, ttl ) VALUES ( 'ooxx.com', '@', 'NS', 'ns', '60' );
INSERT INTO bind.records ( zone, HOST, type, DATA, ttl ) VALUES ( 'ooxx.com', 'ns', 'A', '1.1.1.11', '600' );

启动

named -u named -n1 -g -d1

没看到报错,说明配置没问题

测试

找个windows,改下dns,访问测试

测试结果dlz比文件要慢

测试日志, 可以看到查询过程

Query String: select zone from records where zone = '11.1.1.1.in-addr.arpa' limit 1

14-Apr-2018 02:20:40.595 
Query String: select zone from records where zone = '1.1.1.in-addr.arpa' limit 1

14-Apr-2018 02:20:40.595 
Query String: select zone from records where zone = '1.1.in-addr.arpa' limit 1

14-Apr-2018 02:20:40.595 
Query String: select zone from records where zone = '1.in-addr.arpa' limit 1

14-Apr-2018 02:20:40.595 
Query String: select zone from records where zone = 'in-addr.arpa' limit 1

14-Apr-2018 02:20:40.595 
Query String: select zone from records where zone = 'arpa' limit 1

14-Apr-2018 02:20:40.596 
Query String: select zone from records where zone = 'www.ooxx.com' limit 1

14-Apr-2018 02:20:40.597 
Query String: select zone from records where zone = 'ooxx.com' limit 1

14-Apr-2018 02:20:40.597 
Query String: 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 from records where zone = 'ooxx.com' and host = '@'

14-Apr-2018 02:20:40.597 
Query String: 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 from records where zone = 'ooxx.com' and host = 'www'

bind systemd启动文件

> cat /usr/lib/systemd/system/named.service
[Unit]
Description=Internet domain name server
After=network.target

[Service]
ExecStart=/usr/local/bind/sbin/named -f -u named -4
ExecReload=/usr/local/bind/sbin/rndc reload
ExecStop=/usr/local/bind/sbin/rndc stop

[Install]
WantedBy=multi-user.target
Alias=bind.service

> ln -s /usr/lib/systemd/system/named.service /etc/systemd/system/bind.service
> systemctl daemon-reload
> systemctl start bind
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 141,714评论 1 298
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 60,817评论 1 254
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 93,456评论 0 211
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 40,811评论 0 174
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 48,501评论 1 252
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 38,648评论 1 173
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 30,305评论 2 267
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,107评论 0 165
  • 想象着我的养父在大火中拼命挣扎,窒息,最后皮肤化为焦炭。我心中就已经是抑制不住地欢快,这就叫做以其人之道,还治其人...
    爱写小说的胖达阅读 28,870评论 6 228
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 32,441评论 0 213
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,211评论 2 213
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 30,531评论 1 225
  • 白月光回国,霸总把我这个替身辞退。还一脸阴沉的警告我。[不要出现在思思面前, 不然我有一百种方法让你生不如死。]我...
    爱写小说的胖达阅读 24,189评论 0 31
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,074评论 2 213
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 31,433评论 3 204
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,596评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 25,953评论 0 164
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 33,423评论 2 230
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 33,515评论 2 229

推荐阅读更多精彩内容