您的位置:首页 > 数据库 > MariaDB

rsyslog(四)实现将日志记录于Mariadb数据库中

2018-02-23 20:13 417 查看

实现将日志记录于Mariadb数据库中

准备工作:

1,准备一台存放日志的mariadb数据库服务器
2,确保selinux,iptables是关闭状态

步骤:

准备日志Mariadb数据库服务器

安装Mariadb数据库

yum install mariadb-server
也可选择二进制安装安装数据库参考: http://blog.51cto.com/13598893/2070289[/code] 

启动Mariadb数据库,并设置为开机自动启动

CentOS-6启动服务: service  mariadb start
CentOS-6设为开机自动启动:chkconfig mariadb on
CentOS-7启动服务:systemctl start maridb
CentOS-7设为开机自动启动:systemctl enable maridb

运行Mariadb安全脚本

"注意:为了数据库安全需运行一个安全脚本如下"

"mysql_secure_installation"

第一项问你:输入root密码  回车即可,因为没有
第二项问你:需要设置root密码么,当然要  敲Y
第三项问你:需要删除空账号用户么,当然要  敲Y
第四项问你:禁止root用户远程登入么,根据你们公司的需要
第五项问你:需要删除test测试数据库么,删除
第六项问你:现在重新加载权限表吗 ,当然

配置rsyslog服务器

安装rsyslog-mysql包,此包的作用就是让rsyslog服务具有连接数据库的功能

yum install rsyslog-mysql

查看rsyslog-mysql 包文件

rpm -ql rsyslog-mysql
/lib64/rsyslog/ommysql.so
/usr/share/doc/rsyslog-mysql-5.8.10
/usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql

复制生成数据库的文件至日志mariadb数据库服务器

scp /usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql 目标主机IP:/dir

mariadb服务器,生成日志数据库

进到复制过来的文件的目录下运行该命令
mysql -uroot -p  < createDB.sql
Enter password:
#"-u指定用户,-p输入改用户的密码"

运行完该命令之后会在数据库里生成一个名为Syslog的数据库,里面会有两张表,

检查数据库是否生成

MariaDB [(none)]> show tables from Syslog;
+----------------------------------+
| Tables_in_Syslog             |
+----------------------------------+
| SystemEvents                  |#"存放系统事件"
| SystemEventsProperties  | #"存放系统事件属性"
+----------------------------------+
2 rows in set (0.00 sec)

接下来需要创建一个对于Syslog数据库管理员账号

创建Syslog数据库的管理员

MariaDB [(none)]>grant ALL on Syslog.* to syslog@'192.168.136.6' identified by 'centos';
命令解析:
"grant:"创键授权用户
"ALL:"    表示对Syslog这个数据库拥有所有权限
"syslog@'192.168.136.6'":其中syslog是用户名,而192.168.136.6表示该用户只能在这台主机上登陆,这也是为了安全起见,
"identified by 'centos':"这identified by后面单引号里跟的就是该用户的密码了 'centos'最后别忘了";"结尾

修改rsyslog服务器的配置文件

vim /etc/rsyslog.conf
# 在#### MODULES ###
#"下加上这么一行配置,作用就是让rsyslog支持连接数据库功能"
"$ModLoad ommysql"
# 在#### RULES ####下设置那些日志需要存到数据库里
语法格式:  facility.priority   :ommysql:DBHOST,DBNAME,DBUSER,PASSWORD
例如:
*.info;mail.none;authpriv.none;cron.none  :ommysql:192.168.136.7,Syslog,syslog,centos

重启rsyslog服务

CentOS-6重启:service rsyslog restart
CentOS-7重启:systemctl restart rsyslog

最后的测试

在客户端主机利用logger命令发送日志

logger -p *.info "The is mysqllog  text log"
#该命令作用就是发送一条任意设施,info级别以上的日志,内容是"The is mysqllog  text log"

查看数据库里面的日志数据库是否计入内容了

如果出现这些字样,即表示成功了
MariaDB [Syslog]> select * from  SystemEvents\G
*************************** 1. row ***************************
ID: 1
CustomerID: NULL
ReceivedAt: 2018-02-09 22:05:05
DeviceReportedTime: 2018-02-09 22:05:05
Facility: 0
Priority: 6
FromHost: centos6
Message: Kernel logging (proc) stopped.
NTSeverity: NULL
Importance: NULL
EventSource: NULL
EventUser: NULL
EventCategory: NULL
EventID: NULL
EventBinaryData: NULL
MaxAvailable: NULL
CurrUsage: NULL
MinUsage: NULL
MaxUsage: NULL
InfoUnitID: 1
SysLogTag: kernel:
EventLogType: NULL
GenericFileName: NULL
SystemID: NULL
*************************** 2. row ***************************
ID: 2
CustomerID: NULL
ReceivedAt: 2018-02-09 22:05:05
DeviceReportedTime: 2018-02-09 22:05:05
Facility: 5
Priority: 6
FromHost: centos6
Message:  [origin software="rsyslogd" swVersion="5.8.10" x-pid="12390" x-info="http://www.rsyslog.com"] exiting on signal 15.
NTSeverity: NULL
Importance: NULL
EventSource: NULL
EventUser: NULL
EventCategory: NULL
EventID: NULL
EventBinaryData: NULL
MaxAvailable: NULL
CurrUsage: NULL
MinUsage: NULL
MaxUsage: NULL
InfoUnitID: 1
SysLogTag: rsyslogd:
EventLogType: NULL
GenericFileName: NULL
SystemID: NULL
*************************** 3. row ***************************
ID: 3
CustomerID: NULL
ReceivedAt: 2018-02-09 22:05:05
DeviceReportedTime: 2018-02-09 22:05:05
Facility: 0
Priority: 6
FromHost: centos6
Message: imklog 5.8.10, log source = /proc/kmsg started.
NTSeverity: NULL
Importance: NULL
EventSource: NULL
EventUser: NULL
EventCategory: NULL
EventID: NULL
EventBinaryData: NULL
MaxAvailable: NULL
CurrUsage: NULL
MinUsage: NULL
MaxUsage: NULL
InfoUnitID: 1
SysLogTag: kernel:
EventLogType: NULL
GenericFileName: NULL
SystemID: NULL
*************************** 4. row ***************************
ID: 4
CustomerID: NULL
ReceivedAt: 2018-02-09 22:05:05
DeviceReportedTime: 2018-02-09 22:05:05
Facility: 5
Priority: 6
FromHost: centos6
Message:  [origin software="rsyslogd" swVersion="5.8.10" x-pid="12631" x-info="http://www.rsyslog.com"] start
NTSeverity: NULL
Importance: NULL
EventSource: NULL
EventUser: NULL
EventCategory: NULL
EventID: NULL
EventBinaryData: NULL
MaxAvailable: NULL
CurrUsage: NULL
MinUsage: NULL
MaxUsage: NULL
InfoUnitID: 1
SysLogTag: rsyslogd:
EventLogType: NULL
GenericFileName: NULL
SystemID: NULL
*************************** 5. row ***************************
ID: 5
CustomerID: NULL
ReceivedAt: 2018-02-09 22:05:56
DeviceReportedTime: 2018-02-09 22:05:56
Facility: 1
Priority: 5
FromHost: centos6
Message:  The is mysqllog  text log
NTSeverity: NULL
Importance: NULL
EventSource: NULL
EventUser: NULL
EventCategory: NULL
EventID: NULL
EventBinaryData: NULL
MaxAvailable: NULL
CurrUsage: NULL
MinUsage: NULL
MaxUsage: NULL
InfoUnitID: 1
SysLogTag: root:
EventLogType: NULL
GenericFileName: NULL
SystemID: NULL
5 rows in set (0.00 sec)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息