You are here

MySQL reporting to syslog

There are 2 different possible situations you can face when you have to deal with MySQL and syslog:

  • MySQL is used as back-end for syslog to store the logging information. [ 6 ]
  • MySQL itself should report to the syslog.

In this blog article we look at the second situation: How can you make MySQL reporting to the syslog.

Since the version 5.1.20 MySQL is capable to log to the syslog [ 1 ], [ 2 ]. This is done by the MySQL angel process mysqld_safe.

You can enable the syslog when you add the syslog parameter to the MySQL configuration file (my.cnf) in the mysqld_safe section:

[mysqld_safe]
syslog

Currently MySQL is not capable to log to more than one logging facility at the same time. So you have to decide if you want to log either to the error log or to the syslog.

If you specify both, syslog and error-log, at the same time you will receive an error message if you start mysqld like this:

bin/mysqld_safe --defaults-file=/etc/my.cnf

But I assume that most of the MySQL users are using some kind of start/stop wrapper script like the mysql.server as follows:

/etc/init.d/mysql start

or

rcmysql start

So you will never see the error message indicating you having a conflict between the syslog and the error log. And you are possibly wondering why it is not logging to the syslog. For this problem I have filed a bug report [ 3 ].

If you cannot wait for the fix, this excerpt of mysqld_safe should help [ 8 ]:

364   if [ $want_syslog -eq 1 ]
365   then
366     # User explicitly asked for syslog, so warn that it isn't used
367     logging=file   # This line you have to add!
368     log_error "Can't log to error log and syslog at the same time.
        Remove all --log-error configuration options for --syslog to take effect."
369   fi

Logging to an other logging facility than daemon

mysqld_safe uses the logger command to log the error messages to the syslog. With ps you will find a command which looks like this:

logger -t mysqld -p daemon.error

The logger command uses 2 parameter -t for tag and -p for priority. The tag can be influenced with the syslog-tag parameter [ 4 ].

The priority parameter configures into which facility and on which level the message should be logged.

An excerpt from the logger man page [ 7 ]:

Enter the message with the specified priority. The priority may be specified numerically or as a "facility.level" pair. For example, "-p local3.info" logs the message(s) as informational level in the local3 facility. The default is "user.notice."

Unfortunately the logging facility is hard-coded in the the mysqld_safe script as daemon.error and daemon.notice. It would be nice to have this parameter configurable as well thus I have filed a feature request for it [ 5 ].

When you cannot wait, this code snippets from mysqld_safe possibly will help you [ 8 ]:

26a27
> syslog_facility=daemon
110c111
&2
---
>   log_generic $syslog_facility.error "$@" >&2
114c115
   log_generic $syslog_facility.notice "$@"
128c129
&1 | logger -t '$syslog_tag_mysqld' -p daemon.error"
---
>       cmd="$cmd 2>&1 | logger -t '$syslog_tag_mysqld' -p '$syslog_facility'.error"
189a191
>       --syslog-facility=*) syslog_facility="$val" ;;
366a369
>     logging=file

With the modified mysqld_safe you can change your MySQL configuration file (my.cnf) file as follows:

[mysqld_safe]
syslog
syslog-facility                 = local3

to log to the local3 logging facility for example.

To activate this facility you possibly have to adapt your syslog configuration file (in my case: /etc/rsyslog.d/50-default.conf, in other cases: /etc/syslog.conf) as follows:

local3.*                        /var/log/database

To make these changes active you have to restart the syslog daemon:

kill -KILL $(cat /var/run/rsyslogd.pid)

In my case kill -HUP was not strong enough because it did NOT display configuration errors in the log file.

After the restart of the syslog daemon you should find the created empty log file and you can test if the logging works with the following command:

logger -p local3.info test

Now MySQL should log everything to your syslog facility local3.

How to make MySQL logging to the error log AND the syslog facility?

Under normal circumstances mysqld_safe can not log to more than one logging facility.

With the following syslog configuration you can make MySQL logging to both facilities, the error log AND the syslog. Add the following lines to your syslog configuration file:

$FileOwner mysql
$FileGroup dba
local3.*                        /home/mysql/product/mysql-5.1.42/data/error.log

# Set the owner and group back to its original values
$FileOwner syslog
$FileGroup adm
local3.*                        /var/log/database

Then restart the syslog daemon as described above and you will see logging to both location. The only drawback is, that the messages in the MySQL error log look like the typical syslog messages and not like the typical MySQL error log messages any more. But this should be somehow configurable with the syslog parameters in its configuration file [ 9 ].

If you need more assistance with logging to the syslog please feel free and contact us.

Literature

  1. [ 1 ] MySQL error log
  2. [ 2 ] mysqld_safe syslog parameter
  3. [ 3 ] Bug #50083: error-log and syslog conflict in mysqld_safe is not reported to the log file.
  4. [ 4 ] mysqld_safe syslog-tag parameter
  5. [ 5 ] Bug #50080: syslog priority cannot be configured
  6. [ 6 ] Writing syslog messages to MySQL
  7. [ 7 ] Logger man page
  8. [ 8 ] Modified mysqld_safe for advance syslog logging (5.9 kbyte).
  9. [ 9 ] Examples
Taxonomy upgrade extras: 

Comments

[http://www.blogger.com/profile/13094362409916524291 Antony] said... Alas, the new log/audit framework I had developed for MySQL 6.0 would happily log to multiple targets. I guess I should find the time to extract the code from the defunct 6.0 repository and contribute it to MariaDB.
Antonycomment