How do I log just the raw queries in MySQL?

2019-05-10 04:28发布

I have my log file working, but I get extraneous info on each line like "29 Query", and I can't tell, but it looks like the queries logged are the interpretation of how MySQL treats each query internally. Is there a way to automatically log each query as they were executed by the application without any additional information added to the log by MySQL? Thanks!

EDIT:

As a part of offering the bounty, let me explain my situation. We're using Magento Commerce, which has an EAV database architecture. Tracking anything down, and where it is stored is an absolute nightmare. My thought was to insert a product into the database in the application, and then log every query that was executed during that process. This worked well, but the logs have a ton of other cruft around the queries. I really do just want something like this:

1.) SELECT * FROM <TABLE>;
2.) UPDATE <TABLE> SET <VALUE> = <VALUE>;
3.) ...
4.) ...

Something simple that tells me what was executed so that I don't have to go sifting through controllers and models to try and get all this. I don't need dates, times, line numbers or anything extra.

标签: mysql logging
4条回答
一纸荒年 Trace。
2楼-- · 2019-05-10 05:00

This any help to you?

http://www.bigdbahead.com/?p=99

There's 2 solutions there - one is easier but requires mysql 5.1+.

查看更多
乱世女痞
3楼-- · 2019-05-10 05:22

You could use the mysql query log file. Add this parameter when you start mysql:

--log=/var/log/mysqld.log
查看更多
Emotional °昔
4楼-- · 2019-05-10 05:22

If you are referring to the binary log, you need to use mysqlbinlog to pass it through to get meaningful output.

cat log100.log | mysqlbinlog 
查看更多
叛逆
5楼-- · 2019-05-10 05:23

To enable full Log Query add the following to your my.cnf:

log=/var/log/mysqldquery.log

The above will log all queries to the log file.

Don't forgot to restart mysql service after making changes in my.cnf file.

Example output from actions via SequelPro (mac client):

090721 11:06:45      51 Query       ALTER TABLE `test` ADD `name` varchar(10) DEFAULT NULL
                     51 Query       SHOW COLUMNS FROM `test`
                     51 Query       SHOW INDEX FROM `test`
090721 11:06:57      51 Query       SHOW COLUMNS FROM `test`
                     51 Query       UPDATE `test` SET `id`='1', `name`='test' WHERE `id` = '1' AND `name` IS NULL LIMIT 1
                     51 Query       SELECT * FROM `test` LIMIT 0,100
                     51 Query       SELECT COUNT(1) FROM `test`   
090721 11:07:00      51 Query       UPDATE `test` SET `id`='2', `name`='test' WHERE `id` = '2' AND `name` IS NULL LIMIT 1
                     51 Query       SELECT * FROM `test` LIMIT 0,100
                     51 Query       SELECT COUNT(1) FROM `test`

On *NIX based systems you can use grep to start

grep 'SELECT\|INSERT\|UPDATE' querylog.log

Or get more tricky and start doing things like:

grep 'SELECT\|INSERT\|UPDATE' querylog.log | awk '{$1="";$2="";print}'

This would give you something like this, not perfect but closer:

  51 Query UPDATE `test` SET `id`='2', `name`='test' WHERE `id` = '2' AND `name` IS NULL LIMIT 1
  SELECT * FROM `test` LIMIT 0,100
  SELECT COUNT(1) FROM `test`
  51 Query INSERT INTO `test` (`id`,`name`) VALUES ('3','testing')
  SELECT * FROM `test` LIMIT 0,100
  SELECT COUNT(1) FROM `test`
查看更多
登录 后发表回答