---
title: "syslog 経由で監査ログを記録 – MariaDB Server"
publish_date: 2019-08-27
updated_date: 2023-10-12
author: "MariaDB"
tags:
  - name: "MariaDB Server"
    url: "/ja/resources/blog/tag/mariadb-server-ja.md"
  - name: "Security"
    url: "/ja/resources/blog/tag/security-ja.md"
---

# syslog 経由で監査ログを記録 – MariaDB Server

MariaDB Server のログは，デフォルト設定では `@@datadir` ディレクトリのファイルに記録されます。

例:  
/var/lib/mysql/hostname.err  
/var/lib/mysql/server\_audit.log

今回は syslog ([rsyslog](https://www.rsyslog.com/)) 経由で監査ログ(audit log)を記録する手順について解説いたします。

### テスト環境

- OS: CentOS 7.6.1810
- [MariaDB Server](https://staging-mdb.com/downloads/#mariadb_platform-mariadb_server): 10.4.7 (Community Server)
- rsyslog: 8.24.0-34

### [audit プラグイン](https://staging-mdb.com/kb/en/library/mariadb-audit-plugin-options-and-system-variables)の設定

MariaDB Server の設定ファイルを以下のように変更します。

/etc/my.cnf.d/server.cnf

```
[mariadb]
plugin_load_add = server_audit
server_audit_logging = on
server_audit_output_type = SYSLOG
server_audit_events = 'CONNECT'
server_audit_syslog_ident = 'mariadb-audit'

```

### mariadb service の再起動

mariadb service を再起動します。

```
sudo systemctl restart mariadb

```

MariaDB monitor で MariaDB Server に接続してみます。

```
# mariadb
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.4.7-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

```

監査ログが mariadb-audit という ident で /var/log/messages に記録されていることが確認できます。

```
Aug 22 14:12:56 mdb104 mysql-server_auditing: mdb104,root,localhost,8,0,CONNECT,,,0
Aug 22 14:13:02 mdb104 mysql-server_auditing: mdb104,root,localhost,8,0,DISCONNECT,,,0

```

### rsyslog ommysql モジュールによる MariaDB へのログ保存

rsyslog には様々なモジュールがあり，[ommysql](https://www.rsyslog.com/doc/v8-stable/configuration/modules/ommysql.html) モジュールは MariaDB/MySQL へのログ保存を行うことができます。  
ommysql モジュールを利用するには，まず rsyslog-mysql パッケージをインストールします。

```
sudo yum -y install rsyslog-mysql

```

MariaDB Server 上でログ保存用のデータベース/テーブルを作成します。

```
git clone https://github.com/rsyslog/rsyslog.git
cd plugins/ommysql
sudo mariadb < createDB.sql

```

/etc/rsyslog.conf に以下の設定を追加します。

```
$ModLoad ommysql
*.* :ommysql:127.0.0.1,Syslog,ommysql,password

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514

```

ここで，localhost 上の `Syslog` データベースに `ommysql` ユーザ，`password` というパスワードで接続します。  
rsyslog service を再起動します。

```
sudo systemcrl restart rsyslog

```

Syslog データベースの SystemEvents テーブルを確認します。

```
# mariadb Syslog
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.4.7-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [Syslog]> SELECT ReceivedAt, Message FROM SystemEvents WHERE SysLogTag LIKE 'mariadb-audit:';
+---------------------+--------------------------------------------------+
| ReceivedAt          | Message                                          |
+---------------------+--------------------------------------------------+
| 2019-08-22 19:16:09 |  mdb104,root,localhost,8,0,DISCONNECT,Syslog,,0  |
| 2019-08-22 19:16:09 |  mdb104,ommysql,localhost,9,0,CONNECT,Syslog,,0  |
| 2019-08-22 19:16:10 |  mdb104,root,localhost,10,0,CONNECT,Syslog,,0    |
| 2019-08-22 19:35:33 |  mdb104,root,localhost,10,0,DISCONNECT,Syslog,,0 |
| 2019-08-22 19:41:37 |  mdb104,ommysql,localhost,8,0,CONNECT,Syslog,,0  |
| 2019-08-22 19:42:39 |  mdb104,root,localhost,9,0,CONNECT,Syslog,,0     |
+---------------------+--------------------------------------------------+

```

接続ログが Syslog.SystemEvents テーブルに記録されていることが確認できます。

[前回のブログ投稿](https://staging-mdb.com/ja/resources/blog/encryption-with-aws-kms-plugin/)にて Data-at-Rest Encryption によるテーブルの暗号化を解説しておりますので，暗号化を有効にすることで最終的に監査ログの暗号化を行うことが可能となります。

### まとめ

今回は監査ログを syslog 経由でOSのログファイル，もしくは MariaDB テーブルに保存する手順について解説させて頂きました。Data-at-Rest Encryption を有効にすれば監査ログを暗号化することも可能ですので，高いセキュリティ要件が求められている場合は参考にして頂ければと存じます。