Registrar todas las consultas en mysql
¿Es posible que active el registro de auditoría en mi base de datos mysql?
Básicamente quiero monitorear todas las consultas durante una hora, y volcar el registro a un archivo.
9 answers
Inicie mysql con la opción log log:
mysqld --log=log_file_name
O coloque lo siguiente en su archivo my.cnf
:
log = log_file_name
Cualquiera de los dos registrará todas las consultas en log_file_name.
También puede registrar solo consultas lentas usando la opción --log-slow-queries
en lugar de --log
. De forma predeterminada, las consultas que tardan 10 segundos o más se consideran lentas, puede cambiar esto configurando long_query_time
al número de segundos que una consulta debe tardar en ejecutarse antes de ser registrada.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-16 22:44:07
(Nota : Para mysql-5.6 + esto no funcionará. Hay una solución que se aplica a mysql 5.6+ si desplácese hacia abajo o haga clic aquí.)
Si no desea o no puede reiniciar el servidor MySQL, puede proceder así en su servidor en ejecución:
- Cree sus tablas de registro en la base de datos
mysql
CREATE TABLE `slow_log` (
`start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,
`user_host` mediumtext NOT NULL,
`query_time` time NOT NULL,
`lock_time` time NOT NULL,
`rows_sent` int(11) NOT NULL,
`rows_examined` int(11) NOT NULL,
`db` varchar(512) NOT NULL,
`last_insert_id` int(11) NOT NULL,
`insert_id` int(11) NOT NULL,
`server_id` int(10) unsigned NOT NULL,
`sql_text` mediumtext NOT NULL,
`thread_id` bigint(21) unsigned NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log'
CREATE TABLE `general_log` (
`event_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,
`user_host` mediumtext NOT NULL,
`thread_id` bigint(21) unsigned NOT NULL,
`server_id` int(10) unsigned NOT NULL,
`command_type` varchar(64) NOT NULL,
`argument` mediumtext NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log'
- Habilitar el registro de consultas en la base de datos
SET global general_log = 1;
SET global log_output = 'table';
- Ver el log
select * from mysql.general_log
- Deshabilitar el registro de consultas en la base de datos
SET global general_log = 0;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-23 03:55:23
Además de lo que encontré aquí, ejecutar lo siguiente fue la forma más sencilla de volcar consultas a un archivo de registro sin reiniciar
SET global log_output = 'FILE';
SET global general_log_file='/Applications/MAMP/logs/mysql_general.log';
SET global general_log = 1;
Se puede desactivar con
SET global general_log = 0;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-10-01 15:48:23
La respuesta superior no funciona en mysql 5.6+. Usa esto en su lugar:
[mysqld]
general_log = on
general_log_file=/usr/log/general.log
En tu mi.cnf / my.ini file
Ubuntu/Debian: /etc/mysql/my.cnf
Windows: c:\ProgramData\MySQL\MySQL Servidor 5.x
wamp: c:\wamp\bin\mysql\mysqlx.y.z \ my.ini
xampp: c:\xampp\mysql\bin\my.ini.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-23 19:22:39
Habilitar el registro para la tabla
mysql> SET GLOBAL general_log = 'ON';
mysql> SET global log_output = 'table';
Ver el registro por select query
select * from mysql.general_log
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-20 11:48:28
Forma rápida de habilitar el Registro de Consultas Generales de MySQL sin reiniciar.
mysql> SET GLOBAL general_log = 'ON';
mysql> SET GLOBAL general_log_file = '/var/www/nanhe/log/all.log';
He instalado mysql a través de homebrew, versión mysql: mysql Ver 14.14 Distrib 5.7.15, para osx10.11 (x86_64) usando EditLine wrapper
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-17 18:04:04
Para el registro, general_log y slow_log se introdujeron en 5.1.6:
Http://dev.mysql.com/doc/refman/5.1/en/log-destinations.html
5.2.1. Selección de los Destinos de Salida de Consulta General y Registro de Consulta Lenta
A partir de MySQL 5.1.6, MySQL Server proporciona un control flexible sobre destino de la salida al registro de consultas generales y al registro de consultas lentas, si esos registros están habilitados. Los posibles destinos para las entradas de registro son archivos de registro o el el tablas general_log y slow_log en mysql base de datos
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-09 22:05:44
Debe tener en cuenta que el registro de mysql realmente afecta el rendimiento, pero puede ser una cosa sabia.
Normalmente lo dejo encendido en el servidor dev (excepto cuando nos vuelve locos :))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-22 05:36:29
En caso de usar AWS RDS MYSQL, guía paso a paso aquí.
Cuando se establece como salida 'archivo', puede ver el registro directamente desde la consola "Log" de AWS RDS.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-27 01:24:02