自学nginx(五): 使用fluentd搬运log到mysql
来源:互联网 发布:php数组赋值给js数组 编辑:程序博客网 时间:2024/05/17 22:45
前言
本篇介绍如何使用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文件
<source> @type tail path /tmp/nginx.log pos_file /var/log/td-agent/nginx.log.pos tag nginx.access format /^(?<remote>[^ ]*) - (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" "(?<http_x_forwarded_for>[^\"]*)" "(?<host>[^\"]*)" "(?<country>[^\"]*)" "(?<city>[^\"]*)")?$/ time_format %d/%b/%Y:%H:%M:%S %z</source>
注意:format的地方需要根据自己的nginx的log的格式进行相应的调整
接下去定义一个写入到mysql的match。
<match nginx.access> @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</match>
附上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.gemBuilding native extensions. This could take a while...Successfully installed mysql2-0.4.9Fetching: mysql2-cs-bind-0.0.6.gem (100%)Successfully installed mysql2-cs-bind-0.0.6Fetching: jsonpath-0.8.7.gem (100%)Successfully installed jsonpath-0.8.7Successfully installed fluent-plugin-mysql-0.1.5Parsing documentation for mysql2-0.4.9Installing ri documentation for mysql2-0.4.9Parsing documentation for mysql2-cs-bind-0.0.6Installing ri documentation for mysql2-cs-bind-0.0.6Parsing documentation for jsonpath-0.8.7Installing ri documentation for jsonpath-0.8.7Parsing documentation for fluent-plugin-mysql-0.1.5Installing ri documentation for fluent-plugin-mysql-0.1.5Done installing documentation for mysql2, mysql2-cs-bind, jsonpath, fluent-plugin-mysql after 0 seconds4 gems installed
结语
可能遇到的坑总结一下:
* gem命令的时候,需要使用/usr/sbin/td-agent-gem,td-agent使用的是这个环境的gem依赖
* ruby安装mysql的插件的时候,需要mysql-devel,否则会报错
* nginx的log format可能根据自己的情况调整
* source文件的tail需要该输入文件的目录权限是755
- 自学nginx(五): 使用fluentd搬运log到mysql
- MySQL自学篇(五)
- Mysql自学笔记五(存储过程)
- nginx的log使用
- 用Fluentd实现收集日志到HDFS(上)
- 用Fluentd实现收集日志到HDFS(下)
- [fluentd学习]安装使用
- Enabling Facebook’s Log Infrastructure with Fluentd
- 模拟PT站种子搬运过程(以HDC搬运到未来花园为例)
- 搬运自己的mysql学习笔记3-dbutil的使用
- fluentd
- boost log库使用五
- nginx常用指令及使用(五)
- 使用 Fluentd 管理 Docker 日志
- 使用fluentd管理docker日志
- MySQL使用教程(五)
- fluentd学习——High Availability (多级fluentd配置)
- 搬运system到0地址
- 花一块钱----快速部署一个自己的静态网站(京东云服务器)
- 关于canvas、paint的相关方法汇总
- 扒完社交网络关系才明白,《权力的游戏》凭什么是神作
- 北斗卫星导航定位系统应用及发展前景
- 高仿饿了吗自定义数字加减
- 自学nginx(五): 使用fluentd搬运log到mysql
- 敌兵布阵 HDU
- Python基础学习(七)
- 解决 无法读取到 /data/data/yourPackageName/files/coverage.ec 文件
- 爬虫钛媒体科技新闻
- 自加自减运算符的错误使用和理解【个人学习笔记,如有错误欢迎指正】
- Robotframework(4):创建变量的类型和使用
- iOS 原生库对 https 的处理
- Redis集群:基于twemproxy的实现