4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DBとOpensearch連携手順書

Last updated at Posted at 2022-01-27

###⓪事前準備
AWS aurora(別のエンジンでもOK)・EC2(OS:Amazon Linux、IAMロール→セキュリティグループは適宜お願いします)・OpensearchをAWSコンソールで作成

###①EC2にSSHログイン
###②EC2にmysqlコネクタをインストール
参考:https://qiita.com/ponsuke0531/items/3ae69a9d477809c00917
###③データベースとテーブルをEC2経由で作成
※以下を実行する前にEC2にcsvファイルをインポートすること

ログイン→作成→切り替え→設定変更→テーブル作成→ロード→確認
mysql -h (データベースのエンドポイント) -P 3306 -u admin -p --enable-local-infile
CREATE DATABASE sampledb;
use sampledb;
SET character_set_database=utf8mb4;
SET character_set_server=utf8mb4;
create table sampledb (name varchar(10), type varchar(10), resource varchar(10), place varchar(10), sub varchar(10));
load data local infile "/home/ec2-user/sampledataset.csv" into table sampletable fields terminated by ',';
SELECT * FROM sampletable;

###④jdbcコネクタ

wget https://dev.mysql.com/get/Do wnloads/Connector-J/mysql-connector-java-8.0.27.tar.gz
tar xvzf mysql-connector-java-8.0 .27.tar.gz

###⑤logstashとプラグインのインストール(inputプラグインはもともとついてた)
※念のためソースを共有
https://opensearch.org/downloads.html

$wget https://artifacts.opensearch .org/logstash/logstash-oss-with-opensearch-output-plugin-7.16 .2-linux-x64.tar.gz
$tar xvzf logstash-oss-with-opensearch-output-plugin-7.16.2-linux-x64.tar.gz
$logstash-7.16.2/bin/logstash-plugin install logstash-output-opensearch
$logstash-7.16.2/bin/logstash-plugin list | grep -E "jdbc|opensearch"
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
logstash-integration-jdbc
 tqq logstash-input-jdbc
 tqq logstash-filter-jdbc_streaming
 mqq logstash-filter-jdbc_static
logstash-output-opensearch

###⑥設定ファイル作成
outputの設定のopensearchドメインエンドポイントの後に443ポートを指定しないとデフォルトで9020ポートに接続してしまうので指定必須

logstash_datapipeline.conf
input {
  jdbc {
    jdbc_driver_library => "/home/ec2-user/mysql-connector-java-8.0.27/mysql-connector-java-8.0.27.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_connection_string => "jdbc:mysql://(データベースのエンドポイント):3306/sampledb?useSSL=false"
    jdbc_user => "admin"
    jdbc_password => "password"
    schedule => "* * * * *"
    statement => "SELECT * FROM sampletable"
    jdbc_validate_connection => true
  }
}
output {
       opensearch {
                hosts => ["https://(opensearchドメインエンドポイント):443/"]
                user => "user"
                password => "password"
                index => "sample-%{+yyyy.MM.dd}"
        }
}


###⑦データ同期実行


sudo logstash-7.16.2/bin/logstash -f logstash_datapipeline.conf

この手順で基本的にはkibanaのindexに反映されるはず

##エラーメモ
####再度bin/logstash -f logstash_datapipeline.confを実行するとエラーになる
→ プロセスがすでに立ち上がっているため
(エラー:Failed to execute action)

対処
ps ax | grep logstash
sudo kill -9 25831

####コマンド実行できない
→ パスミスってるかサービス立ち上がっていない

対処
sudo systemctl start logstash
sudo systemctl status logstash

####Couldn't find any output plugin named 'opensearch'. Are you sure this is correct?
→ プラグインミスってる

対処
$logstash-7.16.2/bin/logstash-plugin list | grep -E "jdbc|opensearch"
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
logstash-integration-jdbc
 tqq logstash-input-jdbc
 tqq logstash-filter-jdbc_streaming
 mqq logstash-filter-jdbc_static
logstash-output-opensearch →これがないと上のエラーになる

####[ERROR] Failed to install template. {:message=>"Got response code '403' contacting Elasticsearch at URL 'https://(opensearchドメインエンドポイント):443/(存在しない何か)'

→ 設定ファイルのjdbc_connection_stringが多分間違えているので要確認

###参考リンク
実現したいことに近い例:
https://www.elastic.co/jp/blog/how-to-keep-elasticsearch-synchronized-with-a-relational-database-using-logstash
https://aws.amazon.com/jp/premiumsupport/knowledge-center/opensearch-connect-filebeat-logstash/
logstash-output-opensearch:
https://opensearch.org/docs/latest/clients/logstash/index/#install-logstash
logstash-integration-jdbc:
https://www.elastic.co/guide/en/logstash/current/plugins-inputs-jdbc.html
jdbcドライバー::
https://dev.mysql.com/downloads/connector/j/
logstashインストールパッケージ:
https://www.elastic.co/jp/downloads/logstash

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?