search
HomeDatabaseMysql Tutorialmysql初识之日志文件篇_MySQL

bitsCN.com

mysql初识之日志文件篇

 

日志文件

 

1. err日志

 

    error log 记录mysql在运行的过程中所有较为严重的警告和错误信息,以及mysql server每次启动和关闭的详细信息。系统在默认情况下关闭error log 功能。error log 默认存放在data目录下,默认文件名为主机名.err。error log 通过两种方式开启:

 

1). 启动mysqld时,附加上--log-error参数

 

2) . 在my.cnf中配置log-error系统环境变量

 

当进入mysql后执行 flush logs命令后,mysql会将当前的错误日志文件附加-old文件保存,并且创建一个新的空错误日志文件,仅仅在指定--log-error的情况下。

 

注:error log 不全是保存是警告和错误信息,还保存每次启动和关闭的信息。

 

ps启动时信息:130420 16:19:29 mysqld_safe mysqld from pid file /data0/mysql_data_7706/FZTEST-24178.pid ended130420 16:19:30 mysqld_safe Starting mysqld daemon with databases from /data0/mysql_data_7706error: Found option without preceding group in config file: /data0/mysql_data_7706/my.cnf at line: 1Fatal error in defaults handling. Program aborted130420 16:19:30 [Note] Plugin 'FEDERATED' is disabled.130420 16:19:30 [Note] Plugin 'ndbcluster' is disabled.130420 16:19:30  InnoDB: Initializing buffer pool, size = 8.0M130420 16:19:30  InnoDB: Completed initialization of buffer pool(以下省略) 查看error 日志是否启动:mysql> show variables like 'log_error';+---------------+---------------------+| Variable_name | Value                        |+---------------+---------------------+| log_error          | /var/log/mysqld.log |+---------------+---------------------+1 row in set (0.00 sec)关闭mysql:  mysqladmin -uroot -p shutdown 

 

 

2. 二进制日志(binlog)

 

binlog日志主要记录mysql的更新内容,记录每一条更新语句的执行的时间、消耗的资源,以及相关的事务信息。可以用于实时备份,与master/slave复制。

 

如何打开:

my.cnf中[mysqld] 节点上添加  

 

查看binlog是否打开

 

mysql> show variables  like 'log_%';+---------------------------------+-----------------+| Variable_name                   | Value           |+---------------------------------+-----------------+| log_bin                             | ON              |查看binlog内容: /usr/local/mysql3306/bin/mysqlbinlog mysql-bin.00002log-bin-index文件作用:记录目录所有binlog文件[root@FZTEST-24178 mysql_data_7706]# cat mysql_7706-relay-bin.index./mysql_7706-relay-bin.000032binlog格式说明:[root@localhost ~]# mysqlbinlog  /home/mysql/binlog/binlog.000003  | more/*!40019 SET @@session.max_insert_delayed_threads=0*/;/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;DELIMITER /*!*/;# at 4#120330 16:51:46 server id 1  end_log_pos 98    Start: binlog v 4, server v 5.0.45-log created 120330 16:51:46# Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.# at 196#120330 17:54:15 server id 1  end_log_pos 294   Query   thread_id=3     exec_time=2     error_code=0SET TIMESTAMP=1333101255/*!*/;insert into tt7 select * from tt7/*!*/;# at 294#120330 17:54:46 server id 1  end_log_pos 388   Query   thread_id=3     exec_time=28    error_code=0SET TIMESTAMP=1333101286/*!*/;alter table tt7 engine=innodb/*!*/;

 

 

解析binlog格式

位置

位于文件中的位置,“at 196”说明“事件”的起点,是以第196字节开始;“end_log_pos 294”说明以第294字节结束

 

时间戳

事件发生的时间戳:“120330 17:54:46”

 

事件执行时间

事件执行花费的时间:"exec_time=28"

 

错误码

错误码为:“error_code=0”

 

服务器的标识

服务器的标识id:“server id 1”

 

其他参数说明:

1). binlog_do_db:表示记录指定数据库的二进制日志

2).binlog_ignore_db:表示忽略指定的数据库的二进制日志

3).  max_binlog_cache_size:表示使用binlog时最大的内存值

4). binlog_cache_size

此参数表示binlog使用的内存大小,可以通过状态变量binlog_cache_use和binlog_cache_disk_use来帮助测试。

       binlog_cache_use:使用二进制日志缓存的事务数量

       binlog_cache_disk_use:使用二进制日志缓存但超过binlog_cache_size值并使用临时文件来保存事务中的语句的事务数量

5).max_binlog_size

Binlog最大值,最大和默认值是1GB,该设置并不能严格控制Binlog的大小,尤其是Binlog比较靠近最大值而又遇到一个比较大事务时,为了保证事务的完整性,不可能做切换日志的动作,只能将该事务的所有SQL都记录进当前日志,直到事务结束

6).sync_binlog

这个参数直接影响mysql的性能和完整性

sync_binlog=0:

当事务提交后,Mysql仅仅是将binlog_cache中的数据写入Binlog文件,但不执行fsync之类的磁盘        同步指令通知文件系统将缓存刷新到磁盘,而让Filesystem自行决定什么时候来做同步,这个是性能最好的。

sync_binlog=n,在进行n次事务提交以后,Mysql将执行一次fsync之类的磁盘同步指令,同志文件系统将Binlog文件缓存刷新到磁盘。

Mysql中默认的设置是sync_binlog=0,即不作任何强制性的磁盘刷新指令,这时性能是最好的,但风险也是最大的。一旦系统绷Crash,在文件系统缓存中的所有Binlog信息都会丢失。

 

查询日志

 

查询日志即记录所有的查询语句的日志,一般建议不开启,有些query语句比较大,开启后对性能的也有较大的影响。一般用于跟踪某特殊 的性能问题才会短暂打开功能,默认的查询日志文件名为主机名.log。

 

慢查询日志

 

慢查询日志记录的是查询需要较长时间的query,通过在[mysqld] 下添加log-slow-queries=/tmp/slow_log 打开些功能,默认文件名是hostname-slow.log默认的目录是数据目录。

分析慢查询的工具有:msyqlslowdump、mysqlsla

 

innodb在线日志redo日志

 

innodb是一个事务安全的存储引擎,其事务安全性主要是通过在线redo日志和记录在表空间中的undo信息来保证redo日志中记录了innodb所做的所有物理变更和事务信息,通过redo日志和undo信息,innodb保证了在任何情况下的事务安全性。innodb的redo日志同样默认存放 在数据目录下,可通过innodb_log_group_home_dir来更改设置日志的存放公交车,通过innodb_log_files_in_group 设置日志的数量。

 

bitsCN.com
Statement
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
How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

How to use MySQL functions for data processing and calculationHow to use MySQL functions for data processing and calculationApr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

An efficient way to batch insert data in MySQLAn efficient way to batch insert data in MySQLApr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

Steps to add and delete fields to MySQL tablesSteps to add and delete fields to MySQL tablesApr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function