当前位置:Gxlcms > 数据库问题 > MySQL 日志

MySQL 日志

时间:2021-07-01 10:21:17 帮助过:1人阅读


================================================================================

概述:


================================================================================

MySQL的日志分类:

查询日志:general_log

慢查询日志:log_slow_queries

错误日志:log_error, log_warnings

二进制日志:binlog

中继日志:relay_log

事务日志:innodb_log

日志相关的参数:

MariaDB [(none)]> SHOW VARIABLES LIKE ‘%log%‘;
+-------------------------------------------+--------------------------------------------------------------------------------------------------------------+
| Variable_name                             | Value                                                                                                        |
+-------------------------------------------+--------------------------------------------------------------------------------------------------------------+
| aria_checkpoint_log_activity              | 1048576                                                                                                      |
| aria_log_file_size                        | 1073741824                                                                                                   |
| aria_log_purge_type                       | immediate                                                                                                    |
| aria_sync_log_dir                         | NEWFILE                                                                                                      |
| back_log                                  | 50                                                                                                           |
| binlog_annotate_row_events                | OFF                                                                                                          |
| binlog_cache_size                         | 32768                                                                                                        |
| binlog_checksum                           | NONE                                                                                                         |
| binlog_direct_non_transactional_updates   | OFF                                                                                                          |
| binlog_format                             | STATEMENT                                                                                                    |
| binlog_optimize_thread_scheduling         | ON                                                                                                           |
| binlog_stmt_cache_size                    | 32768                                                                                                        |
| expire_logs_days                          | 0                                                                                                            |
| general_log                               | OFF                                                                                                          |
| general_log_file                          | centos7.log                                                                                                  |
| innodb_flush_log_at_trx_commit            | 1                                                                                                            |
| innodb_locks_unsafe_for_binlog            | OFF                                                                                                          |
| innodb_log_block_size                     | 512                                                                                                          |
| innodb_log_buffer_size                    | 8388608                                                                                                      |
| innodb_log_file_size                      | 5242880                                                                                                      |
| innodb_log_files_in_group                 | 2                                                                                                            |
| innodb_log_group_home_dir                 | ./                                                                                                           |
| innodb_mirrored_log_groups                | 1                                                                                                            |
| innodb_recovery_update_relay_log          | OFF                                                                                                          |
| innodb_use_global_flush_log_at_trx_commit | ON                                                                                                           |
| log                                       | OFF                                                                                                          |
| log_bin                                   | OFF                                                                                                          |
| log_bin_trust_function_creators           | OFF                                                                                                          |
| log_error                                 | /var/log/mariadb/mariadb.log                                                                                 |
| log_output                                | FILE                                                                                                         |
| log_queries_not_using_indexes             | OFF                                                                                                          |
| log_slave_updates                         | OFF                                                                                                          |
| 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                          | OFF                                                                                                          |
| log_slow_rate_limit                       | 1                                                                                                            |
| log_slow_verbosity                        |                                                                                                              |
| log_warnings                              | 1                                                                                                            |
| max_binlog_cache_size                     | 18446744073709547520                                                                                         |
| max_binlog_size                           | 1073741824                                                                                                   |
| max_binlog_stmt_cache_size                | 18446744073709547520                                                                                         |
| max_relay_log_size                        | 0                                                                                                            |
| relay_log                                 |                                                                                                              |
| relay_log_index                           |                                                                                                              |
| relay_log_info_file                       | relay-log.info                                                                                               |
| relay_log_purge                           | ON                                                                                                           |
| relay_log_recovery                        | OFF                                                                                                          |
| relay_log_space_limit                     | 0                                                                                                            |
| slow_query_log                            | OFF                                                                                                          |
| slow_query_log_file                       | centos7-slow.log                                                                                             |
| sql_log_bin                               | ON                                                                                                           |
| sql_log_off                               | OFF                                                                                                          |
| sync_binlog                               | 0                                                                                                            |
| sync_relay_log                            | 0                                                                                                            |
| sync_relay_log_info                       | 0                                                                                                            |
+-------------------------------------------+--------------------------------------------------------------------------------------------------------------+
54 rows in set (0.00 sec)

查询日志:general_log

作用:

  • 记录查询语句;

日志存储位置:

  • 文件:file

      ·general_log_file=HOSTNAME.log (主机名加log的文件)

  • 表:table

      ·mysql.general_log

相关参数:

  • general_log={ON|OFF} 默认是OFF

定义输出位置

  • log_output={FILE|TABLE|NONE}

注意:

  • 想要永久有效,要写到配置文件当中,但实际工作中,没有特殊要求,一般不建议开启,因为数据库查询非常多,开启会对I/O造成严重的影响。

演示:

 1.开启查询日志,设定输出位置为table和file;

MariaDB [hellodb]> SET GLOBAL  general_log = ON;    # 开启查询日志
MariaDB [mysql]> SET GLOBAL log_output = ‘TABLE,FILE‘; # 设定输出位置为TABLE和FILE

MariaDB [mysql]> SHOW VARIABLES LIKE ‘log_output‘;    # 查看输出位置
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| log_output    | FILE,TABLE |
+---------------+------------+
1 row in set (0.01 sec)

MariaDB [mysql]> SELECT @@log_output;
+--------------+
| @@log_output |
+--------------+
| FILE,TABLE   |
+--------------+
1 row in set (0.01 sec)

 2.执行查询语句,并查看表和文件是否记录相关的数据

MariaDB [hellodb]> SELECT * FROM mysql.general_log;
+----------------------------+---------------------------+-----------+-----------+--------------+----------------------------------+
| event_time                 | user_host                 | thread_id | server_id | command_type | argument                         |
+----------------------------+---------------------------+-----------+-----------+--------------+----------------------------------+
| 2016-12-01 10:45:23.715603 | root[root] @ localhost [] |         3 |         0 | Query        | SELECT * FROM mysql.general_log  |
| 2016-12-01 10:45:28.195891 | root[root] @ localhost [] |         3 |         0 | Query        | select * from students           |
| 2016-12-01 10:45:36.486132 | root[root] @ localhost [] |         3 |         0 | Query        | SELECT @@log_output              |
| 2016-12-01 10:45:44.909488 | root[root] @ localhost [] |         3 | &nb                    

人气教程排行