1、查詢日誌的概念:node
查詢日誌記錄MySQL中全部的query,經過"--log[=file_name]"來打開該功能。因爲記錄了全部的query,包括全部的select,體積比較大,開啓後對性能也有比較大的影響,因此請你們慎用該功能。通常只用於跟蹤某些特殊的sql性能問題纔會短暫打開該功能。默認的查詢日誌文件名爲:hostname.log.
To enable the general query log as of MySQL 5.1.6,start mysqld with the --log option,and optionally use --log-ouput to specify the log output destination as described in Section 5.11.1,"Server Log Tables",Before 5.1.6,enable the general query log file with the --log[=file_name]or -l[file_name] option.If no file_name value is give,the default name is host_name,log in the data directory.
Server restarts and log flushing do not cause a new general query log file to be generated (although flushingcloses and reopens it).On Unix,you can rename the file and create a new one by using the following commands:mysql
shell> my host_name.log host_name-old.log
shell> mysqladmin flush-logs
shell> cp host_name-old.log backup-directory
shell> rm host_name-old.logsql
2、實驗部分:shell
----默認狀況下查看是否啓用查詢日誌: [root@node4 mysql5.5]# service mysql start Starting MySQL.... [ OK ] [root@node4 mysql5.5]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.22-log Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show variables like '%log'; +--------------------------------+-------+ | Variable_name | Value | +--------------------------------+-------+ | back_log | 50 | | general_log | OFF | | innodb_locks_unsafe_for_binlog | OFF | | log | OFF | | relay_log | | | slow_query_log | OFF | | sync_binlog | 0 | | sync_relay_log | 0 | +--------------------------------+-------+ 8 rows in set (0.00 sec) ----備註:log和general_log這兩個參數是兼容的。而默認的狀況下查詢日誌是不開啓的 ----使用下面的命令是開啓查詢日誌
mysql> set global log=1; Query OK, 0 rows affected, 1 warning (0.03 sec) mysql> show variables like '%log'; +--------------------------------+-------+ | Variable_name | Value | +--------------------------------+-------+ | back_log | 50 | | general_log | ON | | innodb_locks_unsafe_for_binlog | OFF | | log | ON | | relay_log | | | slow_query_log | OFF | | sync_binlog | 0 | | sync_relay_log | 0 | +--------------------------------+-------+ 8 rows in set (0.00 sec) ----其中log參數是過期的,在啓動選項中使用log參數的話,會在err日誌中顯示出來。
----修改my.cnf文件,添加log的參數設置 [root@node4 mysql5.5]# vi my.cnf [root@node4 mysql5.5]# cat ./my.cnf |grep '^log=' log=/tmp/mysqlgen.log ----清空err日誌
[root@node4 mysql5.5]# cat /dev/null > /tmp/mysql3306.err [root@node4 mysql5.5]# ll /tmp/mysql3306.err -rw-rw---- 1 mysql root 0 Jul 31 07:50 /tmp/mysql3306.err [root@node4 mysql5.5]# service mysql start Starting MySQL... [ OK ] ----啓動數據庫後查看err日誌的內容
[root@node4 mysql5.5]# cat /tmp/mysql3306.err 130731 07:51:32 mysqld_safe Starting mysqld daemon with databases from /opt/mysql5.5/data 130731 7:51:32 [Warning] The syntax '--log' is deprecated and will be removed in a future release. Please use '--general-log'/'--general-log-file' instead. 130731 7:51:33 InnoDB: The InnoDB memory heap is disabled 130731 7:51:33 InnoDB: Mutexes and rw_locks use InnoDB's own implementation 130731 7:51:33 InnoDB: Compressed tables use zlib 1.2.3 130731 7:51:33 InnoDB: Initializing buffer pool, size = 128.0M 130731 7:51:33 InnoDB: Completed initialization of buffer pool 130731 7:51:33 InnoDB: highest supported file format is Barracuda. 130731 7:51:33 InnoDB: Waiting for the background threads to start 130731 7:51:34 InnoDB: 1.1.8 started; log sequence number 1625855 130731 7:51:34 [Note] Event Scheduler: Loaded 0 events 130731 7:51:34 [Note] /opt/mysql5.5/bin/mysqld: ready for connections. Version: '5.5.22-log' socket: '/tmp/mysql.sock' port: 3306 Source distribution ----使用最新的參數 ----general_log和general_log_file。
[root@node4 mysql5.5]# service mysql stop Shutting down MySQL. [ OK ] [root@node4 mysql5.5]# vi my.cnf [root@node4 mysql5.5]# cat ./my.cnf |grep '^general' general_log = 1 general_log_file = /tmp/mysqlgen.log [root@node4 mysql5.5]# service mysql start Starting MySQL... [ OK ] [root@node4 mysql5.5]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.22-log Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show variables like '%log'; +--------------------------------+-------+ | Variable_name | Value | +--------------------------------+-------+ | back_log | 50 | | general_log | ON | | innodb_locks_unsafe_for_binlog | OFF | | log | ON | | relay_log | | | slow_query_log | OFF | | sync_binlog | 0 | | sync_relay_log | 0 | +--------------------------------+-------+ 8 rows in set (0.04 sec) mysql> show variables like '%file'; +---------------------+-----------------------------------+ | Variable_name | Value | +---------------------+-----------------------------------+ | ft_stopword_file | (built-in) | | general_log_file | /tmp/mysqlgen.log | | init_file | | | local_infile | ON | | pid_file | /tmp/mysql3306.pid | | relay_log_info_file | relay-log.info | | slow_query_log_file | /opt/mysql5.5/data/node4-slow.log | +---------------------+-----------------------------------+ 7 rows in set (0.00 sec) ----在上面的操做中能夠看到已經啓用查詢日誌,而且文件目錄是/tmp/mysqlgen.log。
----查詢日誌記錄了哪些東西? 進行下面的查詢
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | test2 | +--------------------+ 5 rows in set (0.08 sec) mysql> use test; Database changed mysql> show tables; Empty set (0.00 sec) mysql> use test2; Database changed mysql> show tables; +-----------------+ | Tables_in_test2 | +-----------------+ | course | | jack | | sc | | student | | t | | teacher | +-----------------+ 6 rows in set (0.07 sec) mysql> drop table t; Query OK, 0 rows affected (0.13 sec) mysql> select * from sc; Empty set (0.04 sec) ----能夠看到上面的操做都記錄在了mysqlgen.log裏面。[root@node4 ~]# tail -f /tmp/mysqlgen.log /opt/mysql5.5/bin/mysqld, Version: 5.5.22-log (Source distribution). started with: Tcp port: 3306 Unix socket: /tmp/mysql.sock Time Id Command Argument 130731 7:55:41 1 Query show databases 130731 7:55:56 1 Query SELECT DATABASE() 1 Init DB test 130731 7:55:59 1 Query show tables 130731 7:56:19 1 Query SELECT DATABASE() 1 Init DB test2 130731 7:56:23 1 Query show tables 130731 7:56:27 1 Query drop table t 130731 7:56:39 1 Query select * from sc