[Elastic] Window 로컬 환경에서 Logstash + MySQL 연동

Seung Hyeon ·2023년 5월 8일
0
post-thumbnail

※ 버전: 7.17.0
※ 참고: https://narup.tistory.com/251

1. Logstash 파일 수정

config/pipelines.yml

# Example of two pipelines:
#
- pipeline.id: "test"
  pipeline.workers: 1
  pipeline.batch.size: 1
  path.config: "C:/Users/USER/logstash-7.17.0/config/*.config"
#   config.string: "input { generator {} } filter { sleep { time => 1 } } output { stdout { codec => dots } }"
# - pipeline.id: another_test
#   queue.type: persisted
  • path.config: 뒤의 경로는 실제로 logstash가 설치된 config 폴더의 경로를 입력

 

config/logstash.conf

# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.

input {
  jdbc {
	jdbc_driver_library => "C:/Users/USER/logstash-7.17.0/lib/mysql-connector-java-8.0.18.jar"
	jdbc_driver_class => "com.mysql.jdbc.Driver"
	jdbc_connection_string => "jdbc:mysql://localhost:3306/<DB이름>?useUnicode=true&serverTimezone=Asia/Seoul"
	jdbc_user => "root" (MySql Username)
	jdbc_password => "<Mysql 접속 비밀번호>"
	statement => "SELECT * FROM 테이블명"
  }
}

output{
   stdout{}
   elasticsearch{
			hosts => ["http://localhost:9200"]
			index => "temp"
			document_id => "%{id}"
   }
}

※ JDBC 드라이버 : mysql-connector-java-8.0.18.jar

  • JDBC 드라이버로 MySQL Database Table에서 데이터를 불러들이는 과정
  • jdbc_driver_library: JDBC 드라이버 라이브러리 경로
  • jdbc_driver_class : 드라이버명 (jdbc 버전에 따라 다름)
    • mysql 8버전 이상은 “com.mysql.jdbc.Driver”
  • jdbc_connection_string: JDBC connection 문자열 넣기
    • 형태: "jdbc:mysql://localhost:3306/testDB?useUnicode=true&serverTimezone=Asia/Seoul"
      • URL : mysql connection localhost 주소 (localhost가 아닌 경우에는 IP:Port 입력)
      • testDB : 연결하고자 하는 DB이름
      • serverTimezone : 시간에 대한 기준을 뭐로 잡을 것인지에 대한 내용
  • jdbc_user : MySQL에 접근 가능한 계정
  • jdbc_password : MySQL 설치 시 지정한 비밀번호
  • statement : 쿼리문 (가져올 데이터)
  • index : 지정할 인덱스명

더 자세한 인자 설명들은 여기로..

 

2. logstash.bat 실행

  • logstash가 설치된 bin 폴더에 들어가서 아래의 명령어를 입력 후 실행
    $ .\logstash.bat -f config/logstash.conf
    (f 인자로 "config/logstash.conf", logstash의 설정 파일 인자를 넘겨주는 작업)

 
※ No config path ~~ bin/config/logstash.conf ~~ 에러 뜰 경우,
C:\Users\USER\logstash-7.17.0 위치에서 $ .\bin\logstash.bat -f config/logstash.conf

 

3. ElasticSearch에 적재 확인

postman으로 확인

temp 라는 인덱스에 잘 적재되었다.

profile
안되어도 될 때까지

0개의 댓글