如何設定 MySQL syslog V008 2020/09/23
版權聲明 N-Partner Technologies Co. 版權所有 未經 N-Partner Technologies Co. 書面許可, 不得以任何形式仿製 拷 貝 謄抄或轉譯本手冊的任何內容 由於產品一直在更新中, N-Partner Technologies Co. 保留不告知變動的權利 商標 本手冊內所提到的任何的公司產品 名稱及註冊商標, 均屬其合法註冊公司所有
目錄 前言..2 1. CentOS 8...3 1.1 設定 My SQL 系統參數...3 1.2 設定 Rsyslog 轉發 My SQL log...4 3. Window s...5 3.1 NXLog...5 3.1.1 NXLog 架構... 5 3.1.2 NXLog 安裝... 6 3.1.3 NXLog 設定檔... 7 3.1.4 NXLog 啟動服務... 9 3.2 MySQL...10 3.2.1 編輯 MySQL 設定檔... 10 3.2.2 重啟 MySQL 服務... 11 4. N-Reporter...12
前言 本文件描述 N-Reporter 使用者, 在 Linux 使用 Rsyslog 和在 Windows 使用 Open Source 工具 NXLog 方式設定 MySQL syslog NXLog 工具將 Windows MySQL 記錄轉成 syslog, 再轉發到 N-Reporter 做正規化 稽核與分析 此文件適用於 CentOS 和 Windows 安裝 MySQL 套件 MySQL Server Logs: https://dev.mysql.com/doc/refman/8.0/en/server-logs.html 2
1. CentOS 8 1.1 設定 MySQL 系統參數 (1) 編輯 MySQL Server 設定檔 # vi /etc/my.cnf.d/mysql-server.cnf (2) 記錄在檔案 # General and Slow logging log-output=file (3) 開啟查詢記錄 general-log=1 (4) 查詢記錄檔案 general_log_file=/var/log/mysql/mysql-gen.log (5) 開啟慢查詢記錄 slow-query-log=1 (6) 慢查詢記錄檔案 slow_query_log_file=/var/log/mysql/mysql-slow.log (7) 執行超過 10 秒記錄 long_query_time=10 (8) 錯誤記錄檔案 # Error Logging. log-error=/var/log/mysql/mysqld.log (9) 重啟 MySQL 服務和確認 MySQL 服務正常 # systemctl restart mysqld && systemctl status mysqld 3
1.2 設定 Rsyslog 轉發 MySQL log (1) 編輯 Rsyslog 設定檔 # vi /etc/rsyslog.conf (2) 加載 imfile 輸入模組 module(load="imfile") # provides support for file logging (3) 編輯 130-mysql 設定檔 # vi /etc/rsyslog.d/130-mysql.conf (4) 設定轉發 MySQL log # Send MySQL log to N-Reporter input(type="imfile" File="/var/log/mysql/mysql-gen.log" Tag="mysql" Severity="info" Facility="local3" Ruleset="nreporter") input(type="imfile" File="/var/log/mysql/mysql-slow.log" Tag="mysql" Severity="warning" Facility="local3" Ruleset="nreporter") input(type="imfile" File="/var/log/mysql/mysqld.log" Tag="mysql" Severity="error" Facility="local3" Ruleset="nreporter") ruleset(name="nreporter"){action(type="omfwd" Target="192.168.1.184" Port="514" Protocol="udp")} 紅色文字部位請輸入 N-Reporter 系統 IP address Target="192.168.1.184" 紅色文字請輸入 MySQL 查詢日誌路徑檔案 File=" /var/log/mysql/mysql-gen.log" 紅色文字請輸入 MySQL 慢查詢日誌路徑檔案 File=" /var/log/mysql/mysql-slow.log" 紅色文字請輸入 MySQL 錯誤日誌路徑檔案 File=" /var/log/mysql/mysqld.log" (5) 重啟 Rsyslog 服務和確認 Rsyslog 服務正常 # systemctl restart rsyslog && systemctl status rsyslog 4
3. Windows 3.1 NXLog 3.1.1 NXLog 架構 NXLog 的 plugin 架構允許任何類型的輸入讀取資料, 解析和轉換訊息的格式, 然後將其發送到任何類型的輸出 可以同時使用不同的輸入, 處理和輸出模組來滿足事件記錄 https://nxlog.co/documentation/nxlog-user-guide#modules-im 5
3.1.2 NXLog 安裝 (1) 下載 NXLog 前往網址 https://nxlog.co/products/nxlog-community-edition/download 下載網址最新版 nxlog-ce-x.x.xxxx.msi, 範例 : nxlog-ce-2.10.2150.msi (2) 安裝 NXLog 點擊 [nxlog-ce-2.10.2150.msi] -> 按 [Install] 到 [Finish] (3) 下載並覆蓋 NXLog 設定檔開啟 [Windows PowerShell] -> 下載 nxlog_mysql.conf 並覆蓋 NXLog 設定檔 PS C:\> Invoke-WebRequest -Uri 'http://www.npartnertech.com/download/tech/nxlog_mysql.conf' -OutFile 'C:\Program Files (x86)\nxlog\conf\nxlog.conf' 6
3.1.3 NXLog 設定檔 ## Please set the ROOT to the folder your nxlog was installed into, otherwise it will not start. define NCloud 192.168.1.184 define BASEDIR C:\Logs define ROOT C:\Program Files (x86)\nxlog Moduledir %ROOT%\modules CacheDir %ROOT%\data Pidfile %ROOT%\data\nxlog.pid SpoolDir %ROOT%\data LogFile %ROOT%\data\nxlog.log ## Load the modules needed by the outputs <Extension syslog> Module xm_syslog </Extension> ## For MySQL general log file use the following: <Input in_generallog> Module im_file File '%BASEDIR%\mysql-gen.log' Exec $SyslogSeverityValue = 6; SavePos True ReadFromLast True </Input> ## For MySQL Slow log file use the following: <Input in_slowlog> Module im_file File '%BASEDIR%\mysql-slow.log' Exec $SyslogSeverityValue = 4; SavePos True ReadFromLast True </Input> ## For MySQL error log file use the following: <Input in_errorlog> Module im_file File '%BASEDIR%\mysql.err' Exec $SyslogSeverityValue = 3; SavePos True ReadFromLast True </Input> <Output out_mysqllog> Module om_udp Host %NCloud% Port 514 Exec $SyslogFacilityValue = 19; Exec to_syslog_bsd(); </Output> 7
<Route mysqllog> Path in_generallog, in_slowlog, in_errorlog => out_mysqllog </Route> 藍色文字部位請輸入 N-Reporter 系統 IP address define NCloud 192.168.1.184 藍色文字部位請輸入 MySQL log 日誌檔資料夾路徑 define BASEDIR C:\Logs 藍色文字部位請輸入 MySQL 查詢日誌檔名稱 File '%BASEDIR%\mysql-gen.log' 藍色文字部位請輸入 MySQL 慢查詢日誌檔名稱 File '%BASEDIR%\mysql-slow.log' 藍色文字部位請輸入 MySQL 錯誤日誌檔名稱 File '%BASEDIR%\mysql.err' 8
3.1.4 NXLog 啟動服務 (1) 開啟 [Windows PowerShell] (2) 啟動 NXLog 服務, 檢查 NXLog 服務和確認 NXLog 沒有錯誤訊息 PS C:\> Start-Service nxlog PS C:\> Get-Service nxlog PS C:\> Get-Content 'C:\Program Files (x86)\nxlog\data\nxlog.log' 9
3.2 MySQL 3.2.1 編輯 MySQL 設定檔 (1) 開啟 [Windows PowerShell], 編輯 my.ini 設定檔 PS C:\> notepad C:\ProgramData\MySQL\MySQL Server 8.0\my.ini 紅色文字部位請輸入 MySQL 服務版本 PS C:\> notepad C:\ProgramData\MySQL\MySQL Server 8.0\my.ini (2) 記錄在檔案 # General and Slow logging log-output=file (3) 開啟查詢記錄 general-log=1 (4) 查詢記錄檔案 general_log_file="mysql-gen.log" (5) 開啟慢查詢記錄 slow-query-log=1 (6) 慢查詢記錄檔案 slow_query_log_file="mysql-slow.log" (7) 執行超過 10 秒記錄 long_query_time=10 (8) 錯誤記錄檔案 # Error Logging log-error="mysql.err" 10
3.2.2 重啟 MySQL 服務 (1) 開啟 [Windows PowerShell] (2) 重啟 MySQL 服務和確認 MySQL 服務狀態 PS C:\> Restart-Service -Name "MySQL80" PS C:\> Get-Service -Name "MySQL80" 紅色文字部位請輸入 MySQL 服務版本 PS C:\> Restart-Service -Name "MySQL80" PS C:\> Get-Service -Name "MySQL80" 11
4. N-Reporter (1) 新增 MySQL 設備 [ 設備管理 ] -> [ 設備樹狀圖 ] -> 點選 [ 新增 ] 12
(2) 設定 MySQL 設備的資料格式和 Facility 輸入名稱和 IP -> 勾選設備種類 : [Syslog] -> 選擇資料格式 : [MySQL] 和 Facility: [(19) local use 3 (local3)] 和設備 Icon: [icon-host] -> 點選接收狀態 : [ 啟用 ] -> 按下 [ 確定 ] 13
14