我是否可以在mysql数据库上打开审计日志?
我基本上想监视一个小时的所有查询,并将日志转储到一个文件中。
我是否可以在mysql数据库上打开审计日志?
我基本上想监视一个小时的所有查询,并将日志转储到一个文件中。
当前回答
打开表的日志
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 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版本:
$ 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
除了我在这里遇到的方法之外,运行下面的方法是在不重新启动的情况下将查询转储到日志文件的最简单方法
SET global log_output = 'FILE';
SET global general_log_file='/Applications/MAMP/logs/mysql_general.log';
SET global general_log = 1;
可以关机用吗
SET global general_log = 0;
值得一提的是,general_log和slow_log是在5.1.6中引入的:
http://dev.mysql.com/doc/refman/5.1/en/log-destinations.html
5.2.1. 选择常规查询和慢速查询日志输出目的地 从MySQL 5.1.6开始,MySQL服务器提供了灵活的控制 输出到一般查询日志和慢速查询日志的目的地, 如果启用了这些日志。日志条目的可能目的地是 日志文件或mysql中的general_log和slow_log表 数据库
打开表的日志
mysql> SET GLOBAL general_log = 'ON';
mysql> SET GLOBAL log_output = 'table';
通过select query查看日志
SELECT * FROM mysql.general_log
如果查询太多,则截断表:
TRUNCATE table mysql.general_log;