---
title: "Flume保存日志到MongoDB"
author: "java_my_life"
date: "2015-08-26T11:42:40Z"
tags: []
categories: ["开源软件"]
slug: "flume-mongodb"
draft: false
lastmod: "2015-09-09T10:23:39Z"
id: 2238085
url: "https://java-my-life.iteye.com/blog/2238085"
---

首先到网站下载Flume

地址：[http://flume.apache.org/download.html](http://flume.apache.org/download.html)

然后放到/usr/local/目录下解压

 

```
tar -zxvf apache-flume-1.5.2-bin.tar.gz
```

 

下载MongoDB插件

地址：[https://github.com/leonlee/flume-ng-mongodb-sink](https://github.com/leonlee/flume-ng-mongodb-sink)

下载后是一个工程，需要自己打包成jar，然后直接放到解压后的Flume目录下的lib目录即可，当然还需要把mongodb驱动一起放入

 

进入Flume目录下的conf目录，编辑flume-server.conf文件如下：

```
# 定义组件名称  
agent2.sources = source2  
agent2.sinks = sink2  
agent2.channels = channel2  
  
# 定义数据入口  
agent2.sources.source2.type = syslogudp 
agent2.sources.source2.host = 0.0.0.0  
agent2.sources.source2.port = 10001  
agent2.sources.source2.channels = channel2  
  
# 定义数据出口  
agent2.sinks.sink2.type = org.riderzen.flume.sink.MongoSink  
agent2.sinks.sink2.host = 192.168.8.30
agent2.sinks.sink2.port = 27017  
agent2.sinks.sink2.model = single
agent2.sinks.sink2.db = test
agent2.sinks.sink2.collection = log  
agent2.sinks.sink2.batch = 2  
agent2.sinks.sink2.channel = channel2  
  
  
  
# 使用内存管道  
agent2.channels.channel2.type = memory  
agent2.channels.channel2.capacity = 1000  
agent2.channels.channel2.transactionCapacity = 100
```

 然后进入Flume的bin目录启动服务

```
./flume-ng agent -n agent2 -c ../conf -f ../conf/flume-server.conf
```

 -n表示当前运行的这个agent名称

 -c表示配置文件存放目录

 -f表示运行agent所使用的配置文件

如果想要程序后台运行，只需要在最后空格一下，加上&即可。（非永久性，远程断开linux时会停止）

如果想要永久性程序后台运行，只需要在最前面加上nohup，然后加一个空格隔开

如果想要使用debug模式查看日志，只需要在最后加上-Dflume.root.logger=INFO,console即可