Linux系统下基于Bind_DLZ和MySQL数据的搭建DNS(亲测可用)

参考: https://www.cnblogs.com/saneri/p/8178065.html


一、安装MySQL
以mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz包为例
包下载地址:https://downloads.mysql.com/archives/community/

安装之前建议先彻底删除原有mysql所有数据,网上大把教程!

1、解压mysql包(在/usr/local 目录下)

tar -zxvf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz

2、将解压后的mysql目录重命名

mv mysql-5.6.40-linux-glibc2.12-x86_64 /usr/local/mysql

3.跳转到/usr/local/mysql下

cd /usr/local/mysql

4.追加mysql用户组和用户

groupadd mysql
useradd -r -g mysql mysql

在这里插入图片描述

5.更改目录权限

chown -R mysql:mysql ./

6.安装依赖库(三个命令可能报错 可以单独执行)

yum -y install autoconf && yum install libaio* -y && yum -y install numactl 

7.执行安装脚本

./scripts/mysql_install_db --user=mysql

mysql目录下生成my.cnf文件

8.修改当前目录拥有者为root用户,修改data目录拥有者为mysql

chown -R root:root ./ 
chown -R mysql:mysql data

9.mysql默认使用的是/etc/my.cnf文件,将/usr/local/mysql/my.cnf复制到/etc/my.cnf

cp my.cnf /etc/my.cnf

10.修改my.cnf内容

vi /etc/my.cnf

内容如下:

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
# server_id = .....
socket = /tmp/mysql.sock

pid-file=/usr/local/mysql/data/mysqld.pid

如图:
在这里插入图片描述
创建默认日志文件

mkdir /var/log/mariadb
touch /var/log/mariadb/mariadb.log

设置my.cnf为用户可读写,其他用户不可写

chmod 644 /usr/local/mysql/my.cnf

11.在mysql/data目录下创建mysqld.pid文件,并且将该文件权限改为mysql用户

cd data
touch mysqld.pid
chown -R mysql:mysql mysqld.pid

12.在mysql目录执行安装脚本

cd ..
./scripts/mysql_install_db --user=mysql

13.启动mysql

./support-files/mysql.server start

启动结果如图:
在这里插入图片描述
14.更改mysql的root用户密码

./bin/mysqladmin -u root -h localhost.localdomain password 'a12345'

如图:
在这里插入图片描述
15.登录mysql

./bin/mysql -h127.0.0.1 -uroot -pa12345

16.到此安装基本完成,但是还不能实现远程连接。

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'a12345' WITH GRANT OPTION;

flush privileges;

如图:
在这里插入图片描述
17.做一个软连接到 /usr/bin 可直接使用mysql命令

ln -s /usr/local/mysql/bin/mysql /usr/bin

完成!

二、下载并安装Bind-DLZ
1、下载并解压Bind-DLZ软件包

mkdir /usr/local/bind-dlz
cd /usr/local/bind-dlz
wget http://ftp.isc.org/isc/bind9/9.11.0-P2/bind-9.11.0-P2.tar.gz
tar -zxvf bind-9.11.0-P2.tar.gz

2、在64位系统上编译,您可能需要设置一些变量,以便找到适当的mysql库:

[root@localhost ~]# export CPPFLAGS="-I/usr/lib64/mysql $CPPFLAGS"
[root@localhost ~]# export LDFLAGS="-L/usr/lib64/mysql $LDFLAGS"
[root@localhost ~]# export LD_LIBRARY_PATH="/usr/lib64/mysql"

3.编译安装Bind-DLZ.(中间三行的命令我是直接全部复制粘贴执行的)

[root@localhost opt]# cd bind-9.11.0-P2
[root@localhost bind-9.11.0-P2]#./configure --prefix=/usr/local/bind  --enable-threads \
--enable-largefile --disable-ipv6 \
--disable-openssl-version-check \
--with-dlz-mysql=yes
[root@localhost bind-9.11.0-P2]# make
[root@localhost bind-9.11.0-P2]# make install

4.查看版本并测试软件是否安装成功

[root@localhost bind-9.11.0-P2]# /usr/local/bind/sbin/named -v
BIND 9.11.0-P2 <id:9713922>

若遇到如下报错:
/usr/local/bind/sbin/named: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
解决:
64位系统应使用下面的这行

ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/

32位系统应使用下面的这行

ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/

5、配置rndc.conf和named.conf文件

cd /usr/local/bind/sbin/

./rndc-confgen -r /dev/urandom > /usr/local/bind/etc/rndc.conf

cd /usr/local/bind/etc/

提供ca文件

wget -O named.ca  http://www.internic.net/domain/named.root

tail -10 rndc.conf | head -9 | sed s/#\ //g > named.conf

cat named.conf 生成的named.conf文件key和controls部分
配置 named.conf

vi named.conf

内容如下:

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

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

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

        };
 };

options {
        listen-on port 53 { 10.111.111.111; };
        directory "/usr/local/bind";
        Pid-file "named.pid";
        allow-query-cache { any; };
        allow-query     { any; };
};

dlz "Mysql zone" {
    database "mysql
    {host=127.0.0.1 dbname=bind ssl=false port=3306 user=root pass=a12345}
    {select zone from dns_records where zone = '$zone$' and  view = 'any' limit 1}
    {select ttl,type,if(mx_priority>0,mx_priority,NULL),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   as   mydata   from   dns_records where zone = '$zone$'   and host = '$record$' and view = 'any'}";
};

注意:配置里边的ip地址和mysql密码

6.创建named用户,使bind服务以named用户运行

[root@localhost ~]#groupadd -r -g 25 named
[root@localhost ~]#useradd -r -u 25 -s /bin/nologin -d /usr/local/named -g named named
[root@localhost ~]#chown -R named:named /usr/local/bind/

7.前台启动named服务,看看配置是否正常.

[root@localhost ~]#/usr/local/bind/sbin/named -c /usr/local/bind/etc/named.conf -f -g -u named

若遇见有报错 连接mysqly出错 先不管 先操作下边的步骤

8.在/etc/resolv.conf 文件中添加本机10.111.111.111为第一dns解析地址:

[root@localhost ~]# vim /etc/resolv.conf
# Generated by NetworkManager
nameserver 10.111.111.111
nameserver 192.168.153.2

9.登录MySQL,并创建库、表和测试数据.

mysql -h127.0.0.1 -uroot -pa12345
mysql> create database bind;

Query OK, 1 row affected (0.00 sec)

mysql> use bind;
Database changed

> CREATE TABLE IF NOT EXISTS `dns_records` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `zone` varchar(255) NOT NULL,
  `host` varchar(255) NOT NULL DEFAULT '@',
  `type` enum('A','MX','CNAME','NS','SOA','PTR','TXT','AAAA','SVR','URL') NOT NULL,
  `data` varchar(255) DEFAULT NULL,
  `ttl` int(11) NOT NULL DEFAULT '3600',
  `mx_priority` int(11) DEFAULT NULL,
  `view`  enum('any', 'Telecom', 'Unicom', 'CMCC', 'ours') NOT NULL  DEFAULT "any" ,
  `priority` tinyint UNSIGNED NOT NULL DEFAULT '255',
  `refresh` int(11) NOT NULL DEFAULT '28800',
  `retry` int(11) NOT NULL DEFAULT '14400',
  `expire` int(11) NOT NULL DEFAULT '86400',
  `minimum` int(11) NOT NULL DEFAULT '86400',
  `serial` bigint(20) NOT NULL DEFAULT '2015050917',
  `resp_person` varchar(64) NOT NULL DEFAULT 'ddns.net',
  `primary_ns` varchar(64) NOT NULL DEFAULT 'ns.ddns.net.',
  PRIMARY KEY (`id`),
  KEY `type` (`type`),
  KEY `host` (`host`),
  KEY `zone` (`zone`)
)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


Query OK, 0 rows affected (0.02 sec)

数据库中插入数据:

mysql> insert into bind.dns_records (zone, host, type, data, ttl) VALUES ('testinfo.com', 'www', 'A', '1.1.1.1', '600');
Query OK, 1 row affected (0.00 sec)

mysql> insert into bind.dns_records (zone, host, type, data, ttl) VALUES ('testinfo.com', 'bbs', 'A', '2.2.2.2', '600');
Query OK, 1 row affected (0.00 sec)

mysql> insert into bind.dns_records (zone, host, type, data, ttl) VALUES ('testinfo.com', 'm', 'A', '3.3.3.3', '600');
Query OK, 1 row affected (0.00 sec)

10.后台启动named服务:
先执行第7步

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

没有报错了后台启动

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

11.解析测试:本地添加的test.info.com域名通过10.111.111.111解析,外网的www.baidu.com使用第二个dns解析.
注:本人试了外网解析地址还是本机

[root@localhost ~]# nslookup
> www.testinfo.com
Server:         10.111.111.111
Address:        10.111.111.111#53

Name:   www.testinfo.com
Address: 1.1.1.1
>
>
> bbs.testinfo.com
Server:         10.111.111.111
Address:        10.111.111.111#53

Name:   bbs.testinfo.com
Address: 2.2.2.2
>
>
> m.testinfo.com
Server:         10.111.111.111
Address:        10.111.111.111#53

Name:   m.testinfo.com
Address: 3.3.3.3
>
>
> www.baidu.com
Server:         192.168.153.2
Address:        192.168.153.2#53

Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
Name:   www.a.shifen.com
Address: 220.181.111.188
Name:   www.a.shifen.com
Address: 220.181.112.244

完成!

三、安装过程中遇见的错误

1、前台启动named服务报错 失败连接mysql

执行

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

报错信息:

12-Oct-2020 17:33:54.021 Loading 'Mysql zone' using driver mysql
12-Oct-2020 17:34:14.048 mysql driver failed to create database connection after 4 attempts
12-Oct-2020 17:34:14.048 SDLZ driver failed to load.
12-Oct-2020 17:34:14.048 DLZ driver failed to load.
12-Oct-2020 17:34:14.048 loading configuration: failure
12-Oct-2020 17:34:14.048 exiting (due to fatal error)

如图:
在这里插入图片描述
解决:

vi /usr/local/bind/etc/named.conf

数据库连接的配置中 host配置为127.0.0.1
在这里插入图片描述
解决!



2、启动mysql报错:
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: NO)
解决问题步骤:
1.在mysql关闭的情况下:

/etc/init.d/mysql start -skip-grant-tables

2.接下来直接连接mysql,进入mysql命令行

mysql -u root p
mysql> UPDATE user SET Password=PASSWORD('a12345') where USER='root';
mysql> FLUSH PRIVILEGES;

3.最后我们重启mysql

/etc/init.d/mysql restart

可以连接了

mysql - u root -p

输入密码 a12345

解决!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值