首页 笔记正文

【CentOS7】DNS服务主从配置使用mysql-bind驱动

從出茳湖 笔记 2020-09-01 14:09:53 1009 0

DNS主从复制.png

准备环境

虚拟机:VMware 15.5.5
网络: Net模式,master: 192.168.10.102  slave: 192.168.10.103
CentOS: 7.3
Python: 3.6.8 wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz
Bind: 9.14.2 下载地址http://ftp.isc.org/isc/bind9/
MySQL: MariaDB5.5.60 下载地址 https://downloads.mariadb.org/mariadb/5.5.60/

Python编译安装

MariaDB编译安装

Bind编译安装

放通端口

firewall-cmd --add-port=53/tcp --permanent
firewall-cmd --add-port=953/tcp --permanent
firewall-cmd --add-port=8053/tcp --permanent
firewall-cmd --reload

安装依赖

yum install -y wget groupinstall pcre-devel zlib zlib-devel gcc gcc-c++ autoconf automake make pcre-devel openssl openssl-devel openldap-devel libcap libcap-devel libuv libuv-devel unixODBC-devel gcc libtool

解压bind包

tar zxvf bind-9.11.4.tar.gz

对bind源码进行补丁

'''复制mysqldb.c mysqldb.h 到bind源码目录'''
cp mysql-bind/mysqldb.h bind-9.11.4/bin/named/include/named
cp mysql-bind/mysqldb.h bind-9.11.4/bin/named/include/
cp mysql-bind/mysqldb.c bind-9.11.4/bin/named/
'''修改mysqldb.c文件使其输出错误日志方便后期调试'''
vi bind-9.11.4/bin/named/mysqldb.c
'''在mysqldb.c的db_connect函数中修改:'''
if (mysql_real_connect ( &dbi->conn, dbi->host, dbi->user, dbi->passwd, dbi->database, 0, NULL, 0 ))
	{
	fprintf(stderr,"\nconnect successfully .............. \n");
	return (ISC_R_SUCCESS);
	}
else
	{
	fprintf(stderr,"\nconnect fail:%s , host %s,user %s,passwd %s,db %s,port %d ...................\n",mysql_error(&dbi->conn),dbi->host,dbi->user,dbi->passwd,dbi->database);
	return (ISC_R_FAILURE);
	}

修改Makefile.in

'''首先获取mysql头文件、库文件位置'''
mysql_config --cflags
# -I/usr/local/mariadb/include/mysql -I/usr/local/mariadb/include/mysql/..
mysql_config --libs
# -L/usr/local/mariadb/lib -lmysqlclient -lpthread -lz -lm -ldl
'''编辑Makefile.in'''
vi bind-9.11.4/bin/named/Makefile.in
DBDRIVER_OBJS = [email protected]@
DBDRIVER_SRCS = mysqldb.c
DBDRIVER_INCLUDES = -I/usr/local/mariadb/include/mysql
DBDRIVER_LIBS = -L/usr/local/mariadb/lib -lmysqlclient -lpthread -lz -lm -ldl
'''修改main.c在nsns_server_create'''
'''
行首添加#include <mysqldb.h>
在nsns_server_create();前添加mysqlddb_init();
在nsns_server_destroy();后添加mysqlddb_clear();
'''
vi bind-9.11.4/bin/named/main.c
#include <mysqldb.h>
mysqlddb_init();
ns_server_create(ns_g_mctx,&ns_g_server);
ns_server_destroy(&ns_g_server);
mysqlddb_clear();

编译安装bind

'''--enable-threads 允许多线程'''
./configure --enable-threads --prefix=/usr/local/bind
'''执行安装'''
make
make install

bind配置

'''bind初始化配置'''
cd /usr/local/bind/etc/
/usr/local/bind/sbin/rndc-confgen > rndc.conf
cat rndc.conf >rndc.key
tail -10 rndc.conf | head -9 | sed s/#\ //g > named.conf
mkdir /var/named
mkdir /var/named/data
mkdir /var/named/dynamic
dig -t NS . > /var/named/named.ca

点击展开

named.conf(master)文件配置
options {
	listen-on port 53 { any; };
	listen-on-v6 port 53 { any; };
	directory    "/var/named";
	dump-file    "/var/named/data/cache_dump.db";
	zone-statistics yes;
	statistics-file    "/var/named/data/named_stats_stats.txt";
	memstatistics-file    "/var/named/data/named_mem_stats.txt";
	allow-query { any; };
	allow-transfer { any; };
	notify yes;
	also-notify { 192.168.10.103; 192.168.10.104; };
	recursion yes;
	allow-recursion { any; };
	forward only;
	forwarders { 192.168.5.201; 192.168.5.202; };
	max-cache-size 50M;
	pid-file "/usr/local/bind/var/run/named/named.pid";
	managed-keys-directory "/var/named/dynamic";
 };
statistics-channels {
	inet 127.0.0.1 port 8053 allow { 127.0.0.1; };
};
key "rndc-key" {
	algorithm hmac-md5;
	secret "xxxxxxxxxxxxxxxxxxxx/W==";
};
controls {
	inet 127.0.0.1 port 953;
		allow { 127.0.0.1; } keys { "rndc-key"; };
};
logging {
	channel default_debug {
		file "/var/log/named.log";
		severity dynamic;
	};
	channel query_log {
		file "/var/log/query.log" versions 7 size 100m;
		severity info;
		print-time yes;
		print-category yes;
	};
	category queries {
		query_log;
	};
};
zone "." IN {
	type hint;
	file "named.ca";
};
zone "a.test.com" {
	type master;
	notify yes;
	database "mysqldb dns `a.test.com` localhost dns bindpass";
};
zone "ww.a.test.com" {
	type master;
	notify yes;
	database "mysqldb dns `a.test.com` localhost dns bindpass";
};
named.conf(slave)文件配置
options {
	listen-on port 53 { any; };
	listen-on-v6 port 53 { any; };
	directory    "/var/named";
	dump-file    "/var/named/data/cache_dump.db";
	zone-statistics yes;
	statistics-file    "/var/named/data/named_stats_stats.txt";
	memstatistics-file    "/var/named/data/named_mem_stats.txt";
	allow-query { any; };
	allow-transfer { any; };
	// notify yes;
	//also-notify { 192.168.10.103; 192.168.10.104; };
	recursion yes;
	allow-recursion { any; };
	forward only;
	forwarders { 192.168.5.201; 192.168.5.202; };
	max-cache-size 50M;
	pid-file "/usr/local/bind/var/run/named/named.pid";
	managed-keys-directory "/var/named/dynamic";
 };
statistics-channels {
	inet 127.0.0.1 port 8053 allow { 127.0.0.1; };
};
key "rndc-key" {
	algorithm hmac-md5;
	secret "xxxxxxxxxxxxxxxxxxxx/W==";
};
controls {
	inet 127.0.0.1 port 953;
		allow { 127.0.0.1; } keys { "rndc-key"; };
};
logging {
	channel default_debug {
		file "/var/log/named.log";
		severity dynamic;
	};
	channel query_log {
		file "/var/log/query.log" versions 7 size 100m;
		severity info;
		print-time yes;
		print-category yes;
	};
	category queries {
		query_log;
	};
};
zone "." IN {
	type hint;
	file "named.ca";
};
zone "a.test.com" {
	type slave;
	masters { 192.168.10.102; };
	file "data/a.test.com";
	masterfile-format text;
};

数据路信息配置

建库

create database dns character set=utf8;

测试数据

create table `a.test.com` (id int(11) NOT NULL AUTO_INCREMENT,name varchar(255) DEFAULT NULL,ttl int(11) DEFAULT NULL,rdtype varchar(255) DEFAULT NULL,rdata varchar(255) DEFAULT NULL,stats int(11) DEFAULT NULL,del_stat tinyint(1) NOT NULL,create_time datetime,update_time datetime,PRIMARY KEY (id)) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

insert into `a.test.com` (name,ttl,rdtype,rdata,stats,del_stat,update_time,create_time)VALUES('a.test.com',86400,'SOA','a.test.com. a.test.com. 2020081801 3600 600 1800 600',1,0,'2020-08-18 15:10:40','2020-08-18 15:10:40');

insert into `a.test.com` (name,ttl,rdtype,rdata,stats,del_stat,update_time,create_time)VALUES('a.test.com',86400,'NS','ns0.a.test.com.',1,0,'2020-08-18 15:10:40','2020-08-18 15:10:40');

insert into `a.test.com` (name,ttl,rdtype,rdata,stats,del_stat,update_time,create_time)VALUES('a.test.com',86400,'NS','ns1.a.test.com.',1,0,'2020-08-18 15:10:40','2020-08-18 15:10:40');

insert into `a.test.com` (name,ttl,rdtype,rdata,stats,del_stat,update_time,create_time)VALUES('ns0.a.test.com',86400,'NS','192.168.10.102',1,0,'2020-08-18 15:10:40','2020-08-18 15:10:40');

insert into `a.test.com` (name,ttl,rdtype,rdata,stats,del_stat,update_time,create_time)VALUES('ns1.a.test.com',86400,'NS','192.168.10.102',1,0,'2020-08-18 15:10:40','2020-08-18 15:10:40');

insert into `a.test.com` (name,ttl,rdtype,rdata,stats,del_stat,update_time,create_time)VALUES('a.test.com',600,'A','192.168.10.103',1,0,'2020-08-18 15:10:40','2020-08-18 15:10:40');

修改mysql.sock文件目录到/var/lib/mysql

cd /var/lib
mkdir mysql
chmod 777 -R mysql
vi /etc/my.cnf
[client]
socket = /var/lib/mysql/mysql.sock
[mysqld]
socket = /var/lib/mysql/mysql.sock
systemctl restart mysql

测试并启动bind

前台运行

/usr/local/bind/sbin/named -c /usr/local/bind/etc/named.conf -g

无报错情况下测试

dig www.a.test.com

没有修改本机nameserver时使用

dig ww.a.test.com @localhost

修改nameserver

vi /etc/resolv.conf
nameserver 主机IP

后台启动

/usr/local/bind/sbin/named -c /usr/local/bind/etc/named.conf -g &

添加named.service

vi /usr/lib/systemd/system/named.service
[Unit]
Description=Bind DNS Service
Documentation=
After=network.target
Wants=network.target

[Service]
Type=forking
Environment=NAMEDCONF=/usr/local/bind/etc/named.conf
PIDFile=/usr/local/bind/var/run/named/named.pid
ExecStartPre=/bin/bash -c 'if [ ! "$DISAABLE_ZONE_CHECHING" == "yes" ]; then /usr/local/bind/sbin/named-checkconf -z "$NAMEDCONF"; else echo "Checking if zone files is disabled"; fi'
ExecStart=/usr/local/bind/sbin/named -c ${NAMEDCONF} $OPTIONS
ExecReload=/bin/sh -c '/usr/local/bind/sbin/rndc reload > /dev/null 2>&1 || /bin/kill -HUP $MAINPID'
ExecStop=/bin/sh -c '/usr/local/bind/sbin/rndc stop > /dev/null 2>&1 || /bin/kill -TERM $MAINPID'
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启动

systemctl start named.service

注意从服务器不需要安装mysql-bind,直接编译安装bind即可

END

本文标题:【CentOS7】DNS服务主从配置使用mysql-bind驱动
本文链接:https://zhong-er.com/post/129.html
作者授权:除特别说明外,本文由 從出茳湖 原创编译并授权 中二青年 刊载发布。
版权声明:本文使用「署名-非商业性使用-相同方式共享 4.0 国际」创作共享协议,转载或使用请遵守署名协议。

评论

Copyright ©2019-2020.Powered by©中青社 豫ICP备17041299号-1   豫公网安备 41050302000069号   本站由提供CDN加速/云存储服务