当前位置:Gxlcms > 数据库问题 > ELK - Logstash连接MySQL

ELK - Logstash连接MySQL

时间:2021-07-01 10:21:17 帮助过:85人阅读

/etc/logstash # 创建文件夹 mkdir drivers

然后将解压好的驱动放到drivers文件夹中。

配置Logstash

在logstash存放配置文件的文件夹中,新建一个连接MySQL的配置文件。

  1. <span style="color: #000000;"># 切换目录
  2. cd </span>/etc/logstash/<span style="color: #000000;">conf.d
  3. # 创建文件
  4. </span><span style="color: #0000ff;">touch</span> logstash-mysql.conf

logstash-mysql.conf文件内容如下(注意,这里的jar包用的是8.0.20):

  1. <span style="color: #000000;">input {
  2. stdin {
  3. }
  4. jdbc {
  5. </span>#<span style="color: #000000;"> 数据库连接
  6. jdbc_connection_string </span>=> "jdbc:mysql://你的数据库IP:3306/你的数据库"
  7. #<span style="color: #000000;"> 用户名密码
  8. jdbc_user </span>=> "用户名"<span style="color: #000000;">
  9. jdbc_password </span>=> "密码"
  10. #<span style="color: #000000;"> 驱动jar包的位置
  11. jdbc_driver_library </span>=> "/etc/logstash/drivers/mysql-connector-java-8.0.20.jar"
  12. #<span style="color: #000000;"> mysql的Driver
  13. jdbc_driver_class </span>=> "com.mysql.jdbc.Driver"<span style="color: #000000;">
  14. jdbc_paging_enabled </span>=> "true"<span style="color: #000000;">
  15. jdbc_page_size </span>=> "50000"<span style="color: #000000;">
  16. statement </span>=> "select * from 表名"<span style="color: #000000;">
  17. schedule </span>=> "* * * * *"
  18. #<span style="color: #000000;"> elastic search的索引名,相当于mysql的表名
  19. </span>#<span style="color: #0000ff;">type</span> => "information_summary"<span style="color: #000000;">
  20. }
  21. }
  22. filter {
  23. json {
  24. source </span>=> "message"<span style="color: #000000;">
  25. remove_field </span>=> ["message"<span style="color: #000000;">]
  26. }
  27. }
  28. output {
  29. elasticsearch {
  30. hosts </span>=> "localhost:9200"
  31. #<span style="color: #000000;"> index名
  32. index </span>=> "index名"
  33. #<span style="color: #000000;"> 需要关联的数据库中有有一个id字段,对应索引的id号
  34. document_id </span>=> "%{id}"<span style="color: #000000;">
  35. }
  36. stdout {
  37. codec </span>=><span style="color: #000000;"> json_lines
  38. }
  39. }</span>

启动Logstash

切换到ubuntu下logstash执行文件目录:

  1. cd /usr/share/logstash/bin/

运行命令,使新的配置文件生效:

  1. ./logstash -f /etc/logstash/conf.d/logstash-mysql.conf

如果你的机器上已经启动过logstash实例的话,可能会报如下错误:

  1. Logstash could <span style="color: #0000ff;">not</span> be started because there is already another instance using the configured data directory. <br><br><span style="color: #0000ff;">If</span> you wish to <span style="color: #0000ff;">run</span> multiple instances, you must change the "path.data" setting

这时候需要把命令改为:

  1. ./logstash -f /etc/logstash/conf.d/logstash-mysql.conf --path.data=/usr/logstash_data/你的index名

这样就会把通过这个配置拿到的数据落在新的文件夹中,也不会报错了。

ELK - Logstash连接MySQL

标签:表名   message   search   set   statement   mamicode   lib   rect   HERE   

人气教程排行