Skip to content

Instantly share code, notes, and snippets.

@onigra
Last active December 16, 2015 19:39
Show Gist options
  • Save onigra/5486586 to your computer and use it in GitHub Desktop.
Save onigra/5486586 to your computer and use it in GitHub Desktop.
mysqlのslow-logをfluentdで集める

環境

  • CentOS 6.3
  • perl 5.10.1
  • MySQL 5.5.30

MySQL、fluent-agent-lite、td-agent全てlocalhostで動かしている

必要なツール

送信設定側(MySQLが動いてるサーバ)

受信設定側(ログ収集サーバ)

fluent-agent-liteの導入

インストール

git clone [email protected]:tagomoris/fluent-agent-lite.git
cd fluent-agent-lite/bin
./install.sh

confの修正

vi /etc/fluent-agent-lite.conf
TAG_PREFIX="mysql"
LOGS=$(cat <<"EOF"
slowlog.db01 /var/lib/mysql/mysqld-slow.log
EOF
)
PRIMARY_SERVER="localhost:24224"

起動

/etc/init.d/fluent-agent-lite start

td-agentの導入

リポジトリの追加

touch /etc/yum.repos.d/td.repo
vi /etc/yum.repos.d/td.repo
[treasuredata]
name=TreasureData
baseurl=http://packages.treasure-data.com/redhat/$basearch
gpgcheck=0

インストール

yum update
yum install td-agent

プラグイン(fluent-plugin-mysqlslowquerylog)のインストール

/usr/lib/fluent/ruby/bin/fluent-gem install fluent-plugin-mysqlslowquerylog

confの修正

vi /etc/td-agent/td-agent.conf
<source>
  type forward
</source>
<match mysql.slowlog.*>
  type mysqlslowquerylog
  add_tag_prefix cocatenated.
</match>
<match cocatenated.mysql.slowlog.*>
  type file
  path /tmp/slowtest
</match>

起動

/etc/init.d/td-agent start

ログの出力確認

正しく動いていれば、/tmpの下にslowtest.20130430.*というようなログが出る。
中を見ると、下記のような感じでログが出ている。

2013-04-30T21:57:28+09:00       cocatenated.mysql.slowlog.db1   {"user":"root[root]","host":"localhost","query_time":0.000376,"lock_time":9.2e-05,"rows_sent":30,"rows_examined":30,"sql":"SET timestamp=1367326648; select * from customers;"}
2013-04-30T21:58:17+09:00       cocatenated.mysql.slowlog.db1   {"user":"root[root]","host":"localhost","query_time":0.000384,"lock_time":9.7e-05,"rows_sent":30,"rows_examined":30,"sql":"SET timestamp=1367326697; select * from customers;"}
2013-04-30T22:05:24+09:00       cocatenated.mysql.slowlog.db1   {"user":"root[root]","host":"localhost","query_time":0.000393,"lock_time":9.8e-05,"rows_sent":30,"rows_examined":30,"sql":"SET timestamp=1367327124; select * from customers;"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment