Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql general query log for a specific database or user?

Tags:

mysql

Is there a way to just log queries for a certain user or database? I know you can set the general log, but I don't want to log all queries across the entire MySQL instance. Rather I would like to only log queries against a specific database.

SET GLOBAL general_log = 'ON';

Is there a non GLOBAL parameter that can be set?

like image 584
keeg Avatar asked Dec 13 '13 18:12

keeg


People also ask

What is general log in RDS?

General log can log every single query on your database in mysql. general_log table. Alternatively you can also log all the queries to a log file, but in RDS instance, logging them to a table was the best option in our situation. More information about general log can be obtained from MySQL documentation.


2 Answers

I accomplished this by using table logging and a cron job to copy entries from the general_log table to my database-specific log table and then truncate the general_log table. So it does have to log every query but you can isolate your database and the table only grows as big as needed to log the queries for your database (plus however many queries run in between executions of the cron job).

This is the procedure I used (for a database named db):

set global general_log = 'OFF';
alter table mysql.general_log ENGINE = MyISAM;
show create table mysql.general_log;

-- create a new table mysql.db_log using the above schema
-- i.e. just replace the table name.

set global log_output = 'TABLE';
set global general_log = 'ON';

The cron job looks like:

mysql -e "insert into mysql.db_log select * from mysql.general_log where user_host like '%[db]%"
mysql -e "truncate mysql.general_log"

Adjust the where clause to match whatever logs you want to save: database, user, ip, table name, etc.

like image 170
Brian Deterling Avatar answered Oct 05 '22 22:10

Brian Deterling


There is no way to do it in MySQL, but you can grep general log file for specific database. If you are going to use pt-query-digest you can filter queries for selected database as well.

like image 37
ravnur Avatar answered Oct 06 '22 00:10

ravnur