MySQL中开启和使用通用查询日志的实例教程_MySQL

WBOY
Lepaskan: 2016-05-27 13:46:20
asal
1314 orang telah melayarinya

开启通用查询日志
因为为了性能考虑,一般通用查询日志general log不会开启。slow log可以定位一些有性能问题的sql,而general log会记录所有的SQL。

mysql5.0版本,如果要开启slow log、general log,需要重启,从MySQL5.1.6版开始,general query log和slow query log开始支持写到文件或者数据库表两种方式,并且日志的开启,输出方式的修改,都可以在Global级别动态修改。

mysql>select version();
Salin selepas log masuk

+————+
| version() |
+————+
| 5.1.37-log |
+————+
1 row in set (0.02 sec)

Salin selepas log masuk

设置日志输出方式为文件(如果设置log_output=table的话,则日志结果会记录到名为gengera_log的表中,这表的默认引擎都是CSV):

mysql>set global log_output=file;
Salin selepas log masuk

Query OK, 0 rows affected (0.00 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

设置general log的日志文件路径:

mysql>set global general_log_file='/tmp/general.log';
Salin selepas log masuk

Query OK, 0 rows affected (0.00 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

开启general log:

mysql>set global general_log=on;
Salin selepas log masuk

Query OK, 0 rows affected (0.02 sec)

Salin selepas log masuk
Salin selepas log masuk

过一段时间后,关闭general log:

mysql>set global general_log=off;
Salin selepas log masuk

Query OK, 0 rows affected (0.02 sec)

Salin selepas log masuk
Salin selepas log masuk

通用查询日志的系统变量

log_output=[none|file|table|file,table] #通用查询日志输出格式
Salin selepas log masuk

general_log=[on|off]      #是否启用通用查询日志

general_log_file[=filename]    #通用查询日志位置及名字

Salin selepas log masuk

通用查询日志的备份

在Linux或Unix中,你可以通过下面的命令重新命名文件

并创建一个新文件:

shell> mv hostname.log hostname-old.log

shell> mysqladmin flush-logs

shell> cp hostname-old.log to-backup-directory

shell> rm hostname-old.log

Salin selepas log masuk

在Windows中,服务器打开日志文件期间不能重新命名日志文件。必须先停止服务器然后重新命名日志文件。然后重启服务器来创建新日志文件。

演示通用查询日志的使用

a、启用通用查询日志

--演示环境
root@localhost[(none)]> show variables like '%version%';
Salin selepas log masuk

+-------------------------+------------------------------+
| Variable_name  | Value  |
+-------------------------+------------------------------+
| innodb_version | 5.5.39  |
| protocol_version | 10   |
| slave_type_conversions |   |
| version  | 5.5.39-log  |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64  |
| version_compile_os | Linux  |
+-------------------------+------------------------------+

Salin selepas log masuk

--查看系统变量
root@localhost[(none)]> show variables like '%general%';
Salin selepas log masuk

+------------------+----------------------------+
| Variable_name | Value  |
+------------------+----------------------------+
| general_log | OFF  |
| general_log_file | /var/lib/mysql/suse11b.log |
+------------------+----------------------------+

Salin selepas log masuk

--查看当前的通用日志,显示无日志文件
root@localhost[(none)]> system ls /var/lib/mysql/suse11b.log 
Salin selepas log masuk

ls: cannot access /var/lib/mysql/suse11b.log: No such file or directory
Salin selepas log masuk

--设置变量general_log以开启通用查询日志
root@localhost[(none)]> set @@global.general_log=1;
Salin selepas log masuk

Query OK, 0 rows affected (0.00 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

--再次查看通用日志文件已存在
root@localhost[(none)]> system ls /var/lib/mysql/suse11b.log 
/var/lib/mysql/suse11b.log
root@localhost[(none)]> select * from tempdb.tb1; --执行查询
Salin selepas log masuk

+------+------+
| id | val |
+------+------+
| 1 | jack |
+------+------+

Salin selepas log masuk
Salin selepas log masuk

--查看通用日志文件内容
root@localhost[(none)]> system more /var/lib/mysql/suse11b.log
/usr/sbin/mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with:
Salin selepas log masuk

Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
Time  Id Command Argument
141003 16:18:12 4 Query show variables like '%general%'
141003 16:18:55 4 Query select * from tempdb.tb1

Salin selepas log masuk


b、更改通用查询日志位置

root@localhost[(none)]> exit
Salin selepas log masuk

Bye
suse11b:~ # service mysql stop
Shutting down MySQL...   done
suse11b:~ # mysqld --general_log_file=/tmp/suse11b.log --user=mysql &
[1] 47009
suse11b:~ # ps -ef|grep mysql|grep -v grep
mysql 47009 44514 1 16:22 pts/0 00:00:00 mysqld --general_log_file=/tmp/suse11b.log --user=mysql
root 47053 44514 0 16:22 pts/0 00:00:00 grep mysql
suse11b:~ # mysql
Salin selepas log masuk

root@localhost[(none)]> system ls /tmp/suse11b.log
Salin selepas log masuk

ls: cannot access /tmp/suse11b.log: No such file or directory

Salin selepas log masuk

root@localhost[(none)]> show variables like '%gener%';
Salin selepas log masuk
Salin selepas log masuk

+------------------+------------------+
| Variable_name | Value |
+------------------+------------------+
| general_log | OFF  |
| general_log_file | /tmp/suse11b.log |
+------------------+------------------+

Salin selepas log masuk
Salin selepas log masuk

root@localhost[(none)]> set global general_log=on;
Salin selepas log masuk

Query OK, 0 rows affected (0.01 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

--此时从系统变量看出,通用日志已经到/tmp目录下
root@localhost[(none)]> show variables like '%gener%';
Salin selepas log masuk

+------------------+------------------+
| Variable_name | Value |
+------------------+------------------+
| general_log | ON  |
| general_log_file | /tmp/suse11b.log |
+------------------+------------------+

Salin selepas log masuk

--发布查询
root@localhost[(none)]> select count(*) from tempdb.tb1;
Salin selepas log masuk

+----------+
| count(*) |
+----------+
| 1 |
+----------+

Salin selepas log masuk

--查看通用日志文件内容
root@localhost[(none)]> system more /tmp/suse11b.log
Salin selepas log masuk

mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
Time  Id Command Argument
141003 16:30:03 1 Query show variables like '%gener%'
141003 16:30:09 1 Query select count(*) from tempdb.tb1

Salin selepas log masuk


c、通用查询日志输出方式

--可以输出为文件,表以及不输出,即TABLE,FILE,NONE
--系统变量log_output
root@localhost[(none)]> show variables like 'log_output';
Salin selepas log masuk

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+

Salin selepas log masuk

--下面修改为输出为表方式
root@localhost[(none)]> set global log_output='TABLE';
Salin selepas log masuk

Query OK, 0 rows affected (0.00 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

root@localhost[(none)]> show variables like 'log_output';
Salin selepas log masuk
Salin selepas log masuk

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | TABLE |
+---------------+-------+

Salin selepas log masuk
Salin selepas log masuk

--发布查询
root@localhost[(none)]> select * from tempdb.tb1;
Salin selepas log masuk

+------+------+
| id | val |
+------+------+
| 1 | jack |
+------+------+

Salin selepas log masuk
Salin selepas log masuk

root@localhost[(none)]> system more /tmp/suse11b.log
Salin selepas log masuk

mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
Time  Id Command Argument
141003 16:30:03 1 Query show variables like '%gener%'
141003 16:30:09 1 Query select count(*) from tempdb.tb1
141003 16:31:00 1 Query show variables like 'log_output'
141003 17:00:48 1 Query set global log_output='TABLE' #通用查询日志输出到文件仅仅记录到全局变量的修改

Salin selepas log masuk

--mysql.general_log记录了通用查询日志的信息
root@localhost[(none)]> desc mysql.general_log;
Salin selepas log masuk

+--------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default  | Extra  |
+--------------+------------------+------+-----+-------------------+-----------------------------+
| event_time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user_host | mediumtext | NO | | NULL  |  |
| thread_id | int(11) | NO | | NULL  |  |
| server_id | int(10) unsigned | NO | | NULL  |  |
| command_type | varchar(64) | NO | | NULL  |  |
| argument | mediumtext | NO | | NULL  |  |
+--------------+------------------+------+-----+-------------------+-----------------------------+

Salin selepas log masuk

--从通用查询日志表里查看通用查询日志的内容
root@localhost[(none)]> select thread_id,command_type,argument from mysql.general_log; 
Salin selepas log masuk

+-----------+--------------+---------------------------------------------------------------+
| thread_id | command_type | argument    |
+-----------+--------------+---------------------------------------------------------------+
| 1 | Query | show variables like 'log_output'   |
| 1 | Query | select * from tempdb.tb1   |
| 1 | Query | desc mysql.general_log   |
| 1 | Query | select thread_id,command_type,argument from mysql.general_log |
+-----------+--------------+---------------------------------------------------------------+

Salin selepas log masuk

root@localhost[(none)]> show variables like 'log_output';
Salin selepas log masuk
Salin selepas log masuk

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | TABLE |
+---------------+-------+

Salin selepas log masuk
Salin selepas log masuk

--使用FILE,TABLE 2者混合输出通用日志
root@localhost[(none)]> set global log_output='file,table';<br />
Salin selepas log masuk

Query OK, 0 rows affected (0.00 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

root@localhost[(none)]> select @@global.log_output;
Salin selepas log masuk

+---------------------+
| @@global.log_output |
+---------------------+
| FILE,TABLE |
+---------------------+

Salin selepas log masuk

root@localhost[(none)]> insert into tempdb.tb1 values(2,'robinson');
Salin selepas log masuk

Query OK, 1 row affected (0.06 sec)

Salin selepas log masuk

root@localhost[(none)]> commit;
Salin selepas log masuk
Salin selepas log masuk

Query OK, 0 rows affected (0.01 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

--验证结果,表和文件里边存在通用的日志记录
root@localhost[(none)]> system tail /tmp/suse11b.log|grep robinson
Salin selepas log masuk

141003 17:41:54 2 Query insert into tempdb.tb1 values(2,'robinson')
Salin selepas log masuk

root@localhost[(none)]> select thread_id,command_type,argument from mysql.general_log
 -> where argument like '%robinson%';
Salin selepas log masuk

+-----------+--------------+------------------------------------------------------------------------+
| thread_id | command_type | argument     |
+-----------+--------------+------------------------------------------------------------------------+
| 2 | Query | insert into tempdb.tb1 values(2,'robinson')  |
| 2 | Query | select thread_id,command_type,argument from mysql.general_log |
|  |  | where argument like ''robinson''   | 
+-----------+--------------+------------------------------------------------------------------------+

Salin selepas log masuk


d、关闭通用查询日志

--可以通过设置系统变量general_log来关闭通用查询日志,此时日志输出设置为FILE,TABLE
root@localhost[(none)]> show variables like 'log_output';
Salin selepas log masuk

+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| log_output | FILE,TABLE |
+---------------+------------+

Salin selepas log masuk

root@localhost[(none)]> set global general_log=off;
Salin selepas log masuk

Query OK, 0 rows affected (0.01 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

root@localhost[(none)]> show variables like '%gener%';
Salin selepas log masuk
Salin selepas log masuk

+------------------+------------------+
| Variable_name | Value |
+------------------+------------------+
| general_log | OFF  |
| general_log_file | /tmp/suse11b.log |
+------------------+------------------+

Salin selepas log masuk
Salin selepas log masuk

root@localhost[(none)]> delete from tempdb.tb1 where id=2;
Salin selepas log masuk

Query OK, 1 row affected (0.12 sec)

Salin selepas log masuk

root@localhost[(none)]> commit;
Salin selepas log masuk
Salin selepas log masuk

Query OK, 0 rows affected (0.00 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

root@localhost[(none)]> system tail -n 1 /tmp/suse11b.log
Salin selepas log masuk
Salin selepas log masuk

141003 17:45:13 2 Query set global general_log=off

Salin selepas log masuk

root@localhost[(none)]> select thread_id,command_type,argument from mysql.general_log
 -> where argument like '%delete%';
Salin selepas log masuk

Empty set (0.00 sec)
<p>--从上面的演示可知,尽管我们设置了log_output为FILE,TABLE,但general_log为OFF,通用日志无任何记录产生</p>
Salin selepas log masuk

root@localhost[(none)]> set global log_output=none;
Salin selepas log masuk

Query OK, 0 rows affected (0.00 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

root@localhost[(none)]> set global general_log=1;
Salin selepas log masuk

Query OK, 0 rows affected (0.00 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

root@localhost[(none)]> truncate table tempdb.tb1;
Salin selepas log masuk

Query OK, 0 rows affected (0.01 sec)

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

root@localhost[(none)]> system tail -n 1 /tmp/suse11b.log
Salin selepas log masuk
Salin selepas log masuk

Time  Id Command Argument

--通过上面的演示,在log_output=none,general_log=on的清下下无任何通用日志输出。

Salin selepas log masuk

 

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan