首页 > 代码库 > bind配置mysql后台

bind配置mysql后台

 

 

参考文档: http://blog.csdn.net/lishangwen_alan/article/details/53332889

              http://www.cnblogs.com/xiongpq/p/3384681.html

 

需求说明:  bind下用mysql可以动态加载DNS记录(dlz), 唯一有点不爽的是mysql  bind 都需要源码编译安装,

简直吐血, 所以整理下来.

 

1. mysql 编译安装:

 安装依赖包
yum -y install make gcc-c++ cmake bison-devel ncurses-devel

下载mysql

wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.35.tar.gz
 tar xvf mysql-5.6.35.tar.gz

cd mysql-5.6.35

编译安装:
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

make && make install

配置mysql
设置权限:
# groupadd mysql
# groupadd -g mysql mysql
# chown -R mysql:mysql /usr/local/mysql

初始化配置:
# cd /usr/local/mysql
# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

注意: 将/etc/my.cnf 改成其他名字,以防冲突

启动MySQl
# cp support-files/mysql.server /etc/init.d/mysql
# chkconfig mysql on
# service mysql start  --启动MySQL

配置用户

# PATH=/usr/local/mysql/bin:$PATH

# export PATH

# source /etc/profile

# mysql -uroot
mysql> set password = password(‘123456‘)

设置远程访问
mysql>
GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘*‘ IDENTIFIED BY ‘123456‘ WITH GRANT OPTION;

mysql 配置完成


2. bind 编译安装:
# ./configure --prefix=/usr/local/bind/ --with-dlz-mysql=/usr/local/mysql --enable-threads=no --enable-largefile --disable-ipv6 --with-openssl=no
  //--enable-threads 多线程支持(官网解析是需要关闭),--enable-largefile 启用大文件支持,--disable-ipv6 关闭ipv6支持,--with-dlz-mysql意思是使用mysql存储域名解析 

# make make install

3. 配置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

4. named.conf文件

key "rndc-key" {
algorithm hmac-md5;
secret "mvCUyhyDvNNGywhoVHbSaQ==";
};

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


options {
listen-on port 53 {any;}; //开启侦听53端口,any表示接受任意ip连接
directory "/usr/local/bind/var";
pid-file "named.pid"; //文件内容就是named进程的id
allow-query{any;}; //允许任意ip查询
forwarders{114.114.114.114;8.8.8.8;}; //设置转发的公网ip
};

dlz "Mysql zone" {
database "mysql
{dbname=dns_data port=3306 host=localhost user=root pass=123456 ssl=false}
{select zone from dns_records where zone = ‘$zone$‘ and status = 1}
{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 status = 1}
{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 status = 1}
{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 status = 1}";
};


5. mysql 配置:

create database dns_data

use dns_data
create table `dns_records` (
`id` bigint(20) not null auto_increment comment ‘主健‘,
`zone` varchar(255) not null default ‘‘ comment ‘域名‘,
`host` varchar(255) not null default ‘@‘ comment ‘记录名称‘,
`type` varchar(255) not null default ‘A‘ comment ‘记录类型‘,
`data` varchar(255) default null comment ‘记录值‘,
`ttl` int(11) not null default ‘800‘ comment ‘ttl(存活时间)‘,
`mx_priority` int(11) default null comment ‘mx优先级‘,
`refresh` int(11) default null comment ‘刷新时间间隔‘,
`retry` int(11) default null comment ‘重试时间间隔‘,
`expire` int(11) default null comment ‘过期时间‘,
`minimum` int(11) default null comment ‘最小时间‘,
`serial` bigint(20) default null comment ‘序列号,每次更改配置都会在原来的基础上加1‘,
`resp_person` varchar(64) default null comment ‘责任人‘,
`primary_ns` varchar(64) default null comment ‘主域名‘,
`status` tinyint(4) default 1 comment ‘0:该记录无效, 1:该记录有效‘,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ‘创建时间‘,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ‘更新时间‘,
primary key (`id`),
key `ix_created_at` (`created_at`),
key `ix_updated_at` (`updated_at`)
) engine=InnoDB default charset=utf8 comment=‘内网DNS记录‘;


6. 插入正向解析数据
INSERT INTO dns_records (zone, host, type, data, ttl) VALUES (‘phpfensi.com‘, ‘www‘, ‘A‘, ‘1.1.1.1‘, ‘60‘);
INSERT INTO dns_records (zone, host, type, data, ttl) VALUES (‘phpfensi.com‘, ‘cloud‘, ‘A‘, ‘2.2.2.2‘, ‘60‘);
INSERT INTO dns_records (zone, host, type, data, ttl) VALUES (‘phpfensi.com‘, ‘ns‘, ‘A‘, ‘3.3.3.3‘, ‘60‘);
INSERT INTO dns_records (zone, host, type, data, ttl) VALUES (‘phpfensi.com‘, ‘blog‘, ‘CNAME‘, ‘cloud.phpfensi.com.‘, ‘60‘);
INSERT INTO dns_records (zone, host, type, data, ttl) VALUES (‘phpfensi.com‘, ‘@‘, ‘NS‘, ‘ns.phpfensi.com.‘, ‘60‘);
INSERT INTO dns_records (zone, host, type, ttl, data,refresh, retry, expire, minimum, serial, resp_person) VALUES (‘phpfensi.com‘, ‘@‘, ‘SOA‘, ‘60‘, ‘ns‘, ‘28800‘, ‘14400‘, ‘86400‘, ‘86400‘, ‘2012020809‘, ‘admin‘);

7. 插入反向解析数据

insert into dns_records (zone,host,type,data,ttl,mx_priority,refresh,retry,expire,minimum,serial,resp_person,primary_ns) values (‘1.168.192in-addr.arpa‘,‘@‘,‘SOA‘,‘node02.example.com‘,86400,NULL,3600,15,86400,3600,2008082700,‘node02.example.com‘,‘node02.example.com‘);   //添加SOA(授权区域定义)记录
insert into dns_records (zone,host,type,data)values(‘1.168.192.in-addr.arpa‘,‘@‘,‘NS‘,‘node02.example.com.‘); //添加NS(标记区域的域名服务器以及授权子域)记录
insert into dns_records(zone,host,type,data)values(‘1.168.192.in-addr.arpa‘,‘250‘,‘PTR‘,‘node02.example.com.‘),(‘1.168.192.in-addr.arpa‘,‘111‘,‘PTR‘,‘x.example.com.‘); //添加PTR(与A记录相反,将ip转换成主机名,反向解析操作)记录

8. debug 模式下运行 bind 服务
# /usr/local/bind/sbin/named -g d 1

-g d 1 是debug的参数 可以查看到后台的日志信息

8. 测试结果:

技术分享

 

9. over

 

 



 



bind配置mysql后台