时间:2021-07-01 10:21:17 帮助过:85人阅读
然后将解压好的驱动放到drivers文件夹中。
在logstash存放配置文件的文件夹中,新建一个连接MySQL的配置文件。
- <span style="color: #000000;"># 切换目录
- cd </span>/etc/logstash/<span style="color: #000000;">conf.d
- # 创建文件
- </span><span style="color: #0000ff;">touch</span> logstash-mysql.conf
logstash-mysql.conf文件内容如下(注意,这里的jar包用的是8.0.20):
- <span style="color: #000000;">input {
- stdin {
- }
- jdbc {
- </span>#<span style="color: #000000;"> 数据库连接
- jdbc_connection_string </span>=> "jdbc:mysql://你的数据库IP:3306/你的数据库"
- #<span style="color: #000000;"> 用户名密码
- jdbc_user </span>=> "用户名"<span style="color: #000000;">
- jdbc_password </span>=> "密码"
- #<span style="color: #000000;"> 驱动jar包的位置
- jdbc_driver_library </span>=> "/etc/logstash/drivers/mysql-connector-java-8.0.20.jar"
- #<span style="color: #000000;"> mysql的Driver
- jdbc_driver_class </span>=> "com.mysql.jdbc.Driver"<span style="color: #000000;">
- jdbc_paging_enabled </span>=> "true"<span style="color: #000000;">
- jdbc_page_size </span>=> "50000"<span style="color: #000000;">
- statement </span>=> "select * from 表名"<span style="color: #000000;">
- schedule </span>=> "* * * * *"
- #<span style="color: #000000;"> elastic search的索引名,相当于mysql的表名
- </span>#<span style="color: #0000ff;">type</span> => "information_summary"<span style="color: #000000;">
- }
- }
- filter {
- json {
- source </span>=> "message"<span style="color: #000000;">
- remove_field </span>=> ["message"<span style="color: #000000;">]
- }
- }
- output {
- elasticsearch {
- hosts </span>=> "localhost:9200"
- #<span style="color: #000000;"> index名
- index </span>=> "index名"
- #<span style="color: #000000;"> 需要关联的数据库中有有一个id字段,对应索引的id号
- document_id </span>=> "%{id}"<span style="color: #000000;">
- }
- stdout {
- codec </span>=><span style="color: #000000;"> json_lines
- }
- }</span>
切换到ubuntu下logstash执行文件目录:
- cd /usr/share/logstash/bin/
运行命令,使新的配置文件生效:
- ./logstash -f /etc/logstash/conf.d/logstash-mysql.conf
如果你的机器上已经启动过logstash实例的话,可能会报如下错误:
- 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
这时候需要把命令改为:
- ./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