时间:2021-07-01 10:21:17 帮助过:41人阅读
MySQL日志:大量的IO操作不建议写到文件中
mysql> show global variables like ‘innodb%‘; mysql> show global variables like ‘%log%‘; general_log | OFF log | OFF
【临时开启记录日志】
mysql> set global log=‘ON‘; mysql> set global general_log=‘ON‘; Query OK, 0 rows affected, 1 warning (0.08 sec) general_log | ON log | ON
【随便操作几次,会生成/mydata/data/pc0003.log 日志文件】
mysql> select * from classes; [root@pc0003 data]# cat /mydata/data/pc0003.log /usr/local/mysql/bin/mysqld, Version: 5.5.45-log (Source distribution). started with: Tcp port: 3306 Unix socket: /tmp/mysql.sock Time Id Command Argument 150911 14:09:39 1 Queryshow global variables like ‘%log%‘ 150911 14:11:46 1 Queryset global general_log=‘ON‘ 150911 14:13:53 1 Queryselect * from classes
【临时关闭】
mysql> set global log=‘off‘;此时设定相对下面的无用!
mysql> set global general_log=‘off‘; Query OK, 0 rows affected (0.03 sec) mysql> select * from classes; +---------+----------------+----------+ | ClassID | Class | NumOfStu | +---------+----------------+----------+ | 1 | Shaolin Pai | 10 | | 2 | Emei Pai | 7 | | 3 | QingCheng Pai | 11 | | 4 | Wudang Pai | 12 | | 5 | Riyue Shenjiao | 31 | | 6 | Lianshan Pai | 27 | | 7 | Ming Jiao | 27 | | 8 | Xiaoyao Pai | 15 | | 9 | Liangshan | 22 | | 10 | TaoYuan | 23 | +---------+----------------+----------+ 10 rows in set (0.00 sec)
【再看一看/mydata/data/pc0003.log 日志文件,只会记录到设定关闭的时刻。】
[root@pc0003 data]# cat /mydata/data/pc0003.log /usr/local/mysql/bin/mysqld, Version: 5.5.45-log (Source distribution). started with: Tcp port: 3306 Unix socket: /tmp/mysql.sock Time Id Command Argument 150911 14:09:39 1 Queryshow global variables like ‘%log%‘ 150911 14:11:46 1 Queryset global general_log=‘ON‘ 150911 14:13:53 1 Queryselect * from classes 150911 14:21:49 1 Queryset global general_log=‘off
【设定日志输出到表中】
mysql> mysql> set global general_log=‘ON‘; Query OK, 0 rows affected (0.01 sec) mysql> set global log=‘ON‘; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> set global log_output=‘TABLE‘; Query OK, 0 rows affected (0.00 sec)
【执行】
mysql> show global variables like ‘%log%‘; mysql> select * from hellodb.classes;
【文件查看】没有刚才的操作记录
[root@pc0003 data]# cat /mydata/data/pc0003.log
【表查看】 记录到指定的位置了
mysql> select * from mysql.general_log; +---------------------+---------------------------+-----------+-----------+--------------+------------------------------------+ | event_time | user_host | thread_id | server_id | command_type | argument | +---------------------+---------------------------+-----------+-----------+--------------+------------------------------------+ | 2015-09-11 14:32:51 | root[root] @ localhost [] | 1 | 1 | Query | show global variables like ‘%log%‘ | | 2015-09-11 14:33:19 | root[root] @ localhost [] | 1 | 1 | Query | select * from hellodb.classes | | 2015-09-11 14:33:20 | root[root] @ localhost [] | 1 | 1 | Query | select * from hellodb.classes | | 2015-09-11 14:35:46 | root[root] @ localhost [] | 1 | 1 | Query | select * from mysql.general_log | +---------------------+---------------------------+-----------+-----------+--------------+------------------------------------+ 4 rows in set (0.00 sec)
【不建议启用查询 日志,除非有特殊需求】
mysql> set global general_log=‘OFF‘; Query OK, 0 rows affected (0.00 sec) mysql> set global log=‘OFF‘; Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show global variables like ‘innodb%‘;
mysql> show global variables like ‘%log%‘;
查询日志
慢查询日志:查询执行时长超过指定时长的查询,即为慢查询
错误日志 应该启用,默认没有启用。
二进制日志:复制功能依赖于此日志
中继日志:
事务日志:提交才同步到文件中
随机I/O转换为顺序I/O
ACID:持久性
日志文件组:至少应该有两个日志文件;
注意:尽可能使用小事务以提升事务引擎的性能;
查询日志:
log={ON|OFF}:是否记录所有语句的日志信息于一般查询日志文件(general_log);
log_output={TABLE|FILE|NONE}
TABLE和FILE可以同时出现,用逗号分隔即可;
general_log:是否启用查询日志;
general_log_file:定义一般查询日志保存的文件
慢查询日志:超出这个时长的就成为慢查询!
mysql> show global variables like ‘long%‘;
long_query_time: 10.000000
slow_query_log={ON|OFF}
设定是否启用慢查询日志;它的输出位置也取决log_output={TABLE|FILE|NONE};
slow_query_log_file=www-slow.log
定义日志文件路径及名称;
log_slow_filter=admin,filesort,filesort_on_disk,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk
log_slow_queries=ON
log_slow_rate_limit=1
log_slow_verbosity
【慢查询演示】
设定慢查询
mysql> mysql> set global slow_query_log=1; Query OK, 0 rows affected (0.05 sec) 设定为文件和表都输出 mysql> set global log_output=‘FILE,TABLE‘; Query OK, 0 rows affected (0.00 sec)
【锁定一个表】
mysql> use hellodb; Database changed mysql> lock tables classes write; Query OK, 0 rows affected (0.00 sec)
【打开一个终端2】
mysql> use hellodb; Database changed mysql> select * from students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | 正常显示 27 rows in set (0.00 sec)
mysql> select * fRom classes; 【fRom 避免与缓存击中】
被阻塞。。。。。。。。
【释放锁,查看日志】
mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) mysql> select * from mysql.general_log; +---------------------+---------------------------+-----------+-----------+--------------+------------------------------------+ | event_time | user_host | thread_id | server_id | command_type | argument | +---------------------+---------------------------+-----------+-----------+--------------+------------------------------------+ | 2015-09-11 14:32:51 | root[root] @ localhost [] | 1 | 1 | Query | show global variables like ‘%log%‘ | | 2015-09-11 14:33:19 | root[root] @ localhost [] | 1 | 1 | Query | select * from hellodb.classes | | 2015-09-11 14:33:20 | root[root] @ localhost [] | 1 | 1 | Query | select * from hellodb.classes | | 2015-09-11 14:35:46 | root[root] @ localhost [] | 1 | 1 | Query | select * from mysql.general_log | | 2015-09-11 14:38:53 | root[root] @ localhost [] | 1 | 1 | Query | set global general_log=‘OFF‘ | +---------------------+---------------------------+-----------+-----------+--------------+------------------------------------+ 5 rows in set (0.00 sec)
错误日志:
服务器启动和关闭过程中的信息;
服务器运行过程中的错误信息;
事件调度器运行一个事件时产生的信息;
在复制架构中的从服务器上启动从服务器线程时产生的信息;
log_error = /path/to/error_log_file
log_warnings = {1|0}
是否记录警告信息于错误日志中;
MySQL日志功能详解查询、慢查询
标签:mysql日志功能详解查询、慢查询