mysql导出txt到client_mysql导出导入txt以及sftp自动下载(一)

本文介绍了如何通过shell脚本实现公网服务器上的MySQL数据库每五分钟导出数据为txt文件,并通过sftp自动同步到内网服务器,再用mysqlimport导入到本地数据库。主要涉及NTP服务同步、ssh密钥配对、mysqlexport和mysqlimport的使用。
摘要由CSDN通过智能技术生成

场景需求:

公网服务器(以下简称server)mysql数据库里每五分钟导出一次统计数据,同时本地内网服务器(以下简称client)每五分钟通过sftp把导出的txt导入到本地测试数据库,使用python(highcharts)做数据图形展示。

需求分析:

1.所有实现命令基于shell环境编写

2.公网内网服务器使用ntp服务,保证两台机器时间同步

3.需要对select结果进行本地导出生成xls或者txt等类型文件(考虑到安全问题,公网mysql不对外开放远程权限,数据只能先存储在本地公网服务器)

4.时间同步以后,server与client通过做ssh密钥,计划任务使用sftp方式达到自动下载数据目的

5.实际操作使用mysqlimport来导入txt文件到本地数据库

本文将记录以上五点实现步骤,python图形展示本人未负责故不记录,可能内容较多分多篇文章记录

NTP服务搭建

client:ip:192.168.2.100  系统版本CentOS release 6.5 (Final)

server:210.72.145.44(中国国家授时中心)

安装配置

CentOS 6.5系统已经自带了NTPD服务,一般默认是安装了的,如果没有安装,先检查下,然后配置好yum仓库,yum方式安装下就OK,具体如下:

[root@localhost ~]# rpm -q ntp

ntp-4.2.6p5-1.el6.centos.x86_64 #这表示已安装,如果没有安装这里是空白

如果没有安装,yum安装一下

# yum install ntp

安装以后设置开机启动

# chkconfig ntpd on

# chkconfig --list ntpd

ntpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

在配置前,先使用ntpdate手动同步下时间,免得本机与外部时间服务器时间差距太大,让ntpd不能正常同步

[root@localhost ~]# ntpdate -u 202.112.10.36

3 Jul 14:29:58 ntpdate[8237]: adjust time server 202.112.10.36 offset -0.000288 sec

准备工作做完,后来开始配置ntpd的核心配置文件/etc/ntp.conf文件,参照以下配置好了就OK。

[root@localhost ~]# cat /etc/ntp.conf

# For more information about this file, see the man pages

# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not

# permit the source to query or modify the service on this system.

restrict default kod nomodify notrap nopeer noquery

restrict -6 default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could

# be tightened as well, but to do so would effect some of

# the administrative functions.

restrict 127.0.0.1

restrict -6 ::1

# Hosts on local network are less restricted.

#允许内网其他机器同步时间

restrict 192.168.2.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.

# Please consider joining the pool (http://www.pool.ntp.org/join.html).

#server 0.centos.pool.ntp.org iburst

#server 1.centos.pool.ntp.org iburst

#server 2.centos.pool.ntp.org iburst

#server 3.centos.pool.ntp.org iburst

#中国这边最活跃的时间服务器 : http://www.pool.ntp.org/zone/cn

server 210.72.145.44 perfer

server 202.112.10.36

server 59.124.196.83

#broadcast 192.168.1.255 autokey    # broadcast server

#broadcastclient            # broadcast client

#broadcast 224.0.1.1 autokey        # multicast server

#multicastclient 224.0.1.1        # multicast client

#manycastserver 239.255.254.254        # manycast server

#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.

#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating

# with symmetric key cryptography.

keys /etc/ntp/keys

# Specify the key identifiers which are trusted.

#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.

#requestkey 8

# Specify the key identifier to use with the ntpq utility.

#controlkey 8

# Enable writing of statistics records.

#statistics clockstats cryptostats loopstats peerstats

参照红色部分,配置完成,保存退出,重启服务

# service ntpd start

查看服务连接和监听

# netstat -tlunp | grep ntp

udp        0      0 192.168.2.100:123           0.0.0.0:*                               2173/ntpd

udp        0      0 127.0.0.1:123               0.0.0.0:*                               2173/ntpd

udp        0      0 0.0.0.0:123                 0.0.0.0:*                               2173/ntpd

udp        0      0 ::1:123                     :::*                                    2173/ntpd

udp        0      0 fe80::290:27ff:fefd:df5b:123 :::*                                    2173/ntpd

udp        0      0 :::123                      :::*                                    2173/ntpd

红色加粗部分表示已监听,采用UDP方式,端口123

……

ntpq -p 查看网络中的NTP服务器,同时显示客户端和每个服务器的关系

[root@localhost ~]# ntpq -p

remote refid st t when poll reach delay offset jitter

==============================================================================

210.72.145.44 .STEP. 16 u - 1024 0 0.000 0.000 0.000

*gus.buptnet.edu 10.3.8.150 5 u 1526 1024 372 26.723 -5.993 5.637

59-124-196-83.H .STEP. 16 u - 1024 0 0.000 0.000 0.000

……

ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。

刚启动的时候,一般是:

# ntpstat

unsynchronised

time server re-starting

polling server every 64 s

连接并同步后:

synchronised to NTP server (202.112.10.36) at stratum 6

time correct to within 751 ms

polling server every 1024 s

OK,内网的NTPD服务已经配置完成,如果所有正常后,开始配置内网的其他设备与这台服务器作为时间同步服务。

NTP服务器搭建参考资料:http://acooly.iteye.com/blog/1993484

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值