我是否可以在mysql数据库上打开审计日志?
我基本上想监视一个小时的所有查询,并将日志转储到一个文件中。
我是否可以在mysql数据库上打开审计日志?
我基本上想监视一个小时的所有查询,并将日志转储到一个文件中。
当前回答
顶部的答案不工作在mysql 5.6+。用这个代替:
[mysqld]
general_log = on
general_log_file=/usr/log/general.log
在my.cnf / my.ini文件中
Ubuntu / Debian: /etc/mysql/my.cnf Windows: c:\ProgramData\MySQL\MySQL Server 5.x mysql wamp: c: \ wamp \ bin \ \ mysqlx.y.z \ my.ini mysql xampp: c: \ xampp \ \ bin \ my.ini。
其他回答
更新:不再适用于mysql 5.6+
使用——log选项启动mysql:
mysqld --log=log_file_name
或者将以下内容放在my.cnf文件中:
log = log_file_name
任何一个都将把所有查询记录到log_file_name。
您也可以使用——log-slow-queries选项而不是——log只记录慢查询。默认情况下,需要10秒或更长时间的查询被认为是慢查询,您可以通过将long_query_time设置为查询在被记录日志之前必须执行的秒数来改变这一点。
打开表的日志
mysql> SET GLOBAL general_log = 'ON';
mysql> SET GLOBAL log_output = 'table';
通过select query查看日志
SELECT * FROM mysql.general_log
如果查询太多,则截断表:
TRUNCATE table mysql.general_log;
操作系统/ mysql版本:
$ uname -a
Darwin Raphaels-MacBook-Pro.local 15.6.0 Darwin Kernel Version 15.6.0: Thu Jun 21 20:07:40 PDT 2018; root:xnu-3248.73.11~1/RELEASE_X86_64 x86_64
$ mysql --version
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.6.23, for osx10.8 (x86_64) using EditLine wrapper
添加日志记录(例如,我不认为/var/log/…是Mac OS上的最佳路径,但这是有效的:
sudo vi ./usr/local/mysql-5.6.23-osx10.8-x86_64/my.cnf
[mysqld]
general_log = on
general_log_file=/var/log/mysql/mysqld_general.log
重新启动Mysql
结果:
$ sudo tail -f /var/log/mysql/mysqld_general.log
181210 9:41:04 21 Connect root@localhost on employees
21 Query /* mysql-connector-java-5.1.47 ( Revision: fe1903b1ecb4a96a917f7ed3190d80c049b1de29 ) */SELECT @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@collation_server AS collation_server, @@collation_connection AS collation_connection, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout, @@license AS license, @@lower_case_table_names AS lower_case_table_names, @@max_allowed_packet AS max_allowed_packet, @@net_buffer_length AS net_buffer_length, @@net_write_timeout AS net_write_timeout, @@query_cache_size AS query_cache_size, @@query_cache_type AS query_cache_type, @@sql_mode AS sql_mode, @@system_time_zone AS system_time_zone, @@time_zone AS time_zone, @@tx_isolation AS transaction_isolation, @@wait_timeout AS wait_timeout
21 Query SET NAMES latin1
21 Query SET character_set_results = NULL
21 Query SET autocommit=1
21 Query SELECT USER()
21 Query SELECT USER()
181210 9:41:10 21 Query show tables
181210 9:41:25 21 Query select count(*) from current_dept_emp
(注意:对于mysql-5.6+,这将不起作用。如果你向下滚动或点击这里,有一个适用于mysql-5.6+的解决方案。)
如果你不想或者不能重新启动MySQL服务器,你可以在你正在运行的服务器上这样做:
在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'
启用数据库的查询日志
SET global general_log = 1;
SET global log_output = 'table';
查看日志
select * from mysql.general_log
禁用数据库查询日志
SET global general_log = 0;
顶部的答案不工作在mysql 5.6+。用这个代替:
[mysqld]
general_log = on
general_log_file=/usr/log/general.log
在my.cnf / my.ini文件中
Ubuntu / Debian: /etc/mysql/my.cnf Windows: c:\ProgramData\MySQL\MySQL Server 5.x mysql wamp: c: \ wamp \ bin \ \ mysqlx.y.z \ my.ini mysql xampp: c: \ xampp \ \ bin \ my.ini。