Fluentd直接传输日志给MongoDB (standalone)

官方文档地址:https://docs.fluentd.org/output/mongo

td-agent版本默认没有包含out_mongo插件,需要安装这个插件才能使用

使用的是td-agent,安装这个插件:

$ /usr/sbin/td-agent-gem install gems
$ /usr/sbin/td-agent-gem install fluent-plugin-mongo

使用的是Fluentd,安装这个插件:

$ fluent-gem install fluent-plugin-mongo

有关插件管理可以看这篇文章,地址:https://www.cnblogs.com/sanduzxcvbnm/p/13936280.html

配置示例

# Single MongoDB
<match mongo.**>
  @type mongo
  host fluentd
  port 27017
  database fluentd
  collection test

  # for capped collection
  capped
  capped_size 1024m

  # authentication
  user michael
  password jordan

  <inject>
    # key name of timestamp
    time_key time
  </inject>

  <buffer>
    # flush
    flush_interval 10s
  </buffer>
</match>

参数说明

  • @type:必填mongo
  • connection_string:必填,MongoDB的URI连接字符串,类型是string,默认是nil
  • host:必填,MongoDB主机名,类型是string,默认是'localhost'
  • port:必填,MongoDB端口号,类型是integer,默认是27017
  • database:必填,MongoDB数据库,类型是string,默认是nil
  • collection:必填,集合名称,类型是string,如果没有设置tag_mapped则默认是'untagged'
  • tag_mapped:是否允许out_mongo使用Fluentd的标记来确定目标集合,类型是bool,默认是false
  • capped:启用capped集合,类型是string
  • capped_size:上限集合大小,类型是size
  • user:用于身份验证的用户名,类型是string
  • password:用于身份验证的密码,类型是string
  • time_key:时间戳的密钥名,类型是string

connection_string参数官方文档上是必填,但是实际使用的时候没找到这个咋用的,官方示例上也没有用这个

如果没有配置tag_mapped,则说使用配置文件中的中的database(数据库)+collection(集合/表)

如下这个示例,设置了tag_mapped,fluentd设置的tag是mongo.foo,但是设置了remove_tag_prefix去掉前缀mongo.只使用foo,结合起来后,最终使用的是database(fluentd)+collection(foo)

<match mongo.*>
  @type mongo
  host fluentd
  port 27017
  database fluentd

  # Set 'tag_mapped' if you want to use tag mapped mode.
  tag_mapped

  # If the tag is "mongo.foo", then the prefix "mongo." is removed.
  # The inserted collection name is "foo".
  remove_tag_prefix mongo.

  # This configuration is used if the tag is not found. The default is 'untagged'.
  collection misc
</match>

用法示例

mongodb安装参考:https://www.cnblogs.com/sanduzxcvbnm/p/13935837.html
这个示例就是没有设置tag_mapped

<source>
  @type tail
  @id input_tail
  <parse>
    @type nginx
  </parse>
  path /usr/local/openresty/nginx/logs/host.access.log
  tag mongo.nginx
</source>

<match mongo.nginx>
  @type mongo
  host 192.168.0.253
  port 27017
  database fluentd # 事先创建好的数据库
  collection test # 相当于数据表,会自动生成

  capped
  capped_size 1024m

  user testadmin # 事先创建好的数据库相对应的用户名
  password 123456 # 事先创建好的数据库相对应的密码

  <inject>
    time_key time
  </inject>

  <buffer>
    flush_interval 10s
  </buffer>
</match>

启动openresty,浏览器访问,然后使用Navicat查看数据

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/13936308.html