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

本文介绍如何使用Fluentd收集并解析Nginx的日志文件,将其转换为MySQL可存储的数据格式,最终存入数据库。文章详细说明了在Amazon Linux AMI环境下安装配置Fluentd及MySQL插件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

本篇介绍如何使用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命令集合

 
  1. /etc/init.d/td-agent start

  2. /etc/init.d/td-agent stop

  3. /etc/init.d/td-agent restart

  4. /etc/init.d/td-agent status

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

编辑fluentd配置文件

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

 
  1. <source>

  2. @type tail

  3. path /tmp/nginx.log

  4. pos_file /var/log/td-agent/nginx.log.pos

  5. tag nginx.access

  6. format /^(?<remote>[^ ]*) - (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" "(?<http_x_forwarded_for>[^\"]*)" "(?<host>[^\"]*)" "(?<country>[^\"]*)" "(?<city>[^\"]*)")?$/

  7. time_format %d/%b/%Y:%H:%M:%S %z

  8. </source>

  9.  

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

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

 
  1. <match nginx.access>

  2. @type mysql_bulk

  3. host your_host

  4. database your_db

  5. username your_username

  6. password your_password

  7. column_names remote,host,user,method,path,code,size,referer,agent,country,city,http_x_forwarded_for,log_time

  8. key_names remote,host,user,method,path,code,size,referer,agent,country,city,http_x_forwarded_for,${time}

  9. table nginx_access

  10. flush_interval 10s

  11. </match>

  12.  

附上mysql的建表语句

 
  1. CREATE TABLE `nginx_access` (

  2. `id` int(11) NOT NULL AUTO_INCREMENT,

  3. `remote` text,

  4. `host` text,

  5. `user` text,

  6. `method` text,

  7. `path` text,

  8. `code` text,

  9. `size` text,

  10. `referer` text,

  11. `agent` text,

  12. `country` text,

  13. `city` text,

  14. `http_x_forwarded_for` text,

  15. `log_time` timestamp NULL DEFAULT NULL,

  16. PRIMARY KEY (`id`)

  17. ) 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。

 
  1. [root@x fluent-plugin-mysql]# /usr/sbin/td-agent-gem install fluent-plugin-mysql-0.1.5.gem

  2. Building native extensions. This could take a while...

  3. Successfully installed mysql2-0.4.9

  4. Fetching: mysql2-cs-bind-0.0.6.gem (100%)

  5. Successfully installed mysql2-cs-bind-0.0.6

  6. Fetching: jsonpath-0.8.7.gem (100%)

  7. Successfully installed jsonpath-0.8.7

  8. Successfully installed fluent-plugin-mysql-0.1.5

  9. Parsing documentation for mysql2-0.4.9

  10. Installing ri documentation for mysql2-0.4.9

  11. Parsing documentation for mysql2-cs-bind-0.0.6

  12. Installing ri documentation for mysql2-cs-bind-0.0.6

  13. Parsing documentation for jsonpath-0.8.7

  14. Installing ri documentation for jsonpath-0.8.7

  15. Parsing documentation for fluent-plugin-mysql-0.1.5

  16. Installing ri documentation for fluent-plugin-mysql-0.1.5

  17. Done installing documentation for mysql2, mysql2-cs-bind, jsonpath, fluent-plugin-mysql after 0 seconds

  18. 4 gems installed

结语

可能遇到的坑总结一下:

  • gem命令的时候,需要使用/usr/sbin/td-agent-gem,td-agent使用的是这个环境的gem依赖
  • ruby安装mysql的插件的时候,需要mysql-devel,否则会报错
  • nginx的log format可能根据自己的情况调整
  • source文件的tail需要该输入文件的目录权限是755
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值