fluentd mysql_使用fluentd实时收到nginx日志到mysql数据库

文章目录

[隐藏]

前言

环境

安装fluentd

启动fluentd命令集合

编辑fluentd配置文件

安装mysql_bulk的插件

结语

前言

本篇介绍如何使用fluentd把nginx的log日志读取,并且解析成为一个一个MySQL的字段,最后存储到mysql的数据库中。

环境

我用的是aws的ec2,操作系统是amazon定制的Amazon Linux AMI

安装fluentd

使用root用户

curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh

安装完毕后,在/usr/sbin/下会有td-agent , td-agent-gem ,td-agent-ui三个可执行文件。其中td-agent-gem用来安装之外的fluent的插件。

配置文件在/etc/td-agent/td-agent.conf

启动fluentd命令集合

/etc/init.d/td-agent start /etc/init.d/td-agent stop /etc/init.d/td-agent restart /etc/init.d/td-agent status

log可以在/var/log/td-agent/td-agent.log查看

编辑fluentd配置文件

定义一个source,读取nginx的log文件

@type tail path /tmp/nginx.log pos_file /var/log/td-agent/nginx.log.pos tag nginx.access format /^(?[^ ]*) - (?[^ ]*) [(?[^]]*)] "(?S+)(?: +(?ath>[^"]*) +S*)?" (?[^ ]*) (?[^ ]*)(?: "(?[^"]*)" "(?[^"]*)" "(?[^"]*)" "(?[^"]*)" "(?[^"]*)" "(?[^"]*)")?$/ time_format %d/%b/%Y:%H:%M:%S %z

注意:format的地方需要根据自己的nginx的log的格式进行相应的调整

接下去定义一个写入到mysql的match。

@type mysql_bulk host your_host database your_db username your_username password your_password column_names remote,host,user,method,path,code,size,referer,agent,country,city,http_x_forwarded_for,log_time key_names remote,host,user,method,path,code,size,referer,agent,country,city,http_x_forwarded_for,${time} table nginx_access flush_interval 10s

附上mysql的建表语句

CREATE TABLE `nginx_access` ( `id` int(11) NOT NULL AUTO_INCREMENT, `remote` text, `host` text, `user` text, `method` text, `path` text, `code` text, `size` text, `referer` text, `agent` text, `country` text, `city` text, `http_x_forwarded_for` text, `log_time` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;

安装mysql_bulk的插件

从https://github.com/tagomoris/fluent-plugin-mysql克隆下项目。由于我用的fluentd是0.12版本,所以对应的fluent-plugin-mysql的版本是v0.1.5,所以Git checkout v0.1.5。

进行gem build fluent-plugin-mysql.gemspec生成.gem文件。

然后用/usr/sbin/td-agent-gem install fluent-plugin-mysql-0.1.5.gem命令来安装插件。

如果安装插件失败的话,很大可能是没有mysql-devel。

mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.

所以先执行yum install mysql-devel。

[root@x fluent-plugin-mysql]# /usr/sbin/td-agent-gem install fluent-plugin-mysql-0.1.5.gem Building native extensions. This could take a while... Successfully installed mysql2-0.4.9 Fetching: mysql2-cs-bind-0.0.6.gem (100%) Successfully installed mysql2-cs-bind-0.0.6 Fetching: jsonpath-0.8.7.gem (100%) Successfully installed jsonpath-0.8.7 Successfully installed fluent-plugin-mysql-0.1.5 Parsing documentation for mysql2-0.4.9 Installing ri documentation for mysql2-0.4.9 Parsing documentation for mysql2-cs-bind-0.0.6 Installing ri documentation for mysql2-cs-bind-0.0.6 Parsing documentation for jsonpath-0.8.7 Installing ri documentation for jsonpath-0.8.7 Parsing documentation for fluent-plugin-mysql-0.1.5 Installing ri documentation for fluent-plugin-mysql-0.1.5 Done installing documentation for mysql2, mysql2-cs-bind, jsonpath, fluent-plugin-mysql after 0 seconds 4 gems installed

结语

可能遇到的坑总结一下:

gem命令的时候,需要使用/usr/sbin/td-agent-gem,td-agent使用的是这个环境的gem依赖

ruby安装mysql的插件的时候,需要mysql-devel,否则会报错

nginx的log format可能根据自己的情况调整

source文件的tail需要该输入文件的目录权限是755

原文出处:csdn -> http://blog.csdn.net/leon_wzm/article/details/77720429

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值