MySQL日志文件_MySQL
bitsCN.com
MySQL日志文件
一、
MySQL有不同类型的日志文件,从它们当中可以查询到MYSQL都做了什么,对于MYSQL的管理是不可或缺的。
1、错误日志(The error log):记录数据库启动、运行以及停止过程中错误信息;
--log-error=[file-name]用来指定错误日志存放的位置。
如果没有指定[file-name],默认hostname.err做为文件名,默认存放在DATADIR目录中。
错误日志文件的格式:
时间 [错误级别] 错误信息
MySQL客户端可以通过下列方法来获取错误日志的详细位置:
1. mysql> show variables like 'log_error';
2. +---------------+-------------------------------------------+
3. | Variable_name | Value |
4. +---------------+-------------------------------------------+
5. | log_error | /usr/local/mysql/var/51ososdb.err |
6. +---------------+-------------------------------------------+
7. 1 row in set (0.00 sec)
通过命令show variables like 'log_error'; 可以得到log_error变量的值,即是错误日志文件的详细位置。
错误日志文件的记录级别:
错误日志有3个level:error、warning 和information。我使用的是mysql-5.1.51,安装在/usr/local/mysql下。
在/usr/local/mysql/include/my_sys.h中
#cat my_sys.h你会发现下面的定义
enum loglevel {
ERROR_LEVEL, //错误级别
WARNING_LEVEL, //警告级别
INFORMATION_LEVEL //信息级别
};
下面是一个错误日志样版:
2.错误信息样板
080313 05:21:55 mysqld started
080313 5:21:55 InnoDB: Started; log sequence number 0 43655
080313 5:21:55 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.0.26-standard-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Edition - Standard (GPL)
080313 5:24:13 [Note] /usr/local/mysql/bin/mysqld: Normal shutdown
080313 5:24:13 InnoDB: Starting shutdown...
080313 5:24:16 InnoDB: Shutdown completed; log sequence number 0 43655
080313 5:24:16 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
080313 05:24:16 mysqld ended
080313 05:24:47 mysqld started
080313 5:24:47 InnoDB: Started; log sequence number 0 43655
080313 5:24:47 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.0.26-standard-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Edition - Standard (GPL)
2、ISAM(索引顺序访问方法)操作日志(The isam log):记录所有对ISAM表的修改,该日志仅仅用于调试ISAM模式;
3、SQL执行日志(the query log):记录了客户端的连接以及所执行的SQL语句;
4、更新日志(the update log):记录了改变数据的语句,已经不建议使用,由二进制日志替代;
5、二进制日志(the binary log):记录了所有对数据库数据的修改语句;
6、超时日志(the slow log):记录所有执行时间超过最大SQL执行时间(long_query_time)或未使用索引的语句;
如果你是在用mysql的复制、备份功能,那么服务器还提供一个叫做relay log的日志文件;
二、
默认情况下所有日志文件会记录在MYSQL的数据目录下,你可以通过强制mysql去关闭并重新打开一个文件进行日志记录,系统会自动加后缀(如.00001),方式有在mysql环境下执行语句mysql>flush logs;或者通过mysqladmin管理程序执行#mysqladmin flush-logs或#mysqladmin refresh
这些日志的启动方式可以在mysql_safe方式启动数据库的时候,后面跟选项参数,也可以在配置文件里配置,推荐采用第二种方式,配置方法很简单。
我只配置了三种日志:
[mysqld]
log=/var/log/mysqld_common.log
log-error=/var/log/mysqld_err.log
log-bin=/var/log/mysqld_bin.bin
日志的查看很简单,大部分都是文本,直接用vim、less、more之类的工具看就可以了,值得说明的是二进制文件的查看:
1). 首先确定是否开启了二进制文件记录功能
mysql>show variables like 'log_bin';
2). 如果你想知道现在记录二进制数据的文件具体信息,你可以通过下列语句看到现在正在记录哪个文件,以及记录的当前位置:
mysql>show master status;
3). 查看二进制数据需要借助程序mysqlbinlog,看看它支持哪些选项,根据自己需要来使用。
mysql>mysqlbinlog /var/log/mysql/mysql-bin.000040;
4)查询某个时间范围的可以执行下列语句,如果记录很多可以将结果定向到一个文件里自己慢慢看:-) :
mysql>mysqlbinlog --start-datetime='2008-01-01 00:00:00' --stop-datetime='2008-08-08 00:00:00' /var/log/mysql/mysql-bin.000040 > ./tmp.log
bitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

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.

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())

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.
