准备工做:mysql
1,准备一台存放日志的mariadb数据库服务器 2,确保selinux,iptables是关闭状态
步骤:linux
yum install mariadb-server 也可选择二进制安装安装数据库参考: https://blog.51cto.com/13598893/2070289
CentOS-6启动服务: service mariadb start CentOS-6设为开机自动启动:chkconfig mariadb on CentOS-7启动服务:systemctl start maridb CentOS-7设为开机自动启动:systemctl enable maridb
"注意:为了数据库安全需运行一个安全脚本以下"sql
"mysql_secure_installation" 第一项问你:输入root密码 回车便可,由于没有 第二项问你:须要设置root密码么,固然要 敲Y 第三项问你:须要删除空帐号用户么,固然要 敲Y 第四项问你:禁止root用户远程登入么,根据大家公司的须要 第五项问你:须要删除test测试数据库么,删除 第六项问你:如今从新加载权限表吗 ,固然
安装rsyslog-mysql包,此包的做用就是让rsyslog服务具备链接数据库的功能数据库
yum install 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
scp /usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql 目标主机IP:/dir
进到复制过来的文件的目录下运行该命令 mysql -uroot -p < createDB.sql Enter password: #"-u指定用户,-p输入改用户的密码"
运行完该命令以后会在数据库里生成一个名为Syslog的数据库,里面会有两张表,vim
MariaDB [(none)]> show tables from Syslog; +----------------------------------+ | Tables_in_Syslog | +----------------------------------+ | SystemEvents |#"存放系统事件" | SystemEventsProperties | #"存放系统事件属性" +----------------------------------+ 2 rows in set (0.00 sec)
接下来须要建立一个对于Syslog数据库管理员帐号centos
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'最后别忘了";"结尾
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
CentOS-6重启:service rsyslog restart CentOS-7重启:systemctl restart rsyslog
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)