我可以在mysql数据库上打开审核日志记录吗?
我基本上想监视一个小时的所有查询,并将日志转储到文件中。
我可以在mysql数据库上打开审核日志记录吗?
我基本上想监视一个小时的所有查询,并将日志转储到文件中。
Answers:
使用--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-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;
CREATE TABLE
命令(如果表不存在)应该在mysql
数据库上执行,而不是在任何用户创建的数据库上执行。也许可以对SQL语句进行更新以反映这一点。
SELECT * FROM mysql.general_log order by (event_time) desc
会更好。:-)
除了我在这里遇到的问题之外,运行以下命令是将查询转储到日志文件而无需重新启动的最简单方法
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;
GRANT SUPER ON *.* TO user1@localhost
最佳答案在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
wamp:c:\ wamp \ bin \ mysql \ mysqlx.yz \ my.ini
xampp:c:\ xampp \ mysql \ bin \ my.ini。
phpMyAdmin
?
general_log_file=filename.log
启用表的日志
mysql> SET GLOBAL general_log = 'ON';
mysql> SET global log_output = 'table';
通过选择查询查看日志
select * from mysql.general_log
作为记录,在5.1.6中引入了general_log和slow_log:
http://dev.mysql.com/doc/refman/5.1/en/log-destinations.html
5.2.1。选择常规查询和慢速查询日志输出目标
从MySQL 5.1.6开始,如果启用了通用查询日志和慢速查询日志,则MySQL Server可以灵活控制它们的输出目标。日志条目的可能目标是日志文件或mysql数据库中的general_log和slow_log表
OS / 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