rsyslog将日志记录于MySQL中

数据库服务器:CentOS7 192.168.119.159

 

日志:CentOS6 192.168.119.129

 

 

  1. 准备mysql数据库 和 用户账户

[root@localhost ~]# yum install mariadb-server

 

[root@localhost ~]# systemctl start mariadb

 

[root@localhost ~]# ss -ntl

State       Recv-Q Send-Q Local Address:Port               Peer Address:Port

LISTEN      0      50                *:3306                          *:*

 

 

  1. 在mysql server上授权rsyslog能连接至当前服务器

授权用户

MariaDB [Syslog]> grant all on Syslog.* to syslog@’%’ identified by ‘1234’;

 

CentOS测试连接

[root@localhost ~]# mysql -h 192.168.119.159 -usyslog -p1234

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 4

Server version: 5.5.56-MariaDB MariaDB Server

 

  1. 在rsyslog服务器上安装mysql模块相关的程序包

[root@localhost ~]# yum info rsyslog-mysql

Description : The rsyslog-mysql package contains a dynamic shared object that will add

: MySQL database support to rsyslog.

 

[root@localhost ~]# yum install rsyslog-mysql

Installed:

rsyslog-mysql.x86_64 0:5.8.10-10.el6_6

 

  1. 为rsyslog创建数据库及表

 

[root@localhost ~]# 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 脚本

 

脚本,需要在mysql的机器上执行,可以先复制到远程主机,也可以创建账户,远程连接执行

 

复制到远程主机

[root@localhost ~]# scp /usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql 192.168.119.159:

 

远程主机查看

[root@localhost ~]# ll

-rw-r–r–. 1 root root 1046 Feb  1 11:06 createDB.sql

 

执行命令

本机执行

[root@localhost ~]# mysql < createDB.sql

或者

MariaDB [(none)]> source createDB.sql

或者远程连接执行

远程连接执行

mysql -usyslog -h192.168.119.159 -p1234 < /usr/share/doc/rsyslog-7.4.7/mysql-createDB.sql

 

查看

MariaDB [Syslog]> show databases;

+——————–+

| Database           |

+——————–+

| Syslog             |

 

MariaDB [Syslog]> show tables;

+————————+

| Tables_in_Syslog       |

+————————+

| SystemEvents           |

| SystemEventsProperties |

+————————+

 

 

 

 

  1. 配置rsyslog将日志保存到mysql中

 

CentOS6修改配置文件

[root@localhost ~]# vim /etc/rsyslog.conf

$ModLoad ommysql

*.info;mail.none;authpriv.none;cron.none                /var/log/messages 本机的

*.info;mail.none;authpriv.none;cron.none                :ommysql:192.168.119.159,Syslog,syslog,1234

 

重启服务

[root@localhost ~]# service rsyslog restart

 

 

CentOS6触发日志

[root@localhost ~]# logger “This is a rsyslog mysql test”

 

查看本机的日志

[root@localhost ~]# tail -f /var/log/messages

Feb  1 11:20:51 localhost root: This is a rsyslog mysql test

 

查看远程的日志

[root@localhost ~]# mysql -h192.168.119.159 -usyslog -p1234

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 6

Server version: 5.5.56-MariaDB MariaDB Server

 

MariaDB [(none)]> use Syslog;

MariaDB [Syslog]> select Message from SystemEvents;

+—————————————————————————————————–+

| Message                                                                                              |

+—————————————————————————————————–+

| imklog 5.8.10, log source = /proc/kmsg started.                                                      |

|  [origin software=”rsyslogd” swVersion=”5.8.10″ x-pid=”2965″ x-info=”http://www.rsyslog.com”] start |

|  This is a rsyslog mysql test                                                                        |

+—————————————————————————————————–+

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/91452

(1)
无言胜千言无言胜千言
上一篇 2018-02-05
下一篇 2018-02-05

相关推荐

  • apache自动化脚本搭建虚拟主机

    1 基于主机名实现三个虚拟主机 (1).创建网站目录与测试文件 (2).创建虚拟主机文件(目录为/etc/httpd/conf.d/下)   (3).修改测试apache主机hosts文件(就不用DNS服务器解析,方便测试),测试httpd配置文件是否有问题,重启httpd服务   (4)修改测试主机hosts文件(就不用DNS服务器解析…

    Linux干货 2016-10-09
  • 显示列表和复制文件的各种实例

    显示/var目录下所有以l开头,以一个小写字母结尾,且中间至少出现一位数字(可以有其它字符)的文件或目录。 [root@localhost tmp]# ls -d /var/l*[[:lower:]] 显示/etc目录下,以任意一个数字开头,且以非数字结尾的文件或目录。 [root@localhost tmp]# ls -d /etc/[0-9]*[^0-9…

    Linux干货 2018-03-01
  • HTTP协议

    HTTP协议   HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议。HTTP是一个基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)。HTTP协议工作于客户端-服务端架构之上。浏览器作为HT…

    Linux干货 2017-02-15
  • Redis 3.0.0正式版发布,全新的分布式高可用数据库

    Redis 3.0.0 正式版终于到来了!最重要的新特性是集群(Redis Cluster),提供Redis功能子集(比如不支持多数据库)的分布式、容错的实现(最多支持1000结点)。   Salvatore 'antirez' Sanfilippo在Google Groups里表示,这是Redis的重要时刻。“我相信今天的Redis 3.0…

    Linux干货 2015-04-03
  • 把编译安装的httpd 实现服务脚本,通过service和chkconfig 进行管理

    把编译安装的httpd 实现服务脚本,通过service和chkconfig 进行管理 1 编译安装httpd 把httpd编译安装在/app/httpd/目录下。 2 在/etc/rc.d/init.d/目录下新建一个文件httpd 这个文件的目的在于让service 命令可以管理编译安装的httpd服务。 文件内容如下: [root@CentOS68 ~…

    Linux干货 2017-05-15
  • 文本处理三剑客之grep

    文本处理三剑客之grep 正则表达式(regex) 文本处理工具 grep egrep 试验环境:CentOS 7.2 正则表达式 Linux的哲学之一“一切皆文件”,学习Linux应得掌握其基本的文本处理工具,这些工具主要包括: 文件内容:less和cat 文件截取:head和tail 文件抽取:cut 关键字搜索:grep 正则表达式是计算机科…

    Linux干货 2016-08-07