Home Database Mysql Tutorial MySQL数据库中的日志文件

MySQL数据库中的日志文件

Jun 07, 2016 pm 03:33 PM
mysql database document log Inquire Configuration

2.3配置查询日志输出路径 从5.1.6版本开始,MySQL提供了更灵活的方式控制日志文件的输出以及输出路径。MySQL的标准日志(特指general_log和slow_log),即可以输出到文件,同时也能够以表的形式保存在数据库mysql中的同名表内,而在5.1.6版本之前,只记录日志

2.3 配置查询日志输出路径

  从5.1.6版本开始,MySQL提供了更灵活的方式控制日志文件的输出以及输出路径。MySQL的标准日志(特指general_log和slow_log),即可以输出到文件,同时也能够以表的形式保存在数据库mysql中的同名表内,而在5.1.6版本之前,只记录日志到文件。

    提示:

    从5.1.6版本开始,日志表会在安装过程中随其它系统表一同创建。如果是从5.1.6之前的版本升级而来,那么DBA需要注意,要手动升级系统表,以确保相关日志表存在。

  当前,日志记录到系统的专用日志表中,要比记录到文件耗费更多的系统资源,因此对于需要启用日志,又需要能够获得更高的系统性能,那么建议优先记录到文件。

  不过,记录到系统表当然也有它的好处,比如说这类表可以通过简单的授权,即可让所有连接到MySQL数据库的用户查看到日志中记录的内容,而且日志表可以通过SQL语句访问,这样也能够比较便捷的通过SQL语句的强大功能进行数据过滤,这都是日志文件不易做到的功能。

2.3.1 服务启动时进行配置

  MySQL的命令行在启动时可以加载很多参数,其中就提供了一个日志专用的参数--log-output,用来指定日志文件的输出方式,注意,说的是输出方式,也就是说到底是记录到操作系统中的文件,还是记录到数据库系统中的专用表。

  --log-output参数可选值有三个:

      
  • TABLE:记录到数据库中的日志表;  
  • FILE:记录到日志文件,默认值即为FILE (在5.1.6到5.1.20版本时,默认值为TABLE);  
  • NONE:不记录。

  上述参数值在设置时可以同时指定多个,相互之间以","逗号分隔即可。

  在指定--log-output参数值不为NONE的基础上,才有可能继续日志文件输出路径的设置,要控制普通查询日志或是慢查询日志文件的生成,又有另外的参数:

      
  • --general_log:控制是否生成普通查询日志,可选值有两个:1表示启用,0表示禁用,默认值为:0,不过当指定了该参数而不指定参数值时,默认值为1;  
  • --general_log_file:5.1.29版本后开始支持该参数,用来指定普通查询日志文件的文件名及输出路径,默认文件名为[host_name].log;  
  • --slow_query_log:控制是否生成慢查询日志,可选值有两个:1表示启用,0表示禁用,默认值为:0,不过当指定了该参数而不指定参数值时,默认值为1;  
  • --slow_query_log_file:5.1.29版本后开始支持该参数,用来指定慢查询日志文件的文件名及输出路径,默认文件名为[host_name]-slow.log:

    提示:

    在5.1.29版本之前,没有--general_log_file和--slow_query_log_file这两个参数,控制文件名及输出路径是通过--log和--log-slow-queries两个参数。
      
  • --log:指定普通查询日志的输出路径,并启用日志输出功能,默认文件名为[host_name].log,该参数在5.1.29版本后废弃;  
  • --log-slow_queries:指定慢查询日志的输出路径,并启用日志输出功能,默认文件名为[host_name]-slow.log,该参数在5.1.29版本后废弃。

  文学描述如果觉着不够清晰,那就看下面几个例子再强化一下吧,比如说:

      
  • 仅启用普通查询日志,并记录到日志文件和日志表,则启动mysql服务时设置参数如下:

    --log-output=TABLE,FILE --general_log 
      
  • 启用普通查询日志和慢查询日志,日志记录到数据库中的日志表,启动mysql服务时设置参数如下:

    --log-output=TABLE --general_log --slow_query_log
      
  • 仅启用慢查询日志,记录到日志文件,设置参数如下:

    --log-output=FILE --slow_query_log
      
  • 仅启用慢查询日志,记录到日志文件,并指定输出路径,设置参数如下:

    --log-output=FILE --slow_query_log --slow_query_log_file=/data/mysql/logs/slow.log

2.3.2 服务运行中进行配置

  MySQL提供了非常多的系统环境变量,用来控制MySQL服务运行时的状态。

  其中,有不少的系统变量与MySQL命令行中的参数相似度极高,甚至包括名称、功能、参数值语法等都一模一样,如果要说有区别的话,就是命令行参数是在命令执行时调用,一经设置,除非重新运行命令行,否则无法修改,而很多系统环境变量,则支持服务运行过程中进行动态的修改,这其中,就包括与日志文件配置相关的一些参数:

      
  • log_output:  
  • general_log&slow_query_log:  
  • general_log_file&slow_query_log_file:

  上面这几个参数均支持全局动态修改,参数的功能与前面命令行中同名参数完全相同,就不多说了,着重描述下面这个参数:

      
  • sql_log_off:可选参数值为ON/OFF(1/0亦可,MySQL系统环境变量的设置非常灵活,将另起章节专门描述),用来指定是否启用/禁用当前会话执行的语句记录到普通查询日志,默认值为OFF。该参数是个会话级参数,用户必须要拥有SUPER权限才能够设置该选项。 

============================================
连载:

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How does Go WebSocket integrate with databases? How does Go WebSocket integrate with databases? Jun 05, 2024 pm 03:18 PM

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

See all articles